Files
Losita 2a96f4c6f9 Version: 0.9.76.dev.260505
后端:
1.阶段 6 agent / memory 服务化收口
- 新增 cmd/agent 独立进程入口,承载 agent zrpc server、agent outbox relay / consumer 和运行时依赖初始化
- 补齐 services/agent/rpc 的 Chat stream 与 conversation meta/list/timeline、schedule-preview、context-stats、schedule-state unary RPC
- 新增 gateway/client/agent 与 shared/contracts/agent,将 /api/v1/agent chat 和非 chat 门面切到 agent zrpc
- 收缩 gateway 本地 AgentService 装配,双 RPC 开关开启时不再初始化本地 agent 编排、LLM、RAG 和 memory reader fallback
- 将 backend/memory 物理迁入 services/memory,私有实现收入 internal,保留 module/model/observe 作为 memory 服务门面
- 调整 memory outbox、memory reader 和 agent 记忆渲染链路的 import 与服务边界,cmd/memory 独占 memory worker / consumer
- 关闭 gateway 侧 agent outbox worker 所有权,agent relay / consumer 由 cmd/agent 独占,gateway 仅保留 HTTP/SSE 门面与迁移期开关回退
- 更新阶段 6 文档,记录 agent / memory 当前切流点、smoke 结果,以及 backend/client 与 gateway/shared 的目录收口口径
2026-05-05 19:31:39 +08:00

159 lines
3.5 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package observe
import (
"sort"
"strings"
"sync"
)
// CounterSnapshot 是轻量计数器的快照视图,供后续排障或接平台时读取。
type CounterSnapshot struct {
Name string
Labels map[string]string
Value int64
}
// MetricsRecorder 描述 memory 模块对计数器的最小依赖。
type MetricsRecorder interface {
AddCounter(name string, delta int64, labels map[string]string)
Snapshot() []CounterSnapshot
}
// NewNopMetrics 返回空实现,保证无观测平台时仍可安全运行。
func NewNopMetrics() MetricsRecorder {
return nopMetrics{}
}
type nopMetrics struct{}
func (nopMetrics) AddCounter(string, int64, map[string]string) {}
func (nopMetrics) Snapshot() []CounterSnapshot {
return nil
}
// MetricsRegistry 是 memory 模块当前阶段的轻量内存计数器实现。
//
// 职责边界:
// 1. 只做线程安全计数,不负责导出协议;
// 2. 标签做低基数归一化,避免治理期临时字段把指标打爆;
// 3. 后续若项目统一接 Prometheus可直接保留调用口径并替换实现。
type MetricsRegistry struct {
mu sync.RWMutex
counters map[string]*counterRecord
}
type counterRecord struct {
name string
labels map[string]string
value int64
}
func NewMetricsRegistry() *MetricsRegistry {
return &MetricsRegistry{
counters: make(map[string]*counterRecord),
}
}
// AddCounter 追加计数值delta<=0 时直接忽略,避免脏数据污染快照。
func (r *MetricsRegistry) AddCounter(name string, delta int64, labels map[string]string) {
if r == nil || delta <= 0 {
return
}
name = strings.TrimSpace(name)
if name == "" {
return
}
normalizedLabels := normalizeLabels(labels)
key := buildCounterKey(name, normalizedLabels)
r.mu.Lock()
defer r.mu.Unlock()
if existing, ok := r.counters[key]; ok {
existing.value += delta
return
}
r.counters[key] = &counterRecord{
name: name,
labels: normalizedLabels,
value: delta,
}
}
// Snapshot 返回当前全部计数器快照,便于后续排障或测试读取。
func (r *MetricsRegistry) Snapshot() []CounterSnapshot {
if r == nil {
return nil
}
r.mu.RLock()
defer r.mu.RUnlock()
if len(r.counters) == 0 {
return nil
}
keys := make([]string, 0, len(r.counters))
for key := range r.counters {
keys = append(keys, key)
}
sort.Strings(keys)
result := make([]CounterSnapshot, 0, len(keys))
for _, key := range keys {
record := r.counters[key]
labels := make(map[string]string, len(record.labels))
for labelKey, labelValue := range record.labels {
labels[labelKey] = labelValue
}
result = append(result, CounterSnapshot{
Name: record.name,
Labels: labels,
Value: record.value,
})
}
return result
}
func normalizeLabels(labels map[string]string) map[string]string {
if len(labels) == 0 {
return nil
}
result := make(map[string]string, len(labels))
for key, value := range labels {
key = strings.TrimSpace(key)
value = strings.TrimSpace(value)
if key == "" || value == "" {
continue
}
result[key] = value
}
if len(result) == 0 {
return nil
}
return result
}
func buildCounterKey(name string, labels map[string]string) string {
if len(labels) == 0 {
return name
}
keys := make([]string, 0, len(labels))
for key := range labels {
keys = append(keys, key)
}
sort.Strings(keys)
var sb strings.Builder
sb.WriteString(name)
for _, key := range keys {
sb.WriteString("|")
sb.WriteString(key)
sb.WriteString("=")
sb.WriteString(labels[key])
}
return sb.String()
}