grafana/public/app/plugins/datasource/grafana-azure-monitor-datasource/datasource.ts

266 lines
8.5 KiB
TypeScript
Raw Normal View History

import _ from 'lodash';
import AzureMonitorDatasource from './azure_monitor/azure_monitor_datasource';
import AppInsightsDatasource from './app_insights/app_insights_datasource';
import AzureLogAnalyticsDatasource from './azure_log_analytics/azure_log_analytics_datasource';
import { AzureMonitorQuery, AzureDataSourceJsonData, AzureQueryType, InsightsAnalyticsQuery } from './types';
import {
DataSourceApi,
DataQueryRequest,
DataSourceInstanceSettings,
DataQueryResponseData,
LoadingState,
ScopedVars,
} from '@grafana/data';
import { Observable, of, from } from 'rxjs';
import { DataSourceWithBackend } from '@grafana/runtime';
import InsightsAnalyticsDatasource from './insights_analytics/insights_analytics_datasource';
import { migrateMetricsDimensionFilters } from './query_ctrl';
export default class Datasource extends DataSourceApi<AzureMonitorQuery, AzureDataSourceJsonData> {
azureMonitorDatasource: AzureMonitorDatasource;
appInsightsDatasource: AppInsightsDatasource;
azureLogAnalyticsDatasource: AzureLogAnalyticsDatasource;
insightsAnalyticsDatasource: InsightsAnalyticsDatasource;
pseudoDatasource: Record<AzureQueryType, DataSourceWithBackend>;
optionsKey: Record<AzureQueryType, string>;
constructor(instanceSettings: DataSourceInstanceSettings<AzureDataSourceJsonData>) {
super(instanceSettings);
this.azureMonitorDatasource = new AzureMonitorDatasource(instanceSettings);
this.appInsightsDatasource = new AppInsightsDatasource(instanceSettings);
this.azureLogAnalyticsDatasource = new AzureLogAnalyticsDatasource(instanceSettings);
this.insightsAnalyticsDatasource = new InsightsAnalyticsDatasource(instanceSettings);
const pseudoDatasource: any = {};
pseudoDatasource[AzureQueryType.ApplicationInsights] = this.appInsightsDatasource;
pseudoDatasource[AzureQueryType.AzureMonitor] = this.azureMonitorDatasource;
pseudoDatasource[AzureQueryType.InsightsAnalytics] = this.insightsAnalyticsDatasource;
pseudoDatasource[AzureQueryType.LogAnalytics] = this.azureLogAnalyticsDatasource;
this.pseudoDatasource = pseudoDatasource;
const optionsKey: any = {};
optionsKey[AzureQueryType.ApplicationInsights] = 'appInsights';
optionsKey[AzureQueryType.AzureMonitor] = 'azureMonitor';
optionsKey[AzureQueryType.InsightsAnalytics] = 'insightsAnalytics';
optionsKey[AzureQueryType.LogAnalytics] = 'azureLogAnalytics';
this.optionsKey = optionsKey;
}
query(options: DataQueryRequest<AzureMonitorQuery>): Observable<DataQueryResponseData> {
const byType: Record<AzureQueryType, DataQueryRequest<AzureMonitorQuery>> = ({} as unknown) as Record<
AzureQueryType,
DataQueryRequest<AzureMonitorQuery>
>;
for (const target of options.targets) {
// Migrate old query structure
if (target.queryType === AzureQueryType.ApplicationInsights) {
if ((target.appInsights as any).rawQuery) {
target.queryType = AzureQueryType.InsightsAnalytics;
target.insightsAnalytics = (target.appInsights as unknown) as InsightsAnalyticsQuery;
delete target.appInsights;
}
}
if (!target.queryType) {
target.queryType = AzureQueryType.AzureMonitor;
}
if (target.queryType === AzureQueryType.AzureMonitor) {
migrateMetricsDimensionFilters(target.azureMonitor);
}
// Check that we have options
const opts = (target as any)[this.optionsKey[target.queryType]];
// Skip hidden queries or ones without properties
if (target.hide || !opts) {
continue;
}
// Initalize the list of queries
let q = byType[target.queryType];
if (!q) {
q = _.cloneDeep(options);
q.targets = [];
byType[target.queryType] = q;
}
q.targets.push(target);
}
// Distinct types are managed by distinct requests
const obs = Object.keys(byType).map((type: AzureQueryType) => {
const req = byType[type];
return this.pseudoDatasource[type].query(req);
});
// Single query can skip merge
if (obs.length === 1) {
return obs[0];
}
if (obs.length > 1) {
// Not accurate, but simple and works
// should likely be more like the mixed data source
const promises = obs.map(o => o.toPromise());
return from(
Promise.all(promises).then(results => {
return { data: _.flatten(results) };
})
);
}
return of({ state: LoadingState.Done });
}
2019-07-06 00:01:22 -05:00
async annotationQuery(options: any) {
return this.azureLogAnalyticsDatasource.annotationQuery(options);
}
AzureMonitor: adds support for multiple subscriptions per datasource (#16922) * chore: AzureMonitor typescript typings Removes some types and using @grafana/ui types instead. Adds some typing for the AzureMonitor query. Also adds a getSubscriptions function that will used in the query editor. * fix: AzureMonitor adds back editor for annotation queries This must have been broken for a month or more. Now possible to edit annotation queries again. * feat: Azure Monitor - support for multiple subscriptions Adds a new dropdown for subscriptions in the query editor. Defaults to the subscription id in jsonData for queries that have no subscription id. * feat: adds Azure Logs multi subscriptions support The subscription id is needed for fetching the list of workspaces. Adds support to the Log Analytics datasource and to the annotations for Log Analytics to be able to choose between multiple subscriptions. * feat: AzureMonitor config page with multiple subs Adds support for multiple subscriptions for the different variations of configuring Azure Monitor and Azure Logs. To be able to show a list of subscriptions, the config has to be saved first - the plugin route fetches the tenant id, client id and client secret from the database so a call to get subscriptions requires that those fields are saved first. If the page has not saved then the use can manually paste in a subscription id. * feat: support for multi subs in Azure Monitor variables Adds an optional subscription parameter to the template variable macros. Also adds a Subscriptions macro. * fix: remove some implicit anys from tests
2019-05-07 08:45:15 -05:00
async metricFindQuery(query: string) {
if (!query) {
return Promise.resolve([]);
}
const aiResult = this.appInsightsDatasource.metricFindQueryInternal(query);
if (aiResult) {
return aiResult;
}
const amResult = this.azureMonitorDatasource.metricFindQueryInternal(query);
if (amResult) {
return amResult;
}
const alaResult = this.azureLogAnalyticsDatasource.metricFindQueryInternal(query);
if (alaResult) {
return alaResult;
}
return Promise.resolve([]);
}
AzureMonitor: adds support for multiple subscriptions per datasource (#16922) * chore: AzureMonitor typescript typings Removes some types and using @grafana/ui types instead. Adds some typing for the AzureMonitor query. Also adds a getSubscriptions function that will used in the query editor. * fix: AzureMonitor adds back editor for annotation queries This must have been broken for a month or more. Now possible to edit annotation queries again. * feat: Azure Monitor - support for multiple subscriptions Adds a new dropdown for subscriptions in the query editor. Defaults to the subscription id in jsonData for queries that have no subscription id. * feat: adds Azure Logs multi subscriptions support The subscription id is needed for fetching the list of workspaces. Adds support to the Log Analytics datasource and to the annotations for Log Analytics to be able to choose between multiple subscriptions. * feat: AzureMonitor config page with multiple subs Adds support for multiple subscriptions for the different variations of configuring Azure Monitor and Azure Logs. To be able to show a list of subscriptions, the config has to be saved first - the plugin route fetches the tenant id, client id and client secret from the database so a call to get subscriptions requires that those fields are saved first. If the page has not saved then the use can manually paste in a subscription id. * feat: support for multi subs in Azure Monitor variables Adds an optional subscription parameter to the template variable macros. Also adds a Subscriptions macro. * fix: remove some implicit anys from tests
2019-05-07 08:45:15 -05:00
async testDatasource() {
const promises: any[] = [];
if (this.azureMonitorDatasource.isConfigured()) {
promises.push(this.azureMonitorDatasource.testDatasource());
}
if (this.appInsightsDatasource.isConfigured()) {
promises.push(this.appInsightsDatasource.testDatasource());
}
if (this.azureLogAnalyticsDatasource.isConfigured()) {
promises.push(this.azureLogAnalyticsDatasource.testDatasource());
}
if (promises.length === 0) {
return {
status: 'error',
message: `Nothing configured. At least one of the API's must be configured.`,
title: 'Error',
};
}
AzureMonitor: adds support for multiple subscriptions per datasource (#16922) * chore: AzureMonitor typescript typings Removes some types and using @grafana/ui types instead. Adds some typing for the AzureMonitor query. Also adds a getSubscriptions function that will used in the query editor. * fix: AzureMonitor adds back editor for annotation queries This must have been broken for a month or more. Now possible to edit annotation queries again. * feat: Azure Monitor - support for multiple subscriptions Adds a new dropdown for subscriptions in the query editor. Defaults to the subscription id in jsonData for queries that have no subscription id. * feat: adds Azure Logs multi subscriptions support The subscription id is needed for fetching the list of workspaces. Adds support to the Log Analytics datasource and to the annotations for Log Analytics to be able to choose between multiple subscriptions. * feat: AzureMonitor config page with multiple subs Adds support for multiple subscriptions for the different variations of configuring Azure Monitor and Azure Logs. To be able to show a list of subscriptions, the config has to be saved first - the plugin route fetches the tenant id, client id and client secret from the database so a call to get subscriptions requires that those fields are saved first. If the page has not saved then the use can manually paste in a subscription id. * feat: support for multi subs in Azure Monitor variables Adds an optional subscription parameter to the template variable macros. Also adds a Subscriptions macro. * fix: remove some implicit anys from tests
2019-05-07 08:45:15 -05:00
return Promise.all(promises).then(results => {
let status = 'success';
let message = '';
for (let i = 0; i < results.length; i++) {
if (results[i].status !== 'success') {
status = results[i].status;
}
message += `${i + 1}. ${results[i].message} `;
}
return {
status: status,
message: message,
title: _.upperFirst(status),
};
});
}
/* Azure Monitor REST API methods */
AzureMonitor: adds support for multiple subscriptions per datasource (#16922) * chore: AzureMonitor typescript typings Removes some types and using @grafana/ui types instead. Adds some typing for the AzureMonitor query. Also adds a getSubscriptions function that will used in the query editor. * fix: AzureMonitor adds back editor for annotation queries This must have been broken for a month or more. Now possible to edit annotation queries again. * feat: Azure Monitor - support for multiple subscriptions Adds a new dropdown for subscriptions in the query editor. Defaults to the subscription id in jsonData for queries that have no subscription id. * feat: adds Azure Logs multi subscriptions support The subscription id is needed for fetching the list of workspaces. Adds support to the Log Analytics datasource and to the annotations for Log Analytics to be able to choose between multiple subscriptions. * feat: AzureMonitor config page with multiple subs Adds support for multiple subscriptions for the different variations of configuring Azure Monitor and Azure Logs. To be able to show a list of subscriptions, the config has to be saved first - the plugin route fetches the tenant id, client id and client secret from the database so a call to get subscriptions requires that those fields are saved first. If the page has not saved then the use can manually paste in a subscription id. * feat: support for multi subs in Azure Monitor variables Adds an optional subscription parameter to the template variable macros. Also adds a Subscriptions macro. * fix: remove some implicit anys from tests
2019-05-07 08:45:15 -05:00
getResourceGroups(subscriptionId: string) {
return this.azureMonitorDatasource.getResourceGroups(subscriptionId);
}
AzureMonitor: adds support for multiple subscriptions per datasource (#16922) * chore: AzureMonitor typescript typings Removes some types and using @grafana/ui types instead. Adds some typing for the AzureMonitor query. Also adds a getSubscriptions function that will used in the query editor. * fix: AzureMonitor adds back editor for annotation queries This must have been broken for a month or more. Now possible to edit annotation queries again. * feat: Azure Monitor - support for multiple subscriptions Adds a new dropdown for subscriptions in the query editor. Defaults to the subscription id in jsonData for queries that have no subscription id. * feat: adds Azure Logs multi subscriptions support The subscription id is needed for fetching the list of workspaces. Adds support to the Log Analytics datasource and to the annotations for Log Analytics to be able to choose between multiple subscriptions. * feat: AzureMonitor config page with multiple subs Adds support for multiple subscriptions for the different variations of configuring Azure Monitor and Azure Logs. To be able to show a list of subscriptions, the config has to be saved first - the plugin route fetches the tenant id, client id and client secret from the database so a call to get subscriptions requires that those fields are saved first. If the page has not saved then the use can manually paste in a subscription id. * feat: support for multi subs in Azure Monitor variables Adds an optional subscription parameter to the template variable macros. Also adds a Subscriptions macro. * fix: remove some implicit anys from tests
2019-05-07 08:45:15 -05:00
getMetricDefinitions(subscriptionId: string, resourceGroup: string) {
return this.azureMonitorDatasource.getMetricDefinitions(subscriptionId, resourceGroup);
}
AzureMonitor: adds support for multiple subscriptions per datasource (#16922) * chore: AzureMonitor typescript typings Removes some types and using @grafana/ui types instead. Adds some typing for the AzureMonitor query. Also adds a getSubscriptions function that will used in the query editor. * fix: AzureMonitor adds back editor for annotation queries This must have been broken for a month or more. Now possible to edit annotation queries again. * feat: Azure Monitor - support for multiple subscriptions Adds a new dropdown for subscriptions in the query editor. Defaults to the subscription id in jsonData for queries that have no subscription id. * feat: adds Azure Logs multi subscriptions support The subscription id is needed for fetching the list of workspaces. Adds support to the Log Analytics datasource and to the annotations for Log Analytics to be able to choose between multiple subscriptions. * feat: AzureMonitor config page with multiple subs Adds support for multiple subscriptions for the different variations of configuring Azure Monitor and Azure Logs. To be able to show a list of subscriptions, the config has to be saved first - the plugin route fetches the tenant id, client id and client secret from the database so a call to get subscriptions requires that those fields are saved first. If the page has not saved then the use can manually paste in a subscription id. * feat: support for multi subs in Azure Monitor variables Adds an optional subscription parameter to the template variable macros. Also adds a Subscriptions macro. * fix: remove some implicit anys from tests
2019-05-07 08:45:15 -05:00
getResourceNames(subscriptionId: string, resourceGroup: string, metricDefinition: string) {
return this.azureMonitorDatasource.getResourceNames(subscriptionId, resourceGroup, metricDefinition);
}
getMetricNames(
subscriptionId: string,
resourceGroup: string,
metricDefinition: string,
resourceName: string,
metricNamespace: string
) {
return this.azureMonitorDatasource.getMetricNames(
subscriptionId,
resourceGroup,
metricDefinition,
resourceName,
metricNamespace
);
}
getMetricNamespaces(subscriptionId: string, resourceGroup: string, metricDefinition: string, resourceName: string) {
return this.azureMonitorDatasource.getMetricNamespaces(
subscriptionId,
resourceGroup,
metricDefinition,
resourceName
);
}
AzureMonitor: adds support for multiple subscriptions per datasource (#16922) * chore: AzureMonitor typescript typings Removes some types and using @grafana/ui types instead. Adds some typing for the AzureMonitor query. Also adds a getSubscriptions function that will used in the query editor. * fix: AzureMonitor adds back editor for annotation queries This must have been broken for a month or more. Now possible to edit annotation queries again. * feat: Azure Monitor - support for multiple subscriptions Adds a new dropdown for subscriptions in the query editor. Defaults to the subscription id in jsonData for queries that have no subscription id. * feat: adds Azure Logs multi subscriptions support The subscription id is needed for fetching the list of workspaces. Adds support to the Log Analytics datasource and to the annotations for Log Analytics to be able to choose between multiple subscriptions. * feat: AzureMonitor config page with multiple subs Adds support for multiple subscriptions for the different variations of configuring Azure Monitor and Azure Logs. To be able to show a list of subscriptions, the config has to be saved first - the plugin route fetches the tenant id, client id and client secret from the database so a call to get subscriptions requires that those fields are saved first. If the page has not saved then the use can manually paste in a subscription id. * feat: support for multi subs in Azure Monitor variables Adds an optional subscription parameter to the template variable macros. Also adds a Subscriptions macro. * fix: remove some implicit anys from tests
2019-05-07 08:45:15 -05:00
getMetricMetadata(
subscriptionId: string,
resourceGroup: string,
metricDefinition: string,
resourceName: string,
metricNamespace: string,
metricName: string
AzureMonitor: adds support for multiple subscriptions per datasource (#16922) * chore: AzureMonitor typescript typings Removes some types and using @grafana/ui types instead. Adds some typing for the AzureMonitor query. Also adds a getSubscriptions function that will used in the query editor. * fix: AzureMonitor adds back editor for annotation queries This must have been broken for a month or more. Now possible to edit annotation queries again. * feat: Azure Monitor - support for multiple subscriptions Adds a new dropdown for subscriptions in the query editor. Defaults to the subscription id in jsonData for queries that have no subscription id. * feat: adds Azure Logs multi subscriptions support The subscription id is needed for fetching the list of workspaces. Adds support to the Log Analytics datasource and to the annotations for Log Analytics to be able to choose between multiple subscriptions. * feat: AzureMonitor config page with multiple subs Adds support for multiple subscriptions for the different variations of configuring Azure Monitor and Azure Logs. To be able to show a list of subscriptions, the config has to be saved first - the plugin route fetches the tenant id, client id and client secret from the database so a call to get subscriptions requires that those fields are saved first. If the page has not saved then the use can manually paste in a subscription id. * feat: support for multi subs in Azure Monitor variables Adds an optional subscription parameter to the template variable macros. Also adds a Subscriptions macro. * fix: remove some implicit anys from tests
2019-05-07 08:45:15 -05:00
) {
return this.azureMonitorDatasource.getMetricMetadata(
subscriptionId,
resourceGroup,
metricDefinition,
resourceName,
metricNamespace,
metricName
AzureMonitor: adds support for multiple subscriptions per datasource (#16922) * chore: AzureMonitor typescript typings Removes some types and using @grafana/ui types instead. Adds some typing for the AzureMonitor query. Also adds a getSubscriptions function that will used in the query editor. * fix: AzureMonitor adds back editor for annotation queries This must have been broken for a month or more. Now possible to edit annotation queries again. * feat: Azure Monitor - support for multiple subscriptions Adds a new dropdown for subscriptions in the query editor. Defaults to the subscription id in jsonData for queries that have no subscription id. * feat: adds Azure Logs multi subscriptions support The subscription id is needed for fetching the list of workspaces. Adds support to the Log Analytics datasource and to the annotations for Log Analytics to be able to choose between multiple subscriptions. * feat: AzureMonitor config page with multiple subs Adds support for multiple subscriptions for the different variations of configuring Azure Monitor and Azure Logs. To be able to show a list of subscriptions, the config has to be saved first - the plugin route fetches the tenant id, client id and client secret from the database so a call to get subscriptions requires that those fields are saved first. If the page has not saved then the use can manually paste in a subscription id. * feat: support for multi subs in Azure Monitor variables Adds an optional subscription parameter to the template variable macros. Also adds a Subscriptions macro. * fix: remove some implicit anys from tests
2019-05-07 08:45:15 -05:00
);
}
/* Application Insights API method */
getAppInsightsMetricNames() {
return this.appInsightsDatasource.getMetricNames();
}
2019-07-06 00:01:22 -05:00
getAppInsightsMetricMetadata(metricName: string) {
return this.appInsightsDatasource.getMetricMetadata(metricName);
}
2019-07-06 00:01:22 -05:00
getAppInsightsColumns(refId: string | number) {
return this.appInsightsDatasource.logAnalyticsColumns[refId];
}
/*Azure Log Analytics */
AzureMonitor: adds support for multiple subscriptions per datasource (#16922) * chore: AzureMonitor typescript typings Removes some types and using @grafana/ui types instead. Adds some typing for the AzureMonitor query. Also adds a getSubscriptions function that will used in the query editor. * fix: AzureMonitor adds back editor for annotation queries This must have been broken for a month or more. Now possible to edit annotation queries again. * feat: Azure Monitor - support for multiple subscriptions Adds a new dropdown for subscriptions in the query editor. Defaults to the subscription id in jsonData for queries that have no subscription id. * feat: adds Azure Logs multi subscriptions support The subscription id is needed for fetching the list of workspaces. Adds support to the Log Analytics datasource and to the annotations for Log Analytics to be able to choose between multiple subscriptions. * feat: AzureMonitor config page with multiple subs Adds support for multiple subscriptions for the different variations of configuring Azure Monitor and Azure Logs. To be able to show a list of subscriptions, the config has to be saved first - the plugin route fetches the tenant id, client id and client secret from the database so a call to get subscriptions requires that those fields are saved first. If the page has not saved then the use can manually paste in a subscription id. * feat: support for multi subs in Azure Monitor variables Adds an optional subscription parameter to the template variable macros. Also adds a Subscriptions macro. * fix: remove some implicit anys from tests
2019-05-07 08:45:15 -05:00
getAzureLogAnalyticsWorkspaces(subscriptionId: string) {
return this.azureLogAnalyticsDatasource.getWorkspaces(subscriptionId);
}
getSubscriptions() {
return this.azureMonitorDatasource.getSubscriptions();
}
interpolateVariablesInQueries(queries: AzureMonitorQuery[], scopedVars: ScopedVars): AzureMonitorQuery[] {
return queries.map(
query => this.pseudoDatasource[query.queryType].applyTemplateVariables(query, scopedVars) as AzureMonitorQuery
);
}
}