可觀測性與健康
先取得模組:go get github.com/yshengliao/gortexa@v0.27.0。以下片段皆對 v0.27.0 編譯驗證過。
health
Section titled “health”三態 registry(Healthy/Degraded/Unhealthy;Degraded 仍視為 serving,只有 Unhealthy 不是)+ 標準 gRPC health 橋接。一般用 App 自帶的 registry(app.Health()),不必自建。
import "github.com/yshengliao/gortexa/health"
app.Health().Register("db", func(ctx context.Context) health.State { return health.Healthy})overall := app.Health().Overall(ctx) // health.Healthy / Degraded / UnhealthyRegister 的 check 會被 /readyz 聚合;Names()、Snapshot(ctx)、State(ctx, name) 供查詢。
observability
Section titled “observability”gRPC 儀表是 StatsHandler(非 interceptor)。tracing/metrics 在有設 OTLP endpoint 時匯出,否則為 no-op。三個 Setup* 各回一個 ShutdownFunc。
import "github.com/yshengliao/gortexa/observability"
log, logShutdown, err := observability.SetupLogs(ctx, cfg.Log, cfg.Observ)traceShutdown, err := observability.SetupTracing(ctx, cfg.Observ)metricShutdown, err := observability.SetupMetrics(ctx, cfg.Observ)// observability.ServerStatsHandler() 傳給 kernel.WithStatsHandler// 三個 ShutdownFunc 各傳給 kernel.WithShutdownHook要對 uptrace 等需要認證 header 的 OTLP 後端匯出,OTel SDK 會讀標準環境變數 OTEL_EXPORTER_OTLP_HEADERS(例如 uptrace-dsn=<DSN>),或改由 collector 中繼。