- Counters: Monotonically increasing values (e.g., request counts, token usage)
- Histograms: Value distributions (e.g., latency, token counts)
- UpDownCounters: Values that can increase or decrease (e.g., active sessions)
- Console: Print metrics to console for debugging
- OTLP: Export to OpenTelemetry Protocol collectors (Jaeger, Grafana, etc.)
- Prometheus: Register metrics with prometheus_client registry for scraping
- MELLEA_METRICS_ENABLED: Enable/disable metrics collection (default: false)
- OTEL_SERVICE_NAME: Service name for metrics (default: mellea)
- MELLEA_METRICS_CONSOLE: Print metrics to console (default: false)
- MELLEA_METRICS_OTLP: Enable OTLP metrics exporter (default: false)
- OTEL_EXPORTER_OTLP_ENDPOINT: OTLP endpoint for all signals (optional)
- OTEL_EXPORTER_OTLP_METRICS_ENDPOINT: Metrics-specific endpoint (optional, overrides general)
- OTEL_METRIC_EXPORT_INTERVAL: Export interval in milliseconds (default: 60000)
- MELLEA_METRICS_PROMETHEUS: Enable Prometheus metric reader (default: false)
- MELLEA_PRICING_FILE: Path to a JSON file with custom model pricing overrides (optional)
- Token counters: mellea.llm.tokens.input, mellea.llm.tokens.output (unit: tokens)
- Latency histograms: mellea.llm.request.duration (unit: s), mellea.llm.ttfb (unit: s, streaming only)
- Error counter: mellea.llm.errors (unit: {error}), categorized by semantic error type
- Cost counter: mellea.llm.cost.usd (unit: USD), estimated cost when pricing data is available
- Sampling counters: mellea.sampling.attempts, mellea.sampling.successes, mellea.sampling.failures (unit: {attempt}/{sample}/{failure})
- Requirement counters: mellea.requirement.checks (unit: {check}), mellea.requirement.failures (unit: {failure})
- Tool counter: mellea.tool.calls (unit: {call}), tagged by tool name and status
Functions
FUNC create_counter
- Total number of requests
- Total tokens processed
- Total errors encountered
name: Metric name (e.g., “mellea.requests.total”)description: Human-readable description of what this metric measuresunit: Unit of measurement (e.g., “1” for count, “ms” for milliseconds)
- Counter instrument (or no-op if metrics disabled)
FUNC create_histogram
- Request latency
- Token counts per request
- Response sizes
name: Metric name (e.g., “mellea.request.duration”)description: Human-readable descriptionunit: Unit of measurement (e.g., “ms”, “tokens”, “bytes”)
- Histogram instrument (or no-op if metrics disabled)
FUNC create_up_down_counter
- Active sessions
- Items in a queue
- Memory usage
name: Metric name (e.g., “mellea.sessions.active”)description: Human-readable descriptionunit: Unit of measurement
- UpDownCounter instrument (or no-op if metrics disabled)
FUNC is_metrics_enabled
- True if metrics are enabled, False otherwise
FUNC record_token_usage_metrics
input_tokens: Number of input tokens (prompt tokens), or None if unavailableoutput_tokens: Number of output tokens (completion tokens), or None if unavailablemodel: Model identifier (e.g., “gpt-4”, “llama2:7b”)provider: Provider name (e.g., “openai”, “ollama”, “watsonx”)
FUNC record_request_duration
duration_s: Request duration in secondsmodel: Model identifier (e.g., “gpt-4”, “llama2:7b”)provider: Provider name (e.g., “openai”, “ollama”, “watsonx”)streaming: Whether the request used streaming mode
FUNC record_ttfb
ttfb_s: Time to first token in secondsmodel: Model identifier (e.g., “gpt-4”, “llama2:7b”)provider: Provider name (e.g., “openai”, “ollama”, “watsonx”)
FUNC classify_error
exc: The exception to classify.
- One of the
ERROR_TYPE_*constants.
FUNC record_error
error_type: Semantic error category (useERROR_TYPE_*constants).model: Model identifier (e.g. “gpt-4”, “llama2:7b”).provider: Provider name (e.g. “openai”, “ollama”).exception_class: Python exception class name (e.g. “RateLimitError”).
FUNC record_cost
compute_cost returned
a non-None value).
Args:
cost: Estimated request cost in US dollars.model: Model identifier (e.g."gpt-4o","claude-sonnet-4-6").provider: Provider name (e.g."openai","ollama").
FUNC record_sampling_attempt
strategy: Sampling strategy class name (e.g."RejectionSamplingStrategy").
FUNC record_sampling_outcome
strategy: Sampling strategy class name (e.g."RejectionSamplingStrategy").success:Trueif at least one attempt passed all requirements.
FUNC record_requirement_check
requirement: Requirement class name (e.g."LLMaJRequirement").
FUNC record_requirement_failure
requirement: Requirement class name (e.g."LLMaJRequirement").reason: Human-readable failure reason fromValidationResult.reason.
FUNC record_tool_call
tool: Name of the tool that was invoked.status:"success"if the tool executed without error,"failure"otherwise.