Skip to content

Commit

Permalink
performance fix (#119)
Browse files Browse the repository at this point in the history
* performance fix

Signed-off-by: nitesh3108 <[email protected]>

* linter fix

Signed-off-by: nitesh3108 <[email protected]>

---------

Signed-off-by: nitesh3108 <[email protected]>
  • Loading branch information
nitesh3108 authored Jan 10, 2025
1 parent 2643725 commit 11abb45
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 5 deletions.
62 changes: 57 additions & 5 deletions internal/service/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ func (mw *MetricsWrapper) Record(_ context.Context, meta interface{},

metrics := metricsMapValue.(*Metrics)

_, _ = mw.Meter.RegisterCallback(func(_ context.Context, obs metric.Observer) error {
done := make(chan struct{})
reg, err := mw.Meter.RegisterCallback(func(_ context.Context, obs metric.Observer) error {
obs.ObserveFloat64(metrics.ReadBW, float64(readBW), metric.ObserveOption(metric.WithAttributes(labels...)))
obs.ObserveFloat64(metrics.WriteBW, float64(writeBW), metric.ObserveOption(metric.WithAttributes(labels...)))
obs.ObserveFloat64(metrics.ReadIOPS, float64(readIOPS), metric.ObserveOption(metric.WithAttributes(labels...)))
Expand All @@ -197,6 +198,9 @@ func (mw *MetricsWrapper) Record(_ context.Context, meta interface{},
obs.ObserveFloat64(metrics.MirrorBW, float64(mirrorBW), metric.ObserveOption(metric.WithAttributes(labels...)))
obs.ObserveFloat64(metrics.DataRemaining, float64(dataRemaining), metric.ObserveOption(metric.WithAttributes(labels...)))

go func() {
done <- struct{}{}
}()
return nil
},
metrics.ReadBW,
Expand All @@ -209,6 +213,12 @@ func (mw *MetricsWrapper) Record(_ context.Context, meta interface{},
metrics.MirrorBW,
metrics.DataRemaining,
)
if err != nil {
return err
}

<-done
_ = reg.Unregister()

return nil
}
Expand Down Expand Up @@ -307,14 +317,25 @@ func (mw *MetricsWrapper) RecordSpaceMetrics(_ context.Context, meta interface{}

metrics := metricsMapValue.(*SpaceMetrics)

_, _ = mw.Meter.RegisterCallback(func(_ context.Context, obs metric.Observer) error {
done := make(chan struct{})
reg, err := mw.Meter.RegisterCallback(func(_ context.Context, obs metric.Observer) error {
obs.ObserveFloat64(metrics.LogicalProvisioned, float64(logicalProvisioned), metric.ObserveOption(metric.WithAttributes(labels...)))
obs.ObserveFloat64(metrics.LogicalUsed, float64(logicalUsed), metric.ObserveOption(metric.WithAttributes(labels...)))

go func() {
done <- struct{}{}
}()
return nil
},
metrics.LogicalProvisioned,
metrics.LogicalUsed,
)
if err != nil {
return err
}

<-done
_ = reg.Unregister()

return nil
}
Expand Down Expand Up @@ -385,14 +406,25 @@ func (mw *MetricsWrapper) RecordArraySpaceMetrics(_ context.Context, arrayID, dr
}

metrics := metricsMapValue.(*ArraySpaceMetrics)
_, _ = mw.Meter.RegisterCallback(func(_ context.Context, obs metric.Observer) error {

done := make(chan struct{})
reg, err := mw.Meter.RegisterCallback(func(_ context.Context, obs metric.Observer) error {
obs.ObserveFloat64(metrics.LogicalProvisioned, float64(logicalProvisioned), metric.ObserveOption(metric.WithAttributes(labels...)))
obs.ObserveFloat64(metrics.LogicalUsed, float64(logicalUsed), metric.ObserveOption(metric.WithAttributes(labels...)))
go func() {
done <- struct{}{}
}()
return nil
},
metrics.LogicalProvisioned,
metrics.LogicalUsed,
)
if err != nil {
return err
}

<-done
_ = reg.Unregister()

return nil
}
Expand Down Expand Up @@ -447,15 +479,25 @@ func (mw *MetricsWrapper) RecordStorageClassSpaceMetrics(_ context.Context, stor
}

metrics := metricsMapValue.(*ArraySpaceMetrics)
_, _ = mw.Meter.RegisterCallback(func(_ context.Context, obs metric.Observer) error {

done := make(chan struct{})
reg, err := mw.Meter.RegisterCallback(func(_ context.Context, obs metric.Observer) error {
obs.ObserveFloat64(metrics.LogicalProvisioned, float64(logicalProvisioned), metric.ObserveOption(metric.WithAttributes(labels...)))
obs.ObserveFloat64(metrics.LogicalUsed, float64(logicalUsed), metric.ObserveOption(metric.WithAttributes(labels...)))
go func() {
done <- struct{}{}
}()
return nil
},
metrics.LogicalProvisioned,
metrics.LogicalUsed,
)
if err != nil {
return err
}

<-done
_ = reg.Unregister()
return nil
}

Expand Down Expand Up @@ -557,7 +599,8 @@ func (mw *MetricsWrapper) RecordFileSystemMetrics(_ context.Context, meta interf

metrics := metricsMapValue.(*Metrics)

_, _ = mw.Meter.RegisterCallback(func(_ context.Context, obs metric.Observer) error {
done := make(chan struct{})
reg, err := mw.Meter.RegisterCallback(func(_ context.Context, obs metric.Observer) error {
obs.ObserveFloat64(metrics.ReadBW, float64(readBW), metric.ObserveOption(metric.WithAttributes(labels...)))
obs.ObserveFloat64(metrics.WriteBW, float64(writeBW), metric.ObserveOption(metric.WithAttributes(labels...)))
obs.ObserveFloat64(metrics.ReadIOPS, float64(readIOPS), metric.ObserveOption(metric.WithAttributes(labels...)))
Expand All @@ -567,6 +610,9 @@ func (mw *MetricsWrapper) RecordFileSystemMetrics(_ context.Context, meta interf
obs.ObserveFloat64(metrics.SyncronizationBW, float64(syncBW), metric.ObserveOption(metric.WithAttributes(labels...)))
obs.ObserveFloat64(metrics.MirrorBW, float64(mirrorBW), metric.ObserveOption(metric.WithAttributes(labels...)))
obs.ObserveFloat64(metrics.DataRemaining, float64(dataRemaining), metric.ObserveOption(metric.WithAttributes(labels...)))
go func() {
done <- struct{}{}
}()
return nil
},
metrics.ReadBW,
Expand All @@ -579,6 +625,12 @@ func (mw *MetricsWrapper) RecordFileSystemMetrics(_ context.Context, meta interf
metrics.MirrorBW,
metrics.DataRemaining,
)
if err != nil {
return err
}

<-done
_ = reg.Unregister()

return nil
}
68 changes: 68 additions & 0 deletions internal/service/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"context"
"testing"

otlexporters "github.com/dell/csm-metrics-powerstore/opentelemetry/exporters"

"github.com/dell/csm-metrics-powerstore/internal/service"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
Expand All @@ -39,6 +41,13 @@ func TestMetricsWrapper_Record(t *testing.T) {
ID: "123",
},
}

exporter := &otlexporters.OtlCollectorExporter{}
err := exporter.InitExporter()
if err != nil {
t.Fatal(err)
}

type args struct {
ctx context.Context
meta interface{}
Expand Down Expand Up @@ -108,6 +117,13 @@ func TestMetricsWrapper_Record_Label_Update(t *testing.T) {
mw := &service.MetricsWrapper{
Meter: otel.Meter("powerstore-test"),
}

exporter := &otlexporters.OtlCollectorExporter{}
err := exporter.InitExporter()
if err != nil {
t.Fatal(err)
}

metaFirst := &service.VolumeMeta{
ID: "123",
PersistentVolumeName: "pvol0",
Expand Down Expand Up @@ -215,6 +231,13 @@ func TestMetricsWrapper_RecordSpaceMetrics(t *testing.T) {
ID: "123",
},
}

exporter := &otlexporters.OtlCollectorExporter{}
err := exporter.InitExporter()
if err != nil {
t.Fatal(err)
}

type args struct {
ctx context.Context
meta interface{}
Expand Down Expand Up @@ -295,6 +318,12 @@ func TestMetricsWrapper_RecordSpaceMetrics_Label_Update(t *testing.T) {
Protocol: "scsi",
}

exporter := &otlexporters.OtlCollectorExporter{}
err := exporter.InitExporter()
if err != nil {
t.Fatal(err)
}

expectedLables := []attribute.KeyValue{
attribute.String("VolumeID", metaSecond.ID),
attribute.String("PersistentVolumeName", metaSecond.PersistentVolumeName),
Expand Down Expand Up @@ -368,6 +397,12 @@ func TestMetricsWrapper_RecordArraySpaceMetrics(t *testing.T) {
Meter: otel.Meter("powerstore-test"),
}

exporter := &otlexporters.OtlCollectorExporter{}
err := exporter.InitExporter()
if err != nil {
t.Fatal(err)
}

type args struct {
ctx context.Context
arrayID string
Expand Down Expand Up @@ -408,6 +443,12 @@ func TestMetricsWrapper_RecordArraySpaceMetrics_Label_Update(t *testing.T) {
Meter: otel.Meter("powerstore-test"),
}

exporter := &otlexporters.OtlCollectorExporter{}
err := exporter.InitExporter()
if err != nil {
t.Fatal(err)
}

array1 := "123"
array2 := "123"
array3 := "125"
Expand Down Expand Up @@ -477,6 +518,13 @@ func TestMetricsWrapper_RecordStorageClassSpaceMetrics(t *testing.T) {
mw := &service.MetricsWrapper{
Meter: otel.Meter("powerstore-test"),
}

exporter := &otlexporters.OtlCollectorExporter{}
err := exporter.InitExporter()
if err != nil {
t.Fatal(err)
}

type args struct {
ctx context.Context
storageclass string
Expand Down Expand Up @@ -517,6 +565,12 @@ func TestMetricsWrapper_RecordStorageClassSpaceMetrics_Label_Update(t *testing.T
Meter: otel.Meter("powerstore-test"),
}

exporter := &otlexporters.OtlCollectorExporter{}
err := exporter.InitExporter()
if err != nil {
t.Fatal(err)
}

array1 := "storageclass"
array2 := "storageclass"
array3 := "storageclass2"
Expand Down Expand Up @@ -598,6 +652,13 @@ func TestMetricsWrapper_RecordFileSystemMetrics(t *testing.T) {
ID: "123",
},
}

exporter := &otlexporters.OtlCollectorExporter{}
err := exporter.InitExporter()
if err != nil {
t.Fatal(err)
}

type args struct {
ctx context.Context
meta interface{}
Expand Down Expand Up @@ -667,6 +728,13 @@ func TestMetricsWrapper_RecordFileSystemMetrics_Label_Update(t *testing.T) {
mw := &service.MetricsWrapper{
Meter: otel.Meter("powerstore-test"),
}

exporter := &otlexporters.OtlCollectorExporter{}
err := exporter.InitExporter()
if err != nil {
t.Fatal(err)
}

metaFirst := &service.VolumeMeta{
ID: "123",
PersistentVolumeName: "pvol0",
Expand Down

0 comments on commit 11abb45

Please sign in to comment.