A unified OpenTelemetry observability package for Python applications
Project description
OTEL Observability Package
A unified OpenTelemetry observability package for Python applications that provides easy-to-use logging, metrics, and tracing with best practices.
Features
- Unified Configuration: Configure all observability components through environment variables or programmatic configuration
- Multiple Exporters: Support for OTLP (gRPC), HTTP, and Console exporters
- Thread-Safe: Singleton pattern ensures thread-safe initialization
- Easy Integration: Simple API for logging, metrics, and tracing
- Decorators: Built-in decorators for automatic tracing and logging
Installation
From PyPI
pip install otel-observability
From Source
git clone https://github.com/Touzi-Mortadha/otel-observability.git
cd otel-observability
pip install -e .
Quick Start
Basic Usage with Environment Variables
from otel_observability import initialize_observability, get_logger, get_metrics, get_traces
# Initialize observability (reads from environment variables)
manager = initialize_observability()
# Get a logger
logger = get_logger(__name__)
logger.info("Application started")
# Get a meter for metrics
meter = get_metrics("my_app")
request_counter = meter.create_counter("requests_total", description="Total requests")
request_counter.add(1, {"endpoint": "/api"})
# Get a tracer for distributed tracing
tracer = get_traces("my_app")
with tracer.start_as_current_span("process_request") as span:
span.set_attribute("user.id", "123")
logger.info("Processing request")
Programmatic Configuration
from otel_observability import ObservabilityManager, ObservabilityConfig
# Create custom configuration
config = ObservabilityConfig(
app_name="my-app",
component="test-component",
otlp_endpoint="localhost:4317", # OTLP gRPC endpoint
log_level=LogLevel.INFO,
insecure=True,
)
# Initialize with custom config
manager = ObservabilityManager(config)
manager.initialize_all()
# Use the manager directly
logger = manager.get_logger(__name__)
meter = manager.get_meter("my_app")
tracer = manager.get_tracer("my_app")
Using Decorators
from otel_observability import ObservabilityDecorators
@ObservabilityDecorators.trace_method()
@ObservabilityDecorators.log_execution()
def process_data(data):
"""Process data with automatic tracing and logging."""
logger.info(f"Processing data: {data}")
return {"processed": True, **data}
result = process_data({"input": "test"})
Configuration
Environment Variables
| Variable | Description | Default |
|---|---|---|
OTEL_APP_NAME |
App name for resource attributes | unknown-service |
OTEL_COMPONENT_NAME |
service name for resource attributes | unknown-component |
OTEL_GRPC_URL |
OTLP gRPC endpoint URL | None |
HTTP_ENDPOINT |
Base HTTP URL for OTLP HTTP exporters | None |
OTEL_HTTP_LOGS_URL |
Specific HTTP URL for logs | {HTTP_ENDPOINT}/v1/logs |
OTEL_HTTP_TRACES_URL |
Specific HTTP URL for traces | {HTTP_ENDPOINT}/v1/traces |
OTEL_HTTP_METRICS_URL |
Specific HTTP URL for metrics | {HTTP_ENDPOINT}/v1/metrics |
OTEL_INSECURE |
Use insecure connection for gRPC | true |
LOG_LEVEL |
Log level (DEBUG, INFO, WARNING, ERROR, CRITICAL) | INFO |
OTEL_METRIC_EXPORT_INTERVAL_MS |
Metrics export interval in milliseconds | 60000 |
ENABLE_CONSOLE_DEBUG |
Enable console output for debugging | false |
Programmatic Configuration
Create an ObservabilityConfig instance with the following parameters:
app_name: Your app name (required)component: Your service name (required)otlp_endpoint: OTLP gRPC endpoint URLhttp_logs_url: HTTP endpoint for logshttp_traces_url: HTTP endpoint for traceshttp_metrics_url: HTTP endpoint for metricsinsecure: Use insecure connection (default: True)log_level: Log level (default: LogLevel.INFO)metric_export_interval_ms: Metrics export interval (default: 60000)enable_console_debug: Enable console debugging (default: False)
API Reference
Core Functions
initialize_observability(): Initialize all components and return managerget_logger(name): Get a configured logger instanceget_metrics(name, version): Get a meter for creating metricsget_traces(name, version): Get a tracer for creating spans
ObservabilityManager
The main manager class providing:
get_logger(name): Get logger by nameget_meter(name, version): Get meter by name and versionget_tracer(name, version): Get tracer by name and versioncreate_counter(): Create a counter metriccreate_histogram(): Create a histogram metricshutdown(): Gracefully shutdown all providers
ObservabilityDecorators
@trace_method(): Automatically trace method execution@log_execution(): Automatically log method execution
Examples
Flask Application
from flask import Flask
from otel_observability import initialize_observability, get_logger, ObservabilityDecorators
app = Flask(__name__)
manager = initialize_observability()
logger = get_logger(__name__)
@app.route('/')
@ObservabilityDecorators.trace_method()
def hello():
logger.info("Hello endpoint called")
return "Hello World!"
if __name__ == '__main__':
app.run()
FastAPI Application
from fastapi import FastAPI
from otel_observability import initialize_observability, get_logger, get_traces
app = FastAPI()
manager = initialize_observability()
logger = get_logger(__name__)
tracer = get_traces("fastapi_app")
@app.get("/")
async def read_root():
with tracer.start_as_current_span("read_root"):
logger.info("Root endpoint called")
return {"Hello": "World"}
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file otel_observability-0.1.2.tar.gz.
File metadata
- Download URL: otel_observability-0.1.2.tar.gz
- Upload date:
- Size: 16.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3aef0e0fed5de460ddfb17508f5fadc873c6bb8c028182c81b6280e6a093e1db
|
|
| MD5 |
c0bfac45d9c4fba42d642fafde299412
|
|
| BLAKE2b-256 |
3f5b6688553f5fe683c7068c0d05b53ac3034acfd14d624cad756499f9ce3753
|
Provenance
The following attestation bundles were made for otel_observability-0.1.2.tar.gz:
Publisher:
python-publish.yml on Touzi-Mortadha/otel-observability
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
otel_observability-0.1.2.tar.gz -
Subject digest:
3aef0e0fed5de460ddfb17508f5fadc873c6bb8c028182c81b6280e6a093e1db - Sigstore transparency entry: 630402527
- Sigstore integration time:
-
Permalink:
Touzi-Mortadha/otel-observability@4f529ffa0c87fdbf07526c78221efa9a45bf5a15 -
Branch / Tag:
refs/tags/0.1.2 - Owner: https://github.com/Touzi-Mortadha
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@4f529ffa0c87fdbf07526c78221efa9a45bf5a15 -
Trigger Event:
release
-
Statement type:
File details
Details for the file otel_observability-0.1.2-py3-none-any.whl.
File metadata
- Download URL: otel_observability-0.1.2-py3-none-any.whl
- Upload date:
- Size: 13.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d7b89be245f753c9bf73f479f45153e91802d67362a072bf8bf9ee97ce238eae
|
|
| MD5 |
653ad0531351bcd29d41b01b6df54349
|
|
| BLAKE2b-256 |
8d731bc76b165485b0582c53deb3da6330821ebc7cb1566f314049ce02d6a7de
|
Provenance
The following attestation bundles were made for otel_observability-0.1.2-py3-none-any.whl:
Publisher:
python-publish.yml on Touzi-Mortadha/otel-observability
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
otel_observability-0.1.2-py3-none-any.whl -
Subject digest:
d7b89be245f753c9bf73f479f45153e91802d67362a072bf8bf9ee97ce238eae - Sigstore transparency entry: 630402528
- Sigstore integration time:
-
Permalink:
Touzi-Mortadha/otel-observability@4f529ffa0c87fdbf07526c78221efa9a45bf5a15 -
Branch / Tag:
refs/tags/0.1.2 - Owner: https://github.com/Touzi-Mortadha
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@4f529ffa0c87fdbf07526c78221efa9a45bf5a15 -
Trigger Event:
release
-
Statement type: