From 5965aa9aca82c47c9367b34356d91dd89ab968c2 Mon Sep 17 00:00:00 2001 From: Sven Grossmann Date: Fri, 15 Nov 2024 12:51:52 +0100 Subject: [PATCH] Loki: Fix healthcheck logger always appending `endpoint` (#96531) * Loki: Fix healthcheck logger always appending `endpoint` * add test --- pkg/plugins/backendplugin/coreplugin/registry.go | 5 +++-- .../backendplugin/coreplugin/registry_test.go | 12 ++++++++++++ pkg/tsdb/loki/healthcheck.go | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/pkg/plugins/backendplugin/coreplugin/registry.go b/pkg/plugins/backendplugin/coreplugin/registry.go index 43b0d78fc72..e346c7893af 100644 --- a/pkg/plugins/backendplugin/coreplugin/registry.go +++ b/pkg/plugins/backendplugin/coreplugin/registry.go @@ -186,8 +186,9 @@ func (l *logWrapper) Level() sdklog.Level { } func (l *logWrapper) With(args ...any) sdklog.Logger { - l.logger = l.logger.New(args...) - return l + return &logWrapper{ + logger: l.logger.New(args...), + } } func (l *logWrapper) FromContext(ctx context.Context) sdklog.Logger { diff --git a/pkg/plugins/backendplugin/coreplugin/registry_test.go b/pkg/plugins/backendplugin/coreplugin/registry_test.go index 1ca04daf3c7..2bb6bed4042 100644 --- a/pkg/plugins/backendplugin/coreplugin/registry_test.go +++ b/pkg/plugins/backendplugin/coreplugin/registry_test.go @@ -5,6 +5,7 @@ import ( "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" "github.com/grafana/grafana/pkg/infra/tracing" + "github.com/grafana/grafana/pkg/plugins/log" "github.com/grafana/grafana/pkg/services/featuremgmt" "github.com/grafana/grafana/pkg/setting" "github.com/stretchr/testify/require" @@ -62,3 +63,14 @@ func TestNewPlugin(t *testing.T) { }) } } + +func TestLogger(t *testing.T) { + t.Run("logger.With should create a new logger", func(t *testing.T) { + wrapper := &logWrapper{ + logger: log.New("test"), + } + newLogger := wrapper.With("key", "value") + + require.NotSame(t, newLogger.(*logWrapper).logger, wrapper.logger, "`With` should not return the same instance") + }) +} diff --git a/pkg/tsdb/loki/healthcheck.go b/pkg/tsdb/loki/healthcheck.go index 1bcd17e6be2..ad6dd904683 100644 --- a/pkg/tsdb/loki/healthcheck.go +++ b/pkg/tsdb/loki/healthcheck.go @@ -20,7 +20,7 @@ const ( func (s *Service) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) { - logger := s.logger.With("endpoint", "CheckHealth") + logger := s.logger.With("endpoint", "checkHealth") ds, err := s.im.Get(ctx, req.PluginContext) // check that the datasource exists if err != nil {