SecretsManager: Introduce metrics and logs (#107582)
Co-authored-by: Michael Mandrus <michael.mandrus@grafana.com>
This commit is contained in:
co-authored by
Michael Mandrus
parent
66d9a33cc9
commit
a59ec345c2
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/registry/apis/secret/contracts"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
"github.com/grafana/grafana/pkg/storage/unified/sql/sqltemplate"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
@@ -19,9 +20,15 @@ type encryptionStoreImpl struct {
|
||||
dialect sqltemplate.Dialect
|
||||
tracer trace.Tracer
|
||||
log log.Logger
|
||||
metrics *DataKeyMetrics
|
||||
}
|
||||
|
||||
func ProvideDataKeyStorage(db contracts.Database, tracer trace.Tracer, features featuremgmt.FeatureToggles) (contracts.DataKeyStorage, error) {
|
||||
func ProvideDataKeyStorage(
|
||||
db contracts.Database,
|
||||
tracer trace.Tracer,
|
||||
features featuremgmt.FeatureToggles,
|
||||
registerer prometheus.Registerer,
|
||||
) (contracts.DataKeyStorage, error) {
|
||||
if !features.IsEnabledGlobally(featuremgmt.FlagGrafanaAPIServerWithExperimentalAPIs) ||
|
||||
!features.IsEnabledGlobally(featuremgmt.FlagSecretsManagementAppPlatform) {
|
||||
return &encryptionStoreImpl{}, nil
|
||||
@@ -32,6 +39,7 @@ func ProvideDataKeyStorage(db contracts.Database, tracer trace.Tracer, features
|
||||
dialect: sqltemplate.DialectForDriver(db.DriverName()),
|
||||
tracer: tracer,
|
||||
log: log.New("encryption.store"),
|
||||
metrics: NewDataKeyMetrics(registerer),
|
||||
}
|
||||
|
||||
return store, nil
|
||||
|
||||
@@ -31,7 +31,7 @@ func TestEncryptionStoreImpl_DataKeyLifecycle(t *testing.T) {
|
||||
testDB := sqlstore.NewTestStore(t, sqlstore.WithMigrator(migrator.New()))
|
||||
tracer := noop.NewTracerProvider().Tracer("test")
|
||||
features := featuremgmt.WithFeatures(featuremgmt.FlagGrafanaAPIServerWithExperimentalAPIs, featuremgmt.FlagSecretsManagementAppPlatform)
|
||||
store, err := ProvideDataKeyStorage(database.ProvideDatabase(testDB, tracer), tracer, features)
|
||||
store, err := ProvideDataKeyStorage(database.ProvideDatabase(testDB, tracer), tracer, features, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
package encryption
|
||||
|
||||
import (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
const (
|
||||
namespace = "grafana_secrets_manager"
|
||||
subsystem = "data_key_storage"
|
||||
)
|
||||
|
||||
// DataKeyMetrics is a struct that contains all the metrics for all operations of encryption storage.
|
||||
type DataKeyMetrics struct {
|
||||
CreateDataKeyDuration prometheus.Histogram
|
||||
GetDataKeyDuration prometheus.Histogram
|
||||
GetCurrentDataKeyDuration prometheus.Histogram
|
||||
GetAllDataKeysDuration prometheus.Histogram
|
||||
DisableDataKeysDuration prometheus.Histogram
|
||||
DeleteDataKeyDuration prometheus.Histogram
|
||||
ReEncryptDataKeysDuration prometheus.Histogram
|
||||
}
|
||||
|
||||
func newDataKeyMetrics() *DataKeyMetrics {
|
||||
return &DataKeyMetrics{
|
||||
CreateDataKeyDuration: prometheus.NewHistogram(prometheus.HistogramOpts{
|
||||
Namespace: namespace,
|
||||
Subsystem: subsystem,
|
||||
Name: "create_data_key_duration_seconds",
|
||||
Help: "Duration of create data key operations",
|
||||
Buckets: prometheus.DefBuckets,
|
||||
}),
|
||||
GetDataKeyDuration: prometheus.NewHistogram(prometheus.HistogramOpts{
|
||||
Namespace: namespace,
|
||||
Subsystem: subsystem,
|
||||
Name: "get_data_key_duration_seconds",
|
||||
Help: "Duration of get data key operations",
|
||||
Buckets: prometheus.DefBuckets,
|
||||
}),
|
||||
GetCurrentDataKeyDuration: prometheus.NewHistogram(prometheus.HistogramOpts{
|
||||
Namespace: namespace,
|
||||
Subsystem: subsystem,
|
||||
Name: "get_current_data_key_duration_seconds",
|
||||
Help: "Duration of get current data key operations",
|
||||
Buckets: prometheus.DefBuckets,
|
||||
}),
|
||||
GetAllDataKeysDuration: prometheus.NewHistogram(prometheus.HistogramOpts{
|
||||
Namespace: namespace,
|
||||
Subsystem: subsystem,
|
||||
Name: "get_all_data_keys_duration_seconds",
|
||||
Help: "Duration of get all data keys operations",
|
||||
Buckets: prometheus.DefBuckets,
|
||||
}),
|
||||
DisableDataKeysDuration: prometheus.NewHistogram(prometheus.HistogramOpts{
|
||||
Namespace: namespace,
|
||||
Subsystem: subsystem,
|
||||
Name: "disable_data_keys_duration_seconds",
|
||||
Help: "Duration of disable data keys operations",
|
||||
Buckets: prometheus.DefBuckets,
|
||||
}),
|
||||
DeleteDataKeyDuration: prometheus.NewHistogram(prometheus.HistogramOpts{
|
||||
Namespace: namespace,
|
||||
Subsystem: subsystem,
|
||||
Name: "delete_data_key_duration_seconds",
|
||||
Help: "Duration of delete data key operations",
|
||||
Buckets: prometheus.DefBuckets,
|
||||
}),
|
||||
ReEncryptDataKeysDuration: prometheus.NewHistogram(prometheus.HistogramOpts{
|
||||
Namespace: namespace,
|
||||
Subsystem: subsystem,
|
||||
Name: "re_encrypt_data_keys_duration_seconds",
|
||||
Help: "Duration of re-encrypt data keys operations",
|
||||
Buckets: prometheus.DefBuckets,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
// NewDataKeyMetrics returns a singleton instance of the SecretsMetrics struct containing registered metrics
|
||||
func NewDataKeyMetrics(reg prometheus.Registerer) *DataKeyMetrics {
|
||||
m := newDataKeyMetrics()
|
||||
|
||||
if reg != nil {
|
||||
reg.MustRegister(
|
||||
m.CreateDataKeyDuration,
|
||||
m.GetDataKeyDuration,
|
||||
m.GetCurrentDataKeyDuration,
|
||||
m.GetAllDataKeysDuration,
|
||||
m.DisableDataKeysDuration,
|
||||
m.DeleteDataKeyDuration,
|
||||
m.ReEncryptDataKeysDuration,
|
||||
)
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func NewTestMetrics() *DataKeyMetrics {
|
||||
return newDataKeyMetrics()
|
||||
}
|
||||
Reference in New Issue
Block a user