Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: moving to otel from opencensus #3011

Merged
merged 30 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
1c2a920
migrating to otel
JaydipGabani Nov 8, 2023
5cd3fde
Merge branch 'master' into opent
JaydipGabani Nov 15, 2023
661e1ee
Merge branch 'master' into opent
JaydipGabani Nov 16, 2023
20195ec
Merge branch 'master' into opent
JaydipGabani Nov 18, 2023
d157c71
Merge branch 'master' into opent
JaydipGabani Nov 27, 2023
e7f8a93
removing unnecessary deltas and refactoring
JaydipGabani Nov 29, 2023
c99e535
refactoring tests
JaydipGabani Dec 4, 2023
97de9de
Merge branch 'master' into opent
JaydipGabani Dec 4, 2023
b48131f
Merge branch 'master' into opent
JaydipGabani Dec 5, 2023
60b13b5
adding read locks and refatoring tests
JaydipGabani Dec 6, 2023
dd39d95
adding test metrics client
JaydipGabani Dec 7, 2023
c71d8a0
Merge branch 'master' into opent
JaydipGabani Dec 14, 2023
8d1d6f2
Merge branch 'master' into opent
JaydipGabani Jan 2, 2024
12b8af9
adding missing mutext and addressing nits
JaydipGabani Jan 2, 2024
74a1efc
using duration flag for time interval
JaydipGabani Jan 2, 2024
776eadf
Merge branch 'master' into opent
JaydipGabani Jan 3, 2024
1991a56
adding detectors for stackdriver
JaydipGabani Jan 4, 2024
c7f31e1
Merge branch 'opent' of github.com:JaydipGabani/gatekeeper into opent
JaydipGabani Jan 4, 2024
18edba5
updating otel-endpoint flag name
JaydipGabani Jan 5, 2024
37f498d
adding resource to autodetect env in stackdriver
JaydipGabani Jan 11, 2024
a22727d
merging to master
JaydipGabani Jan 11, 2024
bc8a472
Merge branch 'JaydipGabani-opent' into opent
JaydipGabani Jan 11, 2024
22410c2
fixing nits and bug for watch metrics
JaydipGabani Jan 15, 2024
d029e54
merging master
JaydipGabani Jan 15, 2024
68aa946
Merge branch 'JaydipGabani-opent' into opent
JaydipGabani Jan 15, 2024
8919e95
Merge branch 'master' into opent
JaydipGabani Jan 16, 2024
20b8d79
fix for running multiple metrics backend
JaydipGabani Jan 16, 2024
e4a707f
fixing unit test
JaydipGabani Jan 16, 2024
37c37c0
refactoring instrument creation
JaydipGabani Jan 17, 2024
b5c3c1a
adding unit tests for common pkg
JaydipGabani Jan 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/metrics/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package metrics
import (
"context"

"github.com/open-policy-agent/gatekeeper/v3/pkg/metrics/exporters/common"
"github.com/open-policy-agent/gatekeeper/v3/pkg/metrics/registry"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
Expand Down Expand Up @@ -34,6 +35,7 @@ func (r *runner) Start(ctx context.Context) error {
defer log.Info("Stopping metrics runner workers")
errCh := make(chan error)
exporters := registry.Exporters()
common.SetRequiredReaders(len(exporters))
for i := range exporters {
startExporter := exporters[i]
go func() {
Expand Down
63 changes: 63 additions & 0 deletions pkg/metrics/exporters/common/common.go
JaydipGabani marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package common

import (
"sync"

"github.com/open-policy-agent/gatekeeper/v3/pkg/metrics/exporters/view"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
)

var (
opts []metric.Option
res *resource.Resource
mutex sync.Mutex
requiredReaders int
)

// SetRequiredReaders sets the number of required readers for the MeterProvider.
func SetRequiredReaders(num int) {
requiredReaders = num
}

// AddReader adds a reader to the options and updates the MeterProvider if the required conditions are met.
func AddReader(opt metric.Option) {
Comment on lines +24 to +25
Copy link
Contributor

@acpana acpana Jan 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

api nits: if we keep this structure, it may be useful to at least spell out the "required conditions". Alternatively splitting this into a AddReader and SetMetricsProvider might offer a cleaner/ more concise api

mutex.Lock()
defer mutex.Unlock()
if opt == nil {
requiredReaders--
} else {
opts = append(opts, opt)
}
setMeterProvider()
}

// SetResource sets the resource to be used by the MeterProvider.
func SetResource(r *resource.Resource) {
mutex.Lock()
defer mutex.Unlock()
res = r
}

// setMeterProvider sets the MeterProvider if the required conditions are met.
func setMeterProvider() {
// Check if we have the required number of readers and at least one reader.
if len(opts) != requiredReaders || len(opts) == 0 {
return
}

// Start with the existing options.
options := opts

// Add views to the options.
options = append(options, metric.WithView(view.Views()...))

// If a resource is available, add it to the options.
if res != nil {
options = append(options, metric.WithResource(res))
}

meterProvider := metric.NewMeterProvider(options...)
otel.SetMeterProvider(meterProvider)
}
27 changes: 10 additions & 17 deletions pkg/metrics/exporters/opentelemetry/opentelemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
"fmt"
"time"

"github.com/open-policy-agent/gatekeeper/v3/pkg/metrics/exporters/view"
"go.opentelemetry.io/otel"
"github.com/open-policy-agent/gatekeeper/v3/pkg/metrics/exporters/common"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
"go.opentelemetry.io/otel/sdk/metric"
)
Expand All @@ -25,27 +24,21 @@ var (

func Start(ctx context.Context) error {
if *otlpEndPoint == "" {
common.AddReader(nil)
return fmt.Errorf("otlp-endpoint must be specified")
}
var err error
exp, err := otlpmetrichttp.New(ctx, otlpmetrichttp.WithInsecure(), otlpmetrichttp.WithEndpoint(*otlpEndPoint))
if err != nil {
common.AddReader(nil)
return err
}
meterProvider := metric.NewMeterProvider(
metric.WithReader(metric.NewPeriodicReader(
exp,
metric.WithTimeout(defaultMetricsTimeout),
metric.WithInterval(*metricInterval),
)),
metric.WithView(view.Views()...),
)

otel.SetMeterProvider(meterProvider)
defer func() {
if err := meterProvider.Shutdown(ctx); err != nil {
panic(err)
}
}()
reader := metric.WithReader(metric.NewPeriodicReader(
exp,
metric.WithTimeout(defaultMetricsTimeout),
metric.WithInterval(*metricInterval),
))
common.AddReader(reader)

<-ctx.Done()
return nil
Expand Down
15 changes: 6 additions & 9 deletions pkg/metrics/exporters/prometheus/prometheus_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import (
"net/http"
"time"

"github.com/open-policy-agent/gatekeeper/v3/pkg/metrics/exporters/view"
"github.com/open-policy-agent/gatekeeper/v3/pkg/metrics/exporters/common"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/sdk/metric"
logf "sigs.k8s.io/controller-runtime/pkg/log"
Expand All @@ -27,21 +26,19 @@ var (
)

func Start(ctx context.Context) error {
var err error
e, err := prometheus.New(
prometheus.WithNamespace(namespace),
prometheus.WithoutScopeInfo(),
)
if err != nil {
common.AddReader(nil)
return err
}
meterProvider := metric.NewMeterProvider(
metric.WithReader(e),
metric.WithView(view.Views()...),
)
server := newPromSrv(*prometheusPort)
otel.SetMeterProvider(meterProvider)
otel.SetLogger(logf.Log.WithName("metrics"))
reader := metric.WithReader(e)
common.AddReader(reader)

server := newPromSrv(*prometheusPort)
errCh := make(chan error)
srv := func() {
err := server.ListenAndServe()
Expand Down
17 changes: 7 additions & 10 deletions pkg/metrics/exporters/stackdriver/stackdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import (

traceapi "cloud.google.com/go/trace/apiv2"
stackdriver "github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric"
"github.com/open-policy-agent/gatekeeper/v3/pkg/metrics/exporters/view"
"github.com/open-policy-agent/gatekeeper/v3/pkg/metrics/exporters/common"
"go.opentelemetry.io/contrib/detectors/aws/ec2"
"go.opentelemetry.io/contrib/detectors/gcp"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
"go.opentelemetry.io/otel/sdk/resource"
Expand All @@ -35,9 +34,11 @@ func Start(ctx context.Context) error {
// Verify that default stackdriver credentials are available
if _, err := google.FindDefaultCredentials(ctx, traceapi.DefaultAuthScopes()...); err != nil {
if *ignoreMissingCreds {
common.AddReader(nil)
log.Error(err, "Missing credentials, cannot start stackdriver exporter")
return nil
}
common.AddReader(nil)
return err
}

Expand All @@ -49,6 +50,7 @@ func Start(ctx context.Context) error {
log.Error(err, "Error initializing stackdriver exporter, not exporting stackdriver metrics")
return nil
}
common.AddReader(nil)
return err
}
res, err := resource.New(ctx,
Expand All @@ -57,17 +59,12 @@ func Start(ctx context.Context) error {
resource.WithFromEnv(),
)
if err != nil {
common.AddReader(nil)
return err
}
reader := metric.NewPeriodicReader(e, metric.WithInterval(*metricInterval))
meterProvider := metric.NewMeterProvider(
metric.WithReader(reader),
metric.WithView(view.Views()...),
metric.WithResource(res),
)

otel.SetMeterProvider(meterProvider)
otel.SetLogger(logf.Log.WithName("metrics"))
common.SetResource(res)
common.AddReader(metric.WithReader(reader))

<-ctx.Done()
return nil
Expand Down
Loading