mirror of
https://github.com/pomerium/pomerium.git
synced 2025-05-28 08:27:26 +02:00
zero/telemetry: internal envoy stats scraper and metrics producer (#5136)
This commit is contained in:
parent
c3534df885
commit
c1dec06afa
19 changed files with 667 additions and 301 deletions
54
internal/zero/telemetry/metrics_producer.go
Normal file
54
internal/zero/telemetry/metrics_producer.go
Normal file
|
@ -0,0 +1,54 @@
|
|||
package telemetry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync/atomic"
|
||||
|
||||
"go.opentelemetry.io/otel/sdk/metric"
|
||||
"go.opentelemetry.io/otel/sdk/metric/metricdata"
|
||||
|
||||
"github.com/pomerium/pomerium/internal/log"
|
||||
)
|
||||
|
||||
// metricsProducer is a wrapper around a metric producer that can be enabled or disabled
|
||||
type metricsProducer[P metric.Producer] struct {
|
||||
enabled atomic.Bool
|
||||
name string
|
||||
producer P
|
||||
}
|
||||
|
||||
func newMetricsProducer[P metric.Producer](name string, p P) *metricsProducer[P] {
|
||||
return &metricsProducer[P]{
|
||||
name: name,
|
||||
producer: p,
|
||||
}
|
||||
}
|
||||
|
||||
// Produce wraps the underlying producer's Produce method and logs any errors,
|
||||
// to prevent the error from blocking the export of other metrics.
|
||||
// also checks if the producer is enabled before producing metrics
|
||||
func (p *metricsProducer[P]) Produce(ctx context.Context) ([]metricdata.ScopeMetrics, error) {
|
||||
if enabled := p.enabled.Load(); !enabled {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
data, err := p.producer.Produce(ctx)
|
||||
if err != nil {
|
||||
// we do not return the error here, as we do not want to block the export of other metrics
|
||||
log.Error(ctx).Err(err).Str("producer", p.name).Msg("failed to produce metrics")
|
||||
return nil, nil
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (p *metricsProducer[P]) SetEnabled(v bool) {
|
||||
p.enabled.Store(v)
|
||||
}
|
||||
|
||||
func (p *metricsProducer[P]) Name() string {
|
||||
return p.name
|
||||
}
|
||||
|
||||
func (p *metricsProducer[P]) Producer() P {
|
||||
return p.producer
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue