22
33[ OpenTelemetry (OTEL)] ( https://opentelemetry.io/ ) is an open-source
44observability 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
77into the performance and behavior of your distributed systems, making it easier
88to 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
101101There are several ways to integrate with Grafana. This documentation describes
102102only 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
104104works well in scenarios where installing agents like ` Grafana Alloy ` is not
105105easy 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
154155exceptions. For unhandled exceptions, OpenTelemetry includes the full
155156stacktrace 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
171173from 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:
198202app = Application()
199203use_application_insights(app, " YOUR_CONN_STRING" )
200204```
201205
202206Observe 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
208212When using OpenTelemetry you can handle your own tracing explicitly.
209213The BlackSheep code includes an asynchronous context manager and example code
210214for granular control of tracing.
211215
212- ``` python
216+ ``` python {linenums="1" hl_lines="9 4 11"}
213217from blacksheep.server.otel import logcall
214218
215219
@@ -265,3 +269,54 @@ have its own independent context, so the current span is correctly tracked even
265269when code is running concurrently. This ensures that in async code, each task
266270sees its own current span, avoiding conflicts or leaks between concurrent
267271executions.
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