2022-09-19 08:50:41 +02:00
|
|
|
import { cloneDeep, find, isEmpty } from 'lodash';
|
|
|
|
|
import { merge, Observable, of } from 'rxjs';
|
2022-04-22 14:33:13 +01:00
|
|
|
|
2019-10-31 10:48:05 +01:00
|
|
|
import {
|
2020-06-04 13:44:48 +02:00
|
|
|
DataFrame,
|
2020-03-24 16:03:53 +01:00
|
|
|
DataQueryRequest,
|
2020-06-04 13:44:48 +02:00
|
|
|
DataQueryResponse,
|
2020-03-24 16:03:53 +01:00
|
|
|
DataSourceInstanceSettings,
|
2021-10-01 13:38:16 +02:00
|
|
|
DataSourceWithLogsContextSupport,
|
2020-06-04 13:44:48 +02:00
|
|
|
LoadingState,
|
|
|
|
|
LogRowModel,
|
2019-10-31 10:48:05 +01:00
|
|
|
ScopedVars,
|
|
|
|
|
} from '@grafana/data';
|
2022-09-19 08:50:41 +02:00
|
|
|
import { DataSourceWithBackend } from '@grafana/runtime';
|
2020-10-01 18:51:23 +01:00
|
|
|
import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv';
|
2022-02-28 09:28:27 +01:00
|
|
|
import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv';
|
|
|
|
|
|
2022-09-19 10:51:46 +02:00
|
|
|
import { RowContextOptions } from '../../../features/logs/components/LogRowContextProvider';
|
|
|
|
|
|
2022-04-27 12:41:48 +02:00
|
|
|
import { CloudWatchAnnotationSupport } from './annotationSupport';
|
2022-09-20 07:50:54 +02:00
|
|
|
import { CloudWatchAPI } from './api';
|
2022-02-28 09:28:27 +01:00
|
|
|
import { SQLCompletionItemProvider } from './cloudwatch-sql/completion/CompletionItemProvider';
|
2022-04-27 12:41:48 +02:00
|
|
|
import { isCloudWatchAnnotationQuery, isCloudWatchLogsQuery, isCloudWatchMetricsQuery } from './guards';
|
2022-02-28 09:28:27 +01:00
|
|
|
import { CloudWatchLanguageProvider } from './language_provider';
|
|
|
|
|
import { MetricMathCompletionItemProvider } from './metric-math/completion/CompletionItemProvider';
|
2022-09-19 08:50:41 +02:00
|
|
|
import { CloudWatchAnnotationQueryRunner } from './query-runner/CloudWatchAnnotationQueryRunner';
|
|
|
|
|
import { CloudWatchLogsQueryRunner } from './query-runner/CloudWatchLogsQueryRunner';
|
|
|
|
|
import { CloudWatchMetricsQueryRunner } from './query-runner/CloudWatchMetricsQueryRunner';
|
2020-04-25 21:48:20 +01:00
|
|
|
import {
|
2022-04-27 12:41:48 +02:00
|
|
|
CloudWatchAnnotationQuery,
|
2020-04-25 21:48:20 +01:00
|
|
|
CloudWatchJsonData,
|
|
|
|
|
CloudWatchLogsQuery,
|
2020-06-04 13:44:48 +02:00
|
|
|
CloudWatchMetricsQuery,
|
|
|
|
|
CloudWatchQuery,
|
2020-04-25 21:48:20 +01:00
|
|
|
} from './types';
|
2022-04-04 10:39:31 -04:00
|
|
|
import { CloudWatchVariableSupport } from './variables';
|
2019-11-14 10:59:41 +01:00
|
|
|
|
2021-10-01 13:38:16 +02:00
|
|
|
export class CloudWatchDatasource
|
|
|
|
|
extends DataSourceWithBackend<CloudWatchQuery, CloudWatchJsonData>
|
2022-07-19 13:59:30 -04:00
|
|
|
implements DataSourceWithLogsContextSupport<CloudWatchLogsQuery>
|
2022-02-02 12:02:32 +00:00
|
|
|
{
|
2022-09-20 07:50:54 +02:00
|
|
|
defaultRegion?: string;
|
2020-04-25 21:48:20 +01:00
|
|
|
languageProvider: CloudWatchLanguageProvider;
|
2022-02-01 22:53:32 -05:00
|
|
|
sqlCompletionItemProvider: SQLCompletionItemProvider;
|
|
|
|
|
metricMathCompletionItemProvider: MetricMathCompletionItemProvider;
|
|
|
|
|
|
2020-10-28 08:36:57 +00:00
|
|
|
type = 'cloudwatch';
|
2021-11-30 10:53:31 +01:00
|
|
|
|
2022-09-19 08:50:41 +02:00
|
|
|
private metricsQueryRunner: CloudWatchMetricsQueryRunner;
|
|
|
|
|
private annotationQueryRunner: CloudWatchAnnotationQueryRunner;
|
|
|
|
|
logsQueryRunner: CloudWatchLogsQueryRunner;
|
2022-09-20 07:50:54 +02:00
|
|
|
api: CloudWatchAPI;
|
2020-10-28 08:36:57 +00:00
|
|
|
|
2019-05-08 00:37:50 -07:00
|
|
|
constructor(
|
2019-11-14 10:59:41 +01:00
|
|
|
instanceSettings: DataSourceInstanceSettings<CloudWatchJsonData>,
|
2020-10-01 18:51:23 +01:00
|
|
|
private readonly templateSrv: TemplateSrv = getTemplateSrv(),
|
2022-09-19 08:50:41 +02:00
|
|
|
timeSrv: TimeSrv = getTimeSrv()
|
2019-05-08 00:37:50 -07:00
|
|
|
) {
|
2019-05-10 02:37:43 -07:00
|
|
|
super(instanceSettings);
|
2017-12-27 13:25:40 +01:00
|
|
|
this.defaultRegion = instanceSettings.jsonData.defaultRegion;
|
2022-09-20 07:50:54 +02:00
|
|
|
this.api = new CloudWatchAPI(instanceSettings, templateSrv);
|
2020-04-25 21:48:20 +01:00
|
|
|
this.languageProvider = new CloudWatchLanguageProvider(this);
|
2022-09-20 07:50:54 +02:00
|
|
|
this.sqlCompletionItemProvider = new SQLCompletionItemProvider(this.api, this.templateSrv);
|
|
|
|
|
this.metricMathCompletionItemProvider = new MetricMathCompletionItemProvider(this.api, this.templateSrv);
|
2022-09-19 08:50:41 +02:00
|
|
|
this.metricsQueryRunner = new CloudWatchMetricsQueryRunner(instanceSettings, templateSrv);
|
|
|
|
|
this.logsQueryRunner = new CloudWatchLogsQueryRunner(instanceSettings, templateSrv, timeSrv);
|
|
|
|
|
this.annotationQueryRunner = new CloudWatchAnnotationQueryRunner(instanceSettings, templateSrv);
|
2022-09-20 07:50:54 +02:00
|
|
|
this.variables = new CloudWatchVariableSupport(this.api, this.logsQueryRunner);
|
|
|
|
|
this.annotations = CloudWatchAnnotationSupport;
|
2017-12-27 13:25:40 +01:00
|
|
|
}
|
|
|
|
|
|
2022-06-17 10:24:38 +02:00
|
|
|
filterQuery(query: CloudWatchQuery) {
|
|
|
|
|
return query.hide !== true || (isCloudWatchMetricsQuery(query) && query.id !== '');
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-09 13:11:13 +01:00
|
|
|
query(options: DataQueryRequest<CloudWatchQuery>): Observable<DataQueryResponse> {
|
2022-01-31 17:30:16 -05:00
|
|
|
options = cloneDeep(options);
|
2017-12-27 13:25:40 +01:00
|
|
|
|
2022-06-17 10:24:38 +02:00
|
|
|
let queries = options.targets.filter(this.filterQuery);
|
2022-09-20 07:50:54 +02:00
|
|
|
|
|
|
|
|
const logQueries: CloudWatchLogsQuery[] = [];
|
|
|
|
|
const metricsQueries: CloudWatchMetricsQuery[] = [];
|
|
|
|
|
const annotationQueries: CloudWatchAnnotationQuery[] = [];
|
|
|
|
|
|
|
|
|
|
queries.forEach((query) => {
|
|
|
|
|
if (isCloudWatchAnnotationQuery(query)) {
|
|
|
|
|
annotationQueries.push(query);
|
|
|
|
|
} else if (isCloudWatchLogsQuery(query)) {
|
|
|
|
|
logQueries.push(query);
|
|
|
|
|
} else {
|
|
|
|
|
metricsQueries.push(query);
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-05-11 12:53:42 +02:00
|
|
|
|
2020-07-09 13:11:13 +01:00
|
|
|
const dataQueryResponses: Array<Observable<DataQueryResponse>> = [];
|
2022-09-20 07:50:54 +02:00
|
|
|
if (logQueries.length) {
|
2022-09-19 08:50:41 +02:00
|
|
|
dataQueryResponses.push(this.logsQueryRunner.handleLogQueries(logQueries, options));
|
2020-07-09 13:11:13 +01:00
|
|
|
}
|
2020-05-11 12:53:42 +02:00
|
|
|
|
2022-09-20 07:50:54 +02:00
|
|
|
if (metricsQueries.length) {
|
2022-09-19 08:50:41 +02:00
|
|
|
dataQueryResponses.push(this.metricsQueryRunner.handleMetricQueries(metricsQueries, options));
|
2020-07-09 13:11:13 +01:00
|
|
|
}
|
2020-05-11 12:53:42 +02:00
|
|
|
|
2022-09-20 07:50:54 +02:00
|
|
|
if (annotationQueries.length) {
|
2022-09-19 08:50:41 +02:00
|
|
|
dataQueryResponses.push(this.annotationQueryRunner.handleAnnotationQuery(annotationQueries, options));
|
2022-04-27 12:41:48 +02:00
|
|
|
}
|
2020-07-09 13:11:13 +01:00
|
|
|
// No valid targets, return the empty result to save a round trip.
|
2021-04-21 08:38:00 +01:00
|
|
|
if (isEmpty(dataQueryResponses)) {
|
2020-07-09 13:11:13 +01:00
|
|
|
return of({
|
|
|
|
|
data: [],
|
|
|
|
|
state: LoadingState.Done,
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-05-11 12:53:42 +02:00
|
|
|
|
2020-07-09 13:11:13 +01:00
|
|
|
return merge(...dataQueryResponses);
|
|
|
|
|
}
|
2020-04-25 21:48:20 +01:00
|
|
|
|
2022-09-19 08:50:41 +02:00
|
|
|
interpolateVariablesInQueries(queries: CloudWatchQuery[], scopedVars: ScopedVars): CloudWatchQuery[] {
|
|
|
|
|
if (!queries.length) {
|
|
|
|
|
return queries;
|
2020-10-28 08:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
2022-09-19 08:50:41 +02:00
|
|
|
return queries.map((query) => ({
|
|
|
|
|
...query,
|
2022-09-20 07:50:54 +02:00
|
|
|
region: this.metricsQueryRunner.replaceVariableAndDisplayWarningIfMulti(
|
|
|
|
|
this.getActualRegion(query.region),
|
|
|
|
|
scopedVars
|
2021-09-15 16:28:34 +02:00
|
|
|
),
|
2022-09-19 08:50:41 +02:00
|
|
|
...(isCloudWatchMetricsQuery(query) &&
|
|
|
|
|
this.metricsQueryRunner.interpolateMetricsQueryVariables(query, scopedVars)),
|
|
|
|
|
}));
|
2022-05-03 13:52:17 +02:00
|
|
|
}
|
|
|
|
|
|
2022-09-19 08:50:41 +02:00
|
|
|
getLogRowContext = async (
|
|
|
|
|
row: LogRowModel,
|
|
|
|
|
context?: RowContextOptions,
|
|
|
|
|
query?: CloudWatchLogsQuery
|
|
|
|
|
): Promise<{ data: DataFrame[] }> => {
|
|
|
|
|
return this.logsQueryRunner.getLogRowContext(row, context, query);
|
2020-07-09 13:11:13 +01:00
|
|
|
};
|
2017-12-27 13:25:40 +01:00
|
|
|
|
2022-09-19 08:50:41 +02:00
|
|
|
targetContainsTemplate(target: any) {
|
|
|
|
|
return (
|
|
|
|
|
this.templateSrv.containsTemplate(target.region) ||
|
|
|
|
|
this.templateSrv.containsTemplate(target.namespace) ||
|
|
|
|
|
this.templateSrv.containsTemplate(target.metricName) ||
|
|
|
|
|
this.templateSrv.containsTemplate(target.expression!) ||
|
|
|
|
|
target.logGroupNames?.some((logGroup: string) => this.templateSrv.containsTemplate(logGroup)) ||
|
|
|
|
|
find(target.dimensions, (v, k) => this.templateSrv.containsTemplate(k) || this.templateSrv.containsTemplate(v))
|
2020-04-25 21:48:20 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-19 08:50:41 +02:00
|
|
|
showContextToggle() {
|
|
|
|
|
return true;
|
2020-04-25 21:48:20 +01:00
|
|
|
}
|
|
|
|
|
|
2022-09-19 08:50:41 +02:00
|
|
|
getQueryDisplayText(query: CloudWatchQuery) {
|
|
|
|
|
if (query.queryMode === 'Logs') {
|
|
|
|
|
return query.expression ?? '';
|
2020-04-25 21:48:20 +01:00
|
|
|
} else {
|
2022-09-19 08:50:41 +02:00
|
|
|
return JSON.stringify(query);
|
2020-04-25 21:48:20 +01:00
|
|
|
}
|
2022-09-19 08:50:41 +02:00
|
|
|
}
|
2020-04-25 21:48:20 +01:00
|
|
|
|
2022-09-19 08:50:41 +02:00
|
|
|
// public
|
2020-11-18 15:10:32 +01:00
|
|
|
getVariables() {
|
2021-01-20 07:59:48 +01:00
|
|
|
return this.templateSrv.getVariables().map((v) => `$${v.name}`);
|
2019-11-14 10:59:41 +01:00
|
|
|
}
|
|
|
|
|
|
2022-09-19 08:50:41 +02:00
|
|
|
getActualRegion(region?: string) {
|
|
|
|
|
if (region === 'default' || region === undefined || region === '') {
|
2022-09-21 10:55:54 +02:00
|
|
|
return this.defaultRegion ?? '';
|
2022-09-19 08:50:41 +02:00
|
|
|
}
|
|
|
|
|
return region;
|
2017-12-27 13:25:40 +01:00
|
|
|
}
|
2020-04-25 21:48:20 +01:00
|
|
|
}
|