【问题标题】:How to strip the Prometheus response content in Django?如何在 Django 中剥离 Prometheus 响应内容?
【发布时间】:2020-03-03 13:24:42
【问题描述】:

我正在尝试在 Django App 上实现 Prometheus 客户端。但是,我得到的输出是无效的,这就是 Prometheus 服务器无法解析指标的原因。

这是我的 Prometheus API 视图

from rest_framework import status
from rest_framework.views import APIView
from rest_framework.response import Response
from prometheus_client import (
    multiprocess,
    CollectorRegistry,
    generate_latest,
    CONTENT_TYPE_LATEST,
)


class PrometheusAPIView(APIView):
    def get(self, request, format=None):
        registry = CollectorRegistry()
        multiprocess.MultiProcessCollector(registry)
        return (
            Response(
                generate_latest(registry),
                status=status.HTTP_200_OK,
                content_type=CONTENT_TYPE_LATEST,
            )
        )

这里是 URL 模式

urlpatterns = [
        url(r'^metrics/', PrometheusAPIView.as_view()),
    ]

当我 curl metrics/ 端点时,我得到了这个

❯ curl localhost:8000/metrics/                                                                                      
"# HELP model_latency Multiprocess metric\n# TYPE model_latency histogram\nmodel_latency_bucket{app=\"app-name\",le=\"10.0\"} 4.0\nmodel_latency_bucket{app=\"app-name\",le=\"0.1\"} 0.0\nmodel_latency_bucket{app=\"app-name\",le=\"0.005\"} 0.0\nmodel_latency_sum{app=\"app-name\"} 7.6447835210005906\nmodel_latency_bucket{app=\"app-name\",le=\"0.25\"} 0.0\nmodel_latency_bucket{app=\"app-name\",le=\"0.75\"} 0.0\nmodel_latency_bucket{app=\"app-name\",le=\"7.5\"} 4.0\nmodel_latency_bucket{app=\"app-name\",le=\"5.0\"} 4.0\nmodel_latency_bucket{app=\"app-name\",le=\"0.5\"} 0.0\nmodel_latency_bucket{app=\"app-name\",le=\"2.5\"} 3.0\nmodel_latency_bucket{app=\"app-name\",le=\"0.075\"} 0.0\nmodel_latency_bucket{app=\"app-name\",le=\"0.01\"} 0.0\nmodel_latency_bucket{app=\"app-name\",le=\"0.05\"} 0.0\nmodel_latency_bucket{app=\"app-name\",le=\"+Inf\"} 4.0\nmodel_latency_bucket{app=\"app-name\",le=\"1.0\"} 0.0\nmodel_latency_bucket{app=\"app-name\",le=\"0.025\"} 0.0\nmodel_latency_count{app=\"app-name\"} 4.0\n# HELP log_count_total Multiprocess metric\n# TYPE log_count_total counter\nlog_count_total{app=\"app-name\",level=\"INFO\"} 8.0\n"

但是,我所期望的如下所示

# HELP model_latency Multiprocess metric
# TYPE model_latency histogram
model_latency_bucket{app="app-name",le="10.0"} 2.0
model_latency_bucket{app="app-name",le="0.1"} 0.0
model_latency_bucket{app="app-name",le="0.005"} 0.0
model_latency_sum{app="app-name"} 4.431863597000756
model_latency_bucket{app="app-name",le="0.25"} 0.0
model_latency_bucket{app="app-name",le="0.75"} 0.0
model_latency_bucket{app="app-name",le="7.5"} 2.0
model_latency_bucket{app="app-name",le="5.0"} 2.0
model_latency_bucket{app="app-name",le="0.5"} 0.0
model_latency_bucket{app="app-name",le="2.5"} 1.0
model_latency_bucket{app="app-name",le="0.075"} 0.0
model_latency_bucket{app="app-name",le="0.01"} 0.0
model_latency_bucket{app="app-name",le="0.05"} 0.0
model_latency_bucket{app="app-name",le="+Inf"} 2.0
model_latency_bucket{app="app-name",le="1.0"} 0.0
model_latency_bucket{app="app-name",le="0.025"} 0.0
model_latency_count{app="app-name"} 2.0
# HELP log_count_total Multiprocess metric
# TYPE log_count_total counter
log_count_total{app="app-name",level="INFO"} 4.0

所以,基本上我需要去掉那些没有转义字符的输出。

我试图像下面那样去掉响应,但那会引发错误

return (
            Response(
                generate_latest(registry),
                status=status.HTTP_200_OK,
                content_type=CONTENT_TYPE_LATEST,
            )
            .__str__()
            .strip()

我该如何解决这个问题?提前致谢

【问题讨论】:

    标签: python django python-3.x django-rest-framework prometheus


    【解决方案1】:

    如果您想/必须为您的处理程序使用APIView 基类,那么您需要使用StaticHTMLRenderer,这样Prometheus 指标数据就不会被转义。

    from rest_framework.renderers import StaticHTMLRenderer
    
    class PrometheusAPIView(APIView):
    
        renderer_classes = [StaticHTMLRenderer]
    
        def get(self, request, format=None):
            registry = CollectorRegistry()
            multiprocess.MultiProcessCollector(registry)
            return (
                Response(
                    generate_latest(registry),
                    status=status.HTTP_200_OK,
                    content_type=CONTENT_TYPE_LATEST,
                )
            )
    

    您也可以考虑完全放弃 APIView 并使用标准的 Django 处理程序:

    from django.http import HttpResponse
    
    
    def metrics(request):
        registry = CollectorRegistry()
        multiprocess.MultiProcessCollector(registry)
        return HttpResponse(
            generate_latest(registry),
            content_type=CONTENT_TYPE_LATEST)
    
    
    
    urlpatterns = [
        path('metrics/', metrics),
    ]
    

    【讨论】:

      猜你喜欢
      • 2019-09-25
      • 2011-07-30
      • 1970-01-01
      • 2015-03-04
      • 2016-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多