2022-06-20 05:33:13 -05:00
|
|
|
import { cloneDeep } from 'lodash';
|
2022-04-22 08:33:13 -05:00
|
|
|
import { forkJoin, Observable, of } from 'rxjs';
|
|
|
|
import { map } from 'rxjs/operators';
|
|
|
|
|
2020-06-01 11:37:39 -05:00
|
|
|
import {
|
2020-10-13 23:48:39 -05:00
|
|
|
DataFrame,
|
2020-06-01 11:37:39 -05:00
|
|
|
DataQueryRequest,
|
2020-10-13 23:48:39 -05:00
|
|
|
DataQueryResponse,
|
2020-10-12 07:59:21 -05:00
|
|
|
DataSourceInstanceSettings,
|
2020-06-25 11:48:18 -05:00
|
|
|
LoadingState,
|
2020-09-09 04:00:43 -05:00
|
|
|
ScopedVars,
|
2020-06-01 11:37:39 -05:00
|
|
|
} from '@grafana/data';
|
2022-06-20 05:33:13 -05:00
|
|
|
import { DataSourceWithBackend } from '@grafana/runtime';
|
2021-11-05 06:21:28 -05:00
|
|
|
import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv';
|
2022-03-03 10:16:10 -06:00
|
|
|
|
|
|
|
import AzureLogAnalyticsDatasource from './azure_log_analytics/azure_log_analytics_datasource';
|
|
|
|
import AzureMonitorDatasource from './azure_monitor/azure_monitor_datasource';
|
2021-05-19 03:31:27 -05:00
|
|
|
import AzureResourceGraphDatasource from './azure_resource_graph/azure_resource_graph_datasource';
|
2022-03-03 10:16:10 -06:00
|
|
|
import ResourcePickerData from './resourcePicker/resourcePickerData';
|
2022-06-20 05:33:13 -05:00
|
|
|
import { AzureDataSourceJsonData, AzureMonitorQuery, AzureQueryType } from './types';
|
2021-08-13 05:05:57 -05:00
|
|
|
import migrateAnnotation from './utils/migrateAnnotation';
|
2022-08-01 10:48:49 -05:00
|
|
|
import migrateQuery from './utils/migrateQuery';
|
2021-11-02 14:30:15 -05:00
|
|
|
import { VariableSupport } from './variables';
|
2022-03-03 10:16:10 -06:00
|
|
|
|
2022-06-20 05:33:13 -05:00
|
|
|
export default class Datasource extends DataSourceWithBackend<AzureMonitorQuery, AzureDataSourceJsonData> {
|
2021-08-13 05:05:57 -05:00
|
|
|
annotations = {
|
|
|
|
prepareAnnotation: migrateAnnotation,
|
|
|
|
};
|
|
|
|
|
2018-12-18 07:54:41 -06:00
|
|
|
azureMonitorDatasource: AzureMonitorDatasource;
|
|
|
|
azureLogAnalyticsDatasource: AzureLogAnalyticsDatasource;
|
2021-05-19 11:39:08 -05:00
|
|
|
resourcePickerData: ResourcePickerData;
|
2021-05-19 03:31:27 -05:00
|
|
|
azureResourceGraphDatasource: AzureResourceGraphDatasource;
|
2020-06-25 11:48:18 -05:00
|
|
|
|
2021-07-14 05:36:11 -05:00
|
|
|
pseudoDatasource: {
|
2022-04-28 03:27:39 -05:00
|
|
|
[key in AzureQueryType]?: AzureMonitorDatasource | AzureLogAnalyticsDatasource | AzureResourceGraphDatasource;
|
2021-07-14 05:36:11 -05:00
|
|
|
} = {};
|
|
|
|
|
2021-07-22 02:09:55 -05:00
|
|
|
declare optionsKey: Record<AzureQueryType, string>;
|
2018-12-18 07:54:41 -06:00
|
|
|
|
2021-03-11 05:37:39 -06:00
|
|
|
constructor(
|
|
|
|
instanceSettings: DataSourceInstanceSettings<AzureDataSourceJsonData>,
|
|
|
|
private readonly templateSrv: TemplateSrv = getTemplateSrv()
|
|
|
|
) {
|
2019-05-10 04:37:43 -05:00
|
|
|
super(instanceSettings);
|
2020-06-01 11:37:39 -05:00
|
|
|
this.azureMonitorDatasource = new AzureMonitorDatasource(instanceSettings);
|
2020-06-05 11:32:10 -05:00
|
|
|
this.azureLogAnalyticsDatasource = new AzureLogAnalyticsDatasource(instanceSettings);
|
2021-05-19 03:31:27 -05:00
|
|
|
this.azureResourceGraphDatasource = new AzureResourceGraphDatasource(instanceSettings);
|
2022-09-22 08:05:29 -05:00
|
|
|
this.resourcePickerData = new ResourcePickerData(instanceSettings, this.azureMonitorDatasource);
|
2020-06-25 11:48:18 -05:00
|
|
|
|
2021-07-14 05:36:11 -05:00
|
|
|
this.pseudoDatasource = {
|
|
|
|
[AzureQueryType.AzureMonitor]: this.azureMonitorDatasource,
|
|
|
|
[AzureQueryType.LogAnalytics]: this.azureLogAnalyticsDatasource,
|
|
|
|
[AzureQueryType.AzureResourceGraph]: this.azureResourceGraphDatasource,
|
|
|
|
};
|
2021-06-17 04:14:56 -05:00
|
|
|
|
2021-11-02 14:30:15 -05:00
|
|
|
this.variables = new VariableSupport(this);
|
2018-12-18 07:54:41 -06:00
|
|
|
}
|
|
|
|
|
2021-11-16 07:02:32 -06:00
|
|
|
filterQuery(item: AzureMonitorQuery): boolean {
|
|
|
|
if (!item.queryType) {
|
2022-12-14 02:54:19 -06:00
|
|
|
return false;
|
2021-11-16 07:02:32 -06:00
|
|
|
}
|
|
|
|
const ds = this.pseudoDatasource[item.queryType];
|
|
|
|
return ds?.filterQuery?.(item) ?? true;
|
|
|
|
}
|
|
|
|
|
2020-10-13 23:48:39 -05:00
|
|
|
query(options: DataQueryRequest<AzureMonitorQuery>): Observable<DataQueryResponse> {
|
2022-04-28 03:27:39 -05:00
|
|
|
const byType = new Map<AzureQueryType, DataQueryRequest<AzureMonitorQuery>>();
|
2020-06-25 11:48:18 -05:00
|
|
|
|
2021-08-17 07:03:18 -05:00
|
|
|
for (const baseTarget of options.targets) {
|
|
|
|
// Migrate old query structures
|
2022-08-01 10:48:49 -05:00
|
|
|
const target = migrateQuery(baseTarget);
|
2020-06-30 15:26:46 -05:00
|
|
|
|
2021-07-14 05:36:11 -05:00
|
|
|
// Skip hidden or invalid queries or ones without properties
|
|
|
|
if (!target.queryType || target.hide || !hasQueryForType(target)) {
|
2020-06-25 11:48:18 -05:00
|
|
|
continue;
|
2020-06-01 11:37:39 -05:00
|
|
|
}
|
|
|
|
|
2020-10-01 12:46:14 -05:00
|
|
|
// Initialize the list of queries
|
2021-04-07 03:25:33 -05:00
|
|
|
if (!byType.has(target.queryType)) {
|
2021-04-21 02:38:00 -05:00
|
|
|
const queryForType = cloneDeep(options);
|
2021-04-07 03:25:33 -05:00
|
|
|
queryForType.requestId = `${queryForType.requestId}-${target.refId}`;
|
|
|
|
queryForType.targets = [];
|
|
|
|
byType.set(target.queryType, queryForType);
|
2020-06-25 11:48:18 -05:00
|
|
|
}
|
2021-04-07 03:25:33 -05:00
|
|
|
|
|
|
|
const queryForType = byType.get(target.queryType);
|
|
|
|
queryForType?.targets.push(target);
|
2018-12-18 07:54:41 -06:00
|
|
|
}
|
|
|
|
|
2021-04-07 03:25:33 -05:00
|
|
|
const observables: Array<Observable<DataQueryResponse>> = Array.from(byType.entries()).map(([queryType, req]) => {
|
2021-07-14 05:36:11 -05:00
|
|
|
const ds = this.pseudoDatasource[queryType];
|
|
|
|
if (!ds) {
|
|
|
|
throw new Error('Data source not created for query type ' + queryType);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ds.query(req);
|
2018-12-18 07:54:41 -06:00
|
|
|
});
|
2021-04-07 03:25:33 -05:00
|
|
|
|
2020-06-25 11:48:18 -05:00
|
|
|
// Single query can skip merge
|
2021-04-07 03:25:33 -05:00
|
|
|
if (observables.length === 1) {
|
|
|
|
return observables[0];
|
2020-06-25 11:48:18 -05:00
|
|
|
}
|
2020-10-13 23:48:39 -05:00
|
|
|
|
2021-04-07 03:25:33 -05:00
|
|
|
if (observables.length > 1) {
|
|
|
|
return forkJoin(observables).pipe(
|
2020-10-13 23:48:39 -05:00
|
|
|
map((results: DataQueryResponse[]) => {
|
|
|
|
const data: DataFrame[] = [];
|
|
|
|
for (const result of results) {
|
|
|
|
for (const frame of result.data) {
|
|
|
|
data.push(frame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return { state: LoadingState.Done, data };
|
2020-06-25 11:48:18 -05:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2020-10-13 23:48:39 -05:00
|
|
|
|
|
|
|
return of({ state: LoadingState.Done, data: [] });
|
2018-12-18 07:54:41 -06:00
|
|
|
}
|
2021-11-05 06:21:28 -05:00
|
|
|
|
|
|
|
targetContainsTemplate(query: AzureMonitorQuery) {
|
2022-02-15 01:53:42 -06:00
|
|
|
if (query.subscription && this.templateSrv.containsTemplate(query.subscription)) {
|
2021-11-05 06:21:28 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
let subQuery;
|
|
|
|
if (query.queryType === AzureQueryType.AzureMonitor) {
|
|
|
|
subQuery = JSON.stringify(query.azureMonitor);
|
|
|
|
} else if (query.queryType === AzureQueryType.LogAnalytics) {
|
|
|
|
subQuery = JSON.stringify(query.azureLogAnalytics);
|
|
|
|
} else if (query.queryType === AzureQueryType.AzureResourceGraph) {
|
|
|
|
subQuery = JSON.stringify([query.azureResourceGraph, query.subscriptions]);
|
|
|
|
}
|
|
|
|
|
2022-02-15 01:53:42 -06:00
|
|
|
return !!subQuery && this.templateSrv.containsTemplate(subQuery);
|
2021-11-05 06:21:28 -05:00
|
|
|
}
|
2018-12-18 07:54:41 -06:00
|
|
|
|
2019-07-06 00:01:22 -05:00
|
|
|
async annotationQuery(options: any) {
|
2018-12-18 07:54:41 -06:00
|
|
|
return this.azureLogAnalyticsDatasource.annotationQuery(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Azure Monitor REST API methods */
|
2019-05-07 08:45:15 -05:00
|
|
|
getResourceGroups(subscriptionId: string) {
|
2022-06-20 05:33:13 -05:00
|
|
|
return this.azureMonitorDatasource.getResourceGroups(this.templateSrv.replace(subscriptionId));
|
2018-12-18 07:54:41 -06:00
|
|
|
}
|
|
|
|
|
2022-07-15 06:10:03 -05:00
|
|
|
getMetricNamespaces(subscriptionId: string, resourceGroup?: string) {
|
|
|
|
let url = `/subscriptions/${subscriptionId}`;
|
|
|
|
if (resourceGroup) {
|
|
|
|
url += `/resourceGroups/${resourceGroup};`;
|
|
|
|
}
|
2022-09-09 03:17:10 -05:00
|
|
|
return this.azureMonitorDatasource.getMetricNamespaces({ resourceUri: url }, true);
|
2022-07-15 06:10:03 -05:00
|
|
|
}
|
|
|
|
|
2022-07-27 08:26:32 -05:00
|
|
|
getResourceNames(subscriptionId: string, resourceGroup?: string, metricNamespace?: string) {
|
2021-03-11 05:37:39 -06:00
|
|
|
return this.azureMonitorDatasource.getResourceNames(
|
2022-06-20 05:33:13 -05:00
|
|
|
this.templateSrv.replace(subscriptionId),
|
|
|
|
this.templateSrv.replace(resourceGroup),
|
2022-07-27 08:26:32 -05:00
|
|
|
this.templateSrv.replace(metricNamespace)
|
2021-03-11 05:37:39 -06:00
|
|
|
);
|
2018-12-18 07:54:41 -06:00
|
|
|
}
|
|
|
|
|
2022-07-27 08:26:32 -05:00
|
|
|
getMetricNames(subscriptionId: string, resourceGroup: string, metricNamespace: string, resourceName: string) {
|
2022-07-20 04:04:01 -05:00
|
|
|
return this.azureMonitorDatasource.getMetricNames({
|
|
|
|
subscription: subscriptionId,
|
|
|
|
resourceGroup,
|
2022-07-27 08:26:32 -05:00
|
|
|
metricNamespace,
|
2022-07-20 04:04:01 -05:00
|
|
|
resourceName,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-18 07:54:41 -06:00
|
|
|
/*Azure Log Analytics */
|
2019-05-07 08:45:15 -05:00
|
|
|
getAzureLogAnalyticsWorkspaces(subscriptionId: string) {
|
|
|
|
return this.azureLogAnalyticsDatasource.getWorkspaces(subscriptionId);
|
|
|
|
}
|
|
|
|
|
|
|
|
getSubscriptions() {
|
|
|
|
return this.azureMonitorDatasource.getSubscriptions();
|
2018-12-18 07:54:41 -06:00
|
|
|
}
|
2020-09-09 04:00:43 -05:00
|
|
|
|
|
|
|
interpolateVariablesInQueries(queries: AzureMonitorQuery[], scopedVars: ScopedVars): AzureMonitorQuery[] {
|
2021-07-14 05:36:11 -05:00
|
|
|
const mapped = queries.map((query) => {
|
|
|
|
if (!query.queryType) {
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ds = this.pseudoDatasource[query.queryType];
|
2022-05-31 11:27:25 -05:00
|
|
|
return {
|
|
|
|
datasource: ds?.getRef(),
|
|
|
|
...(ds?.applyTemplateVariables(query, scopedVars) ?? query),
|
|
|
|
};
|
2021-07-14 05:36:11 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
return mapped;
|
2020-09-09 04:00:43 -05:00
|
|
|
}
|
2021-03-11 05:37:39 -06:00
|
|
|
|
|
|
|
getVariables() {
|
|
|
|
return this.templateSrv.getVariables().map((v) => `$${v.name}`);
|
|
|
|
}
|
2022-07-14 02:48:11 -05:00
|
|
|
|
|
|
|
getVariablesRaw() {
|
|
|
|
return this.templateSrv.getVariables();
|
|
|
|
}
|
2018-12-18 07:54:41 -06:00
|
|
|
}
|
2021-04-07 03:25:33 -05:00
|
|
|
|
2021-07-14 05:36:11 -05:00
|
|
|
function hasQueryForType(query: AzureMonitorQuery): boolean {
|
|
|
|
switch (query.queryType) {
|
|
|
|
case AzureQueryType.AzureMonitor:
|
|
|
|
return !!query.azureMonitor;
|
|
|
|
|
|
|
|
case AzureQueryType.LogAnalytics:
|
|
|
|
return !!query.azureLogAnalytics;
|
|
|
|
|
|
|
|
case AzureQueryType.AzureResourceGraph:
|
|
|
|
return !!query.azureResourceGraph;
|
|
|
|
|
2021-11-02 14:30:15 -05:00
|
|
|
case AzureQueryType.GrafanaTemplateVariableFn:
|
|
|
|
return !!query.grafanaTemplateVariableFn;
|
|
|
|
|
2021-07-14 05:36:11 -05:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|