Documentation
¶
Overview ¶
Package telemetry 统一初始化三信号:slog 日志、OpenTelemetry trace 与 metric。
日志始终可用。Init 为兼容旧服务,仍由 OTEL_EXPORTER_OTLP_ENDPOINT 门控 trace/metric;Bootstrap 对显式 YAML 配置使用 InitWithExportConfig。 未启用 exporter 时全局 provider 保持默认 noop——本地开发无 collector 也零成本。 Telemetry.Shutdown 应作为最后一个 OnStop 注册,保证其余关停钩子产生的 span/metric 也能被 flush。
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// ServiceName 写入 resource 的 service.name。
ServiceName string
// Env 写入 resource 的 deployment.environment(如 dev/staging/prod)。
Env string
// LogLevel 取 debug|info|warn|error,空值等价 info。
LogLevel string
// LogFormat 取 json|text,空值等价 json。
LogFormat string
}
Config 是遥测初始化配置。零值可用:info 级 JSON 日志、不装 trace/metric SDK。
type ExportConfig ¶ added in v0.10.0
type ExportConfig struct {
Traces OTLPExportConfig
Metrics OTLPExportConfig
}
ExportConfig 是 traces 与 metrics 的运行时导出配置。 Bootstrap 从服务配置文件构造它;业务服务通常不应直接初始化 exporter。
type OTLPExportConfig ¶ added in v0.10.0
OTLPExportConfig 配置一个 OTLP/HTTP signal exporter。 Endpoint 使用完整 URL;signal 专属或通用 OTEL endpoint 环境变量存在时, exporter 按 OpenTelemetry 规则优先使用环境变量。
type Telemetry ¶
type Telemetry struct {
// Logger 输出到 stdout;ctx 携带有效 span 时自动附加 trace_id/span_id。
Logger *slog.Logger
// contains filtered or unexported fields
}
Telemetry 持有初始化产物。字段在 Init 返回后不再变更,可并发使用。
func Init ¶
Init 保留既有兼容行为:构造日志器;若 OTEL_EXPORTER_OTLP_ENDPOINT 非空, 同时装配 OTLP HTTP trace/metric SDK;否则全局 provider 保持默认 noop。
func InitWithExportConfig ¶ added in v0.10.0
func InitWithExportConfig(ctx context.Context, cfg Config, exporters ExportConfig) (*Telemetry, error)
InitWithExportConfig 按显式运行时配置构造日志器及 OTLP/HTTP signal exporter。 配置块存在时由调用方决定 Enabled;OTEL endpoint 环境变量仅覆盖地址,不会 打开 Enabled=false 的信号。启用的信号若没有有效 endpoint 则 fail-fast。