question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error with custom labels metrics

See original GitHub issue

Hello, 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:open
  • Created 4 years ago
  • Reactions:1
  • Comments:11

github_iconTop GitHub Comments

1reaction
hsinghc-plantxcommented, Nov 2, 2021

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 the requests_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

class CustomMetrics(Metrics):
    def register_metric(self, metric_cls, name, documentation, labelnames=(), **kwargs):
        ln_list = list(labelnames)
        if len(ln_list) == 0:
            labels = {
                ENV_LABEL: settings.ENV_TYPE
            }
            return super().register_metric(
                metric_cls, name, documentation, labelnames=tuple([ENV_LABEL]), **kwargs
            ).labels(**labels)
        ln_list.append(ENV_LABEL)
        labelnames = tuple(ln_list)
        return super().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 = {
            ENV_LABEL: settings.ENV_TYPE
        }
        new_labels.update(labels)
        try:
            return super().label_metric(metric, request, response=response, **new_labels)
        except ValueError:
            return super().label_metric(metric, request, response=response)
1reaction
samwhocommented, Apr 9, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found