Python Manual Instrumentation with OpenTracing

Want to use OpenTelemetry instead? Read these docs to get started!

Follow these steps to configure your tracer to communicate with the Lightstep Microsatellites and create a single span on your service. You install both the OpenTracing API and Lightstep tracer and then use the OpenTracing and Lightstep APIs to instrument your code.

While Lightstep offers tracers and APIs specific to its tracing software, you will still use the OpenTracing API to fully instrument your code. Be sure you have read and are familiar with both Lightstep tracers and the OpenTracing specification in your application’s language.

Python Instrumentation for OpenTracing

  1. Find your access token in Lightstep. You’ll need this to configure your Lightstep tracer.
    • Click the Project Settings button.
    • In the Access Tokens table, click the Copy icon to copy your access token to the clipboard.
  2. Install the Lightstep tracer and OpenTracing API.

    1
    
    pip install lightstep opentracing
    
  3. Import the Lightstep tracer and OpenTracing API.

    1
    2
    
    import opentracing
    import lightstep
    
  4. Early in your application’s initialization, configure the Lightstep tracer and register it as the OpenTracing global tracer. As part of the configuration, you need to add your access token and add your service’s name.

    1
    2
    3
    4
    
    opentracing.tracer = lightstep.Tracer(
      component_name='YOUR_SERVICE_NAME',
      access_token='YOUR_ACCESS_TOKEN'
     )
    
  5. Test that everything is connected by sending a test span. Annotate the span by adding a tag (key/value pair) and logs, then flush the tracer.

    1
    2
    3
    4
    5
    6
    7
    8
    
      with opentracing.tracer.start_active_span('TestSpan') as scope:
        scope.span.log_event('test message', payload={'life': 42})
        # for tag value, use either "client" or "server" depending on whether
        # this service receives or creates requests
        scope.span.set_tag('span.kind', 'server')
    
      # make sure to send the span by flushing the tracer.       
      opentracing.tracer.flush()
    
  6. Run your app.

  7. Open Lightstep. You should see your service in the Service directory list.

It may take a few moments for your service to display in the Service Directory. To see data immediately, click the Explorer tab to view data in the histogram.

Read the OpenTracing Python docs to learn more.

You can also checkout these samples for examples of how to use tracing in Python:

See also

Python Auto-Instrumentation

Measure your instrumentation quality

Updated Mar 2, 2020