This guide will help you add OpenTelemetry to your web service, show you how to add additional custom attributes to that instrumentation, and ensure that instrumentation data is being sent to Honeycomb.
These instructions will explain how to set up manual instrumentation for a service written in Python. In order to follow along, you will need:
Install these packages to instrument a Flask app with OpenTelemetry:
pip install opentelemetry-api pip install opentelemetry-sdk pip install opentelemetry-exporter-otlp-proto-http pip install opentelemetry-instrumentation-flask pip install opentelemetry-instrumentation-requests
Install these packages to instrument a Flask app with OpenTelemetry:
poetry add opentelemetry-api poetry add opentelemetry-sdk poetry add opentelemetry-exporter-otlp-proto-http poetry add opentelemetry-instrumentation-flask poetry add opentelemetry-instrumentation-requests
Create a new file,
This file will create and initialize a tracer and Flask instrumentation to send data to Honeycomb:
# tracing.py from opentelemetry import trace from opentelemetry.instrumentation.flask import FlaskInstrumentor from opentelemetry.instrumentation.requests import RequestsInstrumentor from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor # Initialize tracing and an exporter that can send data to Honeycomb provider = TracerProvider() processor = BatchSpanProcessor(OTLPSpanExporter()) provider.add_span_processor(processor) trace.set_tracer_provider(provider) tracer = trace.get_tracer(__name__) # Initialize automatic instrumentation with Flask app = flask.Flask(__name__) FlaskInstrumentor().instrument_app(app) RequestsInstrumentor().instrument()
Configure OpenTelemetry to send events to Honeycomb using environment variables.
x-honeycomb-team is your API key.
Your service name will be used as the Service Dataset in Honeycomb, which is where data is stored.
The service name is specified by
export OTEL_EXPORTER_OTLP_ENDPOINT="https://api.honeycomb.io/" export OTEL_EXPORTER_OTLP_HEADERS="x-honeycomb-team=your-api-key" export OTEL_SERVICE_NAME="your-service-name" python app.py tracing.py
If you are a Honeycomb Classic user, the Dataset also must be specified using the
A Dataset is a bucket where data gets stored in Honeycomb.
To use gRPC instead of HTTP, install the package, replace the import, and update the endpoint:
pip install opentelemetry-exporter-otlp-proto-grpc
poetry add opentelemetry-exporter-otlp-proto-grpc
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter # The rest of your code...
To create spans, you need to get a
from opentelemetry import trace tracer = trace.get_tracer("tracer.name.here")
When you create a
Tracer, OpenTelemetry requires you to give it a name as a string.
This string is the only required parameter.
When traces are sent to Honeycomb, the name of the
Tracer is turned into the
library.name field, which can be used to show all spans created from a particular tracer.
In general, pick a name that matches the appropriate scope for your traces. If you have one tracer for each service, then use the service name. If you have multiple tracers that live in different “layers” of your application, then use the name that corresponds to that “layer”.
library.name field is also used with traces created from instrumentation libraries.
Now we have a tracer configured, we can create spans to describe what is happening in your application. For example, this could be a HTTP handler, a long running operation, or a database fetch. Spans are created in a parent-child pattern, so each time you create a new span, the current active span is used as its parent.
from opentelemetry import trace tracer = trace.get_tracer(__name__) with tracer.start_as_current_span("http-handler"): with tracer.start_as_current_span("my-cool-function"): # do something
It is often beneficial to add context to a currently executing span in a trace. For example, you may have an application or service that handles users and you want to associate the user with the span when querying your dataset in Honeycomb. In order to do this, get the current span from the context and set an attribute with the user ID:
from opentelemetry import trace ... span = trace.get_current_span() span.set_attribute("user.id", user.id())
To control how many spans are being sent to Honeycomb, you can configure a sampler to send a portion of your traffic. Configuration of sampling can be done with environment variables:
export OTEL_TRACES_SAMPLER="traceidratio" # set to a float representation of the desired ratio export OTEL_TRACES_SAMPLER_ARG=0.5 # set to the denominator of the desired ratio export OTEL_RESOURCE_ATTRIBUTES="SampleRate=2"
In the example above, our goal is to keep approximately half (1/2) of the data volume. The resource attribute contains the denominator (2), while the OpenTelemetry sampler argument contains the decimal value (0.5).
If you have multiple services that communicate with each other, it is important that they have the same sampling configuration. Otherwise, each service might make a different sampling decision, resulting in incomplete or broken traces. You can sample using a standalone proxy as an alternative, like Honeycomb Refinery, or when you have more robust sampling needs.
When a service calls another service, you want to ensure that the relevant trace information is propagated from one service to the other. This allows Honeycomb to connect the two services in a trace.
Distributed tracing enables you to trace and visualize interactions between multiple instrumented services. For example, your users may interact with a front-end API service, which talks to two internal APIs to fulfill their request. In order to have traces connect spans for all these services, it is necessary to propagate trace context between these services, usually by using an HTTP header.
Both the sending and receiving service must use the same propagation format, and both services must be configured to send data to the same Honeycomb dataset.
This trace has multiple spans sharing the same non-null span ID error appears in Honeycomb, it is likely that your application is not instrumented correctly and is sending the same trace to Honeycomb more than once.
One possible misconfiguration is initializing OpenTelemetry more than once. Make sure to only initialize OpenTelemetry once when the application starts, and then use the Tracing API throughout the application runtime to add manual instrumentation.
Using MyPy requires turning on support for namespace packages.
To turn on support from the command line, run:
Or to turn on support from your project configuration file, add:
[tool.mypy] namespace_packages = true
Using both the
strict option with MyPy and the OpenTelemetry Python SDK (instead of the API only) requires your project configuration file to include:
[mypy-opentelemetry.sdk.*] implicit_reexport = True
The OpenTelemetry Python SDK typically shows errors in the console when applicable.
If no errors appear but your data is not in Honeycomb as expected, use a
ConsoleSpanExporter to print your spans to the console.
This will help confirm whether your app is being instrumented with the data you expect.
ConsoleSpanExporter and temporarily replace the
from opentelemetry.sdk.trace.export import (SimpleSpanProcessor, ConsoleSpanExporter) trace.get_tracer_provider().add_span_processor( SimpleSpanProcessor(ConsoleSpanExporter()) )
Keep in mind that printing to the console is not recommended for production and should only be used for debugging purposes.
The service name is a required configuration value.
If it is unspecified, all trace data will be sent to a default dataset called
Honeycomb supports receiving telemetry data via OpenTelemetry’s native protocol, OTLP, over gRPC, HTTP/protobuf, and HTTP/JSON. The minimum supported versions of OTLP protobuf definitions are 0.7.0 for traces and metrics.
If the protobuf version in use by the SDK does not match a supported version by Honeycomb, a different version of the SDK may need to be used. If the SDK’s protobuf version is older than the minimum supported version, and telemetry is not appearing as expected in Honeycomb, upgrade the SDK to a version with the supported protobuf definitions. If using an added dependency on a proto library, ensure the version of protobuf definitions matches the supported version of the SDK.
You may receive a
464 error response from the Honeycomb API when sending telemetry using gRPC and HTTP1.
The gRPC format depends on using HTTP2 and any request over HTTP1 will be rejected by the Honeycomb servers.
Did you find what you were looking for?