Skip to main content

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

impulse_telemetry-0.1.0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

impulse_telemetry-0.1.0-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file impulse_telemetry-0.1.0.tar.gz.

File metadata

  • Download URL: impulse_telemetry-0.1.0.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for impulse_telemetry-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0e308489f1dc79462f32e9c0fd97ffba6c86594abcb7f95b8323c7686b6714ef
MD5 c39393c37a9c17d00f6d69a5de596138
BLAKE2b-256 506709e4c4a1f7d3ec16e71db98b90bc4bf00c4983c8a576525f58698a50aec8

See more details on using hashes here.

File details

Details for the file impulse_telemetry-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for impulse_telemetry-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6aa507c560e2b741dce78832e45431fd02b98b1cd93ea6d58ee7de06d604f5e4
MD5 26c238664a4cf1d06ff8a81ae24f076a
BLAKE2b-256 026ce7b5dd77c3821321c81477f4d7d0099c669b9f4e62a206eac2795d30506f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page