Use logging from app sdk in the resource mod (#103281)
* Use logging from app sdk in the resource mod * make update-workspace * Use app-sdk logging in SQL backend * Use grafana-app logging in tests * make update-workspace * make update-workspace * make update-workspace * Use default logging * Remove dependency on grafana/grafana * Fix imports
This commit is contained in:
@@ -20,8 +20,7 @@ import (
|
||||
"google.golang.org/protobuf/proto"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
unifiedbackend "github.com/grafana/grafana/pkg/storage/unified/backend"
|
||||
"github.com/grafana/grafana-app-sdk/logging"
|
||||
"github.com/grafana/grafana/pkg/storage/unified/resource"
|
||||
"github.com/grafana/grafana/pkg/storage/unified/sql/db"
|
||||
"github.com/grafana/grafana/pkg/storage/unified/sql/dbutil"
|
||||
@@ -76,7 +75,7 @@ func NewBackend(opts BackendOptions) (Backend, error) {
|
||||
isHA: opts.IsHA,
|
||||
done: ctx.Done(),
|
||||
cancel: cancel,
|
||||
log: log.New("sql-resource-server"),
|
||||
log: logging.DefaultLogger.With("logger", "sql-resource-server"),
|
||||
tracer: opts.Tracer,
|
||||
reg: opts.Reg,
|
||||
dbProvider: opts.DBProvider,
|
||||
@@ -123,7 +122,7 @@ type backend struct {
|
||||
initErr error
|
||||
|
||||
// o11y
|
||||
log log.Logger
|
||||
log logging.Logger
|
||||
tracer trace.Tracer
|
||||
reg prometheus.Registerer
|
||||
storageMetrics *resource.StorageMetrics
|
||||
@@ -343,7 +342,7 @@ func (b *backend) create(ctx context.Context, event resource.WriteEvent) (int64,
|
||||
GUID: guid,
|
||||
}); err != nil {
|
||||
if isRowAlreadyExistsError(err) {
|
||||
return guid, unifiedbackend.ErrResourceAlreadyExists
|
||||
return guid, resource.ErrResourceAlreadyExists
|
||||
}
|
||||
return guid, fmt.Errorf("insert into resource: %w", err)
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
"github.com/mattn/go-sqlite3"
|
||||
|
||||
"github.com/grafana/grafana/pkg/apimachinery/utils"
|
||||
unifiedbackend "github.com/grafana/grafana/pkg/storage/unified/backend"
|
||||
"github.com/grafana/grafana/pkg/storage/unified/resource"
|
||||
"github.com/grafana/grafana/pkg/storage/unified/sql/db/dbimpl"
|
||||
"github.com/grafana/grafana/pkg/storage/unified/sql/test"
|
||||
@@ -251,7 +250,7 @@ func TestBackend_create(t *testing.T) {
|
||||
|
||||
// Then we try to insert the same resource again. This should fail.
|
||||
_, err = b.create(ctx, event)
|
||||
require.ErrorIs(t, err, unifiedbackend.ErrResourceAlreadyExists)
|
||||
require.ErrorIs(t, err, resource.ErrResourceAlreadyExists)
|
||||
})
|
||||
|
||||
t.Run("error inserting into resource", func(t *testing.T) {
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana-app-sdk/logging"
|
||||
"github.com/grafana/grafana/pkg/storage/unified/resource"
|
||||
"github.com/grafana/grafana/pkg/storage/unified/sql/db"
|
||||
"github.com/grafana/grafana/pkg/storage/unified/sql/dbutil"
|
||||
@@ -61,14 +61,14 @@ func newNotifier(b *backend) (eventNotifier, error) {
|
||||
}
|
||||
|
||||
type channelNotifier struct {
|
||||
log log.Logger
|
||||
log logging.Logger
|
||||
bufferSize int
|
||||
|
||||
mu sync.RWMutex
|
||||
subscribers map[chan *resource.WrittenEvent]bool
|
||||
}
|
||||
|
||||
func newChannelNotifier(bufferSize int, log log.Logger) *channelNotifier {
|
||||
func newChannelNotifier(bufferSize int, log logging.Logger) *channelNotifier {
|
||||
return &channelNotifier{
|
||||
subscribers: make(map[chan *resource.WrittenEvent]bool),
|
||||
log: log,
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana-app-sdk/logging"
|
||||
"github.com/grafana/grafana/pkg/storage/unified/resource"
|
||||
"github.com/grafana/grafana/pkg/storage/unified/sql/sqltemplate"
|
||||
)
|
||||
@@ -31,7 +31,7 @@ type pollingNotifier struct {
|
||||
pollingInterval time.Duration
|
||||
watchBufferSize int
|
||||
|
||||
log log.Logger
|
||||
log logging.Logger
|
||||
tracer trace.Tracer
|
||||
storageMetrics *resource.StorageMetrics
|
||||
|
||||
@@ -47,7 +47,7 @@ type pollingNotifierConfig struct {
|
||||
pollingInterval time.Duration
|
||||
watchBufferSize int
|
||||
|
||||
log log.Logger
|
||||
log logging.Logger
|
||||
tracer trace.Tracer
|
||||
storageMetrics *resource.StorageMetrics
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.opentelemetry.io/otel/trace/noop"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana-app-sdk/logging"
|
||||
"github.com/grafana/grafana/pkg/storage/unified/resource"
|
||||
"github.com/grafana/grafana/pkg/storage/unified/sql/sqltemplate"
|
||||
)
|
||||
@@ -30,7 +30,7 @@ func TestPollingNotifierConfig(t *testing.T) {
|
||||
listLatestRVs: func(ctx context.Context) (groupResourceRV, error) { return nil, nil },
|
||||
bulkLock: &bulkLock{},
|
||||
tracer: noop.NewTracerProvider().Tracer("test"),
|
||||
log: log.NewNopLogger(),
|
||||
log: &logging.NoOpLogger{},
|
||||
watchBufferSize: 10,
|
||||
pollingInterval: time.Second,
|
||||
done: make(chan struct{}),
|
||||
@@ -44,7 +44,7 @@ func TestPollingNotifierConfig(t *testing.T) {
|
||||
listLatestRVs: func(ctx context.Context) (groupResourceRV, error) { return nil, nil },
|
||||
bulkLock: &bulkLock{},
|
||||
tracer: noop.NewTracerProvider().Tracer("test"),
|
||||
log: log.NewNopLogger(),
|
||||
log: &logging.NoOpLogger{},
|
||||
watchBufferSize: 10,
|
||||
pollingInterval: time.Second,
|
||||
done: make(chan struct{}),
|
||||
@@ -60,7 +60,7 @@ func TestPollingNotifierConfig(t *testing.T) {
|
||||
},
|
||||
bulkLock: &bulkLock{},
|
||||
tracer: noop.NewTracerProvider().Tracer("test"),
|
||||
log: log.NewNopLogger(),
|
||||
log: &logging.NoOpLogger{},
|
||||
watchBufferSize: 10,
|
||||
pollingInterval: time.Second,
|
||||
done: make(chan struct{}),
|
||||
@@ -76,7 +76,7 @@ func TestPollingNotifierConfig(t *testing.T) {
|
||||
},
|
||||
listLatestRVs: func(ctx context.Context) (groupResourceRV, error) { return nil, nil },
|
||||
tracer: noop.NewTracerProvider().Tracer("test"),
|
||||
log: log.NewNopLogger(),
|
||||
log: &logging.NoOpLogger{},
|
||||
watchBufferSize: 10,
|
||||
pollingInterval: time.Second,
|
||||
done: make(chan struct{}),
|
||||
@@ -92,7 +92,7 @@ func TestPollingNotifierConfig(t *testing.T) {
|
||||
},
|
||||
listLatestRVs: func(ctx context.Context) (groupResourceRV, error) { return nil, nil },
|
||||
bulkLock: &bulkLock{},
|
||||
log: log.NewNopLogger(),
|
||||
log: &logging.NoOpLogger{},
|
||||
watchBufferSize: 10,
|
||||
pollingInterval: time.Second,
|
||||
done: make(chan struct{}),
|
||||
@@ -125,7 +125,7 @@ func TestPollingNotifierConfig(t *testing.T) {
|
||||
listLatestRVs: func(ctx context.Context) (groupResourceRV, error) { return nil, nil },
|
||||
bulkLock: &bulkLock{},
|
||||
tracer: noop.NewTracerProvider().Tracer("test"),
|
||||
log: log.NewNopLogger(),
|
||||
log: &logging.NoOpLogger{},
|
||||
watchBufferSize: 0,
|
||||
pollingInterval: time.Second,
|
||||
done: make(chan struct{}),
|
||||
@@ -142,7 +142,7 @@ func TestPollingNotifierConfig(t *testing.T) {
|
||||
listLatestRVs: func(ctx context.Context) (groupResourceRV, error) { return nil, nil },
|
||||
bulkLock: &bulkLock{},
|
||||
tracer: noop.NewTracerProvider().Tracer("test"),
|
||||
log: log.NewNopLogger(),
|
||||
log: &logging.NoOpLogger{},
|
||||
watchBufferSize: 10,
|
||||
pollingInterval: 0,
|
||||
done: make(chan struct{}),
|
||||
@@ -159,7 +159,7 @@ func TestPollingNotifierConfig(t *testing.T) {
|
||||
listLatestRVs: func(ctx context.Context) (groupResourceRV, error) { return nil, nil },
|
||||
bulkLock: &bulkLock{},
|
||||
tracer: noop.NewTracerProvider().Tracer("test"),
|
||||
log: log.NewNopLogger(),
|
||||
log: &logging.NoOpLogger{},
|
||||
watchBufferSize: 10,
|
||||
pollingInterval: time.Second,
|
||||
dialect: sqltemplate.SQLite,
|
||||
@@ -175,7 +175,7 @@ func TestPollingNotifierConfig(t *testing.T) {
|
||||
listLatestRVs: func(ctx context.Context) (groupResourceRV, error) { return nil, nil },
|
||||
bulkLock: &bulkLock{},
|
||||
tracer: noop.NewTracerProvider().Tracer("test"),
|
||||
log: log.NewNopLogger(),
|
||||
log: &logging.NoOpLogger{},
|
||||
watchBufferSize: 10,
|
||||
pollingInterval: time.Second,
|
||||
done: make(chan struct{}),
|
||||
@@ -243,7 +243,7 @@ func TestPollingNotifier(t *testing.T) {
|
||||
dialect: sqltemplate.SQLite,
|
||||
pollingInterval: 10 * time.Millisecond,
|
||||
watchBufferSize: 10,
|
||||
log: log.NewNopLogger(),
|
||||
log: &logging.NoOpLogger{},
|
||||
tracer: noop.NewTracerProvider().Tracer("test"),
|
||||
bulkLock: &bulkLock{},
|
||||
listLatestRVs: listLatestRVs,
|
||||
@@ -297,7 +297,7 @@ func TestPollingNotifier(t *testing.T) {
|
||||
dialect: sqltemplate.SQLite,
|
||||
pollingInterval: 10 * time.Millisecond,
|
||||
watchBufferSize: 10,
|
||||
log: log.NewNopLogger(),
|
||||
log: &logging.NoOpLogger{},
|
||||
tracer: noop.NewTracerProvider().Tracer("test"),
|
||||
bulkLock: &bulkLock{},
|
||||
listLatestRVs: listLatestRVs,
|
||||
@@ -331,7 +331,7 @@ func TestPollingNotifier(t *testing.T) {
|
||||
dialect: sqltemplate.SQLite,
|
||||
pollingInterval: 10 * time.Millisecond,
|
||||
watchBufferSize: 10,
|
||||
log: log.NewNopLogger(),
|
||||
log: &logging.NoOpLogger{},
|
||||
tracer: noop.NewTracerProvider().Tracer("test"),
|
||||
bulkLock: &bulkLock{},
|
||||
listLatestRVs: func(ctx context.Context) (groupResourceRV, error) { return nil, nil },
|
||||
@@ -368,7 +368,7 @@ func TestPollingNotifier(t *testing.T) {
|
||||
dialect: sqltemplate.SQLite,
|
||||
pollingInterval: 10 * time.Millisecond,
|
||||
watchBufferSize: 10,
|
||||
log: log.NewNopLogger(),
|
||||
log: &logging.NoOpLogger{},
|
||||
tracer: noop.NewTracerProvider().Tracer("test"),
|
||||
bulkLock: &bulkLock{},
|
||||
listLatestRVs: func(ctx context.Context) (groupResourceRV, error) { return nil, nil },
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana-app-sdk/logging"
|
||||
"github.com/grafana/grafana/pkg/storage/unified/resource"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -15,7 +15,7 @@ func TestChannelNotifier(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
|
||||
n := newChannelNotifier(5, log.NewNopLogger())
|
||||
n := newChannelNotifier(5, &logging.NoOpLogger{})
|
||||
|
||||
events, err := n.notify(ctx)
|
||||
require.NoError(t, err)
|
||||
@@ -42,7 +42,7 @@ func TestChannelNotifier(t *testing.T) {
|
||||
|
||||
t.Run("should drop events when buffer is full", func(t *testing.T) {
|
||||
bufferSize := 2
|
||||
n := newChannelNotifier(bufferSize, log.NewNopLogger())
|
||||
n := newChannelNotifier(bufferSize, &logging.NoOpLogger{})
|
||||
|
||||
events, err := n.notify(context.Background())
|
||||
require.NoError(t, err)
|
||||
@@ -58,7 +58,7 @@ func TestChannelNotifier(t *testing.T) {
|
||||
|
||||
t.Run("should close subscriber channels when context cancelled", func(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
n := newChannelNotifier(5, log.NewNopLogger())
|
||||
n := newChannelNotifier(5, &logging.NoOpLogger{})
|
||||
|
||||
events, err := n.notify(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
Reference in New Issue
Block a user