Skip to content

Commit

Permalink
chore: update otel to v0.38.1 and update metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Anish Ramasekar <anish.ramasekar@gmail.com>
  • Loading branch information
aramase committed May 8, 2023
1 parent 45588e2 commit 4023ebd
Show file tree
Hide file tree
Showing 12 changed files with 258 additions and 366 deletions.
41 changes: 21 additions & 20 deletions go.mod
Expand Up @@ -4,17 +4,17 @@ go 1.19

require (
github.com/container-storage-interface/spec v1.6.0
github.com/google/go-cmp v0.5.8
github.com/google/go-cmp v0.5.9
github.com/kubernetes-csi/csi-lib-utils v0.10.0
github.com/kubernetes-csi/csi-test/v4 v4.3.0
github.com/onsi/gomega v1.19.0
github.com/prometheus/client_golang v1.12.2
github.com/stretchr/testify v1.8.1
go.opentelemetry.io/otel v0.20.0
go.opentelemetry.io/otel/exporters/metric/prometheus v0.20.0
go.opentelemetry.io/otel/metric v0.20.0
github.com/prometheus/client_golang v1.15.0
github.com/stretchr/testify v1.8.2
go.opentelemetry.io/otel v1.15.1
go.opentelemetry.io/otel/exporters/prometheus v0.38.1
go.opentelemetry.io/otel/metric v0.38.1
google.golang.org/grpc v1.47.0
google.golang.org/protobuf v1.28.0
google.golang.org/protobuf v1.30.0
k8s.io/api v0.25.0
k8s.io/apimachinery v0.25.0
k8s.io/client-go v0.25.0
Expand All @@ -24,24 +24,26 @@ require (
sigs.k8s.io/controller-runtime v0.13.0
)

require github.com/go-logr/stdr v1.2.2 // indirect

require (
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.8.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/zapr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.5 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.1.2 // indirect
Expand All @@ -50,7 +52,7 @@ require (
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/moby/sys/mountinfo v0.6.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand All @@ -59,22 +61,21 @@ require (
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/spf13/cobra v1.6.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.opentelemetry.io/otel/sdk v0.20.0 // indirect
go.opentelemetry.io/otel/sdk/export/metric v0.20.0 // indirect
go.opentelemetry.io/otel/sdk/metric v0.20.0 // indirect
go.opentelemetry.io/otel/trace v0.20.0 // indirect
go.opentelemetry.io/otel/sdk v1.15.1 // indirect
go.opentelemetry.io/otel/sdk/metric v0.38.1
go.opentelemetry.io/otel/trace v1.15.1 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b
golang.org/x/net v0.7.0 // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/oauth2 v0.5.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/term v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/time v0.0.0-20220609170525-579cf78fd858 // indirect
Expand Down
282 changes: 43 additions & 239 deletions go.sum

Large diffs are not rendered by default.

33 changes: 25 additions & 8 deletions pkg/metrics/prometheus_exporter.go
Expand Up @@ -17,17 +17,34 @@ limitations under the License.
package metrics

import (
"github.com/prometheus/client_golang/prometheus"
otProm "go.opentelemetry.io/otel/exporters/metric/prometheus"
crprometheus "github.com/prometheus/client_golang/prometheus"
"go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/aggregation"
"sigs.k8s.io/controller-runtime/pkg/metrics"
)

func initPrometheusExporter() error {
_, err := otProm.InstallNewPipeline(otProm.Config{
Registry: metrics.Registry.(*prometheus.Registry), // using the controller-runtime prometheus metrics registry
DefaultHistogramBoundaries: []float64{
0.1, 0.2, 0.3, 0.4, 0.5, 1, 1.5, 2, 2.5, 3.0, 5.0, 10.0, 15.0, 30.0,
}})
exporter, err := prometheus.New(
prometheus.WithRegisterer(metrics.Registry.(*crprometheus.Registry)), // using the controller-runtime prometheus metrics registry
)
if err != nil {
return err
}

return err
meterProvider := metric.NewMeterProvider(
metric.WithReader(exporter),
metric.WithView(metric.NewView(
metric.Instrument{Name: "secretsstore_*"},
metric.Stream{
Aggregation: aggregation.ExplicitBucketHistogram{
Boundaries: []float64{0.1, 0.2, 0.3, 0.4, 0.5, 1, 1.5, 2, 2.5, 3.0, 5.0, 10.0, 15.0, 30.0},
}},
)),
)

global.SetMeterProvider(meterProvider)

return nil
}
12 changes: 8 additions & 4 deletions pkg/rotation/reconciler.go
Expand Up @@ -106,11 +106,15 @@ func NewReconciler(client client.Reader,
if err != nil {
return nil, err
}
sr, err := newStatsReporter()
if err != nil {
return nil, err
}

return &Reconciler{
rotationPollInterval: rotationPollInterval,
providerClients: providerClients,
reporter: newStatsReporter(),
reporter: sr,
queue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()),
eventRecorder: recorder,
kubeClient: kubeClient,
Expand Down Expand Up @@ -245,11 +249,11 @@ func (r *Reconciler) reconcile(ctx context.Context, spcps *secretsstorev1.Secret

defer func() {
if err != nil {
r.reporter.reportRotationErrorCtMetric(providerName, errorReason, requiresUpdate)
r.reporter.reportRotationErrorCtMetric(ctx, providerName, errorReason, requiresUpdate)
return
}
r.reporter.reportRotationCtMetric(providerName, requiresUpdate)
r.reporter.reportRotationDuration(time.Since(begin).Seconds())
r.reporter.reportRotationCtMetric(ctx, providerName, requiresUpdate)
r.reporter.reportRotationDuration(ctx, time.Since(begin).Seconds())
}()

// get pod from manager's cache
Expand Down
6 changes: 5 additions & 1 deletion pkg/rotation/reconciler_test.go
Expand Up @@ -67,12 +67,16 @@ func newTestReconciler(client client.Reader, kubeClient kubernetes.Interface, cr
if err != nil {
return nil, err
}
sr, err := newStatsReporter()
if err != nil {
return nil, err
}

return &Reconciler{
rotationPollInterval: rotationPollInterval,
providerClients: secretsstore.NewPluginClientBuilder([]string{socketPath}),
queue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()),
reporter: newStatsReporter(),
reporter: sr,
eventRecorder: fakeRecorder,
kubeClient: kubeClient,
crdClient: crdClient,
Expand Down
75 changes: 48 additions & 27 deletions pkg/rotation/stats_reporter.go
Expand Up @@ -21,49 +21,70 @@ import (
"runtime"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
api "go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel/metric/instrument"
)

var (
providerKey = "provider"
errorKey = "error_type"
osTypeKey = "os_type"
rotatedKey = "rotated"
rotationReconcileTotal metric.Int64Counter
rotationReconcileErrorTotal metric.Int64Counter
rotationReconcileDuration metric.Float64ValueRecorder
runtimeOS = runtime.GOOS
providerKey = "provider"
errorKey = "error_type"
osTypeKey = "os_type"
rotatedKey = "rotated"
runtimeOS = runtime.GOOS
)

type reporter struct {
meter metric.Meter
rotationReconcileTotal instrument.Int64Counter
rotationReconcileErrorTotal instrument.Int64Counter
rotationReconcileDuration instrument.Float64Histogram
}

type StatsReporter interface {
reportRotationCtMetric(provider string, wasRotated bool)
reportRotationErrorCtMetric(provider, errType string, wasRotated bool)
reportRotationDuration(duration float64)
reportRotationCtMetric(ctx context.Context, provider string, wasRotated bool)
reportRotationErrorCtMetric(ctx context.Context, provider, errType string, wasRotated bool)
reportRotationDuration(ctx context.Context, duration float64)
}

func newStatsReporter() StatsReporter {
meter := global.Meter("secretsstore")
rotationReconcileTotal = metric.Must(meter).NewInt64Counter("total_rotation_reconcile", metric.WithDescription("Total number of rotation reconciles"))
rotationReconcileErrorTotal = metric.Must(meter).NewInt64Counter("total_rotation_reconcile_error", metric.WithDescription("Total number of rotation reconciles with error"))
rotationReconcileDuration = metric.Must(meter).NewFloat64ValueRecorder("rotation_reconcile_duration_sec", metric.WithDescription("Distribution of how long it took to rotate secrets-store content for pods"))
return &reporter{meter: meter}
func newStatsReporter() (StatsReporter, error) {
var err error
r := &reporter{}
meter := global.Meter("rotation")

if r.rotationReconcileTotal, err = meter.Int64Counter("total_rotation_reconcile", instrument.WithDescription("Total number of rotation reconciles")); err != nil {
return nil, err
}
if r.rotationReconcileErrorTotal, err = meter.Int64Counter("total_rotation_reconcile_error", instrument.WithDescription("Total number of rotation reconciles with error")); err != nil {
return nil, err
}
if r.rotationReconcileDuration, err = meter.Float64Histogram("rotation_reconcile_duration_sec", instrument.WithDescription("Distribution of how long it took to rotate secrets-store content for pods")); err != nil {
return nil, err
}
return r, nil
}

func (r *reporter) reportRotationCtMetric(provider string, wasRotated bool) {
labels := []attribute.KeyValue{attribute.String(providerKey, provider), attribute.String(osTypeKey, runtimeOS), attribute.Bool(rotatedKey, wasRotated)}
rotationReconcileTotal.Add(context.Background(), 1, labels...)
func (r *reporter) reportRotationCtMetric(ctx context.Context, provider string, wasRotated bool) {
opt := api.WithAttributes(
attribute.Key(providerKey).String(provider),
attribute.Key(osTypeKey).String(runtimeOS),
attribute.Key(rotatedKey).Bool(wasRotated),
)
r.rotationReconcileTotal.Add(ctx, 1, opt)
}

func (r *reporter) reportRotationErrorCtMetric(provider, errType string, wasRotated bool) {
labels := []attribute.KeyValue{attribute.String(providerKey, provider), attribute.String(errorKey, errType), attribute.String(osTypeKey, runtimeOS), attribute.Bool(rotatedKey, wasRotated)}
rotationReconcileErrorTotal.Add(context.Background(), 1, labels...)
func (r *reporter) reportRotationErrorCtMetric(ctx context.Context, provider, errType string, wasRotated bool) {
opt := api.WithAttributes(
attribute.Key(providerKey).String(provider),
attribute.Key(errorKey).String(errType),
attribute.Key(osTypeKey).String(runtimeOS),
attribute.Key(rotatedKey).Bool(wasRotated),
)
r.rotationReconcileErrorTotal.Add(ctx, 1, opt)
}

func (r *reporter) reportRotationDuration(duration float64) {
r.meter.RecordBatch(context.Background(), []attribute.KeyValue{attribute.String(osTypeKey, runtimeOS)}, rotationReconcileDuration.Measurement(duration))
func (r *reporter) reportRotationDuration(ctx context.Context, duration float64) {
opt := api.WithAttributes(
attribute.Key(osTypeKey).String(runtimeOS),
)
r.rotationReconcileDuration.Record(ctx, duration, opt)
}
14 changes: 8 additions & 6 deletions pkg/secrets-store/mocks/stats_reporter_mock.go
Expand Up @@ -16,6 +16,8 @@ limitations under the License.

package mocks // import sigs.k8s.io/secrets-store-csi-driver/pkg/secrets-store/mocks

import "context"

type FakeReporter struct {
reportNodePublishCtMetricInvoked int
reportNodeUnPublishCtMetricInvoked int
Expand All @@ -29,27 +31,27 @@ func NewFakeReporter() *FakeReporter {
return &FakeReporter{}
}

func (f *FakeReporter) ReportNodePublishCtMetric(provider string) {
func (f *FakeReporter) ReportNodePublishCtMetric(ctx context.Context, provider string) {
f.reportNodePublishCtMetricInvoked++
}

func (f *FakeReporter) ReportNodeUnPublishCtMetric() {
func (f *FakeReporter) ReportNodeUnPublishCtMetric(ctx context.Context) {
f.reportNodeUnPublishCtMetricInvoked++
}

func (f *FakeReporter) ReportNodePublishErrorCtMetric(provider, errType string) {
func (f *FakeReporter) ReportNodePublishErrorCtMetric(ctx context.Context, provider, errType string) {
f.reportNodePublishErrorCtMetricInvoked++
}

func (f *FakeReporter) ReportNodeUnPublishErrorCtMetric() {
func (f *FakeReporter) ReportNodeUnPublishErrorCtMetric(ctx context.Context) {
f.reportNodeUnPublishErrorCtMetricInvoked++
}

func (f *FakeReporter) ReportSyncK8SecretCtMetric(provider string, count int) {
func (f *FakeReporter) ReportSyncK8SecretCtMetric(ctx context.Context, provider string, count int) {
f.reportSyncK8SecretCtMetricInvoked++
}

func (f *FakeReporter) ReportSyncK8SecretDuration(duration float64) {
func (f *FakeReporter) ReportSyncK8SecretDuration(ctx context.Context, duration float64) {
f.reportSyncK8SecretDurationInvoked++
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/secrets-store/nodeserver.go
Expand Up @@ -89,10 +89,10 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
klog.ErrorS(unmountErr, "failed to unmounting target path")
}
}
ns.reporter.ReportNodePublishErrorCtMetric(providerName, errorReason)
ns.reporter.ReportNodePublishErrorCtMetric(ctx, providerName, errorReason)
return
}
ns.reporter.ReportNodePublishCtMetric(providerName)
ns.reporter.ReportNodePublishCtMetric(ctx, providerName)
}()

// Check arguments
Expand Down Expand Up @@ -258,10 +258,10 @@ func (ns *nodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpu
startTime := time.Now()
defer func() {
if err != nil {
ns.reporter.ReportNodeUnPublishErrorCtMetric()
ns.reporter.ReportNodeUnPublishErrorCtMetric(ctx)
return
}
ns.reporter.ReportNodeUnPublishCtMetric()
ns.reporter.ReportNodeUnPublishCtMetric(ctx)
}()

// Check arguments
Expand Down
7 changes: 6 additions & 1 deletion pkg/secrets-store/secrets-store.go
Expand Up @@ -45,7 +45,12 @@ func NewSecretsStoreDriver(driverName, nodeID, endpoint string,
tokenClient *k8s.TokenClient) *SecretsStore {
klog.InfoS("Initializing Secrets Store CSI Driver", "driver", driverName, "version", version.BuildVersion, "buildTime", version.BuildTime)

ns, err := newNodeServer(nodeID, mount.New(""), providerClients, client, reader, NewStatsReporter(), tokenClient)
sr, err := NewStatsReporter()
if err != nil {
klog.ErrorS(err, "failed to initialize stats reporter")
os.Exit(1)
}
ns, err := newNodeServer(nodeID, mount.New(""), providerClients, client, reader, sr, tokenClient)
if err != nil {
klog.ErrorS(err, "failed to initialize node server")
os.Exit(1)
Expand Down

0 comments on commit 4023ebd

Please sign in to comment.