Utilidades simplificadas para instrumentación con OpenTelemetry
Project description
OpenTelemetry Utils
A Python library designed to simplify application instrumentation using OpenTelemetry. This library provides an abstraction layer that makes instrumentation more intuitive and less intrusive in your business logic.
Features
- Simplified OpenTelemetry configuration
- Intuitive API for distributed tracing
- Utilities for metrics and structured logging
- OpenTelemetry Collector integration
- Complete context propagation support
- Full compatibility with asynchronous applications
Installation
pip install otel-utils
Basic Usage
Initial Configuration
from otel_utils import OtelConfig, OtelConfigurator
config = OtelConfig(
service_name="my-service",
environment="production",
otlp_endpoint="http://localhost:4318", # Optional
protocol="http", # "http" or "grpc", default "grpc"
trace_sample_rate=1.0, # Sampling rate, default 1.0
metric_export_interval_ms=30000, # Metrics export interval
log_level=logging.INFO, # Logging level
enable_console_logging=True, # Enable console logging
additional_resources={ # Optional additional resources
"deployment.region": "us-east-1",
"team.name": "backend"
}
)
OtelConfigurator(config)
Tracing
from otel_utils import Tracer
tracer = Tracer("my-service")
# Using the decorator
@tracer.trace("my_operation")
async def my_function():
# Your code here
pass
# Using the context manager
with tracer.create_span("my_operation") as span:
span.set_attribute("key", "value")
# Your code here
Metrics
from otel_utils import Metrics
metrics = Metrics("my-service")
# Simple counter
counter = metrics.get_counter("requests_total")
counter.add(1, {"endpoint": "/api/v1/resource"})
# Histogram for latencies
with metrics.measure_duration("request_duration"):
# Your code here
pass
Structured Logging
from otel_utils import StructuredLogger
logger = StructuredLogger("my-service")
with logger.operation_context("process_order", order_id="123"):
logger.info("Starting processing")
# Your code here
OpenTelemetry Collector Integration
This library is designed to work seamlessly with the OpenTelemetry Collector. Telemetry data is sent using the OTLP protocol, which is the OpenTelemetry standard.
Collector Configuration with HTTP
receivers:
otlp:
protocols:
http:
endpoint: 0.0.0.0:4318
exporters:
# configure your exporters here
service:
pipelines:
traces:
receivers: [otlp]
exporters: [your-exporter]
Collector Configuration with gRPC
receivers:
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:4317
exporters:
# configure your exporters here
service:
pipelines:
traces:
receivers: [otlp]
exporters: [your-exporter]
Best Practices
Separation of Concerns
Keep instrumentation separate from business logic by creating domain-specific abstractions. Your business code should remain clean and focused on its primary responsibilities.
Consistent Naming
Use coherent naming conventions for spans, metrics, and logs across your services. This makes it easier to correlate and analyze telemetry data.
Relevant Context
Include useful contextual information in spans and logs, but be mindful of sensitive data. Focus on information that aids debugging and monitoring.
Appropriate Granularity
Don't instrument everything. Focus on significant operations that provide value for monitoring and debugging. Consider the overhead and noise ratio when adding instrumentation.
Development
To set up the development environment:
# Create virtualenv
python -m venv venv
source venv/bin/activate # or `venv\Scripts\activate` on Windows
# Install development dependencies
pip install -e ".[dev]"
# Run tests
pytest
Contributing
- Create a feature branch (
git checkout -b feature/new-feature) - Commit your changes (
git commit -am 'Add new feature') - Push to the branch (
git push origin feature/new-feature) - Create a Pull Request
Project details
Release history Release notifications | RSS feed
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_utils-1.1.1.tar.gz.
File metadata
- Download URL: otel_utils-1.1.1.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.11.13 Linux/6.11.0-1015-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
10d6442d7cddf549010364f14c954130282f637b3757eceb116c4f325ba2dda0
|
|
| MD5 |
26a894734f3d97751e7e8847694c8e18
|
|
| BLAKE2b-256 |
bae554f3992ed3d7485d24557e01a7955701adc833277c20435cff3ef5607849
|
File details
Details for the file otel_utils-1.1.1-py3-none-any.whl.
File metadata
- Download URL: otel_utils-1.1.1-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.11.13 Linux/6.11.0-1015-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
60f2cc31cc17e91b764aac2a39d3f6636831c270d4840968da8095fa1aacd923
|
|
| MD5 |
b095a606501e559a5bcc46d67cafca4a
|
|
| BLAKE2b-256 |
33bdd709fc8a80c42d6704b67fac001d55408226b2d8a790a6c464e793b8f784
|