Skip to main content

OpenTelemetry Wrapper to send traces, metrics and logs to my otel-proxy using OTLP Protocol

Project description

OpenTelemetry Wrapper

Python Tests

A comprehensive Python wrapper for OpenTelemetry that simplifies sending traces, metrics, and logs using the OTLP protocol.

Features

  • Unified API for traces, metrics, and logs
  • Simple configuration with reasonable defaults
  • Configurable endpoints for each signal type (traces, metrics, logs)
  • Distributed tracing with context propagation helpers
  • Multiple metric types (counters, gauges, histograms)
  • Error handling with graceful fallbacks
  • Singleton pattern ensures consistent resources across your application

Installation

pip install otel-wrapper

Or with Poetry:

poetry add otel-wrapper

Quick Start

from otel_wrapper.deps_injector import wrapper_builder

# Initialize the wrapper with your application name
telemetry = wrapper_builder("my-application")

# Create a trace
with telemetry.traces().span_in_context("my-operation") as (span, context):
    # Add span attributes
    span.set_attribute("operation.type", "example")
    
    # Create a log
    telemetry.logs().new_log(
        msg="Operation in progress", 
        tags={"operation": "my-operation"}, 
        level=20  # INFO level
    )
    
    # Record a metric
    telemetry.metrics().metric_increment(
        name="operations.count", 
        tags={"operation": "my-operation"}, 
        value=1.0
    )

Configuration

The wrapper can be configured using environment variables:

  • OTEL_EXPORTER_OTLP_ENDPOINT: Default endpoint for all signals
  • OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: Endpoint for traces
  • OTEL_EXPORTER_OTLP_METRICS_ENDPOINT: Endpoint for metrics
  • OTEL_EXPORTER_OTLP_LOGS_ENDPOINT: Endpoint for logs
  • OTEL_LOG_LEVEL: Log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
  • __SCOPE__: Application environment (defaults to "Production")

API Reference

Traces

# Create a simple span
span = telemetry.traces().new_span("my-span")
span.set_attribute("attribute.name", "value")
span.end()

# Use span as a context manager
with telemetry.traces().span_in_context("my-span") as (span, context):
    # Do something with the span
    pass

# Context propagation
headers = {}
telemetry.traces().inject_context_into_headers(headers)

# Extract context from headers
context = telemetry.traces().extract_context_from_headers(headers)

Metrics

# Increment a counter
telemetry.metrics().metric_increment(
    name="requests.count", 
    tags={"endpoint": "/api/users"}, 
    value=1.0
)

# Record a gauge value
telemetry.metrics().record_gauge(
    name="system.memory.usage", 
    tags={"host": "server-01"}, 
    value=1024.5
)

# Record a histogram value
telemetry.metrics().record_histogram(
    name="request.duration", 
    tags={"endpoint": "/api/users"}, 
    value=0.156
)

Logs

# Create a simple log
telemetry.logs().new_log(
    msg="User logged in", 
    tags={"user_id": "123"}, 
    level=20  # INFO level
)

# Get the logger directly
logger = telemetry.logs().get_logger()
logger.info("Message with structured data", extra={"key": "value"})

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

otel_wrapper-0.1.0.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

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

otel_wrapper-0.1.0-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file otel_wrapper-0.1.0.tar.gz.

File metadata

  • Download URL: otel_wrapper-0.1.0.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.9.21 Linux/6.8.0-1021-azure

File hashes

Hashes for otel_wrapper-0.1.0.tar.gz
Algorithm Hash digest
SHA256 72da842aed240cf93b66d66ff55c8c19732e24cc9d0f3814e3d4b6af9bc3ac19
MD5 d560ede0eba4ced2c0d1d610204b19fc
BLAKE2b-256 288564f58cb03151400f5cb4cef3a02157d1568be6d20a49af3045503700e971

See more details on using hashes here.

File details

Details for the file otel_wrapper-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: otel_wrapper-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.9.21 Linux/6.8.0-1021-azure

File hashes

Hashes for otel_wrapper-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 79c8764ccc0ecc77a5bccf06be0db5bd93f23e1b68adbe45a489931d04a9f2d0
MD5 9fab8bd1b47f91271ec07f8d00768c20
BLAKE2b-256 19bcd891bd21a816c98c6d16818a825b4bcf531b698cc13a9d354fed66efb8e0

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