Skip to content
GitHub

Monitor (lexigram-monitor)

Observability, health checks, and metrics for the Lexigram Framework. Supports Prometheus, OpenTelemetry, structured log export, and /health endpoints that integrate with Kubernetes probes and load-balancer health checks.


lexigram-monitor provides metrics collection, distributed tracing, health checks, and alerting for Lexigram applications. It integrates with Prometheus and OpenTelemetry backends, supports composable health checks with liveness and readiness flavours, and includes decorators for instrumenting services with custom metrics and traces. All services are wired via MonitorProvider, which registers monitoring protocols with the DI container.

Full documentation: docs.lexigram.dev

Terminal window
uv add lexigram-monitor
# Optional extras
uv add "lexigram-monitor[prometheus]" # Prometheus + Grafana
uv add "lexigram-monitor[opentelemetry]" # OTLP / Jaeger / Zipkin
from lexigram import Application
from lexigram.monitor import MonitorModule
async def main() -> None:
async with Application.boot(modules=[MonitorModule.configure()]) as app:
# ... metrics, health checks and /health endpoints active ...
...
if __name__ == "__main__":
import asyncio
asyncio.run(main())
FieldDefaultEnv varDescription
prometheus.enable_default_metricstrueLEX_MONITOR__PROMETHEUS__ENABLE_DEFAULT_METRICSEnable default process metrics
prometheus.port8000LEX_MONITOR__PROMETHEUS__PORTPort for the Prometheus metrics endpoint
prometheus.path/metricsLEX_MONITOR__PROMETHEUS__PATHURL path for metrics scraping
tracing.enabledtrueLEX_MONITOR__TRACING__ENABLEDEnable distributed tracing via OTLP
tracing.sample_rate1.0LEX_MONITOR__TRACING__SAMPLE_RATETrace sampling rate (0.0–1.0; use 0.1 in production)
health.path/healthLEX_MONITOR__HEALTH__PATHBase path for health check endpoints
health.interval30LEX_MONITOR__HEALTH__INTERVALSeconds between background health polls
health.timeout5LEX_MONITOR__HEALTH__TIMEOUTPer-check timeout in seconds
logging.levelINFOLEX_MONITOR__LOGGING__LEVELMinimum log level (DEBUG, INFO, WARNING, ERROR)
logging.formatjsonLEX_MONITOR__LOGGING__FORMATLog output format (json or text)
slo.enabledtrueLEX_MONITOR__SLO__ENABLEDEnable periodic SLO evaluation worker
slo.evaluation_interval60LEX_MONITOR__SLO__EVALUATION_INTERVALSeconds between SLO evaluation cycles
slo.suppression_window_seconds300LEX_MONITOR__SLO__SUPPRESSION_WINDOW_SECONDSMin seconds between duplicate alerts
MethodDescription
MonitorModule.configure(backend, config)Configure with explicit backend and optional MonitorConfig
MonitorModule.stub()Minimal config for testing
MonitorModule.with_slo(backend, config)Configure with SLO exports for the DI container
  • Prometheus — Auto /metrics endpoint; request counters, histograms, gauges
  • OpenTelemetry — Distributed tracing via OTLP exporter to Jaeger / Honeycomb
  • Health checks — Composable checks with liveness + readiness flavours
  • Cached checks — Per-check TTL to avoid thundering-herd on slow dependencies
  • DB instrumentation — Automatic query timing and error tagging
  • HTTP instrumentation — Outbound request tracking for lexigram-http
  • Messaging instrumentation — Kafka / RabbitMQ consumer lag, publish rate
  • Alerting — Configurable alert rules with tier-aware webhook delivery
  • SLO Monitoring — Burn-rate evaluation with configurable suppression window
  • Tiered alerts — P0 (PagerDuty) / P1 (business hours Slack) / P2 (weekly digest) routing
  • Structured loggingjson / text log output via logging.level / logging.format
  • Grafana dashboards — Pre-built dashboard JSON in lexigram-monitor/dashboards/
async with Application.boot(modules=[MonitorModule.stub()]) as app:
# your test code
...
FileWhat it contains
src/lexigram/monitor/module.pyMonitorModule class with factory methods
src/lexigram/monitor/di/provider.pyMonitorProvider — wires monitoring protocols into DI container
src/lexigram/monitor/config.pyMonitorConfig and sub-config dataclasses
src/lexigram/monitor/health/Health check registration and registry (base.py, checker.py, registry.py, …)
src/lexigram/monitor/instrumentation/decorators.py@metered and @traced decorators
src/lexigram/monitor/slo/SLO evaluation, tiered alert dispatchers, channel implementations
src/lexigram/monitor/alerts/Alert dispatcher protocols and tier routing
dashboards/projection-health.jsonGrafana dashboard for SLO health and alerting

Service Level Objectives are evaluated on a configurable interval. Each SLO tracks a metric percentile against a threshold and fires alerts on budget exhaustion.

from datetime import timedelta
from lexigram.contracts.monitor import ProjectionTier
from lexigram.monitor.slo import SLO, SLOMonitor
monitor = SLOMonitor()
slo = SLO(
name="api.p99_latency",
metric="http.request.duration",
percentile=0.99,
threshold_ms=200.0,
window=timedelta(hours=1),
tier=ProjectionTier.P1_BUSINESS_HOURS,
owner="team-api",
runbook_url="https://ops.runbook/api-slo",
)
monitor.register(slo)
monitor.record_sample("http.request.duration", 150.0)
monitor.record_sample("http.request.duration", 350.0)
violations = await monitor.evaluate_and_dispatch()

Violations are routed through the configured AlertDispatcherProtocol. Alerts for the same SLO are suppressed within the suppression window (default 300s) to avoid storms.

TierEnum ValueBehaviour
P0 — PageProjectionTier.P0_PAGERoutes to PagerDuty (or equivalent paging channel) immediately
P1 — Business HoursProjectionTier.P1_BUSINESS_HOURSQueues outside business hours, flushes on schedule
P2 — DigestProjectionTier.P2_DIGESTAccumulates in a weekly digest buffer

Enable periodic evaluation via config:

application.yaml
monitor:
slo:
enabled: true
evaluation_interval: 60
suppression_window_seconds: 300

Or via environment variables:

Terminal window
export LEX_MONITOR__SLO__ENABLED=true
export LEX_MONITOR__SLO__EVALUATION_INTERVAL=60
export LEX_MONITOR__SLO__SUPPRESSION_WINDOW_SECONDS=300