如何剥离 Django 中的 Prometheus 响应内容?
How to strip the Prometheus response content in Django?
我正在尝试在 Django App 上实现 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()),
]
当我卷曲 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()
如何解决这个问题?提前致谢
如果你想/必须为你的处理程序使用 APIView
基础 class 那么你需要使用 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),
]
我正在尝试在 Django App 上实现 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()),
]
当我卷曲 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()
如何解决这个问题?提前致谢
如果你想/必须为你的处理程序使用 APIView
基础 class 那么你需要使用 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),
]