usage

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(v bool) *bool

func ContextWithSpan

func ContextWithSpan(ctx context.Context, span InteractionSpan) context.Context

func Int

func Int(v int) *int

func RenderPrometheus

func RenderPrometheus(snap PrometheusSnapshot, stats RuntimeStats) string

RenderPrometheus formats a snapshot plus pipeline health counters as Prometheus text exposition (version 0.0.4).

Types

type ACPExtension

type ACPExtension struct {
	ServiceID           string
	AgentType           string
	Operation           string
	ThreadID            string
	SessionID           string
	PermissionRequestID string
	FreshSession        *bool
	EventCounts         map[string]int
	UsageJSON           string
	ResultStatus        string
}

type ACPSummary

type ACPSummary struct {
	RequestCount int64 `json:"request_count"`
	TurnCount    int64 `json:"turn_count"`
	SuccessCount int64 `json:"success_count"`
	FailureCount int64 `json:"failure_count"`
	AvgLatencyMS int64 `json:"avg_latency_ms"`
}

type ACPUsageEvent

type ACPUsageEvent struct {
	InteractionEvent
	ServiceID           string
	AgentType           string
	Operation           string
	ThreadID            string
	SessionID           string
	PermissionRequestID string
	FreshSession        *bool
	EventCounts         map[string]int
	UsageJSON           string
	ResultStatus        string
}

type AgentAttribution

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

AgentAttribution is a settable holder for an AgentAttributor. The metrics pipeline (and therefore the observer) is constructed before the agent manager exists, so the concrete attributor is injected later via Set without rebuilding the observer.

func NewAgentAttribution

func NewAgentAttribution() *AgentAttribution

func (*AgentAttribution) ResolveAgentID

func (a *AgentAttribution) ResolveAgentID(routeID, serviceID, sessionID string) (string, bool)

ResolveAgentID delegates to the installed attributor, returning ok=false when none is installed.

func (*AgentAttribution) Set

func (a *AgentAttribution) Set(attr AgentAttributor)

Set installs (or replaces) the active attributor. A nil attributor is ignored.

type AgentAttributor

type AgentAttributor interface {
	ResolveAgentID(routeID, serviceID, sessionID string) (string, bool)
}

AgentAttributor maps an originating route/service/session back to a single agent id for write-time usage attribution. It is implemented by the agent control-plane layer and consumed here so the lower layers never depend on pkg/agent (the dependency arrow stays pkg/agent -> runtime). It returns ok=false when the mapping is empty or ambiguous, in which case the agent id is left empty.

type AttributionFilter

type AttributionFilter struct {
	AgentID       string
	RouteIDs      []string
	ACPServiceIDs []string
}

AttributionFilter selects events that belong to one agent. Write-time agent_id stamping is the primary signal, but it cannot cover events written before the agent existed, before the agent_id column existed, or while the route/service was bound to a different agent. The filter therefore matches the durable agent_id tag OR a route/service the agent currently owns, so per-agent reads (interactions, usage, activity, health) fall back to the route/service mapping for untagged-but-mappable events. An empty filter matches nothing.

ACPServiceIDs is intentionally ACP-only: the ACP runtime service is bound by at most one agent (P0 one-runtime-one-agent), so it is the only unambiguous service-level fallback. MCP service resources carry no uniqueness constraint and are recovered through RouteIDs instead. The query layer keeps this arm ACP-scoped so a same-named MCP service is never matched.

func (*AttributionFilter) IsEmpty

func (f *AttributionFilter) IsEmpty() bool

IsEmpty reports whether the filter carries no selector at all.

type BreakdownOptions

type BreakdownOptions struct {
	From        string
	To          string
	GroupBy     string
	OrderBy     string
	Limit       int
	Filters     map[string]string
	Attribution *AttributionFilter
}

type BreakdownResponse

type BreakdownResponse struct {
	GroupBy string           `json:"group_by,omitempty"`
	Items   []map[string]any `json:"items"`
	Limit   int              `json:"limit"`
}

type Config

type Config struct {
	RetentionDays int `json:"retention_days,omitempty"`
	MaxAgentDepth int `json:"max_agent_depth,omitempty"`
}

func (Config) Normalized

func (c Config) Normalized() Config

type EventListOptions

type EventListOptions struct {
	From        string
	To          string
	Limit       int
	Filters     map[string]string
	Success     *bool
	Attribution *AttributionFilter
}

type EventListResponse

type EventListResponse struct {
	Items []map[string]any `json:"items"`
	Limit int              `json:"limit"`
}

type EventSink

type EventSink interface {
	Enqueue(any) bool
}

type InMemorySink

type InMemorySink struct {
	Dropped atomic.Uint64
	Events  []any
}

