metrics

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 12 Imported by: 0 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 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

View Source
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. 前缀,避免与 用户或其它库的同名标签混淆。

View Source
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 的取值全集。

View Source
const (
	FrameEvent     = "event"
	FrameHeartbeat = "heartbeat"
	FrameError     = "error"
	FrameData      = "data"
	FrameHalfClose = "half_close"
	FrameEnd       = "end"
)

Variables

This section is empty.

Functions

func ContractCall

func ContractCall(ctx context.Context, system, method, code string, start time.Time)

ContractCall 记录一次契约调用。code 是失败时的 apperr 错误码, 成功传空串——错误码是有限的常量集合,可以安全地当标签。

func DBQueryOp

func DBQueryOp(ctx context.Context, op string, err error, start time.Time)

DBQueryOp 记录一次数据库查询。op 必须来自 Operation——完整 SQL 不进标签, 那等于把指标基数交给业务代码写的每一条语句。

func HTTPClientCall added in v0.10.0

func HTTPClientCall(ctx context.Context, method, statusClass string, err error, start time.Time)

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 JobRun

func JobRun(ctx context.Context, name, outcome string, start time.Time)

JobRun 记录周期任务的一轮。outcome 取 OutcomeOK/Error/Skipped (skipped = 锁被其它副本持有,属正常)。

func Meter

func Meter() metric.Meter

Meter 返回框架 scope 下的 meter,供框架内需要自定义仪器的地方使用。

func ObserveOutboxBacklog

func ObserveOutboxBacklog(schema string, observe func(context.Context) (Backlog, error)) func()

ObserveOutboxBacklog 注册 outbox 积压的观测量,返回注销函数。 observe 在采集周期(默认 60s)被调用,未配置 exporter 时全局 provider 是 noop,回调根本不会执行——也就不会有那次数据库查询。

func Operation

func Operation(sql string) string

Operation 从 SQL 提取动词,未识别的一律归为 "other"。

func OutboxDead

func OutboxDead(ctx context.Context, topic string)

OutboxDead 记录一条转入死信的事件。这条曲线非零就该有人看。

func OutboxDelivery

func OutboxDelivery(ctx context.Context, topic string, err error, start time.Time)

OutboxDelivery 记录一条事件的投递结果。事件停摆是最难发现的故障 (探针依然绿着),这条曲线归零就是告警条件。

func Outcome

func Outcome(err error) string

Outcome 把 error 收敛为 outcome 枚举。

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

func StreamDrainForcedClose(ctx context.Context, system, method, transport string)

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

func StreamMessageReceived(ctx context.Context, system, method, direction string)

StreamMessageReceived 记录成功从有界队列取出的消息。

func StreamMessageSent added in v0.9.7

func StreamMessageSent(ctx context.Context, system, method, direction string)

StreamMessageSent 记录成功进入有界队列的消息,不读取或序列化消息内容。

func StreamOpened added in v0.9.7

func StreamOpened(ctx context.Context, system, method string)

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.

Types

type Backlog

type Backlog struct {
	// Pending 是待投递事件数。
	Pending int64
	// OldestAge 是最老一条待投递事件的滞留时长;无积压时为 0。
	// 比条数更适合做告警阈值:积压 1000 条但都在 200ms 内清掉是正常的,
	// 积压 3 条但最老的躺了 20 分钟说明投递已经停了。
	OldestAge time.Duration
}

Backlog 是 outbox 待投递积压的一次观测。

Jump to

Keyboard shortcuts

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