Skip to content

Commit 364bfff

Browse files
Improve the documentation about OpenTelemetry
1 parent 84b0c04 commit 364bfff

File tree

3 files changed

+64
-9
lines changed

3 files changed

+64
-9
lines changed
88.7 KB
Loading
80.4 KB
Loading

blacksheep/docs/opentelemetry.md

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
[OpenTelemetry (OTEL)](https://opentelemetry.io/) is an open-source
44
observability framework for cloud-native software. It provides a standard for
5-
collecting, processing, and exporting telemetry data (such as traces, metrics,
6-
and logs) from applications. By using OpenTelemetry, you can gain deep insights
5+
collecting, processing, and exporting telemetry data such as traces, metrics,
6+
and logs, from applications. By using OpenTelemetry, you can gain deep insights
77
into the performance and behavior of your distributed systems, making it easier
88
to monitor, troubleshoot, and optimize your applications.
99

@@ -39,7 +39,7 @@ from blacksheep.server.otel.otlp import use_open_telemetry_otlp
3939

4040
```
4141

42-
More information is provided below.
42+
Follow the instructions below.
4343

4444
---
4545

@@ -100,13 +100,14 @@ pip install azure-monitor-opentelemetry-exporter
100100

101101
There are several ways to integrate with Grafana. This documentation describes
102102
only manual setup using environment variables for the OTLP protocol. This
103-
approach is flexible because it only requires outgoing HTTP connections, and
103+
approach is flexible because it only requires outgoing HTTP connectivity, and
104104
works well in scenarios where installing agents like `Grafana Alloy` is not
105105
easy or not possible (e.g. cloud `PaaS` services).
106106

107107
- Install the required dependencies like described above.
108108
- Obtain environment variables for the OTLP protocol, using the _OpenTelemetry
109-
getting started guide_ and the option _Send OpenTelemetry data directly to the Grafana Cloud OTLP endpoint._
109+
getting started guide_ and the option _Send OpenTelemetry data directly to
110+
the Grafana Cloud OTLP endpoint._
110111

111112
![Grafana Direct](./img/grafana-otel-direct.png)
112113

@@ -154,9 +155,9 @@ A trace is produced for each web request and for all handled and unhandled
154155
exceptions. For unhandled exceptions, OpenTelemetry includes the full
155156
stacktrace of the exception.
156157

157-
![Grafana traces](./img/grafana-traces.png)
158+
[![Grafana traces](./img/grafana-traces.png)](./img/grafana-traces.png)
158159

159-
![Grafana errors](./img/grafana-errors.png)
160+
[![Grafana errors](./img/grafana-errors.png)](./img/grafana-errors.png)
160161

161162
## Example: Azure Application Insights
162163

@@ -168,6 +169,7 @@ service.
168169
- Configure tracing like in the following example:
169170

170171
```python
172+
# reusable code
171173
from azure.monitor.opentelemetry.exporter import (
172174
AzureMonitorLogExporter,
173175
AzureMonitorTraceExporter,
@@ -195,21 +197,23 @@ def use_application_insights(
195197
AzureMonitorTraceExporter(connection_string=connection_string),
196198
)
197199

200+
201+
# app specific code:
198202
app = Application()
199203
use_application_insights(app, "YOUR_CONN_STRING")
200204
```
201205

202206
Observe how web requests and errors are displayed:
203207

204-
![Azure Application Insights Requests](./img/azure-app-insights-requests.png)
208+
[![Azure Application Insights Requests](./img/azure-app-insights-requests.png)](./img/azure-app-insights-requests.png)
205209

206210
## Logging dependencies
207211

208212
When using OpenTelemetry you can handle your own tracing explicitly.
209213
The BlackSheep code includes an asynchronous context manager and example code
210214
for granular control of tracing.
211215

212-
```python
216+
```python {linenums="1" hl_lines="9 4 11"}
213217
from blacksheep.server.otel import logcall
214218

215219

@@ -265,3 +269,54 @@ have its own independent context, so the current span is correctly tracked even
265269
when code is running concurrently. This ensures that in async code, each task
266270
sees its own current span, avoiding conflicts or leaks between concurrent
267271
executions.
272+
273+
Custom events can be included in the current span:
274+
275+
```python
276+
span = trace.get_current_span()
277+
span.add_event(
278+
"Custom event: home handler called",
279+
{"data": "Example"},
280+
)
281+
```
282+
283+
Beware that not all OpenTelemetry services support custom events.
284+
285+
## Logs instrumentation
286+
287+
The common code includes instrumentation for built-in `logging`. It configures
288+
instrumentation to send logs to the OpenTelemetry service only with priority
289+
higher or equal to `WARNING`.
290+
291+
```python
292+
@app.router.get("/")
293+
async def home(request) -> Response:
294+
logger.warning("Example warning")
295+
```
296+
297+
[![Logs sent to backend](./img/grafana-logs.png)](./img/grafana-logs.png)
298+
299+
To filter by a different priority, set a different level on the root logger:
300+
301+
```python
302+
import logging
303+
304+
305+
logging.getLogger().setLevel(logging.INFO)
306+
```
307+
308+
[![Logs sent to backend](./img/grafana-logs-info.png)](./img/grafana-logs-info.png)
309+
310+
## Service information
311+
312+
The common code includes a function, named `set_attributes`, which can be used
313+
to set the `OTEL_RESOURCE_ATTRIBUTES` environment variable with service metadata
314+
for OpenTelemetry.
315+
316+
This method prepares and sets the OpenTelemetry resource attributes:
317+
service name, namespace, and environment, by updating the corresponding
318+
environment variable. This enables OpenTelemetry instrumentation to correctly
319+
identify and categorize telemetry data for the service.
320+
321+
For more information on standard attributes, refer to: [_Environment Variable
322+
Specification_](https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/).

0 commit comments

Comments
 (0)