Skip to content

Commit 7bd6829

Browse files
Merge pull request #7 from ivanildobarauna-dev/feature-otel-improvements
Enhancement: Comprehensive OpenTelemetry Wrapper Improvements
2 parents a31d67c + 9c31cb7 commit 7bd6829

26 files changed

+1598
-78
lines changed

.github/workflows/python-tests.yml

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Python Tests
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
fail-fast: false
14+
matrix:
15+
python-version: ["3.9", "3.10", "3.11"]
16+
17+
steps:
18+
- uses: actions/checkout@v3
19+
20+
- name: Set up Python ${{ matrix.python-version }}
21+
uses: actions/setup-python@v4
22+
with:
23+
python-version: ${{ matrix.python-version }}
24+
25+
- name: Install Poetry
26+
uses: snok/install-poetry@v1
27+
with:
28+
version: 1.6.1
29+
virtualenvs-create: true
30+
virtualenvs-in-project: true
31+
32+
- name: Install dependencies
33+
run: |
34+
poetry install --no-interaction
35+
36+
- name: Run tests
37+
run: |
38+
poetry run pytest -v
39+
40+
- name: Lint with Black
41+
run: |
42+
poetry run black --check src tests
43+
44+
- name: Display Python version
45+
run: |
46+
echo "Python ${{ matrix.python-version }}"
47+
poetry run python --version

README.md

+137-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,137 @@
1-
# otel-wrapper
2-
OpenTelemetry Wrapper to send traces, metrics and logs to my otel-proxy using OTLP Protocol
1+
# OpenTelemetry Wrapper
2+
3+
[![Python Tests](https://github.com/ivanildobarauna-dev/open-o11y-wrapper/actions/workflows/python-tests.yml/badge.svg)](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

Comments
 (0)