Auto-Instrumentation Example
This page demonstrates how to use Python auto-instrumentation in OpenTelemetry.
The example is based on an OpenTracing example. You can download or view the
source files used in this page from the opentelemetry-python
repo.
This example uses three different scripts. The main difference between them is how they are instrumented:
server_manual.py
is instrumented manually.server_automatic.py
is instrumented automatically.server_programmatic.py
is instrumented programmatically.
Programmatic instrumentation is a kind of instrumentation that requires minimal instrumentation code to be added to the application. Only some instrumentation libraries offer additional capabilities that give you greater control over the instrumentation process when used programmatically.
Run the first script without the automatic instrumentation agent and second with the agent. They should both produce the same results, demonstrating that the automatic instrumentation agent does exactly the same thing as manual instrumentation.
Automatic instrumentation utilizes monkey-patching to dynamically rewrite methods and classes at runtime through instrumentation libraries. This reduces the amount of work required to integrate OpenTelemetry into your application code. Below, you will see the difference between a Flask route instrumented manually, automatically and programmatically.
Manually instrumented server¶
server_manual.py
Automatically-instrumented server¶
server_automatic.py
Programmatically-instrumented server¶
server_programmatic.py
Prepare¶
Execute the following example in a separate virtual environment. Run the following commands to prepare for auto-instrumentation:
Install¶
Run the following commands to install the appropriate packages. The
opentelemetry-distro
package depends on a few others, like opentelemetry-sdk
for custom instrumentation of your own code and opentelemetry-instrumentation
which provides several commands that help automatically instrument a program.
The examples that follow send instrumentation results to the console. Learn more about installing and configuring the OpenTelemetry Distro to send telemetry to other destinations, like an OpenTelemetry Collector.
Note: To use automatic instrumentation through
opentelemetry-instrument
, you must configure it via environment variables or the command line. The agent creates a telemetry pipeline that cannot be modified other than through these means. If you need more customization for your telemetry pipelines, then you need to forego the agent and import the OpenTelemetry SDK and instrumentation libraries into your code and configure them there. You may also extend automatic instrumentation by importing the OpenTelemetry API. For more details, see the API reference.
Execute¶
This section guides you through the manual process of instrumenting a server as well as the process of executing an automatically instrumented server.
Execute the manually instrumented server¶
Execute the server in two separate consoles, one to run each of the scripts that make up this example:
The console running server_manual.py
will display the spans generated by
instrumentation as JSON. The spans should appear similar to the following
example:
Execute the automatically-instrumented server¶
Stop the execution of server_manual.py
by pressing Control+C and
run the following command instead:
In the console where you previously executed client.py
, run the following
command again:
The console running server_automatic.py
will display the spans generated by
instrumentation as JSON. The spans should appear similar to the following
example:
You can see that both outputs are the same because automatic instrumentation does exactly what manual instrumentation does.
Execute the programmatically-instrumented server¶
It is also possible to use the instrumentation libraries (such as
opentelemetry-instrumentation-flask
) by themselves which may have an advantage
of customizing options. However, by choosing to do this it means you forego
using auto-instrumentation by starting your application with
opentelemetry-instrument
as this is mutually exclusive.
Execute the server just like you would do for manual instrumentation, in two separate consoles, one to run each of the scripts that make up this example:
The results should be the same as running with manual instrumentation.
Using programmatic-instrumentation features¶
Some instrumentation libraries include features that allow for more precise control while instrumenting programmatically, the instrumentation library for Flask is one of them.
This example has a line commented out, change it like this:
After running the example again, no instrumentation should appear on the server
side. This is because or the excluded_urls
option passed to instrument_app
that effectively stops the server_request
function from being instrumented as
its URL matches the regular expression passed to excluded_urls
.
Instrumentation while debugging¶
The debug mode can be enabled in the Flask app like this:
The debug mode can break instrumentation from happening because it enables a
reloader. To run instrumentation while the debug mode is enabled, set the
use_reloader
option to False
:
Configure¶
The auto instrumentation can consume configuration from environment variables.
Capture HTTP request and response headers¶
You can capture predefined HTTP headers as span attributes, according to the semantic convention.
To define which HTTP headers you want to capture, provide a comma-separated list
of HTTP header names via the environment variables
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST
and
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE
, e.g.:
These configuration options are supported by the following HTTP instrumentations:
- Django
- Falcon
- FastAPI
- Pyramid
- Starlette
- Tornado
- WSGI
If those headers are available, they will be included in your span: