Skip to content

Commit

Permalink
refactoring views
Browse files Browse the repository at this point in the history
Signed-off-by: Jaydip Gabani <[email protected]>
  • Loading branch information
JaydipGabani committed Oct 23, 2023
1 parent 9b85458 commit 5b37287
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 83 deletions.
11 changes: 11 additions & 0 deletions pkg/audit/stats_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"github.com/open-policy-agent/gatekeeper/v3/pkg/metrics/exporters/view"
)

const (
Expand Down Expand Up @@ -64,6 +66,15 @@ func init() {
if err != nil {
panic(err)
}

view.Register(sdkmetric.NewView(
sdkmetric.Instrument{Name: auditDurationMetricName},
sdkmetric.Stream{
Aggregation: sdkmetric.AggregationExplicitBucketHistogram{
Boundaries: []float64{1 * 60, 3 * 60, 5 * 60, 10 * 60, 15 * 60, 20 * 60, 40 * 60, 80 * 60, 160 * 60, 320 * 60},
},
},
))
}

func (r *reporter) registerCallback() error {
Expand Down
11 changes: 11 additions & 0 deletions pkg/controller/constrainttemplate/stats_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"k8s.io/apimachinery/pkg/types"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"github.com/open-policy-agent/gatekeeper/v3/pkg/metrics/exporters/view"
)

const (
Expand Down Expand Up @@ -54,6 +56,15 @@ func init() {
if err != nil {
panic(err)
}

view.Register(sdkmetric.NewView(
sdkmetric.Instrument{Name: ingestDuration},
sdkmetric.Stream{
Aggregation: sdkmetric.AggregationExplicitBucketHistogram{
Boundaries: []float64{0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.2, 0.3, 0.4, 0.5, 1, 2, 3, 4, 5},
},
},
))
}

func (r *reporter) reportIngestDuration(ctx context.Context, status metrics.Status, d time.Duration) error {
Expand Down
11 changes: 11 additions & 0 deletions pkg/controller/mutators/stats_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"github.com/open-policy-agent/gatekeeper/v3/pkg/metrics/exporters/view"
)

const (
Expand Down Expand Up @@ -72,6 +74,15 @@ func init() {
if err != nil {
panic(err)
}

view.Register(sdkmetric.NewView(
sdkmetric.Instrument{Name: mutatorIngestionDurationMetricName},
sdkmetric.Stream{
Aggregation: sdkmetric.AggregationExplicitBucketHistogram{
Boundaries: []float64{0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.02, 0.03, 0.04, 0.05},
},
},
))
}

// StatsReporter reports mutator-related controller metrics.
Expand Down
8 changes: 5 additions & 3 deletions pkg/metrics/exporters/opentelemetry/opentelemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ import (
const (
Name = "opentelemetry"
metricPrefix = "gatekeeper"
defaultMetricsCollectInterval = 10 * time.Second
defaultMetricsCollectInterval = 10
defaultMetricsTimeout = 30 * time.Second
)

var (
log = logf.Log.WithName("opentelemetry-exporter")
otlpEndPoint = flag.String("otlp-end-point", "", "Opentelemetry exporter endpoint")
metricInterval = flag.Uint("otlp-metric-interval", defaultMetricsCollectInterval, "interval to read metrics for opentelemetry exporter. defaulted to 10 secs if unspecified")
)

func Start(ctx context.Context) error {
Expand All @@ -36,8 +38,8 @@ func Start(ctx context.Context) error {
meterProvider := metric.NewMeterProvider(
metric.WithReader(metric.NewPeriodicReader(
exp,
metric.WithTimeout(defaultMetricsCollectInterval),
metric.WithInterval(defaultMetricsCollectInterval),
metric.WithTimeout(defaultMetricsTimeout),
metric.WithInterval(time.Duration(*metricInterval)*time.Second),
)),
metric.WithView(view.Views()...),
)
Expand Down
13 changes: 3 additions & 10 deletions pkg/metrics/exporters/stackdriver/stackdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import (
const (
Name = "stackdriver"
metricPrefix = "custom.googleapis.com/opencensus/gatekeeper/"
defaultMetricsCollectInterval = 10
)

var (
ignoreMissingCreds = flag.Bool("stackdriver-only-when-available", false, "Only attempt to start the stackdriver exporter if credentials are available")
metricInterval = flag.Uint("stackdriver-metric-interval", defaultMetricsCollectInterval, "interval to read metrics for stackdriver exporter. defaulted to 10 secs if unspecified")
log = logf.Log.WithName("stackdriver-exporter")
)

Expand All @@ -42,7 +44,7 @@ func Start(ctx context.Context) error {
}
return err
}
reader := metric.NewPeriodicReader(e, metric.WithInterval(30*time.Second))
reader := metric.NewPeriodicReader(e, metric.WithInterval(time.Duration(*metricInterval)*time.Second))
meterProvider := metric.NewMeterProvider(
metric.WithReader(reader),
metric.WithView(view.Views()...),
Expand All @@ -51,15 +53,6 @@ func Start(ctx context.Context) error {
otel.SetMeterProvider(meterProvider)
otel.SetLogger(logf.Log.WithName("metrics"))

// if err := exporter.StartMetricsExporter(); err != nil {
// if *ignoreMissingCreds {
// log.Error(err, "Error starting stackdriver exporter, not exporting stackdriver metrics")
// return nil
// }
// return err
// }
// defer exporter.StopMetricsExporter()

<-ctx.Done()
return nil
}
81 changes: 11 additions & 70 deletions pkg/metrics/exporters/view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,75 +4,16 @@ import (
"go.opentelemetry.io/otel/sdk/metric"
)

var views []metric.View

func init() {
views = []metric.View{}
}

func Register(v ...metric.View) {
views = append(views, v...)
}

func Views() []metric.View {
return []metric.View{
metric.NewView(
metric.Instrument{Name: "audit_duration_seconds"},
metric.Stream{
Aggregation: metric.AggregationExplicitBucketHistogram{
Boundaries: []float64{1 * 60, 3 * 60, 5 * 60, 10 * 60, 15 * 60, 20 * 60, 40 * 60, 80 * 60, 160 * 60, 320 * 60},
},
},
),
metric.NewView(
metric.Instrument{Name: "mutator_ingestion_duration_seconds"},
metric.Stream{
Aggregation: metric.AggregationExplicitBucketHistogram{
Boundaries: []float64{0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.02, 0.03, 0.04, 0.05},
},
},
),
metric.NewView(
metric.Instrument{Name: "mutation_system_iterations"},
metric.Stream{
Aggregation: metric.AggregationExplicitBucketHistogram{
Boundaries: []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 50, 100, 200, 500},
},
},
),
metric.NewView(
metric.Instrument{Name: "validation_request_duration_seconds"},
metric.Stream{
Aggregation: metric.AggregationExplicitBucketHistogram{
Boundaries: []float64{0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, 2.5, 3},
},
},
),
metric.NewView(
metric.Instrument{Name: "mutation_request_duration_seconds"},
metric.Stream{
Aggregation: metric.AggregationExplicitBucketHistogram{
Boundaries: []float64{0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, 2.5, 3},
},
},
),
metric.NewView(
metric.Instrument{Name: "constraint_template_ingestion_duration_seconds"},
metric.Stream{
Aggregation: metric.AggregationExplicitBucketHistogram{
Boundaries: []float64{0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.2, 0.3, 0.4, 0.5, 1, 2, 3, 4, 5},
},
},
),
metric.NewView(
metric.Instrument{Name: "sync_duration_seconds"},
metric.Stream{
Aggregation: metric.AggregationExplicitBucketHistogram{
Boundaries: []float64{0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008, 0.0009, 0.001, 0.002, 0.003, 0.004, 0.005, 0.01, 0.02, 0.03, 0.04, 0.05},
},
},
),
metric.NewView(
metric.Instrument{Name: "sync"},
metric.Stream{
Aggregation: metric.AggregationLastValue{},
},
),
metric.NewView(
metric.Instrument{Name: "sync_last_run_time"},
metric.Stream{
Aggregation: metric.AggregationLastValue{},
},
),
}
return views
}
11 changes: 11 additions & 0 deletions pkg/mutation/stats_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"github.com/open-policy-agent/gatekeeper/v3/pkg/metrics/exporters/view"
)

const (
Expand Down Expand Up @@ -37,6 +39,15 @@ func init() {
if err != nil {
panic(err)
}

view.Register(sdkmetric.NewView(
sdkmetric.Instrument{Name: mutationSystemIterationsMetricName},
sdkmetric.Stream{
Aggregation: sdkmetric.AggregationExplicitBucketHistogram{
Boundaries: []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 50, 100, 200, 500},
},
},
))
}

// StatsReporter reports mutator-related metrics.
Expand Down
12 changes: 12 additions & 0 deletions pkg/syncutil/stats_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"github.com/open-policy-agent/gatekeeper/v3/pkg/metrics/exporters/view"
)

const (
Expand Down Expand Up @@ -44,6 +46,16 @@ func init() {
if err != nil {
panic(err)
}

view.Register(
sdkmetric.NewView(
sdkmetric.Instrument{Name: syncDurationMetricName},
sdkmetric.Stream{
Aggregation: sdkmetric.AggregationExplicitBucketHistogram{
Boundaries: []float64{0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008, 0.0009, 0.001, 0.002, 0.003, 0.004, 0.005, 0.01, 0.02, 0.03, 0.04, 0.05},
},
},
))
}

type MetricsCache struct {
Expand Down
21 changes: 21 additions & 0 deletions pkg/webhook/stats_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"github.com/open-policy-agent/gatekeeper/v3/pkg/metrics/exporters/view"
)

const (
Expand Down Expand Up @@ -60,6 +62,25 @@ func init() {
if err != nil {
panic(err)
}

view.Register(
sdkmetric.NewView(
sdkmetric.Instrument{Name: validationRequestDurationMetricName},
sdkmetric.Stream{
Aggregation: sdkmetric.AggregationExplicitBucketHistogram{
Boundaries: []float64{0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, 2.5, 3},
},
},
),
sdkmetric.NewView(
sdkmetric.Instrument{Name: mutationRequestDurationMetricName},
sdkmetric.Stream{
Aggregation: sdkmetric.AggregationExplicitBucketHistogram{
Boundaries: []float64{0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, 2.5, 3},
},
},
),
)
}

// StatsReporter reports webhook metrics.
Expand Down

0 comments on commit 5b37287

Please sign in to comment.