Skip to main content

Integration of comprehend.dev with OpenTelemetry in Python

Project description

comprehend-telemetry

OpenTelemetry integration for comprehend.dev - automatically capture and analyze your Python application's architecture, performance, and runtime metrics.

Installation

pip install comprehend-telemetry

Quick Start

Starting from scratch

If you don't have OpenTelemetry set up yet, here's a complete setup:

pip install comprehend-telemetry opentelemetry-api opentelemetry-sdk opentelemetry-exporter-otlp opentelemetry-instrumentation
import os
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
from opentelemetry.sdk.resources import Resource
from opentelemetry.instrumentation.auto_instrumentation import sitecustomize
from comprehend_telemetry import ComprehendSDK

# Set up OpenTelemetry with your service information
resource = Resource.create({
    "service.name": "my-python-service",
    "service.namespace": "production",
    "deployment.environment": "prod"
})

comprehend = ComprehendSDK(
    organization='your-org',
    token=os.getenv("COMPREHEND_SDK_TOKEN"),
    debug=True  # Optional: enable debug logging (or pass a custom logger function)
)

tracer_provider = TracerProvider(
    resource=resource,
    active_span_processor=comprehend.get_span_processor(),
)
trace.set_tracer_provider(tracer_provider)

meter_provider = MeterProvider(
    resource=resource,
    metric_readers=[
        PeriodicExportingMetricReader(
            comprehend.get_metrics_exporter(),
            export_interval_millis=15000,
        )
    ],
)

Adding to existing OpenTelemetry setup

If you already have OpenTelemetry configured, create a ComprehendSDK instance and add its processor and exporter:

import os
from comprehend_telemetry import ComprehendSDK

comprehend = ComprehendSDK(
    organization='your-org',
    token=os.getenv("COMPREHEND_SDK_TOKEN"),
)

# Add to your existing tracer provider:
tracer_provider.add_span_processor(comprehend.get_span_processor())

# Add metrics reader to your meter provider:
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader

metric_reader = PeriodicExportingMetricReader(
    comprehend.get_metrics_exporter(),
    export_interval_millis=15000,
)

Process metrics (CPU and memory)

For process-level CPU and memory metrics, add opentelemetry-instrumentation-system-metrics:

pip install opentelemetry-instrumentation-system-metrics
from opentelemetry.instrumentation.system_metrics import SystemMetricsInstrumentor

SystemMetricsInstrumentor(config={
    "process.cpu.time": ["user", "system"],
    "process.cpu.utilization": None,
    "process.memory.usage": None,
    "process.memory.virtual": None,
}).instrument()

This collects process.cpu.time, process.cpu.utilization, process.memory.usage, and process.memory.virtual. The config dictionary limits collection to process-level metrics only, avoiding the overhead of system-wide CPU, memory, disk, and network data gathering.

Configuration

Set your comprehend.dev SDK token as an environment variable:

export COMPREHEND_SDK_TOKEN=your-token-here

Note: In production environments, the token should be stored in a secure secret management system (such as AWS Secrets Manager, HashiCorp Vault, Azure Key Vault, Kubernetes Secrets, or your cloud provider's secret management service) and injected into the environment through your container orchestrator's workload definition or service configuration.

What it captures

This integration automatically captures:

  • HTTP Routes - API endpoints and their usage patterns
  • Database Operations - SQL queries (analysis done server-side)
  • Service Dependencies - HTTP client calls to external services
  • Performance Metrics - Request durations, response codes, error rates
  • Service Architecture - Automatically maps your service relationships
  • Trace Spans - Span identity and parent relationships for connecting observations to traces
  • Runtime Metrics - Process CPU and memory metrics
  • Custom Metrics - Server-configured custom metric and span collection

Requirements

  • Python 3.8+
  • OpenTelemetry SDK (peer dependencies: opentelemetry-api, opentelemetry-sdk)

Framework Support

Works with any Python framework that supports OpenTelemetry auto-instrumentation:

  • FastAPI
  • Django
  • Flask
  • SQLAlchemy
  • Requests
  • HTTPx
  • psycopg2
  • And more...

Learn More

Development

See DEVELOPMENT.md for development setup and release instructions.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

comprehend_telemetry-0.2.0.tar.gz (27.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

comprehend_telemetry-0.2.0-py3-none-any.whl (17.5 kB view details)

Uploaded Python 3

File details

Details for the file comprehend_telemetry-0.2.0.tar.gz.

File metadata

  • Download URL: comprehend_telemetry-0.2.0.tar.gz
  • Upload date:
  • Size: 27.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.10

File hashes

Hashes for comprehend_telemetry-0.2.0.tar.gz
Algorithm Hash digest
SHA256 0a2a5ed1a32476704bedfecb9eb1ccc7602f906e0b3874b5333ec4980a4eaf7c
MD5 33109c53f951181cfc8324ffa7c8b156
BLAKE2b-256 28476410cd1ff0aabb21743d32e97017ec8388a47068594d1b80e51a0dae06ba

See more details on using hashes here.

File details

Details for the file comprehend_telemetry-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for comprehend_telemetry-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 172ca158cc74767b57e36801a93282e78ebbc2e07f01b826a576ebc4646cf17e
MD5 73414f50db63ce798e95f99497916b54
BLAKE2b-256 baa2ddd870140619dcca8eef575d62c3958027fdf36392cbea9c55a7720b60b3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page