如何使用Prometheus客户端从作为uwsgi服务器运行的Django应用中导出应用指标?

a_k*_*k_g 7 python django uwsgi prometheus

我阅读了Prometheus客户端的文档https://github.com/prometheus/client_python/#multiprocess-mode-gunicorn ,其中提到了在python中以多进程模式公开指标。我不是通过Gunicorn而是通过uwsgi运行django应用程序。与文档中的代码类似,我在项目的wsgi.py中添加了代码-

class WSGIEnvironment(WSGIHandler):
def __call__(self, environ, start_response):

    **registry = CollectorRegistry()
    multiprocess.MultiProcessCollector(registry)
    data = generate_latest(registry)**
    django.setup()
    return super(WSGIEnvironment, self).__call__(environ, 
    start_response)
application = WSGIEnvironment()
Run Code Online (Sandbox Code Playgroud)

但是通过这种方法公开收集的数据对我来说似乎并不可行。因此,我在Django应用程序/ metrics中公开了一个调用指标视图的api-

def metrics(request):

    registry = CollectorRegistry()
    multiprocess.MultiProcessCollector(registry)
    data = generate_latest(registry)
    print "data", data
    return HttpResponse(
        data,
        content_type=CONTENT_TYPE_LATEST)
Run Code Online (Sandbox Code Playgroud)

我仍然无法查看我的应用程序公开的指标。是否需要任何配置?我想我缺少非常基本的东西。

fli*_*lix 0

看起来确实相当不错。我已经以类似的方式工作了,只需创建一个视图(与您发布的完全一样)并且不修改wsgi.py.

如果没有找到指标,该/metrics视图将提供一个空页面。如果您看到空白页面,则记录指标可能存在问题。如果您看到 404,那么您的urls.py.

一个常见的错误是调用计数器而不增加它:

# right
my_counter.labels(my_label='a').inc()

# wrong
my_counter.labels(my_label='a')
Run Code Online (Sandbox Code Playgroud)

希望有帮助。