Skip to main content
Next, we’ll learn how to instrument the libraries your application depends on. This guide walks through a small example application that calls a slow HTTP endpoint and demonstrates two approaches:
  • Manual (code-based) instrumentation, and
  • Automatic instrumentation using an OpenTelemetry instrumentation library.

Example application (uninstrumented)

A minimal Python app that performs an HTTP request via the requests library:
The call to requests.get(url) makes a network call to a remote service. We want to measure how long that HTTP call takes and capture additional metadata about the request and response.

Manual instrumentation (explicit spans)

You can create spans manually around application code to record timing and metadata. This requires setting up a TracerProvider, span processor, and exporter, and creating spans with the tracer API:
A console exporter prints spans similar to the following (abbreviated):
Manual instrumentation works well for targeted tracing, but applying it across many libraries and frameworks you do not own is time-consuming and error-prone.

The challenge: so many frameworks and libraries

The Python ecosystem (and other ecosystems) contains many frameworks and clients — web frameworks, HTTP clients, ORMs, messaging clients, and more. Instrumenting each call site manually across services quickly becomes unrealistic.
The image lists various frameworks and tools categorized under web frameworks, libraries and clients, ORMs and DB clients, messaging and RPC, and observability targets. It highlights the problem of having too many frameworks.
What we want:
  • Observability out of the box for commonly used libraries
  • Minimal effort for application developers
  • No vendor lock-in
  • Reuse across multiple apps and teams
The image outlines the need for instrumentation libraries, highlighting four key points: "Observability out of the box," "Minimal effort," "No vendor lock-in," and "Reuse across apps," each represented with a respective icon.

OpenTelemetry Instrumentation Libraries

OpenTelemetry instrumentation libraries provide automatic tracing and/or metrics for popular third-party libraries. They are pre-built packages that apply language-specific techniques (in Python this is typically monkey patching) to wrap functions and capture telemetry with minimal or zero changes to your application code. Common targets:
  • HTTP clients: requests, httpx, aiohttp
  • Web frameworks: Flask, Django, FastAPI
  • ORMs and DB drivers: SQLAlchemy, database-specific drivers
  • Messaging and RPC: kafka-python, pika, celery
  • Cloud SDKs: boto3, etc.
Package naming convention (typical):
  • opentelemetry-instrumentation-<library-name>
The image lists five features of OTel Instrumentation Libraries: pre-built packages, language-specific techniques, zero-touch observability, comprehensive coverage, and standardized data.
Examples of instrumentation packages:
The image lists three OpenTelemetry instrumentation examples, each with a colorful numbered icon: requests for HTTP calls, Django for apps, and SQLAlchemy for database queries.

Auto-instrumenting the example using RequestsInstrumentor

Instead of adding spans manually around each HTTP call, enable the requests instrumentation package. Instrumentation libraries themselves use only the OpenTelemetry API — your application still configures the SDK and exporters.
The instrumentation automatically creates spans for the HTTP client with semantic attributes (for example: http.method, http.url, http.status_code). A console-exported span for an instrumented HTTP GET might look like:

How the instrumentation library works (high level)

When you call RequestsInstrumentor().instrument(), the instrumentor finds and wraps the relevant methods in the requests package (for example, get, post, put, delete) and replaces them with instrumented wrappers. This is usually implemented via monkey patching. Each instrumented call typically:
  1. Creates a client span using the OpenTelemetry API,
  2. Executes the original requests call, and
  3. Ends the span and attaches semantic attributes based on the response.
The image is a diagram explaining the functions of an instrumentation library, highlighting method interception, automatic span creation, and data captured without code changes.

Simplified illustration of monkey patching (conceptual)

Instrumentation libraries only use the OpenTelemetry API

Instrumentation packages should depend only on the OpenTelemetry API, not on any particular SDK implementation. The application sets up the SDK (TracerProvider, processors, exporters) to record and export telemetry. This separation ensures:
  • SDK-agnostic instrumentations,
  • App developers can choose exporters and processors,
  • Reusable, composable instrumentations across organizations.
The image is an infographic titled "Instrumentation Libraries" depicting three main points: using only OpenTelemetry API, hooking into library behavior, and starting/ending spans.

When to use instrumentation libraries

Instrumentation libraries are appropriate when:
  • You want automatic observability without modifying application source code.
  • You need consistent semantic data across services (instrumentations follow OpenTelemetry semantic conventions).
  • You want to share observability setup across teams and projects.
  • You want to extend OpenTelemetry support for in-house libraries by authoring custom instrumentations.
The image outlines four scenarios for using instrumentation libraries with OpenTelemetry, focusing on using and sharing frameworks, extending support for custom libraries, achieving consistent observability, and supporting custom or internal libraries.

What instrumentation libraries include — and what they don’t

  • They hook into third-party libraries and start spans or collect metrics where appropriate.
  • They populate semantic attributes automatically.
  • They provide instrumented methods so your source code remains unchanged.
What they do not do:
  • They do not configure or export telemetry; the application must set up the SDK, span processors, and exporters.
  • They should not depend on a particular SDK implementation.
Instrumentation libraries includeInstrumentation libraries do not include
Hook into third-party libraries and create spans/metricsConfigure SDKs, processors, or exporters
Populate semantic attributes automaticallyDepend on a particular SDK implementation
Provide zero/low-touch observability for appsHandle backend-specific exporting or storage
The image compares what instrumentation libraries include versus what they don't. It highlights tasks like wrapping third-party libraries and starting spans as included, while exporting telemetry data and configuring processors are aspects not included.

Design goals when authoring instrumentation libraries

If you author an instrumentation library, follow these design goals:
  • Be SDK-agnostic — use only the OpenTelemetry API.
  • Allow application developers to configure SDKs and exporters.
  • Be composable and pluggable across projects.
  • Follow OpenTelemetry semantic conventions for attribute naming and events.
The image outlines the design goals of an Instrumentation Library, which include being SDK-agnostic, allowing app developers to configure SDK and exporters, being composable and pluggable, and following semantic conventions.
When writing an instrumentation, rely only on the OpenTelemetry API (not the SDK). Let applications decide which SDK, processors, and exporters to use.

Summary — responsibilities: instrumentation libraries vs applications

  • Instrumentation libraries: decide what to collect (spans/metrics), how to name/annotate them, and how to hook into library behavior. They should be reusable and depend only on the OpenTelemetry API.
  • Applications: decide how to process and export telemetry. Applications configure SDKs, span processors, and exporters to send data to chosen observability backends.
The image compares key takeaways between Instrumentation Libraries and Applications, highlighting features like reuse, configuration, and data flow customization in the context of OpenTelemetry.
Further reading and references:
  • OpenTelemetry Documentation: https://opentelemetry.io/docs/
  • Python instrumentation packages: search PyPI for opentelemetry-instrumentation-<library-name>
This article covered instrumentation libraries.

Watch Video