metrics

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

包 metrics 提供基于 Prometheus 的全链路指标采集能力,覆盖 HTTP、LLM、Agent、缓存与数据库五大维度。

概述

本包通过 Collector 统一注册和记录 Prometheus 指标,使用 promauto 自动注册机制,避免手动管理 Registry。所有指标按 namespace 隔离, 支持多维度 label 分组,便于 Grafana 等工具进行可视化与告警。

核心类型

  • Collector:指标收集器,持有 Counter、Histogram、Gauge 等 Prometheus 向量指标,按业务域分组管理。

主要能力

  • HTTP 指标:请求总数、请求耗时、请求/响应体大小, 按 method/path/status 分组,状态码归类为 2xx/3xx/4xx/5xx。
  • LLM 指标:请求总数、请求耗时、Token 用量(prompt/completion)、 调用成本,按 provider/model 分组。
  • Agent 指标:执行总数、执行耗时、状态转换计数, 按 agent_id/agent_type 分组。
  • 缓存指标:命中与未命中计数,按 cache_type 分组。
  • 数据库指标:活跃/空闲连接数 Gauge、查询耗时 Histogram, 按 database/operation 分组。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collector

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

Collector 指标收集器

func NewCollector

func NewCollector(namespace string, logger *zap.Logger) *Collector

NewCollector 创建指标收集器

func (*Collector) RecordAgentExecution

func (c *Collector) RecordAgentExecution(agentType, status string, duration time.Duration)

RecordAgentExecution 记录 Agent 执行 K3 FIX: 移除 agentID 参数,仅使用 agentType(有限枚举值)

func (*Collector) RecordAgentInfo

func (c *Collector) RecordAgentInfo(agentID, agentType string)

RecordAgentInfo 记录 agent_id 到 agent_type 的映射关系(低频调用,仅用于调试)

func (*Collector) RecordAgentStateTransition

func (c *Collector) RecordAgentStateTransition(agentType, fromState, toState string)

RecordAgentStateTransition 记录 Agent 状态转换 K3 FIX: 使用 agentType 替代 agentID,避免 agent_id x from_state x to_state 笛卡尔积爆炸

func (*Collector) RecordCacheEviction

func (c *Collector) RecordCacheEviction(cacheType string)

RecordCacheEviction 记录缓存驱逐

func (*Collector) RecordCacheHit

func (c *Collector) RecordCacheHit(cacheType string)

RecordCacheHit 记录缓存命中

func (*Collector) RecordCacheMiss

func (c *Collector) RecordCacheMiss(cacheType string)

RecordCacheMiss 记录缓存未命中

func (*Collector) RecordCacheSize

func (c *Collector) RecordCacheSize(cacheType string, size int)

RecordCacheSize 记录缓存当前大小

func (*Collector) RecordDBConnections

func (c *Collector) RecordDBConnections(database string, open, idle int)

RecordDBConnections 记录数据库连接数

func (*Collector) RecordDBQuery

func (c *Collector) RecordDBQuery(database, operation string, duration time.Duration)

RecordDBQuery 记录数据库查询

func (*Collector) RecordHTTPRequest

func (c *Collector) RecordHTTPRequest(method, path string, status int, duration time.Duration, requestSize, responseSize int64)

RecordHTTPRequest 记录 HTTP 请求

func (*Collector) RecordLLMRequest

func (c *Collector) RecordLLMRequest(provider, model, status string, duration time.Duration, promptTokens, completionTokens int, cost float64)

RecordLLMRequest 记录 LLM 请求

Jump to

Keyboard shortcuts

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