mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
CloudWatch: Make sure adoption tracking is done on valid, migrated queries (#56872)
* make sure adoption tracking is done on valid, migrated queries * ignore hidden queries * fix test * remove obsolete test
This commit is contained in:
@@ -710,6 +710,30 @@ export const CloudWatchDashboardLoadedEvent = new DashboardLoadedEvent({
|
||||
sqlExpression: '',
|
||||
statistic: 'Average',
|
||||
},
|
||||
{
|
||||
alias: '',
|
||||
datasource: {
|
||||
type: 'cloudwatch',
|
||||
uid: 'abc',
|
||||
},
|
||||
dimensions: {
|
||||
InstanceId: '*',
|
||||
},
|
||||
expression: 'a / ',
|
||||
hide: false,
|
||||
id: '',
|
||||
matchExact: true,
|
||||
metricEditorMode: 0,
|
||||
metricName: 'CPUUtilization',
|
||||
metricQueryType: 0,
|
||||
namespace: 'AWS/EC2',
|
||||
period: '',
|
||||
queryMode: '',
|
||||
refId: 'B',
|
||||
region: 'default',
|
||||
sqlExpression: '',
|
||||
statistic: '',
|
||||
},
|
||||
] as CloudWatchQuery[],
|
||||
},
|
||||
});
|
||||
|
||||
@@ -726,10 +726,6 @@ describe('CloudWatchMetricsQueryRunner', () => {
|
||||
};
|
||||
});
|
||||
|
||||
it('should error if invalid mode', async () => {
|
||||
expect(() => runner.filterMetricQuery(baseQuery)).toThrowError('invalid metric editor mode');
|
||||
});
|
||||
|
||||
describe('metric search queries', () => {
|
||||
beforeEach(() => {
|
||||
baseQuery = {
|
||||
|
||||
@@ -28,11 +28,10 @@ import {
|
||||
CloudWatchMetricsQuery,
|
||||
CloudWatchQuery,
|
||||
DataQueryError,
|
||||
MetricEditorMode,
|
||||
MetricQuery,
|
||||
MetricQueryType,
|
||||
MetricRequest,
|
||||
} from '../types';
|
||||
import { filterMetricsQuery } from '../utils/utils';
|
||||
|
||||
import { CloudWatchRequest } from './CloudWatchRequest';
|
||||
|
||||
@@ -172,21 +171,7 @@ export class CloudWatchMetricsQueryRunner extends CloudWatchRequest {
|
||||
}
|
||||
|
||||
filterMetricQuery(query: CloudWatchMetricsQuery): boolean {
|
||||
const { region, metricQueryType, metricEditorMode, expression, metricName, namespace, sqlExpression, statistic } =
|
||||
query;
|
||||
if (!region) {
|
||||
return false;
|
||||
}
|
||||
if (metricQueryType === MetricQueryType.Search && metricEditorMode === MetricEditorMode.Builder) {
|
||||
return !!namespace && !!metricName && !!statistic;
|
||||
} else if (metricQueryType === MetricQueryType.Search && metricEditorMode === MetricEditorMode.Code) {
|
||||
return !!expression;
|
||||
} else if (metricQueryType === MetricQueryType.Query) {
|
||||
// still TBD how to validate the visual query builder for SQL
|
||||
return !!sqlExpression;
|
||||
}
|
||||
|
||||
throw new Error('invalid metric editor mode');
|
||||
return filterMetricsQuery(query);
|
||||
}
|
||||
|
||||
replaceMetricQueryVars(
|
||||
|
||||
@@ -26,14 +26,14 @@ describe('onDashboardLoadedHandler', () => {
|
||||
grafana_version: 'v9.0.0',
|
||||
org_id: 1,
|
||||
logs_queries_count: 1,
|
||||
metrics_queries_count: 21,
|
||||
metrics_queries_count: 20,
|
||||
metrics_query_builder_count: 3,
|
||||
metrics_query_code_count: 4,
|
||||
metrics_query_count: 7,
|
||||
metrics_search_builder_count: 9,
|
||||
metrics_search_builder_count: 8,
|
||||
metrics_search_code_count: 5,
|
||||
metrics_search_count: 14,
|
||||
metrics_search_match_exact_count: 9,
|
||||
metrics_search_count: 13,
|
||||
metrics_search_match_exact_count: 8,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,8 +2,16 @@ import { DashboardLoadedEvent } from '@grafana/data';
|
||||
import { reportInteraction } from '@grafana/runtime';
|
||||
|
||||
import { isCloudWatchLogsQuery, isCloudWatchMetricsQuery } from './guards';
|
||||
import { migrateMetricQuery } from './migrations/metricQueryMigrations';
|
||||
import pluginJson from './plugin.json';
|
||||
import { CloudWatchMetricsQuery, CloudWatchQuery, MetricEditorMode, MetricQueryType } from './types';
|
||||
import {
|
||||
CloudWatchLogsQuery,
|
||||
CloudWatchMetricsQuery,
|
||||
CloudWatchQuery,
|
||||
MetricEditorMode,
|
||||
MetricQueryType,
|
||||
} from './types';
|
||||
import { filterMetricsQuery } from './utils/utils';
|
||||
|
||||
interface CloudWatchOnDashboardLoadedTrackingEvent {
|
||||
grafana_version?: string;
|
||||
@@ -56,8 +64,21 @@ export const onDashboardLoadedHandler = ({
|
||||
return;
|
||||
}
|
||||
|
||||
const logsQueries = cloudWatchQueries.filter(isCloudWatchLogsQuery);
|
||||
const metricsQueries = cloudWatchQueries.filter(isCloudWatchMetricsQuery);
|
||||
let logsQueries: CloudWatchLogsQuery[] = [];
|
||||
let metricsQueries: CloudWatchMetricsQuery[] = [];
|
||||
|
||||
for (const query of cloudWatchQueries) {
|
||||
if (query.hide) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isCloudWatchLogsQuery(query)) {
|
||||
query.logGroupNames?.length && logsQueries.push(query);
|
||||
} else if (isCloudWatchMetricsQuery(query)) {
|
||||
const migratedQuery = migrateMetricQuery(query);
|
||||
filterMetricsQuery(migratedQuery) && metricsQueries.push(query);
|
||||
}
|
||||
}
|
||||
|
||||
const e: CloudWatchOnDashboardLoadedTrackingEvent = {
|
||||
grafana_version: grafanaVersion,
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { SelectableValue } from '@grafana/data';
|
||||
|
||||
import { CloudWatchMetricsQuery, MetricQueryType, MetricEditorMode } from '../types';
|
||||
|
||||
import { CloudWatchDatasource } from './../datasource';
|
||||
|
||||
export const toOption = (value: string) => ({ label: value, value });
|
||||
@@ -8,3 +10,21 @@ export const appendTemplateVariables = (datasource: CloudWatchDatasource, values
|
||||
...values,
|
||||
{ label: 'Template Variables', options: datasource.getVariables().map(toOption) },
|
||||
];
|
||||
|
||||
export const filterMetricsQuery = (query: CloudWatchMetricsQuery): boolean => {
|
||||
const { region, metricQueryType, metricEditorMode, expression, metricName, namespace, sqlExpression, statistic } =
|
||||
query;
|
||||
if (!region) {
|
||||
return false;
|
||||
}
|
||||
if (metricQueryType === MetricQueryType.Search && metricEditorMode === MetricEditorMode.Builder) {
|
||||
return !!namespace && !!metricName && !!statistic;
|
||||
} else if (metricQueryType === MetricQueryType.Search && metricEditorMode === MetricEditorMode.Code) {
|
||||
return !!expression;
|
||||
} else if (metricQueryType === MetricQueryType.Query) {
|
||||
// still TBD how to validate the visual query builder for SQL
|
||||
return !!sqlExpression;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user