func (*InMemorySink) Enqueue

func (s *InMemorySink) Enqueue(v any) bool

type InteractionDimensions

type InteractionDimensions struct {
	TraceID       string
	SpanID        string
	ParentSpanID  string
	AgentDepth    int
	RouteID       string
	RouteKind     string
	RouteProtocol string
	VirtualKeyID  string
	// AgentID, when set by the caller, is stamped verbatim. When empty the
	// observer attempts attribution from RouteID via the wired AgentAttributor.
	AgentID string
}

type InteractionEvent

type InteractionEvent struct {
	EventID       string
	TraceID       string
	SpanID        string
	ParentSpanID  string
	AgentDepth    int
	StartedAt     time.Time
	FinishedAt    time.Time
	RouteID       string
	RouteKind     string
	RouteProtocol string
	VirtualKeyID  string
	Success       bool
	StatusCode    int
	ErrorType     string
	LatencyMS     int64
	// AgentID is the write-time attribution tag. It is empty for non-agent
	// traffic and for events whose route/service maps to zero or more than one
	// agent (stamp only when unambiguous).
	AgentID string
}

InteractionEvent captures fields common to every gateway interaction.

type InteractionObserver

type InteractionObserver interface {
	Begin(ctx context.Context, dims InteractionDimensions) (InteractionSpan, context.Context)
}

type InteractionOutcome

type InteractionOutcome struct {
	Success    bool
	StatusCode int
	ErrorType  string
	FinishedAt time.Time
}

type InteractionSpan

type InteractionSpan interface {
	SetExtension(v any)
	AddAnnotation(key, value string)
	Finish(outcome InteractionOutcome)
}

func SpanFromContext

func SpanFromContext(ctx context.Context) InteractionSpan

type LLMExtension

type LLMExtension struct {
	LLMAPI           string
	APIOperation     string
	ProviderID       string
	ProviderType     string
	LogicalModel     string
	UpstreamModel    string
	CredentialSource string
	CredentialID     string
	Stream           *bool
	InputTokens      *int
	OutputTokens     *int
	TotalTokens      *int
	UsageFinalized   *bool
	RequestToolCount *int
	RequestToolNames []string
	ToolCallCount    *int
	ToolNames        []string
}

type LLMSummary

type LLMSummary struct {
	RequestCount int64 `json:"request_count"`
	SuccessCount int64 `json:"success_count"`
	FailureCount int64 `json:"failure_count"`
	InputTokens  int64 `json:"input_tokens"`
	OutputTokens int64 `json:"output_tokens"`
	TotalTokens  int64 `json:"total_tokens"`
	AvgLatencyMS int64 `json:"avg_latency_ms"`
}

type LLMUsageEvent

type LLMUsageEvent struct {
	InteractionEvent
	LLMAPI           string
	APIOperation     string
	ProviderID       string
	ProviderType     string
	LogicalModel     string
	UpstreamModel    string
	CredentialSource string
	CredentialID     string
	Stream           bool
	InputTokens      int
	OutputTokens     int
	TotalTokens      int
	UsageFinalized   bool
	RequestToolCount int
	RequestToolNames []string
	ToolCallCount    int
	ToolNames        []string
}

type MCPExtension

type MCPExtension struct {
	RequestID          string
	ServiceID          string
	Method             string
	ToolName           string
	PresentedToolName  string
	ExecutedToolName   string
	ExecutionMode      string
	PolicyAction       string
	ResourceURI        string
	PromptName         string
	CompletionRefType  string
	CompletionArgument string
	ArgCount           *int
	ResultStatus       string
	Cancelled          *bool
	ToolArgsJSON       string
}

type MCPSummary

type MCPSummary struct {
	RequestCount   int64 `json:"request_count"`
	SuccessCount   int64 `json:"success_count"`
	FailureCount   int64 `json:"failure_count"`
	ToolsCallCount int64 `json:"tools_call_count"`
	AvgLatencyMS   int64 `json:"avg_latency_ms"`
}

type MCPUsageEvent

type MCPUsageEvent struct {
	InteractionEvent
	RequestID          string
	ServiceID          string
	Method             string
	ToolName           string
	PresentedToolName  string
	ExecutedToolName   string
	ExecutionMode      string
	PolicyAction       string
	ResourceURI        string
	PromptName         string
	CompletionRefType  string
	CompletionArgument string
	ArgCount           int
	ResultStatus       string
	Cancelled          bool
	ToolArgsJSON       string
}

type NoopObserver

type NoopObserver struct{}

type NoopSpan

type NoopSpan struct{}

func (NoopSpan) AddAnnotation

func (NoopSpan) AddAnnotation(string, string)

