Elasticsearch: Decouple backend from infra/log (#90527)

This commit is contained in:
Nathan Vērzemnieks 2024-07-18 23:51:18 -07:00 committed by GitHub
parent 269d54c984
commit 9caa8151d8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 27 additions and 27 deletions

View File

@ -17,8 +17,8 @@ import (
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
exp "github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/tracing"
)
@ -55,7 +55,7 @@ type Client interface {
// NewClient creates a new elasticsearch client
var NewClient = func(ctx context.Context, ds *DatasourceInfo, logger log.Logger, tracer tracing.Tracer) (Client, error) {
logger = logger.New("entity", "client")
logger = logger.FromContext(ctx).With("entity", "client")
ip, err := newIndexPattern(ds.Interval, ds.Database)
if err != nil {

View File

@ -10,11 +10,11 @@ import (
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/tracing"
)
@ -67,7 +67,7 @@ func TestClient_ExecuteMultisearch(t *testing.T) {
To: to,
}
c, err := NewClient(context.Background(), &ds, log.New("test", "test"), tracing.InitializeTracerForTest())
c, err := NewClient(context.Background(), &ds, log.New(), tracing.InitializeTracerForTest())
require.NoError(t, err)
require.NotNil(t, c)
@ -163,7 +163,7 @@ func TestClient_ExecuteMultisearch(t *testing.T) {
To: to2,
}
c, err := NewClient(context.Background(), &ds, log.New("test", "test"), tracing.InitializeTracerForTest())
c, err := NewClient(context.Background(), &ds, log.New(), tracing.InitializeTracerForTest())
require.NoError(t, err)
require.NotNil(t, c)
@ -260,7 +260,7 @@ func TestClient_Index(t *testing.T) {
To: to,
}
c, err := NewClient(context.Background(), &ds, log.New("test", "test"), tracing.InitializeTracerForTest())
c, err := NewClient(context.Background(), &ds, log.New(), tracing.InitializeTracerForTest())
require.NoError(t, err)
require.NotNil(t, c)

View File

@ -10,10 +10,10 @@ import (
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
"github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/tracing"
es "github.com/grafana/grafana/pkg/tsdb/elasticsearch/client"
)

View File

@ -7,10 +7,10 @@ import (
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/tracing"
es "github.com/grafana/grafana/pkg/tsdb/elasticsearch/client"
)
@ -1862,6 +1862,6 @@ func executeElasticsearchDataQuery(c es.Client, body string, from, to time.Time)
},
},
}
query := newElasticsearchDataQuery(context.Background(), c, &dataRequest, log.New("test.logger"), tracing.InitializeTracerForTest())
query := newElasticsearchDataQuery(context.Background(), c, &dataRequest, log.New(), tracing.InitializeTracerForTest())
return query.execute()
}

View File

