Error with custom labels metrics
See original GitHub issueHello, when I update this solution to the 2.0.0 version and try to use custom labels for metric - https://github.com/korfuri/django-prometheus/blob/master/django_prometheus/tests/end2end/testapp/test_middleware_custom_labels.py#L19-L46. I have an exception for each method.
My middleware settings:
MIDDLEWARE = [
"metrics.middleware_custom_labels.AppMetricsBeforeMiddleware",
# "django_prometheus.middleware.PrometheusBeforeMiddleware",
'django.middleware.security.SecurityMiddleware',
'django.middleware.common.CommonMiddleware',
# "django_prometheus.middleware.PrometheusAfterMiddleware",
"metrics.middleware_custom_labels.AppMetricsBeforeMiddleware",
]
My custom middleware:
import logging
from django.conf import settings
# from prometheus_client import REGISTRY
# from prometheus_client.metrics import MetricWrapperBase
from django_prometheus.middleware import (
Metrics,
PrometheusAfterMiddleware,
PrometheusBeforeMiddleware,
)
logger = logging.getLogger(__name__)
class CustomMetrics(Metrics):
def register_metric(
self, metric_cls, name, documentation, labelnames=tuple(), **kwargs
):
labels = list(labelnames)
labels.extend(("app", "user_agent_type"))
labelnames = tuple(labels)
logger.info(labelnames)
return super(CustomMetrics, self).register_metric(
metric_cls, name, documentation, labelnames=labelnames, **kwargs
)
class AppMetricsBeforeMiddleware(PrometheusBeforeMiddleware):
metrics_cls = CustomMetrics
class AppMetricsAfterMiddleware(PrometheusAfterMiddleware):
metrics_cls = CustomMetrics
def label_metric(self, metric, request, response=None, **labels):
new_labels = labels
new_labels = {"app": "my-application-name", "user_agent_type": "api"}
new_labels.update(labels)
return super(AppMetricsAfterMiddleware, self).label_metric(
metric, request, response=response, **new_labels
)
Versions:
- Django = “2.2.10”
- django-prometheus = “2.0.0”
Exception in log application when I execute some rest method:
[2020-02-24 17:09:30,108 ERROR] django.request | Internal Server Error: /metrics
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/usr/local/lib/python3.7/site-packages/django/utils/deprecation.py", line 93, in __call__
response = self.process_request(request)
File "/usr/local/lib/python3.7/site-packages/django_prometheus/middleware.py", line 177, in process_request
self.metrics.requests_total.inc()
File "/usr/local/lib/python3.7/site-packages/prometheus_client/metrics.py", line 244, in inc
self._value.inc(amount)
AttributeError: 'Counter' object has no attribute '_value'
Internal Server Error: /metrics
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/usr/local/lib/python3.7/site-packages/django/utils/deprecation.py", line 93, in __call__
response = self.process_request(request)
File "/usr/local/lib/python3.7/site-packages/django_prometheus/middleware.py", line 177, in process_request
self.metrics.requests_total.inc()
File "/usr/local/lib/python3.7/site-packages/prometheus_client/metrics.py", line 244, in inc
self._value.inc(amount)
AttributeError: 'Counter' object has no attribute '_value'
[2020-02-24 17:09:30,108 ERROR] django.request | Internal Server Error: /metrics
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/usr/local/lib/python3.7/site-packages/django/utils/deprecation.py", line 93, in __call__
response = self.process_request(request)
File "/usr/local/lib/python3.7/site-packages/django_prometheus/middleware.py", line 177, in process_request
self.metrics.requests_total.inc()
File "/usr/local/lib/python3.7/site-packages/prometheus_client/metrics.py", line 244, in inc
self._value.inc(amount)
AttributeError: 'Counter' object has no attribute '_value'
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:11
Top Results From Across the Web
Can't get custom labels in metrics to work - Sumo Logic Support
I've setup a metric query based on AWS Cloudwatch metrics. I'm showing the Average CPUUtilization. Each series displays as the EC2...
Read more >Error with custom labels metrics - - Bountysource
Error with custom labels metrics ... Hello, when I update this solution to the 2.0.0 version and try to use custom labels for...
Read more >Debugging a failed model training - Rekognition
You might encounter errors during model training. Amazon Rekognition Custom Labels reports training errors in the console and in the response from ...
Read more >Prometheus custom metrics in 1.6.0 not working? - Support
However if we define the custom metrics as described in the Readme we're gettng an error in the log: time="Nov 11 15:02:21" level=info ......
Read more >Understanding Default, Custom, and Missing Metrics
Each metric comes with a set of custom labels, and additional labels can be user-created. ... is shown when queries return no data...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thank you @chrinor2002 for the suggestions. I ran into the same issue. Just being verbose about my understanding, the
self.metrics.requests_total.inc()
call in process_request of the provided Before middleware becomes invalid if we set labelnames to therequests_total
metric. Because in that case,requests_total
has not been initialized with labelvalues yet.Although, I went with a little bit different approach. Let’s say we are adding the environment type as a label
We’ve also run in to this problem, and would really love a solution that doesn’t involve essentially copy pasting the whole middleware classes with a few tweaks.