Skip to content
GitHubDiscord

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.


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.di.module import Module, module
# Import the module from the package
from lexigram.monitor import MonitorModule
@module(imports=[MonitorModule.configure()])
class AppModule(Module):
pass
app = Application(modules=[AppModule])
if __name__ == "__main__":
app.run()

Zero-config usage: Call MonitorModule.configure() with no arguments to use defaults.

application.yaml
monitor:
prometheus:
enabled: true
port: 9090
path: /metrics
tracing:
enabled: false
sample_rate: 1.0
health:
path: /health
interval: 30
logging:
level: INFO
format: json
Section titled “Option 2 — Profiles + Environment Variables (recommended)”
Terminal window
export LEX_MONITOR__ENABLED=true
# Environment variables for each field
from lexigram.monitor.config import MonitorConfig
from lexigram.monitor import MonitorModule
config = MonitorConfig(...)
MonitorModule.configure(backend=backend, config=config)
FieldDefaultEnv varDescription
prometheus.enabledtrueLEX_MONITOR__PROMETHEUS__ENABLEDExpose the /metrics scrape endpoint
prometheus.port9090LEX_MONITOR__PROMETHEUS__PORTPort for the Prometheus metrics endpoint
prometheus.path/metricsLEX_MONITOR__PROMETHEUS__PATHURL path for metrics scraping
tracing.enabledfalseLEX_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)
MethodDescription
MonitorModule.configure(backend, config)Configure with explicit backend and optional MonitorConfig
MonitorModule.stub()Minimal config for testing
  • 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 webhook delivery
  • Log export — Structured log export to OTLP log backend
  • 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.pyHealth check registration and registry
src/lexigram/monitor/instrumentation/decorators.py@metered and @traced decorators