健康检查和监控助手的集合。
python-panopticon的Python项目详细描述
Panopticon是我们使用的健康检查和监视帮助程序的集合 在Mobify为我们服务。
安装
最简单的安装方法是从pypi:
$ pip install python-panopticon
您也可以直接从repo安装:
$ pip install https://github.com/mobify/python-panopticon/archive/master.zip
用django设置
panopticon附带了一个django集成应用程序,简化了设置。制造 当然您已经安装了python-panopticon包。
将panopticon.django应用程序添加到您的INSTALLED_APPS设置和 通过在 设置。你们都完了!
如果您希望您的健康检查在/healthcheck/you自动公开 只需将以下行添加到主项目urls.py:
#urls.pyurlpatterns=[...url(r'',include('panopticon.urls',namespace='panopticon')),]
此时使用此视图需要django-rest-framework(drf) 作为依赖项安装。我们可能会在未来改变,但现在, 我们在项目中使用drf,它提供了一些附加功能。
如果不连接panopticon.urls,只需构建自己的视图 忽略此依赖关系。
可用设置
- DATADOG_STATS_ENABLED:启用或禁用 全景电视。如果禁用panopticon,它将使用mock.Mock对象作为 统计客户端。默认情况下是禁用的。
- DATADOG_STATS_PREFIX:用于all的前缀当 提交给datadog api。默认值是panopticon。
在django中添加自定义健康检查
如果您使用django应用程序将其与django集成,请添加新的运行状况 支票很容易。将检查INSTALLED_APPS中的每个应用程序 healthchecks.py启动时的模块。加载这些模块将 自动注册该模块中的所有运行状况检查。这与 models.py和tasks.py(芹菜)起作用。
假设我们有一个monitoringdjango应用程序,它应该包含一些简单的 健康检查。首先要做的是创建一个healthchecks.py文件。 在这个文件中,我们现在可以创建一个简单的函数来测试数据库 连接。我们要做的就是把它登记为健康检查 并提供其成功的详细信息:
fromdjango.dbimportconnection,DatabaseError@HealthCheck.register_healthcheckdefdatabase(data):cursor=connection.cursor()healthy=Truestatus='database is available.'try:cursor.execute('SELECT 1;')exceptDatabaseErrorasexc:status='error connecting to the database: {}'.format(str(exc))finally:cursor.close()data[HealthCheck.HEALTHY]=healthydata[HealthCheck.STATUS_MESSAGE]=statusreturndata
函数名,即本例中的database,将用作 响应格式中定义的运行状况检查结果的组件名称 下面。
响应格式
我们使用的健康检查格式确保所有健康检查都返回 同意json响应。这样可以确保某些属性始终 存在并可用于外部处理,例如service_healthy, timestamp、components和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版本来说很简单。例如 使用python 2.7.10运行tox:
$ pip install tox $ tox -e py27
或者对于python 3.5:
$ tox -e py35
如果您希望在virtualenv中安装并运行测试,可以安装 VirtualEnv中的所有测试和发布要求,使用:
$ pip install -e ".[test]" $ pip install -e ".[dev]"
创建版本
创建新版本很简单。我们使用bumpversion来确保命名标记和 确保更新python代码中的all版本号。创建新的 版本指定版本bump的类型(可以是major、minor或patch) bumpversion会做剩下的。修补程序如下:
$ bumpversion patch
这将执行以下操作:
- 更改在bumpversion.cfg中定义的所有版本字符串。
- 创建新提交。
- 创建具有给定版本号的新标记。
note只有在masterbranch 一个或多个更改已合并并测试。
一个发布新版本后,提交和标记必须推送到github:
$ git push $ git push --tags
现在可以使用makefile将此版本发布到pypi。这需要 要在shell中导出的mobify用户的密码。你可以找到 最后一关:
$ PYPI_PASSWORD=<the password> make release
许可证
此代码是根据MIT License授权的。