metrics

package
v2.23.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: Apache-2.0 Imports: 15 Imported by: 2 modules, 3 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package metrics provides the unified PromptKit metrics collector.

The Collector records both pipeline operational metrics (from EventBus events) and eval result metrics (from pack-defined MetricDefs) into a Prometheus registry.

Pipeline metric families are prefixed with the namespace (default "promptkit") and cover the pipeline (pipeline_duration_seconds), LLM provider calls (provider_request_duration_seconds, provider_requests_total and the provider_*_tokens_total and provider_cost_total counters), tool calls (tool_call_duration_seconds, tool_calls_total), validations (validation_duration_seconds, validations_total), and the tts_, stt_, embedding_, image_gen_ and inference_ families for those capabilities. Eval metrics are created on first observation from the pack's MetricDef.

Usage:

reg := prometheus.NewRegistry()
collector := metrics.NewCollector(metrics.CollectorOpts{
    Registerer: reg,
    Namespace:  "myapp",
    ConstLabels: prometheus.Labels{"env": "prod"},
    InstanceLabels: []string{"tenant"},
})

// Per-conversation binding:
ctx := collector.Bind(prometheus.Labels{"tenant": "acme"})
bus.SubscribeAll(ctx.OnEvent) // pipeline metrics
recorder := ctx                // evals.MetricRecorder

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewEventBusHealthCollector

func NewEventBusHealthCollector(
	bus *events.EventBus,
	namespace string,
	constLabels prometheus.Labels,
) prometheus.Collector

NewEventBusHealthCollector builds the collector for a bus. A nil bus yields a collector whose Describe/Collect are safe no-ops (nothing to export).

Types

type Collector

type Collector struct {
	// contains filtered or unexported fields
}

Collector records both pipeline and eval metrics into a Prometheus registry. Created once per process via NewCollector(). Shared across conversations.

Pipeline events are recorded via MetricContext.OnEvent (implements events.Listener). Eval results are recorded via MetricContext.Record (implements evals.MetricRecorder).

Instance labels (varying per conversation) are passed via a MetricContext that the SDK creates per conversation using Bind().

func NewCollector

func NewCollector(opts CollectorOpts) *Collector

NewCollector creates a unified metrics collector and registers pipeline metrics into the provided Registerer (or prometheus.DefaultRegisterer).

func NewEvalOnlyCollector

func NewEvalOnlyCollector(opts CollectorOpts) *Collector

NewEvalOnlyCollector creates a Collector that only records eval result metrics, with pipeline operational metrics disabled. This is a convenience wrapper for:

metrics.NewCollector(metrics.CollectorOpts{
    Registerer:             reg,
    DisablePipelineMetrics: true,
    ...
})

Use this for standalone eval workers (e.g. sdk.Evaluate()) that don't run a live pipeline and therefore don't need provider, tool, or pipeline metrics.

func (*Collector) Bind

func (c *Collector) Bind(instanceLabels map[string]string) *MetricContext

Bind creates a MetricContext for a specific conversation, binding instance label values. The returned context implements both events.Listener (via OnEvent) and evals.MetricRecorder (via Record).

Label key ordering in the map does not matter. The Collector sorts InstanceLabels internally for deterministic Prometheus label ordering. Only the keys need to match; values are looked up by key, not position.

If the Collector has no InstanceLabels, pass nil.

func (*Collector) Registry

func (c *Collector) Registry() *prometheus.Registry

Registry returns the underlying *prometheus.Registry if one was provided, or nil if a non-Registry Registerer was used.

type CollectorOpts

type CollectorOpts struct {
	// Registerer is the Prometheus registerer to register metrics into.
	// If nil, prometheus.DefaultRegisterer is used.
	// If the value is a *prometheus.Registry, it is accessible via
	// Collector.Registry() for use with promhttp.HandlerFor().
	Registerer prometheus.Registerer

	// Namespace overrides the metric name prefix (default: "promptkit").
	Namespace string

	// ConstLabels are added to every metric as constant label pairs.
	// These must be constant for the lifetime of the process:
	// they are baked into the metric descriptor at registration time.
	// Use for process-level dimensions: env, region, service_name.
	ConstLabels prometheus.Labels

	// InstanceLabels are label NAMES that vary per conversation.
	// They become additional dimensions on every Vec metric, allowing
	// multiple conversations to share one Collector without registration
	// conflicts. Values are provided per-conversation via Bind().
	// Use for: tenant, prompt_name, conversation_type.
	InstanceLabels []string

	// DisablePipelineMetrics disables operational metrics (provider, tool, pipeline,
	// validation). Set this to true when you only need eval result metrics, for
	// example in standalone eval workers that use sdk.Evaluate() without a live
	// pipeline. See also NewEvalOnlyCollector() for a convenience constructor.
	DisablePipelineMetrics bool

	// DisableEvalMetrics disables eval result metrics.
	DisableEvalMetrics bool

	// EvalDimensionLimit turns on export of eval dimensions (the named
	// sub-scores in evals.EvalResult.Metrics) and caps how many distinct
	// dimensions each eval metric may export for the life of the process.
	// Zero, the default, exports none: results, events and traces still carry
	// them, only Prometheus does not.
	//
	// Each dimension is a gauge in a family derived from the eval's metric,
	// <metric>_dimension{dimension="<name>"}, so the eval's own family and its
	// existing series are untouched. Dimension names come from handler output
	// (a judge model can invent a new one on any call), so the cap is what
	// bounds cardinality: a series, once created, is never deleted. Names
	// that are not snake_case, and dimensions past the cap, are not exported
	// and are counted in <namespace>_eval_dimensions_dropped_total.
	EvalDimensionLimit int

	// EvalHistogramBuckets sets the bucket upper bounds for eval metrics of
	// type histogram whose MetricDef declares no range. Nil, the default, uses
	// prometheus.DefBuckets, so a histogram that records a latency or a count
	// keeps the buckets it had. The collector sorts the values and drops
	// duplicates.
	//
	// A metric that declares both range.min and range.max gets ten equal-width
	// buckets across that range instead: a score histogram declares
	// range {min: 0, max: 1} to get 0, 0.1, ... 1.0.
	EvalHistogramBuckets []float64
}

CollectorOpts configures a Collector.

type MetricContext

type MetricContext struct {
	// contains filtered or unexported fields
}

MetricContext is a per-conversation handle that carries instance label values. It implements events.Listener (via OnEvent) and evals.MetricRecorder (via Record), forwarding observations to the shared Collector with bound labels.

func (*MetricContext) OnEvent

func (mc *MetricContext) OnEvent(event *events.Event)

OnEvent processes a pipeline event and records relevant metrics. This method is designed to be used with EventBus.SubscribeAll.

func (*MetricContext) Record

func (mc *MetricContext) Record(result evals.EvalResult, metric *evals.MetricDef) error

Record implements evals.MetricRecorder. It records an eval result into the Prometheus registry using the metric definition from the pack.

EvalResult passed by value to satisfy MetricRecorder interface

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL