observability

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: MIT Imports: 22 Imported by: ? modules, 0 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 observability 提供 kite 的可观测性基础设施。

包含结构化日志(slog)、链路追踪(OTel 最小初始化)、敏感字段脱敏、 统一字段名常量、高频日志采样。所有日志只写 stdout,不落盘。

Package observability 提供 kite 的可观测性基础设施。

Package observability 提供 kite 的可观测性基础设施。

metrics.go 定义 Prometheus 指标,供 server middleware、service 层、worker 统一埋点。所有指标以 kite_ 为前缀,遵循 Prometheus 命名规范。

Package observability 提供 kite 的可观测性基础设施。

Package observability 提供 kite 的可观测性基础设施。

Package observability 提供 kite 的可观测性基础设施。

Package observability 提供 kite 的可观测性基础设施。

Index

Constants

View Source
const (
	// FieldPlatform 是音乐平台标识(netease / huawei)。
	FieldPlatform = "platform"

	// FieldUserID 是脱敏后的用户标识。
	FieldUserID = "user_id"

	// FieldRequestID 是 HTTP 请求级 ID,由中间件生成。
	FieldRequestID = "request_id"

	// FieldTaskID 是 worker 任务级 ID。
	FieldTaskID = "task_id"

	// FieldTraceID 是 OTel 自动注入的链路追踪 ID。
	FieldTraceID = "trace_id"

	// FieldSpanID 是 OTel span ID。
	FieldSpanID = "span_id"

	// FieldUpstreamLatencyMS 是调用上游(网易云)的耗时,单位毫秒。
	FieldUpstreamLatencyMS = "upstream_latency_ms"

	// FieldCacheHit 表示是否命中缓存(true / false)。
	FieldCacheHit = "cache_hit"

	// FieldErrorCode 是统一错误码,对应 errors 包定义的类型。
	FieldErrorCode = "error_code"

	// FieldMethod 是 HTTP 请求方法。
	FieldMethod = "method"

	// FieldPath 是 HTTP 请求路径。
	FieldPath = "path"

	// FieldStatus 是 HTTP 响应状态码。
	FieldStatus = "status"

	// FieldDurationMS 是请求处理耗时,单位毫秒。
	FieldDurationMS = "duration_ms"

	// FieldCookieHash 是 Cookie 的 SHA256 前 8 位,用于排障关联但不泄露内容。
	FieldCookieHash = "cookie_hash"

	// FieldPhoneHash 是手机号的 SHA256 前 8 位。
	FieldPhoneHash = "phone_hash"
)

字段名常量,OTel semantic conventions 风格。

所有日志和指标的属性键统一用这些常量,避免拼写不一致 (user_id vs userId vs uid)导致日志检索困难。

Variables

View Source
var LevelVar = new(slog.LevelVar)

LevelVar 是全局动态日志等级变量。

通过 SIGHUP 信号可在运行时调整日志级别,无需重启进程。 排障时临时调到 Debug 级别,看完再调回 Info。

Functions

func HandleSIGHUP

func HandleSIGHUP()

HandleSIGHUP 注册 SIGHUP 信号处理,收到时在 Info / Debug 间切换日志等级。

调用一次即可,在 main 中注册。

func InitLogger

func InitLogger(env string) *slog.Logger

InitLogger 初始化全局 slog Logger。

生产环境用 JSON handler 输出到 stdout,开发环境用 tint 彩色文本。 日志等级由 LevelVar 控制,默认 Info,可通过 SIGHUP 信号动态调整。

所有日志只写 stdout,不落盘。日志采集交给 sidecar(Fluent Bit / Vector) 转发到 Loki / ClickHouse。

func InitTracer

func InitTracer(otelCfg config.OTelConfig) (shutdown func(context.Context) error, err error)

InitTracer 初始化 OTel tracer,按 config 选择 exporter。

exporter 类型:

  • none:noop exporter,不导出 span(本地开发默认)。
  • otlp-grpc:通过 gRPC 发到 OTLP collector。
  • otlp-http:通过 HTTP 发到 OTLP collector。

sampler 用 ParentBased(TraceIDRatioBased),既尊重上游采样决策 又能控制本地根 span 采样率。

必须在 InitLogger 之前调用,确保 otel_handler 能拿到有效 SpanContext。

func StartSpan

func StartSpan(ctx context.Context, name string) (context.Context, trace.Span)

StartSpan 在当前 context 上开启一个新 span,返回带 span 的 context 和 span。

供 handler / service 在关键操作处调用,自动生成 trace_id 注入日志。

Types

type Metrics

type Metrics struct {
	// RequestTotal 是 HTTP 请求总数(按 method / path / status 标签)。
	RequestTotal *prometheus.CounterVec

	// RequestDuration 是 HTTP 请求处理耗时(秒)。
	RequestDuration *prometheus.HistogramVec

	// CacheHits 是缓存命中次数。
	CacheHits prometheus.Counter

	// CacheMisses 是缓存未命中次数。
	CacheMisses prometheus.Counter

	// UpstreamErrors 是上游调用失败次数。
	UpstreamErrors prometheus.Counter

	// UpstreamLatency 是上游调用耗时(秒)。
	UpstreamLatency *prometheus.HistogramVec

	// CookieHealthStatus 是 Cookie 健康检查结果(1=可用,0=失效)。
	CookieHealthStatus *prometheus.GaugeVec
}

Metrics 持有 kite 全量 Prometheus 指标。

server middleware、service 层、worker 各自在对应位置调用方法递增指标。 通过依赖注入传入各组件,避免全局变量散落。

func NewMetrics

func NewMetrics() *Metrics

NewMetrics 创建并注册全量指标到默认 registry。

func NewTestMetrics

func NewTestMetrics() *Metrics

NewTestMetrics 创建不注册到全局 registry 的 Metrics,供测试用。

避免多次注册同一指标导致 panic。

func (*Metrics) ObserveUpstreamLatency

func (m *Metrics) ObserveUpstreamLatency(operation string, seconds float64)

ObserveUpstreamLatency 记录上游调用耗时。

func (*Metrics) RecordCacheHit

func (m *Metrics) RecordCacheHit()

RecordCacheHit 递增缓存命中计数。

func (*Metrics) RecordCacheMiss

func (m *Metrics) RecordCacheMiss()

RecordCacheMiss 递增缓存未命中计数。

func (*Metrics) RecordUpstreamError

func (m *Metrics) RecordUpstreamError()

RecordUpstreamError 递增上游错误计数。

func (*Metrics) SetCookieHealth

func (m *Metrics) SetCookieHealth(userID string, healthy bool)

SetCookieHealth 更新 Cookie 健康状态。

Jump to

Keyboard shortcuts

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