Skip to content

Commit 4e83474

Browse files
author
あで
committed
Add exemplar support
1 parent 5a8a344 commit 4e83474

File tree

8 files changed

+207
-153
lines changed

8 files changed

+207
-153
lines changed

CHANGELOG.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1212

1313
### Added
1414

15-
-
15+
- Exemplars support (#51)
1616

1717
### Changed
1818

@@ -30,9 +30,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
3030

3131
### Security
3232

33-
-
34-
35-
---
33+
- ***
3634

3735
## [0.5](https://github.com/autometrics-dev/autometrics-py/releases/tag/0.5) - 2023-05-11
3836

README.md

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
![GitHub_headerImage](https://user-images.githubusercontent.com/3262610/221191767-73b8a8d9-9f8b-440e-8ab6-75cb3c82f2bc.png)
22

3-
# autometrics-py
3+
[![Discord Shield](https://discordapp.com/api/guilds/950489382626951178/widget.png?style=shield)](https://discord.gg/kHtwcH8As9)
44

5-
A Python library that exports a decorator that makes it easy to understand the error rate, response time, and production usage of any function in your code. Jump straight from your IDE to live Prometheus charts for each HTTP/RPC handler, database method, or other piece of application logic.
5+
> A Python port of the Rust
6+
> [autometrics-rs](https://github.com/fiberplane/autometrics-rs) library
7+
8+
**Autometrics is a library that exports a decorator that makes it easy to understand the error rate, response time, and production usage of any function in your code.** Jump straight from your IDE to live Prometheus charts for each HTTP/RPC handler, database method, or other piece of application logic.
69

710
Autometrics for Python provides:
811

@@ -22,6 +25,7 @@ See [Why Autometrics?](https://github.com/autometrics-dev#why-autometrics) for m
2225
- [🚨 Define alerts](#alerts--slos) using SLO best practices directly in your source code
2326
- [📊 Grafana dashboards](#dashboards) work out of the box to visualize the performance of instrumented functions & SLOs
2427
- [⚙️ Configurable](#metrics-libraries) metric collection library (`opentelemetry`, `prometheus`, or `metrics`)
28+
- [📍 Attach exemplars](#exemplars) to connect metrics with traces
2529
- ⚡ Minimal runtime overhead
2630

2731
## Using autometrics-py
@@ -130,6 +134,16 @@ The `version` is read from the `AUTOMETRICS_VERSION` environment variable, and t
130134

131135
This follows the method outlined in [Exposing the software version to Prometheus](https://www.robustperception.io/exposing-the-software-version-to-prometheus/).
132136

137+
## Exemplars
138+
139+
> **NOTE** - As of writing, exemplars aren't supported by the default tracker (`AUTOMETRICS_TRACKER=OPEN_TELEMETRY`).
140+
> You can track the progress of this feature here: https://github.com/autometrics-dev/autometrics-py/issues/41
141+
142+
Exemplars are a way to associate a metric sample to a trace by attaching `trace_id` and `span_id` to it. You can then use this information to jump from a metric to a trace in your tracing system (for example Jaeger). If you have an OpenTelemetry tracer configured, autometrics will automatically pick up the current span from it.
143+
144+
To use exemplars, you need to first switch to a tracker that supports them by setting `AUTOMETRICS_TRACKER=prometheus` and enable
145+
exemplar collection by setting `AUTOMETRICS_EXEMPLARS=true`. You also need to enable exemplars in Prometheus by launching Prometheus with the `--enable-feature=exemplar-storage` flag.
146+
133147
## Development of the package
134148

135149
This package uses [poetry](https://python-poetry.org) as a package manager, with all dependencies separated into three groups:

examples/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ You should be able to run each example by:
55
- cloning this repository
66
- run `poetry install --with examples`
77
- and execute `poetry run python examples/<example>.py` from the root of the repo.
8+
- for django and starlette examples, you can find the exact commands below
89

910
You can change the base url for Prometheus links via the `PROMETHEUS_URL` environment variable. So, if your local Prometheus were on a non-default port, like 9091, you would run:
1011

@@ -57,3 +58,19 @@ Autometrics also tracks a label, `caller`, which is the name of the function tha
5758
This is an example that shows you how to use autometrics to get metrics on http handlers with FastAPI. In this case, we're setting up the API ourselves, which means we need to expose a `/metrics` endpoint manually.
5859

5960
> Don't forget to configure Prometheus itself to scrape the metrics endpoint. Refer to the example `prometheus.yaml` file in the root of this project on how to set this up.
61+
62+
## `django-example`
63+
64+
This is a default Django project with autometrics configured. You can find examples of instrumenting function and class based views in `django_example/views`. To run the example, navigate to `django_example` directory and run the standard command:
65+
66+
`python manage.py runserver 8080`
67+
68+
> Don't forget to configure Prometheus itself to scrape the metrics endpoint. Refer to the example `prometheus.yaml` file in the root of this project on how to set this up.
69+
70+
## `starlette-otel-exemplars.py`
71+
72+
This app shows how to use the OpenTelemetry integration to add exemplars to your metrics. In a distributed system, it allows you to track a request as it flows through your system by adding trace/span ids to it. We can catch these ids from OpenTelemetry and expose them to Prometheus as exemplars. Do note that exemplars are an experimental feature and you need to enable it in Prometheus with a `--enable-feature=exemplar-storage` flag. Run the example with a command:
73+
74+
`AUTOMETRICS_TRACKER=prometheus AUTOMETRICS_EXEMPLARS=true uvicorn starlette-otel-exemplars:app --port 8080`
75+
76+
> Don't forget to configure Prometheus itself to scrape the metrics endpoint. Refer to the example `prometheus.yaml` file in the root of this project on how to set this up.

examples/starlette-otel-exemplars.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from opentelemetry import trace
2+
from autometrics import autometrics
3+
from prometheus_client import REGISTRY
4+
from prometheus_client.openmetrics.exposition import generate_latest
5+
from starlette import applications
6+
from starlette.responses import PlainTextResponse
7+
from starlette.routing import Route
8+
from opentelemetry import trace
9+
from opentelemetry.sdk.trace import TracerProvider
10+
from opentelemetry.sdk.trace.export import (
11+
BatchSpanProcessor,
12+
ConsoleSpanExporter,
13+
)
14+
15+
# Let's start by setting up the OpenTelemetry SDK with some defaults
16+
provider = TracerProvider()
17+
processor = BatchSpanProcessor(ConsoleSpanExporter())
18+
provider.add_span_processor(processor)
19+
trace.set_tracer_provider(provider)
20+
21+
# Now we can instrument our Starlette application
22+
tracer = trace.get_tracer(__name__)
23+
24+
25+
# We need to add tracer decorator before autometrics so that we see the spans
26+
@tracer.start_as_current_span("request")
27+
@autometrics
28+
def outer_function(request):
29+
response = inner_function()
30+
return PlainTextResponse(response)
31+
32+
33+
# This function will also get an exemplar because it is called within
34+
# the span of the outer_function
35+
@autometrics
36+
def inner_function():
37+
return "Hello world!"
38+
39+
40+
def metrics(request):
41+
# Exemplars are not supported by default prometheus format, so we specifically
42+
# make an endpoint that uses the OpenMetrics format that supoorts exemplars.
43+
body = generate_latest(REGISTRY)
44+
return PlainTextResponse(body, media_type="application/openmetrics-text")
45+
46+
47+
app = applications.Starlette(
48+
routes=[Route("/", outer_function), Route("/metrics", metrics)]
49+
)
50+
51+
# Now, start the app (env variables are required to enable exemplars):
52+
# AUTOMETRICS_TRACKER=prometheus AUTOMETRICS_EXEMPLARS=true uvicorn starlette-otel-exemplars:app --port 8080
53+
# And make some requests to /. You should see the spans in the console.
54+
# With autometrics extension installed, you can now hover over the hello handler
55+
# and see the charts and queries associated with them. Open one of the queries
56+
# in Prometheus and you should see exemplars added to the metrics. Don't forget
57+
# to click the Show Exemplars button in Prometheus to see them!

0 commit comments

Comments
 (0)