pomerium/internal/telemetry/metrics.go
Denis Mishin 3cf420afc9
telemetry: backport component (#5655)
## Summary

Backport `telemetry.Component` that provides centralized tracing,
logging and metrics for operations.

## Related issues

<!-- For example...
- #159
-->

## User Explanation

<!-- How would you explain this change to the user? If this
change doesn't create any user-facing changes, you can leave
this blank. If filled out, add the `docs` label -->

## Checklist

- [ ] reference any related issues
- [ ] updated unit tests
- [ ] add appropriate label (`enhancement`, `bug`, `breaking`,
`dependencies`, `ci`)
- [ ] ready for review

---------

Co-authored-by: Kenneth Jenkins <51246568+kenjenkins@users.noreply.github.com>
2025-06-16 13:10:51 -04:00

56 lines
1.1 KiB
Go

package telemetry
import (
"sync"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/metric"
)
var (
metricLock sync.RWMutex
counters = map[[2]string]metric.Int64Counter{}
histograms = map[[2]string]metric.Float64Histogram{}
)
func getInt64Counter(component, name string) metric.Int64Counter {
metricLock.RLock()
c, ok := counters[[2]string{component, name}]
metricLock.RUnlock()
if ok {
return c
}
metricLock.Lock()
defer metricLock.Unlock()
c, ok = counters[[2]string{component, name}]
if ok {
return c
}
c, _ = otel.Meter(component).Int64Counter(component + "." + name)
counters[[2]string{component, name}] = c
return c
}
func getFloat64Histogram(component, name string, options ...metric.Float64HistogramOption) metric.Float64Histogram {
metricLock.RLock()
h, ok := histograms[[2]string{component, name}]
metricLock.RUnlock()
if ok {
return h
}
metricLock.Lock()
defer metricLock.Unlock()
h, ok = histograms[[2]string{component, name}]
if ok {
return h
}
h, _ = otel.Meter(component).Float64Histogram(component+"."+name, options...)
histograms[[2]string{component, name}] = h
return h
}