diff --git a/pkg/storage/unified/apistore/restoptions.go b/pkg/storage/unified/apistore/restoptions.go index d880c663f29..161be84d152 100644 --- a/pkg/storage/unified/apistore/restoptions.go +++ b/pkg/storage/unified/apistore/restoptions.go @@ -17,6 +17,7 @@ import ( flowcontrolrequest "k8s.io/apiserver/pkg/util/flowcontrol/request" "k8s.io/client-go/tools/cache" + "github.com/grafana/grafana/pkg/infra/log" secret "github.com/grafana/grafana/pkg/registry/apis/secret/contracts" "github.com/grafana/grafana/pkg/storage/unified/resource" ) @@ -66,6 +67,7 @@ func NewRESTOptionsGetterMemory(originalStorageConfig storagebackend.Config, sec backend, err := resource.NewKVStorageBackend(resource.KVBackendOptions{ KvStore: kv, WithExperimentalClusterScope: true, + Log: log.New(), }) if err != nil { return nil, err @@ -105,6 +107,7 @@ func NewRESTOptionsGetterForFileXX(path string, kv := resource.NewBadgerKV(db) backend, err := resource.NewKVStorageBackend(resource.KVBackendOptions{ KvStore: kv, + Log: log.New(), }) if err != nil { return nil, err diff --git a/pkg/storage/unified/client.go b/pkg/storage/unified/client.go index e5c396907e3..d92e2a6395e 100644 --- a/pkg/storage/unified/client.go +++ b/pkg/storage/unified/client.go @@ -23,6 +23,7 @@ import ( "github.com/grafana/dskit/services" grafanarest "github.com/grafana/grafana/pkg/apiserver/rest" infraDB "github.com/grafana/grafana/pkg/infra/db" + "github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/tracing" secrets "github.com/grafana/grafana/pkg/registry/apis/secret/contracts" "github.com/grafana/grafana/pkg/services/apiserver/options" @@ -121,6 +122,7 @@ func newClient(opts options.StorageOptions, kv := resource.NewBadgerKV(db) backend, err := resource.NewKVStorageBackend(resource.KVBackendOptions{ KvStore: kv, + Log: log.New(), }) if err != nil { return nil, err diff --git a/pkg/storage/unified/resource/notifier.go b/pkg/storage/unified/resource/notifier.go index 8f15a68b810..4251ecbf7ce 100644 --- a/pkg/storage/unified/resource/notifier.go +++ b/pkg/storage/unified/resource/notifier.go @@ -7,7 +7,7 @@ import ( "sync" "github.com/grafana/dskit/backoff" - "github.com/grafana/grafana-app-sdk/logging" + "github.com/grafana/grafana/pkg/infra/log" gocache "github.com/patrickmn/go-cache" "time" @@ -32,11 +32,11 @@ type notifier interface { type pollingNotifier struct { eventStore *eventStore - log logging.Logger + log log.Logger } type notifierOptions struct { - log logging.Logger + log log.Logger useChannelNotifier bool } @@ -57,24 +57,20 @@ func defaultWatchOptions() watchOptions { } func newNotifier(eventStore *eventStore, opts notifierOptions) notifier { - if opts.log == nil { - opts.log = &logging.NoOpLogger{} - } - if opts.useChannelNotifier { - return newChannelNotifier(opts.log) + return newChannelNotifier(opts.log.New("notifier", "channelNotifier")) } - return &pollingNotifier{eventStore: eventStore, log: opts.log} + return &pollingNotifier{eventStore: eventStore, log: opts.log.New("notifier", "pollingNotifier")} } type channelNotifier struct { - log logging.Logger + log log.Logger subscribers map[chan Event]struct{} mu sync.Mutex } -func newChannelNotifier(log logging.Logger) *channelNotifier { +func newChannelNotifier(log log.Logger) *channelNotifier { return &channelNotifier{ log: log, subscribers: make(map[chan Event]struct{}), @@ -82,6 +78,7 @@ func newChannelNotifier(log logging.Logger) *channelNotifier { } func (cn *channelNotifier) Watch(ctx context.Context, opts watchOptions) <-chan Event { + cn.log.Info("creating new notifier", "buffer_size", opts.BufferSize) events := make(chan Event, opts.BufferSize) cn.mu.Lock() @@ -132,6 +129,13 @@ func (n *pollingNotifier) Watch(ctx context.Context, opts watchOptions) <-chan E opts.MaxBackoff = defaultMaxBackoff } + n.log.Info("creating new notifier", + "lookback", opts.LookbackPeriod, + "buffer_size", opts.BufferSize, + "min_backoff", opts.MinBackoff, + "max_backoff", opts.MaxBackoff, + ) + cacheTTL := opts.LookbackPeriod cacheCleanupInterval := 2 * opts.LookbackPeriod diff --git a/pkg/storage/unified/resource/notifier_test.go b/pkg/storage/unified/resource/notifier_test.go index 6ec85b57409..16ca63cf66b 100644 --- a/pkg/storage/unified/resource/notifier_test.go +++ b/pkg/storage/unified/resource/notifier_test.go @@ -7,8 +7,8 @@ import ( "testing/synctest" "time" - "github.com/grafana/grafana-app-sdk/logging" "github.com/grafana/grafana/pkg/infra/db" + "github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/storage/unified/sql/db/dbimpl" "github.com/grafana/grafana/pkg/util/testutil" @@ -24,7 +24,7 @@ func setupTestNotifier(t *testing.T) (*pollingNotifier, *eventStore) { }) kv := NewBadgerKV(db) eventStore := newEventStore(kv) - notifier := newNotifier(eventStore, notifierOptions{log: &logging.NoOpLogger{}}) + notifier := newNotifier(eventStore, notifierOptions{log: log.NewNopLogger()}) return notifier.(*pollingNotifier), eventStore } @@ -35,7 +35,7 @@ func setupTestNotifierSqlKv(t *testing.T) (*pollingNotifier, *eventStore) { kv, err := NewSQLKV(eDB) require.NoError(t, err) eventStore := newEventStore(kv) - notifier := newNotifier(eventStore, notifierOptions{log: &logging.NoOpLogger{}}) + notifier := newNotifier(eventStore, notifierOptions{log: log.NewNopLogger()}) return notifier.(*pollingNotifier), eventStore } @@ -501,7 +501,7 @@ func testNotifierWatchMultipleEvents(t *testing.T, ctx context.Context, notifier } func TestChannelNotifier(t *testing.T) { - log := &logging.NoOpLogger{} + log := log.NewNopLogger() opts := watchOptions{BufferSize: 5} var eventCount int64 diff --git a/pkg/storage/unified/resource/storage_backend.go b/pkg/storage/unified/resource/storage_backend.go index b48b110b5d5..9194d071022 100644 --- a/pkg/storage/unified/resource/storage_backend.go +++ b/pkg/storage/unified/resource/storage_backend.go @@ -15,13 +15,13 @@ import ( "github.com/bwmarrin/snowflake" "github.com/google/uuid" - "github.com/grafana/grafana-app-sdk/logging" "github.com/prometheus/client_golang/prometheus" "go.opentelemetry.io/otel/trace" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "github.com/grafana/grafana/pkg/apimachinery/utils" + "github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/storage/unified/resourcepb" "github.com/grafana/grafana/pkg/storage/unified/sql/db" "github.com/grafana/grafana/pkg/storage/unified/sql/rvmanager" @@ -63,7 +63,7 @@ type kvStorageBackend struct { eventStore *eventStore notifier notifier builder DocumentBuilder - log logging.Logger + log log.Logger withPruner bool eventRetentionPeriod time.Duration eventPruningInterval time.Duration @@ -90,6 +90,7 @@ type KVBackendOptions struct { EventPruningInterval time.Duration // How often to run the event pruning (default: 5 minutes) Tracer trace.Tracer // TODO add tracing Reg prometheus.Registerer // TODO add metrics + Log log.Logger UseChannelNotifier bool // Adding RvManager overrides the RV generated with snowflake in order to keep backwards compatibility with @@ -101,6 +102,11 @@ func NewKVStorageBackend(opts KVBackendOptions) (KVBackend, error) { ctx := context.Background() kv := opts.KvStore + logger := opts.Log + if opts.Log == nil { + logger = log.NewNopLogger() + } + s, err := snowflake.NewNode(rand.Int64N(1024)) if err != nil { return nil, fmt.Errorf("failed to create snowflake node: %w", err) @@ -122,10 +128,10 @@ func NewKVStorageBackend(opts KVBackendOptions) (KVBackend, error) { bulkLock: NewBulkLock(), dataStore: newDataStore(kv), eventStore: eventStore, - notifier: newNotifier(eventStore, notifierOptions{useChannelNotifier: opts.UseChannelNotifier}), + notifier: newNotifier(eventStore, notifierOptions{log: logger, useChannelNotifier: opts.UseChannelNotifier}), snowflake: s, builder: StandardDocumentBuilder(), // For now we use the standard document builder. - log: &logging.NoOpLogger{}, // Make this configurable + log: logger, eventRetentionPeriod: eventRetentionPeriod, eventPruningInterval: eventPruningInterval, withExperimentalClusterScope: opts.WithExperimentalClusterScope, @@ -139,6 +145,8 @@ func NewKVStorageBackend(opts KVBackendOptions) (KVBackend, error) { // Start the event cleanup background job go backend.runCleanupOldEvents(ctx) + logger.Info("backend initialized", "kv", fmt.Sprintf("%T", kv)) + return backend, nil } diff --git a/pkg/storage/unified/sql/server.go b/pkg/storage/unified/sql/server.go index b96d559ba55..8ee768ab855 100644 --- a/pkg/storage/unified/sql/server.go +++ b/pkg/storage/unified/sql/server.go @@ -13,6 +13,7 @@ import ( "github.com/grafana/dskit/services" infraDB "github.com/grafana/grafana/pkg/infra/db" + "github.com/grafana/grafana/pkg/infra/log" secrets "github.com/grafana/grafana/pkg/registry/apis/secret/contracts" inlinesecurevalue "github.com/grafana/grafana/pkg/registry/apis/secret/inline" "github.com/grafana/grafana/pkg/services/featuremgmt" @@ -113,6 +114,7 @@ func NewResourceServer(opts ServerOptions) (resource.ResourceServer, error) { Tracer: opts.Tracer, Reg: opts.Reg, UseChannelNotifier: !isHA, + Log: log.New("storage-backend"), } ctx := context.Background()