@@ -16,7 +16,7 @@ The OpenTelemetry Prometheus Exporter package is available on PyPI::
1616Usage
1717-----
1818
19- The Prometheus exporter starts an HTTP server that collects metrics and serializes them to
19+ The Prometheus exporter starts an HTTP server that collects metrics and serializes them to
2020Prometheus text format on request::
2121
2222 from prometheus_client import start_http_server
@@ -39,6 +39,47 @@ Prometheus text format on request::
3939 provider = MeterProvider(resource=resource, metric_readers=[reader])
4040 metrics.set_meter_provider(provider)
4141
42+ Resource attributes
43+ -------------------
44+
45+ By default, resource attributes are exported on the ``target_info `` metric. To
46+ also add selected resource attributes as Prometheus labels on every exported
47+ metric, pass a ``resource_attr_filter `` callback to ``PrometheusMetricReader ``.
48+ The callback receives the original resource attribute key and returns ``True ``
49+ for attributes that should be copied to metric labels::
50+
51+ from prometheus_client import start_http_server
52+
53+ from opentelemetry import metrics
54+ from opentelemetry.exporter.prometheus import PrometheusMetricReader
55+ from opentelemetry.sdk.metrics import MeterProvider
56+ from opentelemetry.sdk.resources import SERVICE_NAME, Resource
57+
58+ resource = Resource.create(
59+ attributes={
60+ SERVICE_NAME: "checkout-service",
61+ "service.namespace": "shop",
62+ "deployment.environment": "production",
63+ }
64+ )
65+
66+ start_http_server(port=9464, addr="localhost")
67+ included_resource_attrs = {SERVICE_NAME, "service.namespace"}
68+ reader = PrometheusMetricReader(
69+ resource_attr_filter=lambda key: key in included_resource_attrs
70+ )
71+ provider = MeterProvider(resource=resource, metric_readers=[reader])
72+ metrics.set_meter_provider(provider)
73+
74+ meter = metrics.get_meter(__name__)
75+ counter = meter.create_counter("orders")
76+ counter.add(1)
77+
78+ The exported metric includes ``service_name="checkout-service" `` and
79+ ``service_namespace="shop" `` labels. Resource attribute keys are sanitized to
80+ valid Prometheus label names, and metric attributes with the same sanitized name
81+ take precedence over copied resource attributes.
82+
4283Configuration
4384-------------
4485
@@ -56,4 +97,4 @@ References
5697----------
5798
5899* `Prometheus <https://prometheus.io/ >`_
59- * `OpenTelemetry Project <https://opentelemetry.io/ >`_
100+ * `OpenTelemetry Project <https://opentelemetry.io/ >`_
0 commit comments