OpenTelemetry is an important specification that defines how we send telemetry data to observability backends like LaunchDarkly, Grafana, and others. OpenTelemetry is great because it is vendor agnostic, and can be used with several observability backends. If you’re new to OpenTelemetry, you can learn more about it here. Today, we’ll go through a complete guide to using OpenTelemetry in Python, including the high-level concepts as well as how to send traces and logs to your OpenTelemetry backend of choice.
Step One: Signals
In OpenTelemetry, a signal is a type of telemetry data that is collected and sent to an observability backend. There are three primary types of signals in OpenTelemetry: traces, metrics, and logs.- Traces: Traces represent the end-to-end journey of a request as it travels through various services and components in a distributed system. They are composed of spans, which are individual units of work within the trace. Traces help you understand the flow of requests and identify performance bottlenecks or errors in your system.
- Metrics: Metrics are numerical measurements that provide insights into the performance and health of your system. They can include data such as request counts, error rates, and latency. Metrics are typically aggregated over time and used to monitor trends and set up alerts.
- Logs: Logs are timestamped records of events that occur within your application. They provide detailed information that is helpful for monitoring, debugging and troubleshooting the application’s behavior. Logs can include various levels of severity, such as info, warning, and error.
Components of Instrumentation
Beyond signals, when working with OpenTelemetry instrumented in your application code, there are several key components that ultimately make up the OpenTelemetry API. These components are designed to be flexible and can be used for all signals. After these signals leave your application, they then can hit an OpenTelemetry collector (or multiple). In LaunchDarkly, we host a cluster of collectors that can be used to send data to, but you can also choose to host your own. Let’s go through each of them. You can refer to the diagram below for a visual representation of how these components interact:
Diagram of the OpenTelemetry pipeline showing how providers, processors, and exporters interact to send telemetry data from an application to a collector.
Provider
A provider is the API entry point that holds the configuration for telemetry data. In the context of tracing, this would be aTracerProvider, and for logging, it would be a LoggerProvider. The provider is responsible for setting up the environment and ensuring that all necessary configurations are in place. This can include configuring a vendor specific api key, or something as simple as setting the service name and environment.
For example, a TracerProvider could set up the resource attributes like service name and environment, and set the LaunchDarkly project key so that the traces are associated with your LaunchDarkly project.
Here’s a quick example of what this looks like in code:
Processor
A processor defines any pre-processing that should be done on the created signals, such as batching, sampling, filtering or even enriching data. This is important because you may have specific needs on the machine that you’re sending data from that require customization. As a very simple example, aBatchSpanProcessor collects spans in batches and sends them to the exporter, which is more efficient than sending each span individually.
Here’s an example of how you might configure a BatchSpanProcessor:
BatchSpanProcessor with an OTLPSpanExporter that sends the spans to the LaunchDarkly collector (more about this later). We’ve also configured the processor to batch the spans and send them to the exporter every second with a queue size of 1000.
Exporter
Finally, an exporter sends the telemetry data to the backend. This is where you configure the endpoint and any other necessary settings related to the backend you’re sending data to. For example, anOTLPSpanExporter would configure the endpoint and any necessary headers, while an ConsoleSpanExporter would simply print the spans to the console.
Here’s an example of how you might configure an OTLPSpanExporter:
foo header to bar.
Instrumenting your application
Logging
Now that we’re familiar with the high-level concepts, let’s see how we can instrument our application to send logs to an OpenTelemetry backend. In this example, we’ll assume that we’re sending data to LaunchDarkly, but the same principles would apply to any other backend that supports OpenTelemetry. First, lets install the necessary packages:Tracing
Similar to logging, we can instrument our application to send traces to an OpenTelemetry backend. Lets start with installing the necessary packages:tracer and logger objects to start sending data to your backend, like so:
Metrics
Lastly, we can instrument our application to send metrics. In this example, we’ll send a simple count metric, but you can also send other types of metrics like histograms, gauges, and more (see the OpenTelemetry docs for more information). First, we need to install the necessary OpenTelemetry packages for metrics:Auto-instrumentation & Middleware
Last but not least, in addition to manual instrumentation, OpenTelemetry also supports auto-instrumentation for popular libraries and frameworks. This allows you to automatically collect telemetry data without having to modify your application code. For example, in Python, you could use the OpenTelemetry Distro SDK or the Zero Code Python setup to automatically instrument your application. The downside of these options, however, is that it requires that you change the way your application is run (and may affect your deployment strategy). As a good alternative, you could use middleware to automatically instrument your application. Middleware is a layer of code that sits between your application and the OpenTelemetry backend, and can be used to automatically collect telemetry data. For example, in a Python FastAPI application, you could write a simple middleware to wrap your application and automatically create traces for each request, like so:Putting it all together
Let’s take the various pieces of the OpenTelemetry configuration and put them into a single file that can easily be imported by our application. Create the fileo11y.py with the following contents:
main.py, we just need to set up the OpenTelemetry resources: