Skip to content

Commit

Permalink
fixing nits and bug for watch metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Jaydip Gabani <[email protected]>
  • Loading branch information
JaydipGabani committed Jan 15, 2024
1 parent bc8a472 commit 22410c2
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 52 deletions.
4 changes: 2 additions & 2 deletions pkg/controller/constraint/stats_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/expansion/stats_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 0 additions & 1 deletion pkg/metrics/exporters/opentelemetry/opentelemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

const (
Name = "opentelemetry"
metricPrefix = "gatekeeper"
defaultMetricsCollectInterval = 10 * time.Second
defaultMetricsTimeout = 30 * time.Second
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/metrics/exporters/prometheus/prometheus_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
26 changes: 9 additions & 17 deletions pkg/syncutil/stats_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package syncutil

import (
"context"
"errors"
"strings"
"sync"
"time"
Expand All @@ -27,25 +26,25 @@ 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)
}
syncDurationM, err = meter.Float64Histogram(syncDurationMetricName, metric.WithDescription("Latency of sync operation in seconds"), metric.WithUnit("s"))
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)
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/syncutil/stats_reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
39 changes: 18 additions & 21 deletions pkg/watch/stats_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package watch

import (
"context"
"errors"
"sync"

"go.opentelemetry.io/otel"
Expand All @@ -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)
}
Expand All @@ -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 {
Expand All @@ -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
}
9 changes: 5 additions & 4 deletions pkg/watch/stats_reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand Down

0 comments on commit 22410c2

Please sign in to comment.