我在运行我的 Django 应用程序的同一台服务器上安装了 Prometheus。我正在尝试使用 API 检索指标,并且 curl 响应的格式似乎正确。
API(网址和视图):
`ROUTER.register('organization_metrics', simulation.views.OrganizationViewSet, basename='organization_metrics')
class OrganizationViewSet(viewsets.ViewSet):
renderer_classes = [StaticHTMLRenderer]
def list(self, request):
organizations = ["org1", "org2", "org3"] # Replace with your own data
# Create a new CollectorRegistry object
registry = CollectorRegistry()
# Create a new Counter metric to track the number of organizations
#org_counter = Counter('organizations_total', 'Total number of organizations', registry=registry)
org_counter = Counter('organizations_total', 'Total_number_of_organizations', ['org_name'], registry=registry)
# Increment the counter for each organization
for org in organizations:
org_counter.labels(org_name=org).inc()
# org_counter.inc()
# Generate the Prometheus format response
#response_data = generate_latest(registry)
# Return the response with the content type 'text/plain'
logging.info('here I am')
#return Response(response_data, content_type='text/plain')
return (
Response(
generate_latest(registry),
status=status.HTTP_200_OK,
content_type='text/plain',
)
)`
Curl response looks good and in right format:
`curl --request GET http://localhost:8070/api/v1/organization_metrics/ -H 'Accept: text/html'
# HELP organizations_total Total_number_of_organizations
# TYPE organizations_total counter
organizations_total{org_name="org1"} 1.0
organizations_total{org_name="org2"} 1.0
organizations_total{org_name="org3"} 1.0
# TYPE organizations_created gauge
organizations_created{org_name="org1"} 1.6776296973446844e+09
organizations_created{org_name="org2"} 1.6776296973447082e+09
organizations_created{org_name="org3"} 1.6776296973447242e+09`
在我的 prometheus.yml 文件中,我包含了相关的 scrape_configs:
` - job_name: 'myapp-organization-metrics'
scrape_interval: 6s
metrics_path: '/api/v1/organization_metrics/'
static_configs:
- targets: ['localhost:8070']
scheme: http
metric_relabel_configs:
- source_labels: [__name__]
regex: 'organizations_total|organizations_created'
action: keep
`
在 django 日志文件中,我看到我们每 6 秒到达一次“我在这里” 除了普罗米修斯,没有人称这个 api。 然而,尽管如此,我还是无法在 http://localhost:9090/metrics 响应中找到 organizations_total 和 organizations_created 的指标。
如有任何帮助,我们将不胜感激。
谢谢。