Python
Integration
Section titled “Integration”Instrument your Python applications to send traces, logs, and metrics to Kopai.
Installation
Section titled “Installation”pip install opentelemetry-api opentelemetry-sdk opentelemetry-exporter-otlp-proto-httpBasic Setup
Section titled “Basic Setup”Initialize OpenTelemetry in your application:
from opentelemetry import trace, metricsfrom opentelemetry.sdk.resources import Resourcefrom opentelemetry.sdk.trace import TracerProviderfrom opentelemetry.sdk.trace.export import BatchSpanProcessorfrom opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporterfrom opentelemetry.sdk.metrics import MeterProviderfrom opentelemetry.sdk.metrics.export import PeriodicExportingMetricReaderfrom opentelemetry.exporter.otlp.proto.http.metric_exporter import OTLPMetricExporter
# Create resourceresource = Resource.create({"service.name": "my-service"})
# Setup tracestrace_provider = TracerProvider(resource=resource)trace_provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))trace.set_tracer_provider(trace_provider)
# Setup metricsmetric_reader = PeriodicExportingMetricReader(OTLPMetricExporter())meter_provider = MeterProvider(resource=resource, metric_readers=[metric_reader])metrics.set_meter_provider(meter_provider)The SDK reads the OTLP endpoint from standard OpenTelemetry environment variables.
Running Your Application
Section titled “Running Your Application”Set the required environment variables and run:
export OTEL_EXPORTER_OTLP_ENDPOINT="https://otlp.kopai.app"export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer YOUR_TOKEN"export OTEL_SERVICE_NAME="my-service"python app.py| Variable | Description |
|---|---|
OTEL_EXPORTER_OTLP_ENDPOINT | Kopai OTLP endpoint |
OTEL_EXPORTER_OTLP_HEADERS | Auth header with your backend token |
OTEL_SERVICE_NAME | Name shown in Kopai UI |
Auto-Instrumentation
Section titled “Auto-Instrumentation”For automatic instrumentation, use the distro package:
pip install opentelemetry-distroopentelemetry-bootstrap -a installopentelemetry-instrument python app.pyThis provides automatic instrumentation for Flask, Django, FastAPI, requests, and more.
Working Example
Section titled “Working Example”For a complete working example: