benbjohnson / litestream

Streaming replication for SQLite.
https://litestream.io
Apache License 2.0
11.12k stars 256 forks source link

Allow configuring metrics collection #518

Open hifi opened 1 year ago

hifi commented 1 year ago

When using Litestream as a library we found out that since litestream.NewDB exposes new promauto metrics there are a couple of issues. For one we can't disable metrics collection while we use Prometheus metrics within the main program so we're currently forced to expose per-database metrics.

Other is that we hit an unexpected deadlock as it seems the go Prometheus module holds a global lock while scraping and if the scraper is locked out and new metrics are being added it will deadlock as it will access the same lock. Making it easier to configure metrics collection and not explicitly adding them in NewDB would be a good start.

goroutine 1 [sync.RWMutex.Lock, 13 minutes]:
sync.runtime_SemacquireRWMutex(0x1?, 0x90?, 0xc000a06730?)
    /usr/local/go/src/runtime/sema.go:87 +0x25
sync.(*RWMutex).Lock(0x0?)
    /usr/local/go/src/sync/rwmutex.go:152 +0x6a
github.com/prometheus/client_golang/prometheus.(*metricMap).getOrCreateMetricWithLabelValues(0xc000824240, 0xc001f84f30?, {0xc001f84f30?, 0x1, 0x1}, {0x0?, 0x0, 0x0})
    /go/pkg/mod/github.com/prometheus/client_golang@v1.16.0/prometheus/vec.go:513 +0x145
github.com/prometheus/client_golang/prometheus.(*MetricVec).GetMetricWithLabelValues(0xc000824210, {0xc001f84f20?, 0xc000a06840?, 0x413005?})
    /go/pkg/mod/github.com/prometheus/client_golang@v1.16.0/prometheus/vec.go:233 +0x9c
github.com/prometheus/client_golang/prometheus.(*GaugeVec).GetMetricWithLabelValues(0xc000a06920?, {0xc001f84f20?, 0x46ec8e?, 0xc002c7632b?})
    /go/pkg/mod/github.com/prometheus/client_golang@v1.16.0/prometheus/gauge.go:203 +0x1b
github.com/prometheus/client_golang/prometheus.(*GaugeVec).WithLabelValues(...)
    /go/pkg/mod/github.com/prometheus/client_golang@v1.16.0/prometheus/gauge.go:236
github.com/benbjohnson/litestream.NewDB({0xc002c76300, 0x38})
    /go/pkg/mod/github.com/beeper/litestream@v0.0.0-20231025101501-ed7121bbfb71/db.go:133 +0x35c
github.com/beeper/liteserv/internal/state.(*Database).Open(0xc0004f9130, 0xc000556b00)
    /build/internal/state/database.go:153 +0x93
github.com/beeper/liteserv/internal/state.(*State).tryStartReplicate(0xc0000e1600, {0x1f7e310, 0x2cdad40}, {{0x1f69ef0, 0xc00057e260}, 0x0, {0x0, 0x0}, {0xc00525f400, 0x5a, ...}, ...}, ...)
    /build/internal/state/state.go:258 +0x1ba
github.com/beeper/liteserv/internal/state.(*State).Start(0xc0000e1600, {0x1f7e310, 0x2cdad40})
    /build/internal/state/state.go:349 +0x103b
main.main()
    /build/cmd/liteserv/main.go:69 +0x445
hifi commented 1 year ago

I haven't really figured out any neat way. The per-database metrics are what I'd want to disable but keep the replica ones as they are aggregates. Promauto is very handy but it's also an anti-pattern for libraries when you want to control what metrics are being exported.