|
1 |
| -# otel-wrapper |
2 |
| -OpenTelemetry Wrapper to send traces, metrics and logs to my otel-proxy using OTLP Protocol |
| 1 | +# OpenTelemetry Wrapper |
| 2 | + |
| 3 | +[](https://github.com/ivanildobarauna-dev/open-o11y-wrapper/actions/workflows/python-tests.yml) |
| 4 | + |
| 5 | +A comprehensive Python wrapper for OpenTelemetry that simplifies sending traces, metrics, and logs using the OTLP protocol. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- **Unified API** for traces, metrics, and logs |
| 10 | +- **Simple configuration** with reasonable defaults |
| 11 | +- **Configurable endpoints** for each signal type (traces, metrics, logs) |
| 12 | +- **Distributed tracing** with context propagation helpers |
| 13 | +- **Multiple metric types** (counters, gauges, histograms) |
| 14 | +- **Error handling** with graceful fallbacks |
| 15 | +- **Singleton pattern** ensures consistent resources across your application |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +```bash |
| 20 | +pip install otel-wrapper |
| 21 | +``` |
| 22 | + |
| 23 | +Or with Poetry: |
| 24 | + |
| 25 | +```bash |
| 26 | +poetry add otel-wrapper |
| 27 | +``` |
| 28 | + |
| 29 | +## Quick Start |
| 30 | + |
| 31 | +```python |
| 32 | +from otel_wrapper.deps_injector import wrapper_builder |
| 33 | + |
| 34 | +# Initialize the wrapper with your application name |
| 35 | +telemetry = wrapper_builder("my-application") |
| 36 | + |
| 37 | +# Create a trace |
| 38 | +with telemetry.traces().span_in_context("my-operation") as (span, context): |
| 39 | + # Add span attributes |
| 40 | + span.set_attribute("operation.type", "example") |
| 41 | + |
| 42 | + # Create a log |
| 43 | + telemetry.logs().new_log( |
| 44 | + msg="Operation in progress", |
| 45 | + tags={"operation": "my-operation"}, |
| 46 | + level=20 # INFO level |
| 47 | + ) |
| 48 | + |
| 49 | + # Record a metric |
| 50 | + telemetry.metrics().metric_increment( |
| 51 | + name="operations.count", |
| 52 | + tags={"operation": "my-operation"}, |
| 53 | + value=1.0 |
| 54 | + ) |
| 55 | +``` |
| 56 | + |
| 57 | +## Configuration |
| 58 | + |
| 59 | +The wrapper can be configured using environment variables: |
| 60 | + |
| 61 | +- `OTEL_EXPORTER_OTLP_ENDPOINT`: Default endpoint for all signals |
| 62 | +- `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT`: Endpoint for traces |
| 63 | +- `OTEL_EXPORTER_OTLP_METRICS_ENDPOINT`: Endpoint for metrics |
| 64 | +- `OTEL_EXPORTER_OTLP_LOGS_ENDPOINT`: Endpoint for logs |
| 65 | +- `OTEL_LOG_LEVEL`: Log level (DEBUG, INFO, WARNING, ERROR, CRITICAL) |
| 66 | +- `__SCOPE__`: Application environment (defaults to "Production") |
| 67 | + |
| 68 | +## API Reference |
| 69 | + |
| 70 | +### Traces |
| 71 | + |
| 72 | +```python |
| 73 | +# Create a simple span |
| 74 | +span = telemetry.traces().new_span("my-span") |
| 75 | +span.set_attribute("attribute.name", "value") |
| 76 | +span.end() |
| 77 | + |
| 78 | +# Use span as a context manager |
| 79 | +with telemetry.traces().span_in_context("my-span") as (span, context): |
| 80 | + # Do something with the span |
| 81 | + pass |
| 82 | + |
| 83 | +# Context propagation |
| 84 | +headers = {} |
| 85 | +telemetry.traces().inject_context_into_headers(headers) |
| 86 | + |
| 87 | +# Extract context from headers |
| 88 | +context = telemetry.traces().extract_context_from_headers(headers) |
| 89 | +``` |
| 90 | + |
| 91 | +### Metrics |
| 92 | + |
| 93 | +```python |
| 94 | +# Increment a counter |
| 95 | +telemetry.metrics().metric_increment( |
| 96 | + name="requests.count", |
| 97 | + tags={"endpoint": "/api/users"}, |
| 98 | + value=1.0 |
| 99 | +) |
| 100 | + |
| 101 | +# Record a gauge value |
| 102 | +telemetry.metrics().record_gauge( |
| 103 | + name="system.memory.usage", |
| 104 | + tags={"host": "server-01"}, |
| 105 | + value=1024.5 |
| 106 | +) |
| 107 | + |
| 108 | +# Record a histogram value |
| 109 | +telemetry.metrics().record_histogram( |
| 110 | + name="request.duration", |
| 111 | + tags={"endpoint": "/api/users"}, |
| 112 | + value=0.156 |
| 113 | +) |
| 114 | +``` |
| 115 | + |
| 116 | +### Logs |
| 117 | + |
| 118 | +```python |
| 119 | +# Create a simple log |
| 120 | +telemetry.logs().new_log( |
| 121 | + msg="User logged in", |
| 122 | + tags={"user_id": "123"}, |
| 123 | + level=20 # INFO level |
| 124 | +) |
| 125 | + |
| 126 | +# Get the logger directly |
| 127 | +logger = telemetry.logs().get_logger() |
| 128 | +logger.info("Message with structured data", extra={"key": "value"}) |
| 129 | +``` |
| 130 | + |
| 131 | +## Contributing |
| 132 | + |
| 133 | +Contributions are welcome! Please feel free to submit a Pull Request. |
| 134 | + |
| 135 | +## License |
| 136 | + |
| 137 | +This project is licensed under the MIT License - see the LICENSE file for details. |
0 commit comments