Support Center Home


Instrumenting a Python application

There are two ways to approach OpenTelemetry-facilitated instrumentation for a Java application:

  1. (Recommended) Automatic instrumentation is the fastest way to get started, but gives you less control over how the trace data is emitted. With auto-instrumentation, you’ll download a few packages specific to the supported  applications in use, configure an OTLP exporter, and run the instrumented Python application. Operation names are automatically set.
  2. Manual instrumentation is your only option for applications that use libraries and frameworks that are not supported by auto-instrumentation. With manual instrumentation, you’ll write code in your application that uses the OpenTelemetry SDK to capture and emit the trace data.

In the end, you may need a combination of both manual and auto-instrumentation.

Prerequisites

Automatic Instrumentation

LogicMonitor provides a configuration wizard for automatic Instrumentation of Python applications.

1. At the top right of the Traces page, select Onboarding.

2. Select Python, and confirm automatic instrumentation.

The wizard will prompt you to specify the Python libraries that your application uses, such as requests of grpc, and then the UI will provide a pip install command for each library.

3. (Required) Install three client libraries generic to OpenTelemetry instrumentation and one instrumentation client library for each application library you specify.

4. Once you’ve installed the necessary libraries for instrumentation, you will see a script tracer.py on the next screen.

This script show how the tracer can be initialized for an app called “server”. You can use this script directly, customizing it with the values specific to your Python application.

5. Next, the wizard will prompt you for the following information:

  • (Required) The name of the service you are instrumenting. This name will also be used as the display name for the service in your LogicMonitor portal. Special characters, such as spaces, are not allowed.
  • (Recommended) Custom tags which will be associated with all the operations for this service. For example, you may want to add tags that represent the business or technical priority of the service. See Note.
  • (Optional) The service namespace, which can be used to represent a grouping of services. If specified, it will be added as a property to each auto-created service and a parent service will be created to represent the namespace. You may find this useful for organizing applications with multiple underlying services.

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 host.name (which should be the pod name for Kubernetes). We recommend using these custom tags when possible to ensure that traces can be shown in the context of logs, metrics, and alerts.

6. Based on what you entered in the three fields, an opentelemetry-instrument command for instrumenting the application in tracer.py will be generated. Copy and run this command on the server where your application is running to start your application.

Kubernetes configuration

(Recommended) When instrumenting your application in Kubernetes, extend your Docker image to incorporate the instrumentation steps. For example, you may 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 
ENV LMOTEL_ENDPOINT="http://10.74.13.181:55680" 
ENV OTEL_RESOURCE_ATTRIBUTES=service.name=recommendationServiceProd,resource.type=kubernetes-pod,ip=$POD_IP,host.name=$POD_NAME

Change the entrypoint to start your application with the instrumentation:

ENTRYPOINT ["opentelemetry-instrument", "python", "tracer.py"]

Manual Instrumentation

You may need to manually instrument some or all of your Python applications if (1) you are using applications that aren’t supported for the OpenTelemetry auto-instrumentation; or (2) you want to customize how traces are generated by auto-instrumentation (e.g. how operations are named). 

The best source of instruction for manually instrumenting your Python application is the official OpenTelemetry documentation. This documentation will guide you through instantiating the OpenTelemetry SDK and a tracer, creating traces, and enriching them.

In This Article