func (NoopSpan) Finish

func (NoopSpan) Finish(InteractionOutcome)

func (NoopSpan) SetExtension

func (NoopSpan) SetExtension(any)

type Observer

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

func NewObserver

func NewObserver(sink EventSink) Observer

func NewObserverWithAttribution

func NewObserverWithAttribution(sink EventSink, attribution *AgentAttribution) Observer

NewObserverWithAttribution wires an attribution holder so the observer stamps agent_id at Begin from the route id when the caller did not set it.

func (Observer) Begin

type Pipeline

type Pipeline interface {
	EventSink
	Start()
	Close() error
	DroppedEvents() uint64
	WriteFailures() uint64
}

type PrometheusProvider

type PrometheusProvider interface {
	PrometheusSnapshot() PrometheusSnapshot
}

PrometheusProvider is implemented by the in-process Prometheus sink and lets the Admin API expose an O(1) /metrics scrape without re-aggregating SQLite.

type PrometheusSnapshot

type PrometheusSnapshot struct {
	RequestsByKind map[string]int64
	FailuresByKind map[string]int64
	TokensByKind   map[string]int64
}

PrometheusSnapshot is a low-cardinality counter view of usage events keyed by interaction kind (llm/mcp/acp). It lives in this package so both the sink that produces it (pkg/metrics/pipeline) and the Admin API that renders it can share the type without an import cycle.

type QueryService

type QueryService interface {
	Summary() (Summary, error)
	ListEvents(kind string, opts EventListOptions) (EventListResponse, error)
	ListInteractions(opts EventListOptions) (EventListResponse, error)
	LLMTimeseries(opts TimeseriesOptions) (SeriesResponse, error)
	LLMBreakdown(opts BreakdownOptions) (BreakdownResponse, error)
	MCPTimeseries(opts TimeseriesOptions) (SeriesResponse, error)
	MCPBreakdown(opts BreakdownOptions) (BreakdownResponse, error)
	MCPToolsSummary(opts SummaryOptions) (BreakdownResponse, error)
	ACPTimeseries(opts TimeseriesOptions) (SeriesResponse, error)
	ACPBreakdown(opts BreakdownOptions) (BreakdownResponse, error)
	ACPSummary(opts BreakdownOptions) (BreakdownResponse, error)
	InteractionsSummary(opts BreakdownOptions) (BreakdownResponse, error)
}

type RuntimeStats

type RuntimeStats interface {
	DroppedEvents() uint64
	WriteFailures() uint64
}

RuntimeStats exposes pipeline health counters for the metrics Admin API so silently dropped or failed usage writes remain observable.

type SQLDBProvider

type SQLDBProvider interface {
	UsageDB() *gorm.DB
}

type SeriesResponse

type SeriesResponse struct {
	Bucket  string           `json:"bucket"`
	GroupBy string           `json:"group_by,omitempty"`
	Items   []map[string]any `json:"items"`
}

type Summary

type Summary struct {
	LLM LLMSummary `json:"llm"`
	MCP MCPSummary `json:"mcp"`
	ACP ACPSummary `json:"acp"`
}

type SummaryOptions

type SummaryOptions struct {
	From        string
	To          string
	Limit       int
	Filters     map[string]string
	Attribution *AttributionFilter
}

type TimeseriesOptions

type TimeseriesOptions struct {
	From        string
	To          string
	Bucket      string
	GroupBy     string
	Filters     map[string]string
	Attribution *AttributionFilter
}

type UsageService

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

func NewUsageService

func NewUsageService(pipeline Pipeline, query QueryService) *UsageService

func (*UsageService) AttachPrometheus

func (s *UsageService) AttachPrometheus(p PrometheusProvider)

AttachPrometheus registers the in-process Prometheus snapshot provider so the Admin API can expose an O(1) /metrics scrape.

func (*UsageService) Attribution

func (s *UsageService) Attribution() *AgentAttribution

Attribution returns the settable agent attribution holder. Callers inject the concrete attributor (the agent manager) after the gateway is bootstrapped, so the observer can stamp agent_id at write time.

func (*UsageService) Close

func (s *UsageService) Close() error

func (*UsageService) DroppedEvents

func (s *UsageService) DroppedEvents() uint64

func (*UsageService) Observer

func (s *UsageService) Observer() InteractionObserver

func (*UsageService) Prometheus

func (s *UsageService) Prometheus() PrometheusProvider

func (*UsageService) Query

func (s *UsageService) Query() QueryService

func (*UsageService) Start

func (s *UsageService) Start()

func (*UsageService) WriteFailures

func (s *UsageService) WriteFailures() uint64

Jump to

Keyboard shortcuts

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