Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: add AppMetricsBeforeMiddleware.label_metric() #335

Open
wilsoniya opened this issue Aug 17, 2022 · 0 comments
Open

Feature request: add AppMetricsBeforeMiddleware.label_metric() #335

wilsoniya opened this issue Aug 17, 2022 · 0 comments

Comments

@wilsoniya
Copy link

wilsoniya commented Aug 17, 2022

Background

At present, metrics emitted by AppMetricsBeforeMiddleware are not labeled and no hook exists to apply ad-hoc labels to these metrics; see:

class CustomMetrics(Metrics):
def register_metric(self, metric_cls, name, documentation, labelnames=(), **kwargs):
if name in EXTENDED_METRICS:
labelnames.extend(("view_type", "user_agent_type"))
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 = labels
if metric._name in EXTENDED_METRICS:
new_labels = {"view_type": "foo", "user_agent_type": "browser"}
new_labels.update(labels)
return super().label_metric(metric, request, response=response, **new_labels)
@override_settings(
MIDDLEWARE=get_middleware(
"testapp.test_middleware_custom_labels.AppMetricsBeforeMiddleware",

Proposal

Add a label_metric() method to django_prometheus.middleware.AppMetricsBeforeMiddleware which would function identically to django_prometheus.middleware.AppMetricsAfterMiddleware.label_metric(), which already exists.

Use case

The ability to apply labels unconditionally to all emitted metrics. For example, applying the namespace label allows the django_prometheus grafana dashboard to query on a namespace label: https://grafana.com/grafana/dashboards/9528-django-prometheus/

📣 If the authors are amenable to this change, I'm happy to implement it and open a PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant