segmentio / stats

Go package for abstracting stats collection
https://godoc.org/github.com/segmentio/stats
MIT License
208 stars 32 forks source link

Prometheus Handler: add the ability to ignore labels by name #110

Closed schigh closed 2 years ago

schigh commented 5 years ago

TL;DR:

This PR is based on the need to exclude certain high-cardinality labels from the prometheus handler when collecting stats.
In our specific use case, we needed to filter out the http_req_path label (from github.com/segmentio/stats/httpstats) because many of our URIs contain UUIDs and the like.

Usage:

// create prometheus handler
handler := &prometheus.Handler{
    TrimPrefix: "my_service_name",
}
// set HC tags
handler.IgnoreLabels([]string{"http_req_path"})

Benchmarks:

go test -bench BenchmarkHandleMetric -benchmem

current master:

goos: darwin
goarch: amd64
pkg: github.com/segmentio/stats/prometheus
BenchmarkHandleMetric/counter-8                  5687409               189 ns/op               0 B/op          0 allocs/op
BenchmarkHandleMetric/gauge-8                    6038472               187 ns/op               0 B/op          0 allocs/op
BenchmarkHandleMetric/histogram-8                5275975               230 ns/op               0 B/op          0 allocs/op
PASS
ok      github.com/segmentio/stats/prometheus   4.078s

this branch:

goos: darwin
goarch: amd64
pkg: github.com/segmentio/stats/prometheus
BenchmarkHandleMetric/counter-8                  6561445               186 ns/op               0 B/op          0 allocs/op
BenchmarkHandleMetric/gauge-8                    6449792               184 ns/op               0 B/op          0 allocs/op
BenchmarkHandleMetric/histogram-8                5353644               223 ns/op               0 B/op          0 allocs/op
PASS
ok      github.com/segmentio/stats/prometheus   4.224s