telemetry

package
v0.5.4 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2026 License: MIT Imports: 16 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 telemetry 统一初始化三信号:slog 日志、OpenTelemetry trace 与 metric。

日志始终可用;trace/metric SDK 只在 OTEL_EXPORTER_OTLP_ENDPOINT 存在时装配, 否则全局 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 Telemetry

type Telemetry struct {
	// Logger 输出到 stdout;ctx 携带有效 span 时自动附加 trace_id/span_id。
	Logger *slog.Logger
	// contains filtered or unexported fields
}

Telemetry 持有初始化产物。字段在 Init 返回后不再变更,可并发使用。

func Init

func Init(ctx context.Context, cfg Config) (*Telemetry, error)

Init 构造日志器;若 OTEL_EXPORTER_OTLP_ENDPOINT 存在,再装配 OTLP HTTP trace/metric SDK 并设置全局 provider 与 W3C propagator。

func (*Telemetry) Shutdown

func (t *Telemetry) Shutdown(ctx context.Context) error

Shutdown flush 缓冲中的 span/metric 后关闭 SDK;未装 SDK 时是空操作。

Jump to

Keyboard shortcuts

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