@ -18,16 +18,14 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend/datasource"
"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
exp "github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
exphttpclient "github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource/httpclient"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/tracing"
es "github.com/grafana/grafana/pkg/tsdb/elasticsearch/client"
)
var eslog = log.New("tsdb.elasticsearch")
const (
// headerFromExpression is used by data sources to identify expression queries
headerFromExpression = "X-Grafana-From-Expr"
@ -40,21 +38,21 @@ const (
type Service struct {
im instancemgmt.InstanceManager
tracer tracing.Tracer
logger *log.ConcreteLogger
logger log.Logger
}
func ProvideService(httpClientProvider *httpclient.Provider, tracer tracing.Tracer) *Service {
return &Service{
im: datasource.NewInstanceManager(newInstanceSettings(httpClientProvider)),
tracer: tracer,
logger: eslog,
logger: backend.NewLoggerWith("logger", "tsdb.elasticsearch"),
}
}
func (s *Service) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
dsInfo, err := s.getDSInfo(ctx, req.PluginContext)
_, fromAlert := req.Headers[headerFromAlert]
logger := s.logger.FromContext(ctx).New("fromAlert", fromAlert)
logger := s.logger.FromContext(ctx).With("fromAlert", fromAlert)
if err != nil {
logger.Error("Failed to get data source info", "error", err)
@ -193,7 +191,7 @@ func (s *Service) getDSInfo(ctx context.Context, pluginCtx backend.PluginContext
}
func (s *Service) CallResource(ctx context.Context, req *backend.CallResourceRequest, sender backend.CallResourceResponseSender) error {
logger := eslog.FromContext(ctx)
logger := s.logger.FromContext(ctx)
// allowed paths for resource calls:
// - empty string for fetching db version
// - /_mapping for fetching index mapping, e.g. requests going to `index/_mapping`

View File

@ -13,7 +13,7 @@ import (
)
func (s *Service) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
logger := eslog.FromContext(ctx)
logger := s.logger.FromContext(ctx)
ds, err := s.getDSInfo(ctx, req.PluginContext)
if err != nil {

View File

@ -10,6 +10,7 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
es "github.com/grafana/grafana/pkg/tsdb/elasticsearch/client"
"github.com/stretchr/testify/assert"
)
@ -75,6 +76,7 @@ func (*FakeInstanceManager) Do(_ context.Context, _ backend.PluginContext, _ ins
func GetMockService(isDsHealthy bool) *Service {
return &Service{
im: &FakeInstanceManager{isDsHealthy: isDsHealthy},
im: &FakeInstanceManager{isDsHealthy: isDsHealthy},
logger: log.New(),
}
}

View File

@ -2,9 +2,9 @@ package elasticsearch
import (
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/log"
)
func parseQuery(tsdbQuery []backend.DataQuery, logger log.Logger) ([]*Query, error) {

View File

@ -3,7 +3,7 @@ package elasticsearch
import (
"testing"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
"github.com/stretchr/testify/require"
)
@ -61,7 +61,7 @@ func TestParseQuery(t *testing.T) {
}`
dataQuery, err := newDataQuery(body)
require.NoError(t, err)
queries, err := parseQuery(dataQuery.Queries, log.New("test.logger"))
queries, err := parseQuery(dataQuery.Queries, log.New())
require.NoError(t, err)
require.Len(t, queries, 1)

View File

@ -10,8 +10,8 @@ import (
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/tracing"
es "github.com/grafana/grafana/pkg/tsdb/elasticsearch/client"
)
@ -142,7 +142,7 @@ func queryDataTestWithResponseCode(queriesBytes []byte, responseStatusCode int,
return nil
})
result, err := queryData(context.Background(), &req, dsInfo, log.New("test.logger"), tracing.InitializeTracerForTest())
result, err := queryData(context.Background(), &req, dsInfo, log.New(), tracing.InitializeTracerForTest())
if err != nil {
return queryDataTestResult{}, err
}

View File

@ -12,6 +12,7 @@ import (
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
"go.opentelemetry.io/otel/attribute"
@ -19,7 +20,6 @@ import (
"go.opentelemetry.io/otel/trace"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/tracing"
es "github.com/grafana/grafana/pkg/tsdb/elasticsearch/client"
"github.com/grafana/grafana/pkg/tsdb/elasticsearch/instrumentation"

View File

@ -9,12 +9,12 @@ import (
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana-plugin-sdk-go/experimental"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/tracing"
es "github.com/grafana/grafana/pkg/tsdb/elasticsearch/client"
)
@ -3677,12 +3677,12 @@ func parseTestResponse(tsdbQueries map[string]string, responseBody string, keepL
return nil, err
}
queries, err := parseQuery(tsdbQuery.Queries, log.New("test.logger"))
queries, err := parseQuery(tsdbQuery.Queries, log.New())
if err != nil {
return nil, err
}
return parseResponse(context.Background(), response.Responses, queries, configuredFields, keepLabelsInResponse, log.New("test.logger"), tracing.InitializeTracerForTest())
return parseResponse(context.Background(), response.Responses, queries, configuredFields, keepLabelsInResponse, log.New(), tracing.InitializeTracerForTest())
}
func requireTimeValue(t *testing.T, expected int64, frame *data.Frame, index int) {