Python Application
This document explains how to instrument the Python application with KloudMate, using OpenTelemetry.
This instrumentation can be done using the OpenTelemetry operator, which supports injecting and configuring auto-instrumentation libraries for .NET, Java, Node.js, Python, and Go service.
Pre-requisites:
- Running a Kubernetes cluster.
- The cert-manager must be installed. If you use the helm chart, there is an option to generate a self-signed cert instead.
Step 1: First, install the OpenTelemetry Operator into your cluster.
You can do this with the Operator release manifest, the Operator helm chart, or with Operator Hub.
Step 2: Create an OpenTelemetry collector to send telemetry from containers to a collector instead of directly to a backend.
For example, here is a demo collector used
Change your Kloudmate workspace API key to configure the collector to export data to Kloudmate.
Step 3: Configure automatic instrumentation
To be able to manage automatic instrumentation, the Operator needs to be configured to know what pods to instrument and which automatic instrumentation to use for those pods. This is done via the Instrumentation CRD
For our example, we used Python SDK auto instrumentation.
The following command creates a basic Instrumentation resource that is configured for instrumenting Python services.
By default, Python logs auto-instrumentation is disabled. By applying the above instrumentation user can only able to get the metrics and traces. To enable this feature(logs), you must set the OTEL_LOGS_EXPORTER and OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED environment variables as needed.
The following command creates instrumentation for metrics, logs, and traces.
The endpoint used under exporter must be the same as the service name of the collector created in Step 2. Use only one instrumentation command as per the requirement.
Step 4: Now opt-in your services to automatic instrumentation. This is done by updating your service’s spec.template.metadata.annotations to include a language-specific annotation
python:instrumentation.opentelemetry.io/inject-python: "true"
To patch your existing Python application with the necessary annotation use the below command
Python auto-instrumentation logs only work with certain libraries. After instrumentation, it's possible that some logs are missing. To fix this issue file log receiver could be a great choice.
To set up the file log receiver you only have to make some changes on the collector yaml file.
The example collector yaml file:
The above YAML configuration file is designed to configure a file log receiver that collects pod logs and exports them to KloudMate. Additionally, it is set up to export metrics and trace them to KloudMate.
If you're using the file log receiver to export logs instead of auto instrumentation, ensure that auto instrumentation logging is disabled. Otherwise, duplicate logs may appear. (The user can use the instrumentation command for metrics and traces)