OpenTelemetry Python Configuration

Ship traces from Python to OpenSearch with OpenTelemetry

Use OpenTelemetry to easily send Python traces to your Logit.io Stack.

Install Integration

Please click on the Install Integration button to configure your stack for this source.

This sample app was created and tested with

Python 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42)

pip 22.0.4

Install

Create a new directory for your project and name it opentelemetry_python_example.

Open a Terminal window or Command Prompt and navigate into the new opentelemetry_python_example folder. Create a virtual environment for your project to manage dependencies, this is done by entering the following command.

python -m venv venv
venv\Scripts\activate

We now need to install the necessary OpenTelemetry packages using pip, the packages that we will be using are as follows:

  • opentelemetry-api: Core API for OpenTelemetry
  • opentelemetry-sdk: SDK implementation
  • opentelemetry-exporter-otlp: Exporter to send data to the OpenTelemetry Collector or other OTLP-compatible backends
  • opentelemetry-instrumentation-flask: Automatically instrument your Flask applications for tracing with OpenTelemetry

We install them with the following command.

pip install flask opentelemetry-api opentelemetry-sdk opentelemetry-instrumentation-flask opentelemetry-exporter-otlp

Create two new files in the folder and call them app.py and config.py. There should now be a two files in the opentelemetry_python_example folder and a folder called venv.

Configuring the App

Copy and Paste the code below into config.py.

⚠️

You will need to update any placeholder variables with your OpenTelemetry details.

config.py
# config.py
 
SERVICE_NAME = "LogitPythonTestApp"
OTLP_ENDPOINT = "https://@opentelemetry.endpointAddress"
PORT = "@opentelemetry.httpsPort"
USERNAME = "@opentelemetry.username"
PASSWORD = "@opentelemetry.password"

Copy and Paste the code below into app.py

app.py
# app.py
 
import base64
from datetime import datetime
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from flask import Flask
 
app = Flask(__name__)
 
# Importing configuration variables from config.py
from config import SERVICE_NAME, OTLP_ENDPOINT, PORT, USERNAME, PASSWORD
 
def get_basic_auth_header(username, password):
    """Encodes the username and password to base64 for Basic Auth."""
    credentials = f"{username}:{password}"
    auth_bytes = base64.b64encode(credentials.encode('utf-8'))
    auth_header = auth_bytes.decode('utf-8')
    return f"Basic {auth_header}"
 
def init_tracer():
    # Create a Resource to associate with all traces
    resource = Resource.create({"service.name": SERVICE_NAME})
 
    # Set up the TracerProvider and add the OTLP HTTP exporter with Basic Auth
    tracer_provider = TracerProvider(resource=resource)
    trace.set_tracer_provider(tracer_provider)
 
    # Generate the Basic Auth header
    auth_header = get_basic_auth_header(USERNAME, PASSWORD)
  
    #Generate the full endpoint
    endpoint = f"{OTLP_ENDPOINT}:{PORT}/v1/traces"
 
    # Create the OTLP HTTP exporter with the Basic Auth header
    otlp_exporter = OTLPSpanExporter(
        endpoint=endpoint,
        headers={"Authorization": auth_header}
    )
 
    # Add the exporter to the tracer provider
    span_processor = BatchSpanProcessor(otlp_exporter)
    tracer_provider.add_span_processor(span_processor)
 
@app.route('/')
def hello_world():
    # Initialize the tracer
    init_tracer()
    tracer = trace.get_tracer(__name__)
 
    with tracer.start_as_current_span("parent-span") as parent_span:
        now = datetime.now()
        parent_span.set_attribute("FirstMessage", "This is a test!")
        parent_span.set_attribute("SecondMessage", "Message sent at " + now.strftime("%H:%M:%S"))
 
        # Start the second span within the first span
        with tracer.start_as_current_span("child-span") as child_span:
            child_span.set_attribute("FirstMessage", "This is another test!")
    
    with tracer.start_as_current_span("hello-span"):
        return "Hello, World!"
      
 
if __name__ == '__main__':
    app.run()

Run the Python App

Run the Python app with the following command in Terminal or the Command Prompt window.

python app.py

You will see feedback from the app so that you know that is running, make a note of the address and port that it is running on.

Python Endpoint

Open your browser and enter the address and port you made a note of in the previous step, the browser will return the following

Browser Page

The action of browsing to the page served by the Python app with sent traces to your Stack.

Launch Logit.io to view your traces

Launch APM

How to diagnose no data in Stack

If you don't see data appearing in your stack after following this integration, take a look at the troubleshooting guide for steps to diagnose and resolve the problem or contact our support team and we'll be happy to assist.