diff --git a/pkg/tsdb/cloudwatch/cloudwatch.go b/pkg/tsdb/cloudwatch/cloudwatch.go index ee6eb400207..66a2d6c4789 100644 --- a/pkg/tsdb/cloudwatch/cloudwatch.go +++ b/pkg/tsdb/cloudwatch/cloudwatch.go @@ -47,12 +47,17 @@ type datasourceInfo struct { HTTPClient *http.Client } -const cloudWatchTSFormat = "2006-01-02 15:04:05.000" -const defaultRegion = "default" +const ( + cloudWatchTSFormat = "2006-01-02 15:04:05.000" + defaultRegion = "default" -// Constants also defined in datasource/cloudwatch/datasource.ts -const logIdentifierInternal = "__log__grafana_internal__" -const logStreamIdentifierInternal = "__logstream__grafana_internal__" + // Constants also defined in datasource/cloudwatch/datasource.ts + logIdentifierInternal = "__log__grafana_internal__" + logStreamIdentifierInternal = "__logstream__grafana_internal__" + + alertMaxAttempts = 8 + alertPollPeriod = 1000 * time.Millisecond +) var plog = log.New("tsdb.cloudwatch") var aliasFormat = regexp.MustCompile(`\{\{\s*(.+?)\s*\}\}`) @@ -222,9 +227,6 @@ func (e *cloudWatchExecutor) getRGTAClient(region string, pluginCtx backend.Plug func (e *cloudWatchExecutor) alertQuery(ctx context.Context, logsClient cloudwatchlogsiface.CloudWatchLogsAPI, queryContext backend.DataQuery, model *simplejson.Json) (*cloudwatchlogs.GetQueryResultsOutput, error) { - const maxAttempts = 8 - const pollPeriod = 1000 * time.Millisecond - startQueryOutput, err := e.executeStartQuery(ctx, logsClient, model, queryContext.TimeRange) if err != nil { return nil, err @@ -235,7 +237,7 @@ func (e *cloudWatchExecutor) alertQuery(ctx context.Context, logsClient cloudwat "queryId": *startQueryOutput.QueryId, }) - ticker := time.NewTicker(pollPeriod) + ticker := time.NewTicker(alertPollPeriod) defer ticker.Stop() attemptCount := 1 @@ -247,7 +249,7 @@ func (e *cloudWatchExecutor) alertQuery(ctx context.Context, logsClient cloudwat if isTerminated(*res.Status) { return res, err } - if attemptCount >= maxAttempts { + if attemptCount >= alertMaxAttempts { return res, fmt.Errorf("fetching of query results exceeded max number of attempts") } @@ -322,13 +324,6 @@ func (e *cloudWatchExecutor) executeLogAlertQuery(ctx context.Context, req *back return nil, err } - result, err := e.executeStartQuery(ctx, logsClient, model, q.TimeRange) - if err != nil { - return nil, err - } - - model.Set("queryId", *result.QueryId) - getQueryResultsOutput, err := e.alertQuery(ctx, logsClient, q, model) if err != nil { return nil, err diff --git a/pkg/tsdb/cloudwatch/log_actions.go b/pkg/tsdb/cloudwatch/log_actions.go index a9099d6cb87..cd553b23b14 100644 --- a/pkg/tsdb/cloudwatch/log_actions.go +++ b/pkg/tsdb/cloudwatch/log_actions.go @@ -18,7 +18,10 @@ import ( "golang.org/x/sync/errgroup" ) -var LimitExceededException = "LimitExceededException" +const ( + LimitExceededException = "LimitExceededException" + defaultLimit = 10 +) type AWSError struct { Code string @@ -126,7 +129,7 @@ func (e *cloudWatchExecutor) executeLogAction(ctx context.Context, model *simple func (e *cloudWatchExecutor) handleGetLogEvents(ctx context.Context, logsClient cloudwatchlogsiface.CloudWatchLogsAPI, parameters *simplejson.Json) (*data.Frame, error) { queryRequest := &cloudwatchlogs.GetLogEventsInput{ - Limit: aws.Int64(parameters.Get("limit").MustInt64(10)), + Limit: aws.Int64(parameters.Get("limit").MustInt64(defaultLimit)), StartFromHead: aws.Bool(parameters.Get("startFromHead").MustBool(false)), } diff --git a/public/app/plugins/datasource/cloudwatch/datasource.ts b/public/app/plugins/datasource/cloudwatch/datasource.ts index e30e77c081e..3ff9da21226 100644 --- a/public/app/plugins/datasource/cloudwatch/datasource.ts +++ b/public/app/plugins/datasource/cloudwatch/datasource.ts @@ -1,6 +1,5 @@ import React from 'react'; -import angular from 'angular'; -import { find, findLast, isEmpty, isString, set } from 'lodash'; +import { cloneDeep, find, findLast, isEmpty, isString, set } from 'lodash'; import { from, lastValueFrom, merge, Observable, of, throwError, zip } from 'rxjs'; import { catchError, concatMap, finalize, map, mergeMap, repeat, scan, share, takeWhile, tap } from 'rxjs/operators'; import { DataSourceWithBackend, FetchError, getBackendSrv, toDataQueryResponse } from '@grafana/runtime'; @@ -123,7 +122,7 @@ export class CloudWatchDatasource } query(options: DataQueryRequest): Observable { - options = angular.copy(options); + options = cloneDeep(options); let queries = options.targets.filter((item) => item.id !== '' || item.hide !== true); const { logQueries, metricsQueries } = this.getTargetsByQueryMode(queries);