指标
ling-base common/metrics 模块文档
在线 Playground
在浏览器中直接体验本页相关 API,无需本地安装 Go 环境。
metrics
Reusable wrapper around the OpenTelemetry Go metrics SDK with a Prometheus exporter, handling provider setup, resource attributes, and custom histogram buckets.
Features
- OpenTelemetry MeterProvider with Prometheus exporter
- Configurable service name, version, namespace, and environment
- Custom default histogram bucket boundaries
- Prometheus-compatible HTTP handler for
/metrics - Private Prometheus registry by default (avoids global pollution)
- Optional global MeterProvider registration
- Convenience attribute helpers (KeyValue, KeyInt, KeyFloat, KeyBool)
Key types
Config-- provider configuration (service info, attributes, buckets, registry)Provider-- wraps MeterProvider with exporter and HTTP handler
Key functions
Init(cfg)-- create and optionally register a global ProviderNewProvider(cfg)-- create Provider without setting globalsProvider.Meter()-- get anotelmetric.Meterfor instrument creationProvider.HTTPHandler()-- Prometheus/metricsendpoint handlerProvider.Shutdown(ctx)-- flush and shut downKeyValue,KeyInt,KeyFloat,KeyBool-- attribute helpers
Quick start
import (
"net/http"
"github.com/LingByte/ling-base/common/metrics"
)
provider, err := metrics.Init(metrics.Config{
ServiceName: "my-service",
ServiceVersion: "1.0.0",
})
if err != nil {
log.Fatal(err)
}
defer provider.Shutdown(ctx)
http.Handle("/metrics", provider.HTTPHandler())
go http.ListenAndServe(":9090", nil)
counter, _ := provider.Meter().Float64Counter("requests_total")
counter.Add(ctx, 1)License
MIT