Chore: Add context to datasource service (#42294)

* Add context to datasource service

* Adjust wire for ShouldBeReported method

* Replace inTransactionCtx
This commit is contained in:
idafurjes
2021-11-26 18:10:36 +01:00
committed by GitHub
parent 6aa05c5d05
commit 725dbf8d95
12 changed files with 69 additions and 64 deletions
+3 -3
View File
@@ -33,7 +33,7 @@ func (e *AlertEngine) QueryUsageStats(ctx context.Context) (*UsageStats, error)
return nil, err
}
dsUsage, err := e.mapRulesToUsageStats(cmd.Result)
dsUsage, err := e.mapRulesToUsageStats(ctx, cmd.Result)
if err != nil {
return nil, err
}
@@ -43,7 +43,7 @@ func (e *AlertEngine) QueryUsageStats(ctx context.Context) (*UsageStats, error)
}, nil
}
func (e *AlertEngine) mapRulesToUsageStats(rules []*models.Alert) (DatasourceAlertUsage, error) {
func (e *AlertEngine) mapRulesToUsageStats(ctx context.Context, rules []*models.Alert) (DatasourceAlertUsage, error) {
// map of datasourceId type and frequency
typeCount := map[int64]int{}
for _, a := range rules {
@@ -63,7 +63,7 @@ func (e *AlertEngine) mapRulesToUsageStats(rules []*models.Alert) (DatasourceAle
result := map[string]int{}
for k, v := range typeCount {
query := &models.GetDataSourceQuery{Id: k}
err := e.Bus.DispatchCtx(context.TODO(), query)
err := e.Bus.DispatchCtx(ctx, query)
if err != nil {
return map[string]int{}, nil
}
+1 -1
View File
@@ -257,7 +257,7 @@ func (e *AlertEngine) registerUsageMetrics() {
metrics := map[string]interface{}{}
for dsType, usageCount := range alertingUsageStats.DatasourceUsage {
if e.usageStatsService.ShouldBeReported(dsType) {
if e.usageStatsService.ShouldBeReported(ctx, dsType) {
metrics[fmt.Sprintf("stats.alerting.ds.%s.count", dsType)] = usageCount
} else {
alertingOtherCount += usageCount
+5 -5
View File
@@ -30,7 +30,7 @@ func NewDashAlertExtractor(dash *models.Dashboard, orgID int64, user *models.Sig
}
}
func (e *DashAlertExtractor) lookupQueryDataSource(panel *simplejson.Json, panelQuery *simplejson.Json) (*models.DataSource, error) {
func (e *DashAlertExtractor) lookupQueryDataSource(ctx context.Context, panel *simplejson.Json, panelQuery *simplejson.Json) (*models.DataSource, error) {
dsName := ""
dsUid := ""
@@ -48,14 +48,14 @@ func (e *DashAlertExtractor) lookupQueryDataSource(panel *simplejson.Json, panel
if dsName == "" && dsUid == "" {
query := &models.GetDefaultDataSourceQuery{OrgId: e.OrgID}
if err := bus.DispatchCtx(context.TODO(), query); err != nil {
if err := bus.DispatchCtx(ctx, query); err != nil {
return nil, err
}
return query.Result, nil
}
query := &models.GetDataSourceQuery{Name: dsName, Uid: dsUid, OrgId: e.OrgID}
if err := bus.DispatchCtx(context.TODO(), query); err != nil {
if err := bus.DispatchCtx(ctx, query); err != nil {
return nil, err
}
@@ -174,7 +174,7 @@ func (e *DashAlertExtractor) getAlertFromPanels(ctx context.Context, jsonWithPan
return nil, ValidationError{Reason: reason}
}
datasource, err := e.lookupQueryDataSource(panel, panelQuery)
datasource, err := e.lookupQueryDataSource(ctx, panel, panelQuery)
if err != nil {
return nil, err
}
@@ -184,7 +184,7 @@ func (e *DashAlertExtractor) getAlertFromPanels(ctx context.Context, jsonWithPan
Datasources: []*models.DataSource{datasource},
}
if err := bus.Dispatch(&dsFilterQuery); err != nil {
if err := bus.DispatchCtx(ctx, &dsFilterQuery); err != nil {
if !errors.Is(err, bus.ErrHandlerNotFound) {
return nil, err
}