korfuri / django-prometheus

Export Django monitoring metrics for Prometheus.io
Apache License 2.0
1.44k stars 244 forks source link

'Counter' object has no attribute '_value' #349

Closed nicolasroy11 closed 1 year ago

nicolasroy11 commented 1 year ago

This may be a prometheus-client issue, but no one seems to be around there much, so I'll try my chance here. I tried this a million different ways, and no cigar. Even the Python Intellisense on VSCode sees that an inc() method is available, but it throws this error everytime I try to increase counter value:

request_exceptions_total = Counter(
    name="request_exceptions_total",
    documentation="Total number of excepted requests.",
    labelnames=["endpoint", "user", "status_code", "message"],
)

company_name = get_company_info_from_api_key(req=request)
request_exceptions_total.labels(
    endpoint=request.path,
    user=company_name,
    status_code=status_code,
    message=message
)

request_exceptions_total.inc(amount=1)

Error: Exception has occurred: AttributeError 'Counter' object has no attribute '_value'

I have tried leaving out the label values when a label is created, but no success there either. I went to the Google answers page, but no one had an answer for this.

andrew-cybsafe commented 1 year ago

If a metric has labels then the labels need to be set before the metrics can be incremented or changed. So in your case the following will likely work better:

request_exceptions_total.labels(
    endpoint=request.path,
    user=company_name,
    status_code=status_code,
    message=message
).inc(amount=1)
nicolasroy11 commented 1 year ago

Yep. That did it! Thanks!