跳转到主要内容

健康检查和监控辅助工具集合。

项目描述

https://travis-ci.org/elbaschid/python-panopticon.svg?branch=master

Panopticon是我们用于服务的健康检查和监控辅助工具集合,我们在Mobify中使用。

安装

安装它的最简单方法是来自PyPI

$ pip install python-panopticon

您也可以直接从存储库安装它

$ pip install https://github.com/elbaschid/python-panopticon/archive/master.zip

使用Django设置

panopticon附带一个Django集成应用程序,可简化设置。请确保已安装python-panopticon包。

panopticon.django应用程序添加到您的INSTALLED_APPS设置中,并通过指定设置中的DATADOG_API_KEY配置Datadog的API密钥。您就完成了!

如果要将健康检查自动暴露在/healthcheck/上,您只需将以下行添加到主项目的urls.py

#urls.py
urlpatterns = [
    # all your other URLs

    url(r'', include('panopticon.django.urls', namespace='panopticon')),
]

在此点使用此视图需要安装作为依赖项的django-rest-framework (DRF)。我们可能会在未来更改它,但现在我们正在我们的项目中使用DRF,并且它提供了一些额外的功能。

如果您未连接panopticon.urls,您可以直接构建自己的视图并忽略此依赖项。

可用设置

  • DATADOG_STATS_ENABLED:启用或禁用Panopticon中的Datadog包装器。如果您禁用Panopticon,它将使用mock.Mock对象作为统计客户端。默认情况下是禁用的。

  • DATADOG_STATS_PREFIX:将用于提交到Datadog API的所有Datadog指标的命名前缀。默认值为panopticon

在Django中添加自定义健康检查

如果您正在使用Django应用将其与Django集成,添加新的健康检查非常简单。启动时,会检查INSTALLED_APPS中的每个应用程序,以查找healthchecks.py模块。加载这些模块中的每个模块将自动注册该模块中的所有健康检查。这与models.pytasks.py(Celery)的工作方式类似。

假设我们有一个名为monitoring的Django应用,该应用应包含一些简单的健康检查。首先要做的是创建一个healthchecks.py文件。在这个文件中,我们可以创建一个简单的函数来测试数据库连接。我们只需将其注册为健康检查并提供其成功详情即可。

from django.db import connection, DatabaseError

@HealthCheck.register_healthcheck
def database(data):
    cursor = connection.cursor()

    healthy = True
    status = 'database is available.'

    try:
        cursor.execute('SELECT 1;')
    except DatabaseError as exc:
        status = 'error connecting to the database: {}'.format(str(exc))
    finally:
        cursor.close()

    data[HealthCheck.HEALTHY] = healthy
    data[HealthCheck.STATUS_MESSAGE] = status

    return data

函数的名称,即本例中的database,将被用作健康检查结果的组件名称,如下面的响应格式中定义。

响应格式

我们使用的健康检查格式确保所有健康检查都返回一个约定的JSON响应。这确保了某些属性始终存在,并且可以依赖于外部处理,例如service_healthytimestampcomponents以及每个组件内的healthy

{
    // This represents the overall health of the service
    // If all of the components are healthy this should be true, false otherwise.
    "service_healthy": true,

    // The instant when the response was generated. This is useful to determine
    // if the health check response is up to date or stale, for example because it
    // was cached. This is in ISO8601 format.
    "timestamp": "2014-09-03T23:09:38.702Z",

    // We also expose the health status for each internal component
    // of the service. Besides a “healthy” flag this can also include
    // metadata like the number of queued jobs or average processing times.
    // We expose this information in a list so that monitoring tools can parse
    // and visualize this information easily.
    "components": {
        "database": {
            "healthy":  true,
            "response_time": 0.00123,
            "friendly_status": "The database is working awesomely great!"
        },
        "background_jobs": {
            "healthy":  true,
            "response_time": 0.00123,
            "queued_jobs": 423
        }
    }
}

设置开发环境

开发环境使用tox来针对Python的各个版本进行测试。对于您在本地上安装的给定Python版本,运行tox测试非常简单。例如,运行Python 3.5的tox测试

$ tox -e py35

如果您喜欢在虚拟环境中安装和运行测试,可以使用以下命令在虚拟环境中安装所有测试和发布需求

$ pip install -e ".[test]"
$ pip install -e ".[dev]"

创建发布版本

创建新的发布版本很简单。我们使用bumpversion,它可以确保命名标签和更新Python代码中的所有版本号。要创建新版本,请指定版本升级类型(majorminorpatch),bumpversion将完成其余操作。对于补丁,它看起来像这样

$ bumpversion patch

这将执行以下操作

  • 更改在bumpversion.cfg中定义的所有版本字符串。

  • 创建一个新的提交。

  • 创建一个新的带有给定版本号的标签。

注意在合并并测试一个或多个更改之后,才应在master分支上创建新的发布版本。

发布新版本后,必须将提交和标签推送到GitHub

$ git push
$ git push --tags

现在您可以使用Makefile发布此版本。这需要将mobify用户的密码导出到您的shell中。您可以在LastPass中找到它

$ PYPI_PASSWORD=<the password> make release

许可证

此代码受MIT许可证的许可。

项目详情


下载文件

下载您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源代码分发

python-panopticon-0.4.0.tar.gz (14.0 kB 查看散列值)

上传时间 源代码

构建分发

python_panopticon-0.4.0-py2.py3-none-any.whl (12.4 kB 查看散列值)

上传时间 Python 2 Python 3

支持者