The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/public/app/plugins/datasource/azuremonitor/variables.ts

174 lines
6.4 KiB

import { from, lastValueFrom, Observable } from 'rxjs';
import {
CustomVariableSupport,
DataQueryRequest,
DataQueryResponse,
MetricFindValue,
toDataFrame,
} from '@grafana/data';
import { getTemplateSrv, TemplateSrv } from '@grafana/runtime';
import VariableEditor from './components/VariableEditor/VariableEditor';
import DataSource from './datasource';
import { migrateQuery } from './grafanaTemplateVariableFns';
import { AzureMonitorQuery, AzureQueryType } from './types';
import { GrafanaTemplateVariableQuery } from './types/templateVariables';
import messageFromError from './utils/messageFromError';
export class VariableSupport extends CustomVariableSupport<DataSource, AzureMonitorQuery> {
constructor(
private readonly datasource: DataSource,
private readonly templateSrv: TemplateSrv = getTemplateSrv()
) {
super();
this.datasource = datasource;
}
editor = VariableEditor;
hasValue(...values: string[]) {
return values.every((v) => !!this.templateSrv.replace(v));
}
query(request: DataQueryRequest<AzureMonitorQuery>): Observable<DataQueryResponse> {
const promisedResults = async () => {
const queryObj = await migrateQuery(request.targets[0], { datasource: this.datasource });
try {
switch (queryObj.queryType) {
case AzureQueryType.SubscriptionsQuery:
const res = await this.datasource.getSubscriptions();
return {
data: res?.length ? [toDataFrame(res)] : [],
};
case AzureQueryType.ResourceGroupsQuery:
if (queryObj.subscription && this.hasValue(queryObj.subscription)) {
const rgs = await this.datasource.getResourceGroups(queryObj.subscription);
return {
data: rgs?.length ? [toDataFrame(rgs)] : [],
};
}
return { data: [] };
case AzureQueryType.NamespacesQuery:
if (queryObj.subscription && this.hasValue(queryObj.subscription)) {
const rgs = await this.datasource.getMetricNamespaces(queryObj.subscription, queryObj.resourceGroup);
return {
data: rgs?.length ? [toDataFrame(rgs)] : [],
};
}
return { data: [] };
case AzureQueryType.ResourceNamesQuery:
if (queryObj.subscription && this.hasValue(queryObj.subscription)) {
const rgs = await this.datasource.getResourceNames(
queryObj.subscription,
queryObj.resourceGroup,
queryObj.namespace,
queryObj.region
);
return {
data: rgs?.length ? [toDataFrame(rgs)] : [],
};
}
return { data: [] };
case AzureQueryType.MetricNamesQuery:
if (
queryObj.subscription &&
queryObj.resourceGroup &&
queryObj.namespace &&
queryObj.resource &&
this.hasValue(queryObj.subscription, queryObj.resourceGroup, queryObj.namespace, queryObj.resource)
) {
const rgs = await this.datasource.getMetricNames(
queryObj.subscription,
queryObj.resourceGroup,
queryObj.namespace,
queryObj.resource
);
return {
data: rgs?.length ? [toDataFrame(rgs)] : [],
};
}
return { data: [] };
case AzureQueryType.WorkspacesQuery:
if (queryObj.subscription && this.hasValue(queryObj.subscription)) {
const rgs = await this.datasource.getAzureLogAnalyticsWorkspaces(queryObj.subscription);
return {
data: rgs?.length ? [toDataFrame(rgs)] : [],
};
}
return { data: [] };
case AzureQueryType.GrafanaTemplateVariableFn:
if (queryObj.grafanaTemplateVariableFn) {
const templateVariablesResults = await this.callGrafanaTemplateVariableFn(
queryObj.grafanaTemplateVariableFn
);
return {
data: templateVariablesResults?.length ? [toDataFrame(templateVariablesResults)] : [],
};
}
return { data: [] };
case AzureQueryType.LocationsQuery:
if (queryObj.subscription && this.hasValue(queryObj.subscription)) {
const locationMap = await this.datasource.azureMonitorDatasource.getLocations([queryObj.subscription]);
const res: Array<{ text: string; value: string }> = [];
locationMap.forEach((loc) => {
res.push({ text: loc.displayName, value: loc.name });
});
return {
data: res?.length ? [toDataFrame(res)] : [],
};
}
default:
request.targets[0] = queryObj;
const queryResp = await lastValueFrom(this.datasource.query(request));
return {
data: queryResp.data,
error: queryResp.error ? new Error(messageFromError(queryResp.error)) : undefined,
};
}
} catch (err) {
return { data: [], error: new Error(messageFromError(err)) };
}
};
return from(promisedResults());
}
// Deprecated
callGrafanaTemplateVariableFn(query: GrafanaTemplateVariableQuery): Promise<MetricFindValue[]> | null {
if (query.kind === 'SubscriptionsQuery') {
return this.datasource.getSubscriptions();
}
if (query.kind === 'ResourceGroupsQuery') {
return this.datasource.getResourceGroups(this.replaceVariable(query.subscription));
}
if (query.kind === 'ResourceNamesQuery') {
return this.datasource.getResourceNames(
this.replaceVariable(query.subscription),
this.replaceVariable(query.resourceGroup),
this.replaceVariable(query.metricNamespace)
);
}
if (query.kind === 'MetricNamespaceQuery') {
return this.datasource.azureMonitorDatasource.getMetricNamespaces(query, true);
}
if (query.kind === 'MetricNamesQuery') {
return this.datasource.azureMonitorDatasource.getMetricNames(query);
}
if (query.kind === 'WorkspacesQuery') {
return this.datasource.azureLogAnalyticsDatasource.getWorkspaces(this.replaceVariable(query.subscription));
}
return null;
}
replaceVariable(metric: string) {
return this.templateSrv.replace((metric || '').trim());
}
}