Observability SDK for Impulse microservices
Project description
impulse_telemetry
Observability SDK for Impulse microservices. One init() call wires up distributed tracing, Prometheus metrics, structured logging, and FastAPI middleware.
Install
pip install impulse-telemetry
# With SQLAlchemy / Redis / Celery auto-instrumentation
pip install "impulse-telemetry[extras]"
# With ML data quality checks (pandas required)
pip install "impulse-telemetry[ml]"
Quickstart
from fastapi import FastAPI
from impulse_telemetry import init
app = FastAPI()
init(app, service="my-service", version="1.0.0", env="production")
Every HTTP request now emits RED metrics, a distributed trace, and a structured JSON log line — automatically.
init() parameters
| Parameter | Default | Description |
|---|---|---|
app |
None |
FastAPI app. Pass to enable HTTP middleware. |
service |
required | Service name — appears on all telemetry. |
version |
"0.0.0" |
Semver string tagged on all telemetry. |
env |
$ENV |
"production" | "staging" | "dev" |
otlp_endpoint |
$OTEL_EXPORTER_OTLP_ENDPOINT |
OTLP Collector gRPC address. |
prometheus_port |
None |
Expose /metrics on this port (for workers without HTTP). |
log_level |
"INFO" |
"DEBUG" | "INFO" | "WARNING" | "ERROR" |
enable_ml |
False |
Register ML-specific metric instruments. |
Logging
from impulse_telemetry.logging import get_logger
log = get_logger(__name__)
log.info("prediction_served", model_id="rec-v3", latency_ms=43)
# {"service":"my-service","trace_id":"abc..","event":"prediction_served","model_id":"rec-v3",...}
trace_id and span_id are injected automatically from the active OTEL span.
Per-request context
Bind fields once per request — they appear on every subsequent log line for that request.
from impulse_telemetry.logging import bind_request_context, clear_request_context
token = bind_request_context(user_id="usr_123", request_id="req_456")
log.info("doing_work") # user_id + request_id injected automatically
clear_request_context(token)
Metrics
Prometheus metrics are pre-registered and labeled with service and env.
Automatic (via middleware)
| Metric | Type | Description |
|---|---|---|
http_requests_total |
Counter | Requests by route, method, status |
http_request_errors_total |
Counter | 4xx / 5xx responses |
http_request_duration_seconds |
Histogram | Request latency |
http_active_requests |
Gauge | In-flight requests |
Manual
from impulse_telemetry.metrics import get_metrics
m = get_metrics()
m.dependency_latency.labels(**m.labels(dependency="redis")).observe(elapsed)
m.dependency_errors.labels(**m.labels(dependency="redis")).inc()
Available instruments: rate_limiter_hits, rate_limiter_remaining, dependency_latency, dependency_errors, queue_depth, circuit_breaker.
Tracing
from impulse_telemetry.tracing import get_tracer, inject_headers
tracer = get_tracer(__name__)
with tracer.start_as_current_span("my_operation") as span:
span.set_attribute("key", "value")
headers = inject_headers({}) # propagate W3C traceparent to downstream
requests.get("http://other-service/api", headers=headers)
Auto-instrumented libraries (when installed): requests, httpx, SQLAlchemy, Redis, Celery.
ML Monitoring
Enable at startup with enable_ml=True.
from impulse_telemetry.ml import MLMonitor
monitor = MLMonitor(model_id="rec-v3", user_id=user_id)
Inference tracing
# Context manager
with monitor.inference(features=df) as span:
result = model.predict(df)
span.record_output(result)
# Decorator
@monitor.trace
def predict(features):
return model.predict(features)
Both record inference latency, error count, and run data quality checks (missing values, schema violations) automatically.
Drift & performance metrics
# Call from batch evaluation jobs
monitor.record_drift("age", score=0.18, metric="psi")
monitor.record_performance(rmse=0.042, precision=0.87, recall=0.81)
ML metric instruments
| Metric | Type | Description |
|---|---|---|
ml_inference_duration_seconds |
Histogram | Per-model inference latency |
ml_inference_requests_total |
Counter | Inference count |
ml_inference_errors_total |
Counter | Inference errors |
ml_missing_feature_rate |
Gauge | Missing values per column |
ml_schema_violations_total |
Counter | Schema violation count |
ml_feature_drift |
Gauge | Feature drift score |
ml_prediction_drift |
Gauge | Prediction drift score |
ml_rmse |
Gauge | Rolling RMSE |
ml_precision |
Gauge | Rolling precision |
ml_recall |
Gauge | Rolling recall |
Background workers
For services without an HTTP server, expose metrics on a dedicated port:
init(service="ingest-worker", prometheus_port=9090)
# Prometheus scrapes localhost:9090/metrics
Environment variables
| Variable | Description |
|---|---|
OTEL_EXPORTER_OTLP_ENDPOINT |
OTLP Collector gRPC address |
ENV |
Deployment environment (production, staging, dev) |
See also
examples.py — minimal, runnable code for every feature.
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 impulse_telemetry-0.2.0.tar.gz.
File metadata
- Download URL: impulse_telemetry-0.2.0.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f571c3e86e9ca71b8a1da5875a95c6bc97107418dc0644b8f71c8088b0bf509
|
|
| MD5 |
1f53e7e2082409a4c15b83c5382eb7c1
|
|
| BLAKE2b-256 |
07f0d768db7f5b5b04cfa67a406e71bf3bcae005dc0a2f35d81b280dd1fb8e3f
|
File details
Details for the file impulse_telemetry-0.2.0-py3-none-any.whl.
File metadata
- Download URL: impulse_telemetry-0.2.0-py3-none-any.whl
- Upload date:
- Size: 3.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a9e13021393548031bbbe081675a591705f969179185066f23c1be886c7f7d80
|
|
| MD5 |
3061076ef3b53c4b948290a3184fc318
|
|
| BLAKE2b-256 |
8fbdb66c44d4826ecb88233fa0fc85ad895baf72d6d19c85936c3a007f2bd5c5
|