diff --git a/pkg/controller/constraint/stats_reporter.go b/pkg/controller/constraint/stats_reporter.go index 315e678b0cb..e1d746c21bc 100644 --- a/pkg/controller/constraint/stats_reporter.go +++ b/pkg/controller/constraint/stats_reporter.go @@ -31,7 +31,7 @@ func init() { } } -func (r *reporter) observeConstraints(ctx context.Context, observer metric.Observer) error { +func (r *reporter) observeConstraints(_ context.Context, observer metric.Observer) error { r.mux.RLock() defer r.mux.RUnlock() for t, v := range r.constraintsReport { @@ -40,7 +40,7 @@ func (r *reporter) observeConstraints(ctx context.Context, observer metric.Obser return nil } -func (r *reporter) reportConstraints(ctx context.Context, t tags, v int64) error { +func (r *reporter) reportConstraints(_ context.Context, t tags, v int64) error { r.mux.Lock() defer r.mux.Unlock() if r.constraintsReport == nil { diff --git a/pkg/controller/expansion/stats_reporter.go b/pkg/controller/expansion/stats_reporter.go index 46e9d6d2960..877e73b091c 100644 --- a/pkg/controller/expansion/stats_reporter.go +++ b/pkg/controller/expansion/stats_reporter.go @@ -64,7 +64,7 @@ func (r *etRegistry) remove(key types.NamespacedName) { r.dirty = true } -func (r *etRegistry) report(ctx context.Context) { +func (r *etRegistry) report(_ context.Context) { if !r.dirty { return } diff --git a/pkg/metrics/exporters/opentelemetry/opentelemetry.go b/pkg/metrics/exporters/opentelemetry/opentelemetry.go index c7118a69a9c..29ab161ba7f 100644 --- a/pkg/metrics/exporters/opentelemetry/opentelemetry.go +++ b/pkg/metrics/exporters/opentelemetry/opentelemetry.go @@ -14,7 +14,6 @@ import ( const ( Name = "opentelemetry" - metricPrefix = "gatekeeper" defaultMetricsCollectInterval = 10 * time.Second defaultMetricsTimeout = 30 * time.Second ) diff --git a/pkg/metrics/exporters/prometheus/prometheus_exporter.go b/pkg/metrics/exporters/prometheus/prometheus_exporter.go index 85a5714c979..38863989efc 100644 --- a/pkg/metrics/exporters/prometheus/prometheus_exporter.go +++ b/pkg/metrics/exporters/prometheus/prometheus_exporter.go @@ -68,7 +68,7 @@ func newPromSrv(port int) *http.Server { server := &http.Server{ Addr: fmt.Sprintf(":%v", port), Handler: sm, - ReadHeaderTimeout: 30 * time.Second, + ReadHeaderTimeout: readHeaderTimeout, } return server } diff --git a/pkg/syncutil/stats_reporter.go b/pkg/syncutil/stats_reporter.go index 07c872e2ef6..c43402b6bac 100644 --- a/pkg/syncutil/stats_reporter.go +++ b/pkg/syncutil/stats_reporter.go @@ -2,7 +2,6 @@ package syncutil import ( "context" - "errors" "strings" "sync" "time" @@ -27,17 +26,17 @@ const ( ) var ( - syncM metric.Int64ObservableGauge syncDurationM metric.Float64Histogram - lastRunSyncM metric.Float64ObservableGauge meter metric.Meter + r *Reporter ) func init() { var err error meter = otel.GetMeterProvider().Meter("gatekeeper") + r = &Reporter{now: now} - syncM, err = meter.Int64ObservableGauge(syncMetricName, metric.WithDescription("Total number of resources of each kind being cached")) + _, err = meter.Int64ObservableGauge(syncMetricName, metric.WithDescription("Total number of resources of each kind being cached"), metric.WithInt64Callback(r.observeSync)) if err != nil { panic(err) } @@ -45,7 +44,7 @@ func init() { if err != nil { panic(err) } - lastRunSyncM, err = meter.Float64ObservableGauge(lastRunTimeMetricName, metric.WithDescription("Timestamp of last sync operation"), metric.WithUnit("s")) + _, err = meter.Float64ObservableGauge(lastRunTimeMetricName, metric.WithDescription("Timestamp of last sync operation"), metric.WithUnit("s"), metric.WithFloat64Callback(r.observeLastSync)) if err != nil { panic(err) } @@ -180,14 +179,7 @@ type Reporter struct { // NewStatsReporter creates a reporter for sync metrics. func NewStatsReporter() (*Reporter, error) { - r := &Reporter{now: now} - return r, r.registerCallback() -} - -func (r *Reporter) registerCallback() error { - _, err1 := meter.RegisterCallback(r.observeSync, syncM) - _, err2 := meter.RegisterCallback(r.observeLastSync, lastRunSyncM) - return errors.Join(err1, err2) + return r, nil } func (r *Reporter) ReportSyncDuration(d time.Duration) error { @@ -213,18 +205,18 @@ func (r *Reporter) ReportSync(t Tags, v int64) error { return nil } -func (r *Reporter) observeLastSync(_ context.Context, observer metric.Observer) error { +func (r *Reporter) observeLastSync(_ context.Context, observer metric.Float64Observer) error { r.mu.RLock() defer r.mu.RUnlock() - observer.ObserveFloat64(lastRunSyncM, r.lastRun) + observer.Observe(r.lastRun) return nil } -func (r *Reporter) observeSync(_ context.Context, observer metric.Observer) error { +func (r *Reporter) observeSync(_ context.Context, observer metric.Int64Observer) error { r.mu.RLock() defer r.mu.RUnlock() for t, v := range r.syncReport { - observer.ObserveInt64(syncM, v, metric.WithAttributes(attribute.String(kindKey, t.Kind), attribute.String(statusKey, string(t.Status)))) + observer.Observe(v, metric.WithAttributes(attribute.String(kindKey, t.Kind), attribute.String(statusKey, string(t.Status)))) } return nil } diff --git a/pkg/syncutil/stats_reporter_test.go b/pkg/syncutil/stats_reporter_test.go index 243aa98b95a..5353ee7a33f 100644 --- a/pkg/syncutil/stats_reporter_test.go +++ b/pkg/syncutil/stats_reporter_test.go @@ -9,6 +9,7 @@ import ( testmetric "github.com/open-policy-agent/gatekeeper/v3/test/metrics" "github.com/stretchr/testify/assert" "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/metric" sdkmetric "go.opentelemetry.io/otel/sdk/metric" "go.opentelemetry.io/otel/sdk/metric/metricdata" "go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" @@ -20,14 +21,13 @@ func initializeTestInstruments(t *testing.T) (rdr *sdkmetric.PeriodicReader, r * mp := sdkmetric.NewMeterProvider(sdkmetric.WithReader(rdr)) meter = mp.Meter("test") - syncM, err = meter.Int64ObservableGauge(syncMetricName) + r, err = NewStatsReporter() assert.NoError(t, err) - syncDurationM, err = meter.Float64Histogram(syncDurationMetricName) + _, err = meter.Int64ObservableGauge(syncMetricName, metric.WithInt64Callback(r.observeSync)) assert.NoError(t, err) - lastRunSyncM, err = meter.Float64ObservableGauge(lastRunTimeMetricName) + syncDurationM, err = meter.Float64Histogram(syncDurationMetricName) assert.NoError(t, err) - - r, err = NewStatsReporter() + _, err = meter.Float64ObservableGauge(lastRunTimeMetricName, metric.WithFloat64Callback(r.observeLastSync)) assert.NoError(t, err) return rdr, r diff --git a/pkg/watch/stats_reporter.go b/pkg/watch/stats_reporter.go index 6ba8dcec8aa..015bb6e3b93 100644 --- a/pkg/watch/stats_reporter.go +++ b/pkg/watch/stats_reporter.go @@ -2,7 +2,6 @@ package watch import ( "context" - "errors" "sync" "go.opentelemetry.io/otel" @@ -15,25 +14,28 @@ const ( ) var ( - meter metric.Meter - gvkCountM metric.Int64ObservableGauge - gvkIntentCountM metric.Int64ObservableGauge + meter metric.Meter + r *reporter ) func init() { var err error meterProvider := otel.GetMeterProvider() meter = meterProvider.Meter("gatekeeper") - gvkCountM, err = meter.Int64ObservableGauge( + r = &reporter{} + _, err = meter.Int64ObservableGauge( gvkCountMetricName, metric.WithDescription("The total number of Group/Version/Kinds currently watched by the watch manager"), + metric.WithInt64Callback(r.observeGvkCount), ) if err != nil { panic(err) } - gvkIntentCountM, err = meter.Int64ObservableGauge( + _, err = meter.Int64ObservableGauge( gvkIntentCountMetricName, - metric.WithDescription("The total number of Group/Version/Kinds that the watch manager has instructions to watch. This could differ from the actual count due to resources being pending, non-existent, or a failure of the watch manager to restart")) + metric.WithDescription("The total number of Group/Version/Kinds that the watch manager has instructions to watch. This could differ from the actual count due to resources being pending, non-existent, or a failure of the watch manager to restart"), + metric.WithInt64Callback(r.observeGvkIntentCount), + ) if err != nil { panic(err) } @@ -53,17 +55,9 @@ func (r *reporter) reportGvkIntentCount(count int64) error { return nil } -func (r *reporter) registerCallback() error { - _, err1 := meter.RegisterCallback(r.observeGvkIntentCount, gvkIntentCountM) - _, err2 := meter.RegisterCallback(r.observeGvkCount, gvkCountM) - - return errors.Join(err1, err2) -} - // newStatsReporter creates a reporter for watch metrics. func newStatsReporter() (*reporter, error) { - r := &reporter{} - return r, r.registerCallback() + return r, nil } type reporter struct { @@ -72,14 +66,17 @@ type reporter struct { intentCount int64 } -func (r *reporter) observeGvkCount(ctx context.Context, observer metric.Observer) error { - log.Info("reporting gvk count") - observer.ObserveInt64(gvkCountM, r.gvkCount) +func (r *reporter) observeGvkCount(_ context.Context, observer metric.Int64Observer) error { + r.mu.RLock() + defer r.mu.RUnlock() + observer.Observe(r.gvkCount) return nil } // count returns total gvk count across all registrars. -func (r *reporter) observeGvkIntentCount(_ context.Context, observer metric.Observer) error { - observer.ObserveInt64(gvkIntentCountM, r.intentCount) +func (r *reporter) observeGvkIntentCount(_ context.Context, observer metric.Int64Observer) error { + r.mu.RLock() + defer r.mu.RUnlock() + observer.Observe(r.intentCount) return nil } diff --git a/pkg/watch/stats_reporter_test.go b/pkg/watch/stats_reporter_test.go index 0839c92d2f3..7dd04a383d2 100644 --- a/pkg/watch/stats_reporter_test.go +++ b/pkg/watch/stats_reporter_test.go @@ -6,6 +6,7 @@ import ( testmetric "github.com/open-policy-agent/gatekeeper/v3/test/metrics" "github.com/stretchr/testify/assert" + "go.opentelemetry.io/otel/metric" sdkmetric "go.opentelemetry.io/otel/sdk/metric" "go.opentelemetry.io/otel/sdk/metric/metricdata" "go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" @@ -17,12 +18,12 @@ func initializeTestInstruments(t *testing.T) (rdr *sdkmetric.PeriodicReader, r * mp := sdkmetric.NewMeterProvider(sdkmetric.WithReader(rdr)) meter = mp.Meter("test") - // Ensure the pipeline has a callback setup - gvkIntentCountM, err = meter.Int64ObservableGauge(gvkIntentCountMetricName) + r, err = newStatsReporter() assert.NoError(t, err) - gvkCountM, err = meter.Int64ObservableGauge(gvkCountMetricName) + // Ensure the pipeline has a callback setup + _, err = meter.Int64ObservableGauge(gvkIntentCountMetricName, metric.WithInt64Callback(r.observeGvkIntentCount)) assert.NoError(t, err) - r, err = newStatsReporter() + _, err = meter.Int64ObservableGauge(gvkCountMetricName, metric.WithInt64Callback(r.observeGvkCount)) assert.NoError(t, err) return rdr, r }