mirror of
https://github.com/luxfi/metric.git
synced 2026-07-27 04:12:44 +00:00
de11c8d9ca5d21ed4ed05d0f2013ee29eb559609
Adds a fire-and-forget metric exporter that ships MetricFamily
snapshots over a luxfi/zap envelope to the o11y collector. Mirrors
luxfi/trace's ZAP exporter (MsgSpanBatch=1) — zero protobuf, zero
OTLP, zero gRPC. Wire format:
zap envelope, MsgType=MsgMetricBatch
└─ root object
└─ FieldPayload (bytes): JSON-encoded MetricBatch
MetricBatch carries app/version/resource metadata + a list of
MetricFamily rows (Gatherer.Gather() output translated to a
JSON-stable wire shape). +Inf histogram bucket dropped on encode
(JSON can't represent +Inf and SampleCount already conveys total).
NewZAPExporter creates the exporter; ExportGatherer(ctx, reg) drives
it from a scrape loop. Connect failures are silent — exporter retries
on the next call so the host never blocks on collector reachability.
Test is gated on -tags metrics (matches the rest of the test suite;
the package's no-op default-build skips Gather population entirely).
Receiver side lands in hanzoai/o11y/pkg/zapmetricreceiver.
Lux Metrics Library
github.com/luxfi/metric is the native metrics library for Lux. It produces a
scrape-compatible text exposition format and is designed for low overhead in
hot paths.
Features
- Single, native API for counters, gauges, histograms, summaries, and vectors
- Scrape-compatible text format encoder and HTTP handler
- Registry + gatherer model for composition
- Optional no-op build for benchmark runs (build-tagged swap)
Installation
go get github.com/luxfi/metric@latest
Quick Start
package main
import (
"net/http"
"github.com/luxfi/metric"
)
func main() {
m := metric.New("myapp")
requests := m.NewCounter("requests_total", "Total requests")
latency := m.NewHistogram("request_seconds", "Request latency (s)", metric.DefBuckets)
requests.Inc()
latency.Observe(0.123)
http.Handle("/metrics", metric.Handler())
_ = http.ListenAndServe(":8080", nil)
}
Custom Registry
reg := metric.NewRegistry()
m := metric.NewWithRegistry("myapp", reg)
requests := m.NewCounter("requests_total", "Total requests")
requests.Inc()
handler := metric.NewHTTPHandler(reg, metric.HandlerOpts{})
Vector Metrics
m := metric.New("myapp")
byRoute := m.NewCounterVec("requests_total", "Requests by route", []string{"method", "route"})
byRoute.WithLabelValues("GET", "/").Inc()
Metrics Off Build
For benchmark runs, you can swap the entire package to no-op implementations using build tags. This keeps call sites unchanged while minimizing overhead.
go test -tags metrics ./...
When built without the metrics tag, metric.NewRegistry() and the package
defaults return no-op implementations. Pre-bind label values in hot paths to
avoid argument construction overhead.
Description
Prometheus metrics helpers: standard counters, gauges, histograms, and instrumentation conventions for all Lux components.
821 KiB
Languages
Go
88.4%
HTML
10.5%
Makefile
1.1%