Python Application Instrumentation

Last updated on 13 March, 2023

You can use the following methods for OpenTelemetry-facilitated instrumentation of a Python application:

  • (Recommended) Automatic instrumentation—This option is the fastest way to get started, but gives you less control over how the trace data is emitted. LogicMonitor provides a wizard for automatic instrumentation of Python applications.
    Alternatively, you can automatically instrument a Python application outside your LogicMonitor portal. This requires you to download libraries specific to the supported applications in use, configure an OTLP exporter, and run the instrumented Python application. Operation names are automatically set. For more information, see the supported applications from OpenTelemetry.
    If your environment uses Kubernetes to run your Python applications, you can automatically instrument the application within your Kubernetes infrastructure. This involves extending your Docker image using a command-line interface (CLI).
  • Manual instrumentation—This is the only option for applications that use libraries and frameworks that are not supported by automatic instrumentation. With manual instrumentation, you write code in your application that uses the OpenTelemetry SDK to capture and emit the trace data.

Depending on your environment, you may need a combination of both automatic and manual instrumentation.

Requirements for Instrumenting a Python Application with LogicMonitor

To instrument a Python application, you must install and configure an OpenTelemetry Collector to forward trace data to your LogicMonitor portal. For more information, see OpenTelemetry Collector Installation.

Automatically Instrumenting a Python Application using the Instrumentation Wizard in LogicMonitor

Note: To automatically instrument a Python application outside your LogicMonitor portal, see OpenTelemetry’s Automatic Instrumentation documentation.

  1. Navigate to Traces > Onboarding and select Instrument Your Application.
  2. Click Select for Python, and then select Automatic (Recommended).
  3. When prompted, specify the Python libraries your application uses (for example, requests of grpc), and LogicMonitor provides a pip install command for each library.

Important: Install three client libraries generic to OpenTelemetry instrumentation and one instrumentation client library for each application library you specify.

  1. At the “Initialize Tracer” step of the wizard, use the provided script to customize the values specific to your Python application.

Note: The script is for a sample app called “server”.

  1. When prompted, enter the following information:
    • (Recommended) Service Namespace
      This is used to represent a grouping of services. If specified, it is added as a property to each auto-created service and a parent service is created to represent the namespace. You may find this useful for organizing applications with multiple underlying services.
    • (Required) Service Name
      This is also used as the display name for the service in your LogicMonitor portal. Special characters, including spaces, are not allowed.
    • (Recommended) Custom Tags
      These are associated with all the operations for this service. For example, you can add tags that represent the business or technical priority of the service.

Note: LogicMonitor requires the following custom tags to map traces to existing monitored resources: ip, resource.type (which should be set to kubernetes-pod, cloud, or host), and (which should be the pod name for Kubernetes). Use these custom tags when possible to ensure that traces are shown in the context of logs, metrics, and alerts.

  1. Copy the commands that are generated based on the information you provided and run them on the server where your application is running.

For more information about automatically instrumenting a Python application outside your LogicMonitor portal, see OpenTelemetry’s Automatic Instrumentation documentation.

Automatically Instrumenting a Python Application in Kubernetes

  1. Extend your Docker image to incorporate the instrumentation steps. For example, you can add the following to your Dockerfile:
#add OpenTelemetry instrumentation packages and set env vars (alternatively can be done in Kube YAML)
RUN pip install opentelemetry-api opentelemetry-sdk opentelemetry-exporter-otlp-proto-grpc opentelemetry-instrumentation-grpc 
  1. Change the ENTRYPOINT to start your application with the instrumentation:
ENTRYPOINT ["opentelemetry-instrument", "python", ""]

Manually Instrumenting a Python Application

You can manually instrument your Python application if your application is not supported for OpenTelemetry automatic instrumentation, or if you want to customize how traces are generated by automatic instrumentation. For example, you may want to customize how operations are named. For more information, see the supported applications for OpenTelemetry.

For more information about manually instrumenting your Python application, the OpenTelemetry SDK, creating and enriching traces, see OpenTelemetry’s Manual Instrumentation documentation. 

In This Article