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