Skip to content

Commit 5030016

Browse files
committed
update documentation
1 parent 67b76c8 commit 5030016

2 files changed

Lines changed: 53 additions & 3 deletions

File tree

  • docs/exporter/prometheus
  • exporter/opentelemetry-exporter-prometheus/src/opentelemetry/exporter/prometheus

docs/exporter/prometheus/prometheus.rst

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The OpenTelemetry Prometheus Exporter package is available on PyPI::
1616
Usage
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
2020
Prometheus 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+
4283
Configuration
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/>`_

exporter/opentelemetry-exporter-prometheus/src/opentelemetry/exporter/prometheus/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,16 @@ def _convert_buckets(
132132

133133

134134
class PrometheusMetricReader(MetricReader):
135-
"""Prometheus metric exporter for OpenTelemetry."""
135+
"""Prometheus metric exporter for OpenTelemetry.
136+
137+
Args:
138+
disable_target_info: Whether to disable the ``target_info`` metric.
139+
prefix: Prefix added to exported Prometheus metric names.
140+
resource_attr_filter: Optional callback to select resource attributes
141+
that are copied as labels on exported metrics. The callback receives
142+
the original resource attribute key. Selected keys are sanitized to
143+
valid Prometheus label names.
144+
"""
136145

137146
def __init__(
138147
self,

0 commit comments

Comments
 (0)