Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#5076](https://github.com/open-telemetry/opentelemetry-python/pull/5076))
- `opentelemetry-semantic-conventions`: use `X | Y` union annotation
([#5096](https://github.com/open-telemetry/opentelemetry-python/pull/5096))
- `opentelemetry-exporter-prometheus`: add support for Resource attributes configuration for Prometheus exporter
([#5122](https://github.com/open-telemetry/opentelemetry-python/pull/5122))


## Version 1.41.0/0.62b0 (2026-04-09)
Expand Down
45 changes: 43 additions & 2 deletions docs/exporter/prometheus/prometheus.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The OpenTelemetry Prometheus Exporter package is available on PyPI::
Usage
-----

The Prometheus exporter starts an HTTP server that collects metrics and serializes them to
The Prometheus exporter starts an HTTP server that collects metrics and serializes them to
Prometheus text format on request::

from prometheus_client import start_http_server
Expand All @@ -39,6 +39,47 @@ Prometheus text format on request::
provider = MeterProvider(resource=resource, metric_readers=[reader])
metrics.set_meter_provider(provider)

Resource attributes
-------------------

By default, resource attributes are exported on the ``target_info`` metric. To
also add selected resource attributes as Prometheus labels on every exported
metric, pass a ``resource_attr_filter`` callback to ``PrometheusMetricReader``.
The callback receives the original resource attribute key and returns ``True``
for attributes that should be copied to metric labels::

from prometheus_client import start_http_server

from opentelemetry import metrics
from opentelemetry.exporter.prometheus import PrometheusMetricReader
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.resources import SERVICE_NAME, Resource

resource = Resource.create(
attributes={
SERVICE_NAME: "checkout-service",
"service.namespace": "shop",
"deployment.environment": "production",
}
)

start_http_server(port=9464, addr="localhost")
included_resource_attrs = {SERVICE_NAME, "service.namespace"}
reader = PrometheusMetricReader(
resource_attr_filter=lambda key: key in included_resource_attrs
)
provider = MeterProvider(resource=resource, metric_readers=[reader])
metrics.set_meter_provider(provider)

meter = metrics.get_meter(__name__)
counter = meter.create_counter("orders")
counter.add(1)

The exported metric includes ``service_name="checkout-service"`` and
``service_namespace="shop"`` labels. Resource attribute keys are sanitized to
valid Prometheus label names, and metric attributes with the same sanitized name
take precedence over copied resource attributes.

Configuration
-------------

Expand All @@ -56,4 +97,4 @@ References
----------

* `Prometheus <https://prometheus.io/>`_
* `OpenTelemetry Project <https://opentelemetry.io/>`_
* `OpenTelemetry Project <https://opentelemetry.io/>`_
Loading
Loading