SecretsManager: Refactor and clean metrics (#108908)

This commit is contained in:
lean.dev
2025-07-30 20:00:30 +01:00
committed by GitHub
parent 8fb1483a9f
commit 6bf542889a
8 changed files with 317 additions and 66 deletions
@@ -292,8 +292,13 @@ func TestIntegrationDecrypt(t *testing.T) {
require.NotEmpty(t, exposed)
require.Equal(t, "value", exposed.DangerouslyExposeAndConsumeValue())
require.Len(t, fakeLogger.InfoArgs, 1)
args := fakeLogger.InfoArgs[0]
require.Len(t, fakeLogger.InfoMsgs, 2)
require.Equal(t, fakeLogger.InfoMsgs[0], "SecureValueMetadataStorage.Read")
require.Equal(t, fakeLogger.InfoMsgs[1], "Secrets Audit Log")
require.Len(t, fakeLogger.InfoArgs, 2)
// we only want to check the audit log args
args := fakeLogger.InfoArgs[1]
require.Contains(t, args, "grafana_decrypter_identity")
require.Contains(t, args, "decrypter_identity")
for i, arg := range args {
+6 -40
View File
@@ -25,12 +25,8 @@ type StorageMetrics struct {
KeeperMetadataListCount prometheus.Counter
KeeperMetadataGetKeeperConfigDuration prometheus.Histogram
SecureValueMetadataCreateDuration prometheus.Histogram
SecureValueMetadataCreateCount prometheus.Counter
SecureValueMetadataUpdateDuration prometheus.Histogram
SecureValueMetadataUpdateCount prometheus.Counter
SecureValueMetadataDeleteDuration prometheus.Histogram
SecureValueMetadataDeleteCount prometheus.Counter
SecureValueMetadataCreateDuration *prometheus.HistogramVec
SecureValueMetadataCreateCount *prometheus.CounterVec
SecureValueMetadataGetDuration prometheus.Histogram
SecureValueMetadataGetCount prometheus.Counter
SecureValueMetadataListDuration prometheus.Histogram
@@ -119,45 +115,19 @@ func newStorageMetrics() *StorageMetrics {
}),
// Secure value metrics
SecureValueMetadataCreateDuration: prometheus.NewHistogram(prometheus.HistogramOpts{
SecureValueMetadataCreateDuration: prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "secure_value_metadata_create_duration_seconds",
Help: "Duration of secure value metadata create operations",
Buckets: prometheus.DefBuckets,
}),
SecureValueMetadataCreateCount: prometheus.NewCounter(prometheus.CounterOpts{
}, []string{"successful"}),
SecureValueMetadataCreateCount: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "secure_value_metadata_create_count",
Help: "Count of secure value metadata create operations",
}),
SecureValueMetadataUpdateDuration: prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "secure_value_metadata_update_duration_seconds",
Help: "Duration of secure value metadata update operations",
Buckets: prometheus.DefBuckets,
}),
SecureValueMetadataUpdateCount: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "secure_value_metadata_update_count",
Help: "Count of secure value metadata update operations",
}),
SecureValueMetadataDeleteDuration: prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "secure_value_metadata_delete_duration_seconds",
Help: "Duration of secure value metadata delete operations",
Buckets: prometheus.DefBuckets,
}),
SecureValueMetadataDeleteCount: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "secure_value_metadata_delete_count",
Help: "Count of secure value metadata delete operations",
}),
}, []string{"successful"}),
SecureValueMetadataGetDuration: prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: subsystem,
@@ -241,10 +211,6 @@ func NewStorageMetrics(reg prometheus.Registerer) *StorageMetrics {
m.KeeperMetadataGetKeeperConfigDuration,
m.SecureValueMetadataCreateDuration,
m.SecureValueMetadataCreateCount,
m.SecureValueMetadataUpdateDuration,
m.SecureValueMetadataUpdateCount,
m.SecureValueMetadataDeleteDuration,
m.SecureValueMetadataDeleteCount,
m.SecureValueMetadataGetDuration,
m.SecureValueMetadataGetCount,
m.SecureValueMetadataListDuration,
@@ -3,18 +3,21 @@ package metadata
import (
"context"
"fmt"
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"github.com/grafana/grafana-app-sdk/logging"
secretv1beta1 "github.com/grafana/grafana/apps/secret/pkg/apis/secret/v1beta1"
"github.com/grafana/grafana/pkg/registry/apis/secret/contracts"
"github.com/grafana/grafana/pkg/registry/apis/secret/xkube"
"github.com/grafana/grafana/pkg/storage/secret/metadata/metrics"
"github.com/grafana/grafana/pkg/storage/unified/sql"
"github.com/grafana/grafana/pkg/storage/unified/sql/sqltemplate"
"go.opentelemetry.io/otel/codes"
)
var _ contracts.SecureValueMetadataStorage = (*secureValueMetadataStorage)(nil)
@@ -40,16 +43,39 @@ type secureValueMetadataStorage struct {
tracer trace.Tracer
}
func (s *secureValueMetadataStorage) Create(ctx context.Context, sv *secretv1beta1.SecureValue, actorUID string) (*secretv1beta1.SecureValue, error) {
func (s *secureValueMetadataStorage) Create(ctx context.Context, sv *secretv1beta1.SecureValue, actorUID string) (_ *secretv1beta1.SecureValue, svmCreateErr error) {
start := time.Now()
name := sv.GetName()
namespace := sv.GetNamespace()
ctx, span := s.tracer.Start(ctx, "SecureValueMetadataStorage.Create", trace.WithAttributes(
attribute.String("name", sv.GetName()),
attribute.String("namespace", sv.GetNamespace()),
attribute.String("name", name),
attribute.String("namespace", namespace),
attribute.String("actorUID", actorUID),
))
defer span.End()
// Set inside of the transaction callback
defer func() {
args := []any{
"name", name,
"namespace", namespace,
"actorUID", actorUID,
}
success := svmCreateErr == nil
args = append(args, "success", success)
if !success {
span.SetStatus(codes.Error, "SecureValueMetadataStorage.Create failed")
span.RecordError(svmCreateErr)
args = append(args, "error", svmCreateErr)
}
logging.FromContext(ctx).Info("SecureValueMetadataStorage.Create", args...)
s.metrics.SecureValueMetadataCreateDuration.WithLabelValues(strconv.FormatBool(success)).Observe(time.Since(start).Seconds())
s.metrics.SecureValueMetadataCreateCount.WithLabelValues(strconv.FormatBool(success)).Inc()
}()
// Set inside the transaction callback
var row *secureValueDB
err := s.db.Transaction(ctx, func(ctx context.Context) error {
@@ -145,9 +171,6 @@ func (s *secureValueMetadataStorage) Create(ctx context.Context, sv *secretv1bet
return nil, fmt.Errorf("convert to kubernetes object: %w", err)
}
s.metrics.SecureValueMetadataCreateDuration.Observe(time.Since(start).Seconds())
s.metrics.SecureValueMetadataCreateCount.Inc()
return createdSecureValue, nil
}
@@ -230,7 +253,7 @@ func (s *secureValueMetadataStorage) readActiveVersion(ctx context.Context, name
return secureValue, nil
}
func (s *secureValueMetadataStorage) Read(ctx context.Context, namespace xkube.Namespace, name string, opts contracts.ReadOpts) (*secretv1beta1.SecureValue, error) {
func (s *secureValueMetadataStorage) Read(ctx context.Context, namespace xkube.Namespace, name string, opts contracts.ReadOpts) (_ *secretv1beta1.SecureValue, readErr error) {
start := time.Now()
ctx, span := s.tracer.Start(ctx, "SecureValueMetadataStorage.Read", trace.WithAttributes(
attribute.String("name", name),
@@ -239,6 +262,13 @@ func (s *secureValueMetadataStorage) Read(ctx context.Context, namespace xkube.N
))
defer span.End()
defer func() {
logging.FromContext(ctx).Info("SecureValueMetadataStorage.Read", "namespace", namespace, "name", name, "success", readErr != nil, "error", readErr)
s.metrics.SecureValueMetadataGetDuration.Observe(time.Since(start).Seconds())
s.metrics.SecureValueMetadataGetCount.Inc()
}()
secureValue, err := s.readActiveVersion(ctx, namespace, name, opts)
if err != nil {
return nil, err
@@ -249,9 +279,6 @@ func (s *secureValueMetadataStorage) Read(ctx context.Context, namespace xkube.N
return nil, fmt.Errorf("convert to kubernetes object: %w", err)
}
s.metrics.SecureValueMetadataGetDuration.Observe(time.Since(start).Seconds())
s.metrics.SecureValueMetadataGetCount.Inc()
return secureValueKub, nil
}