Documentation
¶
Overview ¶
Package metrics 集中定义 appkit 自动埋点的指标。
指标名与标签集在这里钉死,业务代码不参与——这是刻意的:指标的成本不在 采集而在基数,一个把 URL 路径、错误消息或用户 id 当标签的埋点足以打爆 后端存储,而这类事故通常由散落各处的"顺手加一个标签"造成。所有标签值 要么是代码里的常量(系统名、方法名、topic、任务名),要么是本包收敛过的 枚举(outcome、SQL 动词),没有第三种。
埋点覆盖 RED 三件套(Rate / Errors / Duration,直方图一并给出)的五条路径: 契约调用、Local Stream、outbox 投递、周期任务、数据库查询;SSE/WSS 另记录传输帧、 应用载荷字节、协议错误与 WebSocket 强制关闭。Host ManagedService 另提供按稳定 module/service 名与固定状态枚举聚合的当前实例数。HTTP 入站不在此列——otelhttp 已经 产出 http.server.request.duration(含 http.route),再埋一遍就是双重计数。
未配置 OTLP 端点时全局 MeterProvider 是 noop,各 Record 调用近乎零成本, 因此埋点无开关。
Index ¶
- Constants
- func ContractCall(ctx context.Context, system, method, code string, start time.Time)
- func DBQueryOp(ctx context.Context, op string, err error, start time.Time)
- func HTTPClientCall(ctx context.Context, method, statusClass string, err error, start time.Time)
- func Initialize()
- func JobRun(ctx context.Context, name, outcome string, start time.Time)
- func Meter() metric.Meter
- func ObserveOutboxBacklog(schema string, observe func(context.Context) (Backlog, error)) func()
- func Operation(sql string) string
- func OutboxDead(ctx context.Context, topic string)
- func OutboxDelivery(ctx context.Context, topic string, err error, start time.Time)
- func Outcome(err error) string
- func StreamBackpressureWait(ctx context.Context, system, method, direction string, duration time.Duration)
- func StreamClosed(ctx context.Context, system, method string, err error, duration time.Duration, ...)
- func StreamDrainForcedClose(ctx context.Context, system, method, transport string)
- func StreamFirstMessage(ctx context.Context, system, method, direction string, duration time.Duration)
- func StreamMessageReceived(ctx context.Context, system, method, direction string)
- func StreamMessageSent(ctx context.Context, system, method, direction string)
- func StreamOpened(ctx context.Context, system, method string)
- func StreamProtocolError(ctx context.Context, system, method, transport, direction string, err error)
- func StreamTransportBytesReceived(ctx context.Context, system, method, transport, direction string, n int64)
- func StreamTransportBytesSent(ctx context.Context, system, method, transport, direction string, n int64)
- func StreamTransportFrameReceived(ctx context.Context, system, method, transport, direction, frameType string)
- func StreamTransportFrameSent(ctx context.Context, system, method, transport, direction, frameType string)
- type Backlog
Constants ¶
const ( AttrSystem = "appkit.contract.system" AttrMethod = "appkit.contract.method" AttrTopic = "appkit.outbox.topic" AttrSchema = "appkit.outbox.schema" AttrJob = "appkit.job.name" AttrOperation = "db.operation" AttrOutcome = "appkit.outcome" AttrErrorCode = "appkit.error.code" AttrTransport = "appkit.contract.stream.transport" AttrDirection = "appkit.contract.stream.direction" AttrFrameType = "appkit.contract.stream.frame.type" AttrHostServiceModule = "appkit.host.managed_service.module" AttrHostServiceName = "appkit.host.managed_service.name" AttrHostServiceState = "appkit.host.managed_service.state" )
标签键。与 OTel 语义约定不冲突的一律加 appkit. 前缀,避免与 用户或其它库的同名标签混淆。
const ( OutcomeOK = "ok" OutcomeError = "error" OutcomeSkipped = "skipped" OutcomeCanceled = "canceled" OutcomeTimeout = "timeout" OutcomeOther = "other" TransportLocal = "local" TransportSSE = "sse" TransportWebSocket = "websocket" DirectionClientToServer = "client_to_server" DirectionServerToClient = "server_to_client" )
outcome 的取值全集。
const ( FrameEvent = "event" FrameHeartbeat = "heartbeat" FrameError = "error" FrameData = "data" FrameHalfClose = "half_close" FrameEnd = "end" )
Variables ¶
This section is empty.
Functions ¶
func ContractCall ¶
ContractCall 记录一次契约调用。code 是失败时的 apperr 错误码, 成功传空串——错误码是有限的常量集合,可以安全地当标签。
func HTTPClientCall ¶ added in v0.10.0
HTTPClientCall records one outbound request using only bounded attributes.
func Initialize ¶ added in v0.9.7
func Initialize()
Initialize eagerly binds instruments to the current global MeterProvider. telemetry.Init calls this immediately after installing its provider.
func ObserveOutboxBacklog ¶
ObserveOutboxBacklog 注册 outbox 积压的观测量,返回注销函数。 observe 在采集周期(默认 60s)被调用,未配置 exporter 时全局 provider 是 noop,回调根本不会执行——也就不会有那次数据库查询。
func OutboxDead ¶
OutboxDead 记录一条转入死信的事件。这条曲线非零就该有人看。
func OutboxDelivery ¶
OutboxDelivery 记录一条事件的投递结果。事件停摆是最难发现的故障 (探针依然绿着),这条曲线归零就是告警条件。
func StreamBackpressureWait ¶ added in v0.9.7
func StreamBackpressureWait(ctx context.Context, system, method, direction string, duration time.Duration)
StreamBackpressureWait 记录发送开始时队列已满后的等待时长。
func StreamClosed ¶ added in v0.9.7
func StreamClosed(ctx context.Context, system, method string, err error, duration time.Duration, timedOut bool)
StreamClosed 记录唯一终态。错误码只保留框架已知的有限集合,其余值折叠为 other。
func StreamDrainForcedClose ¶ added in v0.9.7
StreamDrainForcedClose records the first forced close of one managed stream connection. Callers are responsible for deduplicating per connection.
func StreamFirstMessage ¶ added in v0.9.7
func StreamFirstMessage(ctx context.Context, system, method, direction string, duration time.Duration)
StreamFirstMessage 记录某一接收方向首条成功消息的延迟;同一方向只由调用方记录一次。
func StreamMessageReceived ¶ added in v0.9.7
StreamMessageReceived 记录成功从有界队列取出的消息。
func StreamMessageSent ¶ added in v0.9.7
StreamMessageSent 记录成功进入有界队列的消息,不读取或序列化消息内容。
func StreamOpened ¶ added in v0.9.7
StreamOpened 记录一次成功建立的 Local Contract Stream。
func StreamProtocolError ¶ added in v0.9.7
func StreamProtocolError(ctx context.Context, system, method, transport, direction string, err error)
StreamProtocolError records a rejected inbound application frame. Error codes are restricted to the framework whitelist; unknown values collapse to other.
func StreamTransportBytesReceived ¶ added in v0.9.7
func StreamTransportBytesReceived(ctx context.Context, system, method, transport, direction string, n int64)
StreamTransportBytesReceived records the payload bytes returned by a successful transport read.
func StreamTransportBytesSent ¶ added in v0.9.7
func StreamTransportBytesSent(ctx context.Context, system, method, transport, direction string, n int64)
StreamTransportBytesSent records bytes accepted by the transport writer. A partial write is recorded by its actual positive byte count.
func StreamTransportFrameReceived ¶ added in v0.9.7
func StreamTransportFrameReceived(ctx context.Context, system, method, transport, direction, frameType string)
StreamTransportFrameReceived records one successfully read application frame.
func StreamTransportFrameSent ¶ added in v0.9.7
func StreamTransportFrameSent(ctx context.Context, system, method, transport, direction, frameType string)
StreamTransportFrameSent records one successfully written application frame. frameType and transport are normalized to fixed transport-specific enums.