CloudWatch: small logs refactors (#44677)

This commit is contained in:
Isabella Siu 2022-01-31 17:30:16 -05:00 committed by GitHub
parent 4f44ff7ce8
commit cbbf451b36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 22 deletions

View File

@ -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

View File

@ -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)),
}

View File

@ -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<CloudWatchQuery>): Observable<DataQueryResponse> {
options = angular.copy(options);
options = cloneDeep(options);
let queries = options.targets.filter((item) => item.id !== '' || item.hide !== true);
const { logQueries, metricsQueries } = this.getTargetsByQueryMode(queries);