Files
traces/pkg/collector/metrics_collector.go
Carles GarciaandGitHub ecff132e51 frontend: refactor response metrics collection (#4997)
* frontend: refactor metrics collection

The main goal is to provide accurate inspected bytes metrics (and othes) in responses for a potential future fair-use policy. Until now, we were counting inspected bytes regardless of the cache. With this PR, the responses that come from cache do not count towards inspected bytes.

- ensure that we collect response metrics in the same way for all endpoints.
- add cache header to querier responses, to indicate whether the response came from the cache
- set 0 to metrics from cached querier responses

* Update changelog

* Ignore gosec integer conversion rule in a few lines
2025-06-05 17:04:18 +02:00

30 lines
809 B
Go

package collector
import (
"go.uber.org/atomic"
)
// MetricsCollector is a thread-safe collector that uses atomic operations
// to accumulate metrics. We primarily use it to collect the total bytes read from
// a reader across a request
type MetricsCollector struct {
totalValue *atomic.Uint64
}
func NewMetricsCollector() *MetricsCollector {
return &MetricsCollector{
totalValue: atomic.NewUint64(0),
}
}
// Add adds new bytes read to TotalValue. this method is thread safe and
// satisfies the common.MetricsCallback type so it's used as callback at a lot of places
func (mc *MetricsCollector) Add(value uint64) {
mc.totalValue.Add(value)
}
// TotalValue returns the sum of total values collected by the collector
func (mc *MetricsCollector) TotalValue() uint64 {
return mc.totalValue.Load()
}