datasources: querier: report metrics related to the whole request (#109384)
* refactor * add metric
This commit is contained in:
@@ -25,7 +25,7 @@ type singleTenantInstance struct {
|
||||
cfg *setting.Cfg
|
||||
}
|
||||
|
||||
func (t *singleTenantInstance) GetDataSourceClient(_ context.Context, _ data.DataSourceRef, _ map[string]string) (clientapi.QueryDataClient, error) {
|
||||
func (t *singleTenantInstance) GetDataSourceClient(_ context.Context, _ data.DataSourceRef) (clientapi.QueryDataClient, error) {
|
||||
return t.client, nil
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ func NewSingleTenantInstanceProvider(cfg *setting.Cfg, features featuremgmt.Feat
|
||||
}
|
||||
}
|
||||
|
||||
func (s *singleTenantInstanceProvider) GetInstance(_ context.Context) (clientapi.Instance, error) {
|
||||
func (s *singleTenantInstanceProvider) GetInstance(_ context.Context, _ map[string]string) (clientapi.Instance, error) {
|
||||
return &singleTenantInstance{
|
||||
client: s.client,
|
||||
features: s.features,
|
||||
@@ -59,3 +59,7 @@ func (s *singleTenantInstance) GetLogger(parent log.Logger) log.Logger {
|
||||
// currently we do not add any extra info
|
||||
return parent.New()
|
||||
}
|
||||
|
||||
func (s *singleTenantInstance) ReportMetrics() {
|
||||
// we do not report any metrics currently
|
||||
}
|
||||
|
||||
@@ -29,12 +29,13 @@ type InstanceConfigurationSettings struct {
|
||||
}
|
||||
|
||||
type Instance interface {
|
||||
GetDataSourceClient(ctx context.Context, ref data.DataSourceRef, headers map[string]string) (QueryDataClient, error)
|
||||
GetDataSourceClient(ctx context.Context, ref data.DataSourceRef) (QueryDataClient, error)
|
||||
// fetch information on the grafana instance (e.g. feature toggles)
|
||||
GetSettings() InstanceConfigurationSettings
|
||||
GetLogger(parent log.Logger) log.Logger
|
||||
ReportMetrics() // some metrics are only reported at the end
|
||||
}
|
||||
|
||||
type InstanceProvider interface {
|
||||
GetInstance(ctx context.Context) (Instance, error)
|
||||
GetInstance(ctx context.Context, headers map[string]string) (Instance, error)
|
||||
}
|
||||
|
||||
@@ -246,7 +246,7 @@ func handleQuery(ctx context.Context, raw query.QueryDataRequest, b QueryAPIBuil
|
||||
|
||||
headers := ExtractKnownHeaders(httpreq.Header)
|
||||
|
||||
instance, err := b.instanceProvider.GetInstance(ctx)
|
||||
instance, err := b.instanceProvider.GetInstance(ctx, headers)
|
||||
if err != nil {
|
||||
connectLogger.Error("failed to get instance configuration settings", "err", err)
|
||||
responder.Error(err)
|
||||
@@ -260,7 +260,6 @@ func handleQuery(ctx context.Context, raw query.QueryDataRequest, b QueryAPIBuil
|
||||
mtDsClientBuilder := mtdsclient.NewMtDatasourceClientBuilderWithInstance(
|
||||
instance,
|
||||
ctx,
|
||||
headers,
|
||||
dsQuerierLoggerWithSlug,
|
||||
)
|
||||
|
||||
@@ -281,6 +280,10 @@ func handleQuery(ctx context.Context, raw query.QueryDataRequest, b QueryAPIBuil
|
||||
|
||||
qdr, err := service.QueryData(ctx, dsQuerierLoggerWithSlug, cache, exprService, mReq, mtDsClientBuilder, headers)
|
||||
|
||||
// tell the `instance` structure that it can now report
|
||||
// metrics that are only reported once during a request
|
||||
instance.ReportMetrics()
|
||||
|
||||
if err != nil {
|
||||
return qdr, err
|
||||
}
|
||||
|
||||
@@ -250,18 +250,21 @@ type mockClient struct {
|
||||
stubbedFrame *data.Frame
|
||||
}
|
||||
|
||||
func (m mockClient) GetInstance(ctx context.Context) (clientapi.Instance, error) {
|
||||
func (m mockClient) GetInstance(ctx context.Context, headers map[string]string) (clientapi.Instance, error) {
|
||||
mclient := mockClient{
|
||||
stubbedFrame: m.stubbedFrame,
|
||||
}
|
||||
return mclient, nil
|
||||
}
|
||||
|
||||
func (m mockClient) ReportMetrics() {
|
||||
}
|
||||
|
||||
func (m mockClient) GetLogger(parent log.Logger) log.Logger {
|
||||
return parent.New()
|
||||
}
|
||||
|
||||
func (m mockClient) GetDataSourceClient(ctx context.Context, ref dataapi.DataSourceRef, headers map[string]string) (clientapi.QueryDataClient, error) {
|
||||
func (m mockClient) GetDataSourceClient(ctx context.Context, ref dataapi.DataSourceRef) (clientapi.QueryDataClient, error) {
|
||||
mclient := mockClient{
|
||||
stubbedFrame: m.stubbedFrame,
|
||||
}
|
||||
|
||||
@@ -26,7 +26,6 @@ func NewNullMTDatasourceClientBuilder() MTDatasourceClientBuilder {
|
||||
type MtDatasourceClientBuilderWithInstance struct {
|
||||
instance clientapi.Instance
|
||||
ctx context.Context
|
||||
headers map[string]string
|
||||
logger log.Logger
|
||||
}
|
||||
|
||||
@@ -37,7 +36,6 @@ func (b *MtDatasourceClientBuilderWithInstance) BuildClient(pluginId string, uid
|
||||
Type: pluginId,
|
||||
UID: uid,
|
||||
},
|
||||
b.headers,
|
||||
)
|
||||
if err != nil {
|
||||
b.logger.Debug("failed to get mt ds client", "error", err)
|
||||
@@ -50,13 +48,11 @@ func (b *MtDatasourceClientBuilderWithInstance) BuildClient(pluginId string, uid
|
||||
func NewMtDatasourceClientBuilderWithInstance(
|
||||
instance clientapi.Instance,
|
||||
ctx context.Context,
|
||||
headers map[string]string,
|
||||
logger log.Logger,
|
||||
) MTDatasourceClientBuilder {
|
||||
return &MtDatasourceClientBuilderWithInstance{
|
||||
instance: instance,
|
||||
ctx: ctx,
|
||||
headers: headers,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user