| 1 | --- |
| 2 | title: Exemplars |
| 3 | weight: 8 |
| 4 | --- |
| 5 | |
| 6 | Exemplars can be added to counter and histogram metrics. Exemplars can be |
| 7 | specified by passing a dict of label value pairs to be exposed as the exemplar. |
| 8 | For example with a counter: |
| 9 | |
| 10 | ```python |
| 11 | from prometheus_client import Counter |
| 12 | c = Counter('my_requests_total', 'HTTP Failures', ['method', 'endpoint']) |
| 13 | c.labels('get', '/').inc(exemplar={'trace_id': 'abc123'}) |
| 14 | c.labels('post', '/submit').inc(1.0, {'trace_id': 'def456'}) |
| 15 | ``` |
| 16 | |
| 17 | And with a histogram: |
| 18 | |
| 19 | ```python |
| 20 | from prometheus_client import Histogram |
| 21 | h = Histogram('request_latency_seconds', 'Description of histogram') |
| 22 | h.observe(4.7, {'trace_id': 'abc123'}) |
| 23 | ``` |
| 24 | |
| 25 | Exemplars are only rendered in the OpenMetrics exposition format. If using the |
| 26 | HTTP server or apps in this library, content negotiation can be used to specify |
| 27 | OpenMetrics (which is done by default in Prometheus). Otherwise it will be |
| 28 | necessary to use `generate_latest` from |
| 29 | `prometheus_client.openmetrics.exposition` to view exemplars. |
| 30 | |
| 31 | To view exemplars in Prometheus it is also necessary to enable the the |
| 32 | exemplar-storage feature flag: |
| 33 | ``` |
| 34 | --enable-feature=exemplar-storage |
| 35 | ``` |
| 36 | Additional information is available in [the Prometheus |
| 37 | documentation](https://prometheus.io/docs/prometheus/latest/feature_flags/#exemplars-storage). |