2026-04-15 19:53:10 +01:00
|
|
|
package chatloop
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
|
|
"charm.land/fantasy"
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
2026-04-17 16:16:30 +01:00
|
|
|
|
|
|
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chaterror"
|
2026-04-15 19:53:10 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
metricsNamespace = "coderd"
|
|
|
|
|
metricsSubsystem = "chatd"
|
|
|
|
|
|
|
|
|
|
// Label values for Chats.
|
|
|
|
|
StateStreaming = "streaming"
|
|
|
|
|
|
|
|
|
|
// Label values for CompactionTotal.
|
|
|
|
|
CompactionResultSuccess = "success"
|
|
|
|
|
CompactionResultError = "error"
|
|
|
|
|
CompactionResultTimeout = "timeout"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Metrics holds Prometheus metrics for the chatd subsystem.
|
|
|
|
|
type Metrics struct {
|
2026-08-18 19:12:47 +02:00
|
|
|
Chats *prometheus.GaugeVec
|
|
|
|
|
MessageCount *prometheus.HistogramVec
|
|
|
|
|
PromptSizeBytes *prometheus.HistogramVec
|
|
|
|
|
ToolResultSizeBytes *prometheus.HistogramVec
|
|
|
|
|
ToolResultTruncatedTotal *prometheus.CounterVec
|
|
|
|
|
ToolErrorsTotal *prometheus.CounterVec
|
|
|
|
|
TTFTSeconds *prometheus.HistogramVec
|
|
|
|
|
CompactionTotal *prometheus.CounterVec
|
|
|
|
|
StepsTotal *prometheus.CounterVec
|
|
|
|
|
StreamRetriesTotal *prometheus.CounterVec
|
|
|
|
|
StreamBufferDroppedTotal prometheus.Counter
|
|
|
|
|
FindToolsCallsTotal prometheus.Counter
|
|
|
|
|
FindToolsEmptyTotal prometheus.Counter
|
|
|
|
|
FindToolsMatchCount prometheus.Histogram
|
|
|
|
|
FindToolsActivationsTotal prometheus.Counter
|
2026-04-15 19:53:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewMetrics creates a new Metrics instance registered with the
|
|
|
|
|
// given registerer.
|
|
|
|
|
func NewMetrics(reg prometheus.Registerer) *Metrics {
|
|
|
|
|
factory := promauto.With(reg)
|
|
|
|
|
return &Metrics{
|
|
|
|
|
Chats: factory.NewGaugeVec(prometheus.GaugeOpts{
|
|
|
|
|
Namespace: metricsNamespace,
|
|
|
|
|
Subsystem: metricsSubsystem,
|
|
|
|
|
Name: "chats",
|
|
|
|
|
Help: "Number of chats being processed, by state.",
|
|
|
|
|
}, []string{"state"}),
|
|
|
|
|
MessageCount: factory.NewHistogramVec(prometheus.HistogramOpts{
|
|
|
|
|
Namespace: metricsNamespace,
|
|
|
|
|
Subsystem: metricsSubsystem,
|
|
|
|
|
Name: "message_count",
|
|
|
|
|
Help: "Number of messages in the prompt per LLM request.",
|
2026-04-16 09:43:54 +01:00
|
|
|
Buckets: prometheus.ExponentialBuckets(1, 2, 11), // 1, 2, 4, ..., 1024
|
2026-04-17 16:16:30 +01:00
|
|
|
}, []string{"provider", "model"}),
|
2026-04-15 19:53:10 +01:00
|
|
|
PromptSizeBytes: factory.NewHistogramVec(prometheus.HistogramOpts{
|
|
|
|
|
Namespace: metricsNamespace,
|
|
|
|
|
Subsystem: metricsSubsystem,
|
|
|
|
|
Name: "prompt_size_bytes",
|
|
|
|
|
Help: "Estimated byte size of the prompt per LLM request.",
|
|
|
|
|
Buckets: prometheus.ExponentialBuckets(1024, 4, 10), // 1KB .. 256MB
|
2026-04-17 16:16:30 +01:00
|
|
|
}, []string{"provider", "model"}),
|
2026-04-15 19:53:10 +01:00
|
|
|
ToolResultSizeBytes: factory.NewHistogramVec(prometheus.HistogramOpts{
|
|
|
|
|
Namespace: metricsNamespace,
|
|
|
|
|
Subsystem: metricsSubsystem,
|
|
|
|
|
Name: "tool_result_size_bytes",
|
|
|
|
|
Help: "Size in bytes of each tool execution result.",
|
|
|
|
|
Buckets: prometheus.ExponentialBuckets(64, 4, 9), // 64B .. 4MB
|
2026-04-17 16:16:30 +01:00
|
|
|
}, []string{"provider", "model", "tool_name"}),
|
2026-06-24 09:16:38 -06:00
|
|
|
ToolResultTruncatedTotal: factory.NewCounterVec(prometheus.CounterOpts{
|
|
|
|
|
Namespace: metricsNamespace,
|
|
|
|
|
Subsystem: metricsSubsystem,
|
|
|
|
|
Name: "tool_result_truncated_total",
|
|
|
|
|
Help: "Total tool results truncated to fit the model context window.",
|
|
|
|
|
}, []string{"provider", "model", "tool_name"}),
|
2026-04-22 17:19:56 +01:00
|
|
|
ToolErrorsTotal: factory.NewCounterVec(prometheus.CounterOpts{
|
|
|
|
|
Namespace: metricsNamespace,
|
|
|
|
|
Subsystem: metricsSubsystem,
|
|
|
|
|
Name: "tool_errors_total",
|
|
|
|
|
Help: "Total tool calls that returned an error result.",
|
|
|
|
|
}, []string{"provider", "model", "tool_name"}),
|
2026-04-15 19:53:10 +01:00
|
|
|
TTFTSeconds: factory.NewHistogramVec(prometheus.HistogramOpts{
|
|
|
|
|
Namespace: metricsNamespace,
|
|
|
|
|
Subsystem: metricsSubsystem,
|
|
|
|
|
Name: "ttft_seconds",
|
|
|
|
|
Help: "Time-to-first-token: wall time from LLM request to first streamed chunk.",
|
|
|
|
|
Buckets: []float64{0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 30, 60},
|
2026-04-17 16:16:30 +01:00
|
|
|
}, []string{"provider", "model"}),
|
2026-04-15 19:53:10 +01:00
|
|
|
CompactionTotal: factory.NewCounterVec(prometheus.CounterOpts{
|
|
|
|
|
Namespace: metricsNamespace,
|
|
|
|
|
Subsystem: metricsSubsystem,
|
|
|
|
|
Name: "compaction_total",
|
|
|
|
|
Help: "Total compaction outcomes (only recorded when compaction was triggered or failed).",
|
2026-04-17 16:16:30 +01:00
|
|
|
}, []string{"provider", "model", "result"}),
|
2026-04-15 19:53:10 +01:00
|
|
|
StepsTotal: factory.NewCounterVec(prometheus.CounterOpts{
|
|
|
|
|
Namespace: metricsNamespace,
|
|
|
|
|
Subsystem: metricsSubsystem,
|
|
|
|
|
Name: "steps_total",
|
|
|
|
|
Help: "Total agentic loop steps across all chats.",
|
2026-04-17 16:16:30 +01:00
|
|
|
}, []string{"provider", "model"}),
|
|
|
|
|
StreamRetriesTotal: factory.NewCounterVec(prometheus.CounterOpts{
|
|
|
|
|
Namespace: metricsNamespace,
|
|
|
|
|
Subsystem: metricsSubsystem,
|
|
|
|
|
Name: "stream_retries_total",
|
|
|
|
|
Help: "Total LLM stream retries.",
|
2026-07-06 11:57:12 +01:00
|
|
|
}, []string{"provider", "model", "kind"}),
|
2026-08-18 19:12:47 +02:00
|
|
|
FindToolsCallsTotal: factory.NewCounter(prometheus.CounterOpts{
|
|
|
|
|
Namespace: metricsNamespace,
|
|
|
|
|
Subsystem: metricsSubsystem,
|
|
|
|
|
Name: "find_tools_calls_total",
|
|
|
|
|
Help: "Total find_tools calls.",
|
|
|
|
|
}),
|
|
|
|
|
FindToolsEmptyTotal: factory.NewCounter(prometheus.CounterOpts{
|
|
|
|
|
Namespace: metricsNamespace,
|
|
|
|
|
Subsystem: metricsSubsystem,
|
|
|
|
|
Name: "find_tools_empty_total",
|
|
|
|
|
Help: "Total find_tools calls with no matches.",
|
|
|
|
|
}),
|
|
|
|
|
FindToolsMatchCount: factory.NewHistogram(prometheus.HistogramOpts{
|
|
|
|
|
Namespace: metricsNamespace,
|
|
|
|
|
Subsystem: metricsSubsystem,
|
|
|
|
|
Name: "find_tools_match_count",
|
|
|
|
|
Help: "Number of matches returned by find_tools calls.",
|
|
|
|
|
Buckets: prometheus.LinearBuckets(0, 2, 11),
|
|
|
|
|
}),
|
|
|
|
|
FindToolsActivationsTotal: factory.NewCounter(prometheus.CounterOpts{
|
|
|
|
|
Namespace: metricsNamespace,
|
|
|
|
|
Subsystem: metricsSubsystem,
|
|
|
|
|
Name: "find_tools_activations_total",
|
|
|
|
|
Help: "Total deferred tool activations returned by find_tools.",
|
|
|
|
|
}),
|
2026-04-17 16:16:30 +01:00
|
|
|
StreamBufferDroppedTotal: factory.NewCounter(prometheus.CounterOpts{
|
|
|
|
|
Namespace: metricsNamespace,
|
|
|
|
|
Subsystem: metricsSubsystem,
|
|
|
|
|
Name: "stream_buffer_dropped_total",
|
|
|
|
|
Help: "Number of chat stream buffer events dropped due to the per-chat buffer cap.",
|
|
|
|
|
}),
|
2026-04-15 19:53:10 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NopMetrics returns a Metrics instance that discards all data.
|
|
|
|
|
// Useful for tests and when metrics collection is not desired.
|
|
|
|
|
func NopMetrics() *Metrics {
|
|
|
|
|
return NewMetrics(prometheus.NewRegistry())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RecordCompaction classifies and records a compaction attempt.
|
|
|
|
|
// It is a no-op when m is nil.
|
2026-04-17 16:16:30 +01:00
|
|
|
func (m *Metrics) RecordCompaction(provider, model string, compacted bool, err error) {
|
2026-04-15 19:53:10 +01:00
|
|
|
if m == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
switch {
|
|
|
|
|
case err != nil && errors.Is(err, context.DeadlineExceeded):
|
2026-04-17 16:16:30 +01:00
|
|
|
m.CompactionTotal.WithLabelValues(provider, model, CompactionResultTimeout).Inc()
|
2026-04-15 19:53:10 +01:00
|
|
|
case err != nil && errors.Is(err, context.Canceled):
|
|
|
|
|
// User interruption, not a compaction failure.
|
|
|
|
|
return
|
|
|
|
|
case err != nil:
|
2026-04-17 16:16:30 +01:00
|
|
|
m.CompactionTotal.WithLabelValues(provider, model, CompactionResultError).Inc()
|
2026-04-15 19:53:10 +01:00
|
|
|
case compacted:
|
2026-04-17 16:16:30 +01:00
|
|
|
m.CompactionTotal.WithLabelValues(provider, model, CompactionResultSuccess).Inc()
|
2026-04-15 19:53:10 +01:00
|
|
|
// !compacted && err == nil means threshold not reached -- not
|
|
|
|
|
// recorded.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-17 16:16:30 +01:00
|
|
|
// RecordStreamRetry increments stream_retries_total. The caller
|
|
|
|
|
// must obtain classified via chaterror.Classify (non-empty Kind).
|
2026-07-06 11:57:12 +01:00
|
|
|
// No-op when m is nil.
|
2026-04-17 16:16:30 +01:00
|
|
|
func (m *Metrics) RecordStreamRetry(provider, model string, classified chaterror.ClassifiedError) {
|
|
|
|
|
if m == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-05-11 17:43:40 +01:00
|
|
|
m.StreamRetriesTotal.WithLabelValues(
|
|
|
|
|
provider,
|
|
|
|
|
model,
|
|
|
|
|
string(classified.Kind),
|
|
|
|
|
).Inc()
|
2026-04-17 16:16:30 +01:00
|
|
|
}
|
|
|
|
|
|
2026-04-22 17:19:56 +01:00
|
|
|
// RecordToolError increments tool_errors_total for the given
|
|
|
|
|
// tool. No-op when m is nil.
|
|
|
|
|
func (m *Metrics) RecordToolError(provider, model, toolLabel string) {
|
|
|
|
|
if m == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
m.ToolErrorsTotal.WithLabelValues(provider, model, toolLabel).Inc()
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-24 09:16:38 -06:00
|
|
|
// RecordToolResultTruncated increments tool_result_truncated_total for
|
|
|
|
|
// the given tool. No-op when m is nil. An empty tool label is
|
|
|
|
|
// normalized to "unknown" to match the other tool metrics.
|
|
|
|
|
func (m *Metrics) RecordToolResultTruncated(provider, model, toolLabel string) {
|
|
|
|
|
if m == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if toolLabel == "" {
|
|
|
|
|
toolLabel = "unknown"
|
|
|
|
|
}
|
|
|
|
|
m.ToolResultTruncatedTotal.WithLabelValues(provider, model, toolLabel).Inc()
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-17 16:16:30 +01:00
|
|
|
// RecordStreamBufferDropped increments stream_buffer_dropped_total
|
|
|
|
|
// once per dropped event. No-op when m is nil.
|
|
|
|
|
func (m *Metrics) RecordStreamBufferDropped() {
|
|
|
|
|
if m == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
m.StreamBufferDroppedTotal.Inc()
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-15 19:53:10 +01:00
|
|
|
// EstimatePromptSize returns a cheap byte-size estimate of a
|
|
|
|
|
// fantasy prompt by summing the text content lengths of all
|
|
|
|
|
// message parts. This avoids JSON marshaling overhead.
|
|
|
|
|
func EstimatePromptSize(messages []fantasy.Message) int {
|
|
|
|
|
var size int
|
|
|
|
|
for _, msg := range messages {
|
|
|
|
|
for _, part := range msg.Content {
|
|
|
|
|
size += ContentPartSize(part)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return size
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ContentPartSize returns the byte length of a MessagePart's
|
|
|
|
|
// primary text or data field.
|
|
|
|
|
func ContentPartSize(part fantasy.MessagePart) int {
|
|
|
|
|
switch p := part.(type) {
|
|
|
|
|
case fantasy.TextPart:
|
|
|
|
|
return len(p.Text)
|
|
|
|
|
case fantasy.ReasoningPart:
|
|
|
|
|
return len(p.Text)
|
|
|
|
|
case fantasy.FilePart:
|
|
|
|
|
return len(p.Data)
|
|
|
|
|
case fantasy.ToolCallPart:
|
|
|
|
|
return len(p.Input)
|
|
|
|
|
case fantasy.ToolResultPart:
|
|
|
|
|
return toolResultOutputSize(p.Output)
|
|
|
|
|
default:
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ToolResultSize returns the byte length of a
|
|
|
|
|
// ToolResultContent's primary text or data field.
|
|
|
|
|
func ToolResultSize(r fantasy.ToolResultContent) int {
|
|
|
|
|
return toolResultOutputSize(r.Result)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func toolResultOutputSize(output fantasy.ToolResultOutputContent) int {
|
|
|
|
|
if output == nil {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
switch v := output.(type) {
|
|
|
|
|
case fantasy.ToolResultOutputContentText:
|
|
|
|
|
return len(v.Text)
|
|
|
|
|
case fantasy.ToolResultOutputContentError:
|
|
|
|
|
if v.Error != nil {
|
|
|
|
|
return len(v.Error.Error())
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
case fantasy.ToolResultOutputContentMedia:
|
|
|
|
|
return len(v.Data)
|
|
|
|
|
default:
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
}
|