Azure Monitor: Include datasource ref when interpolating variables (#49543)

* Azure Monitor: include datasource ref when interpolating variables

* add tests

* fix test
This commit is contained in:
Kevin Yu 2022-05-31 09:27:25 -07:00 committed by GitHub
parent b03657b0e0
commit 7156935d12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 11 deletions

View File

@ -1,18 +1,30 @@
import { AzureMonitorQuery, AzureQueryType } from '../types';
export default function createMockQuery(): AzureMonitorQuery {
export default function createMockQuery(overrides?: Partial<AzureMonitorQuery>): AzureMonitorQuery {
return {
queryType: AzureQueryType.AzureMonitor,
refId: 'A',
subscription: '99999999-cccc-bbbb-aaaa-9106972f9572',
subscriptions: ['99999999-cccc-bbbb-aaaa-9106972f9572'],
datasource: {
type: 'grafana-azure-monitor-datasource',
uid: 'AAAAA11111BBBBB22222CCCC',
},
...overrides,
azureLogAnalytics: {
query:
'//change this example to create your own time series query\n<table name> //the table to query (e.g. Usage, Heartbeat, Perf)\n| where $__timeFilter(TimeGenerated) //this is a macro used to show the full charts time range, choose the datetime column here\n| summarize count() by <group by column>, bin(TimeGenerated, $__interval) //change “group by column” to a column in your table, such as “Computer”. The $__interval macro is used to auto-select the time grain. Can also use 1h, 5m etc.\n| order by TimeGenerated asc',
resultFormat: 'time_series',
workspace: 'e3fe4fde-ad5e-4d60-9974-e2f3562ffdf2',
resource: 'test-resource',
...overrides?.azureLogAnalytics,
},
azureResourceGraph: {
query: 'Resources | summarize count()',
resultFormat: 'table',
...overrides?.azureResourceGraph,
},
azureMonitor: {
@ -32,15 +44,7 @@ export default function createMockQuery(): AzureMonitorQuery {
alias: '',
// timeGrains: [],
top: '10',
},
queryType: AzureQueryType.AzureMonitor,
refId: 'A',
subscription: '99999999-cccc-bbbb-aaaa-9106972f9572',
subscriptions: ['99999999-cccc-bbbb-aaaa-9106972f9572'],
datasource: {
type: 'grafana-azure-monitor-datasource',
uid: 'AAAAA11111BBBBB22222CCCC',
...overrides?.azureMonitor,
},
};
}

View File

@ -0,0 +1,37 @@
import { createMockInstanceSetttings } from './__mocks__/instanceSettings';
import createMockQuery from './__mocks__/query';
import Datasource from './datasource';
describe('Azure Monitor Datasource', () => {
describe('interpolateVariablesInQueries()', () => {
it('should interpolate variables in the queries', () => {
const ds = new Datasource(createMockInstanceSetttings());
const queries = [createMockQuery({ azureMonitor: { resourceGroup: '$resourceGroup' } })];
const interpolatedQueries = ds.interpolateVariablesInQueries(queries, {
resourceGroup: { text: 'the-resource-group', value: 'the-resource-group' },
});
expect(interpolatedQueries).toContainEqual(
expect.objectContaining({
azureMonitor: expect.objectContaining({ resourceGroup: 'the-resource-group' }),
})
);
});
it('should include a datasource ref when interpolating queries', () => {
const ds = new Datasource(createMockInstanceSetttings());
const query = createMockQuery();
delete query.datasource;
const queries = [query];
const interpolatedQueries = ds.interpolateVariablesInQueries(queries, {});
expect(interpolatedQueries).toContainEqual(
expect.objectContaining({
datasource: expect.objectContaining({ type: 'azuremonitor', uid: 'abc' }),
})
);
});
});
});

View File

@ -203,7 +203,10 @@ export default class Datasource extends DataSourceApi<AzureMonitorQuery, AzureDa
}
const ds = this.pseudoDatasource[query.queryType];
return ds?.applyTemplateVariables(query, scopedVars) ?? query;
return {
datasource: ds?.getRef(),
...(ds?.applyTemplateVariables(query, scopedVars) ?? query),
};
});
return mapped;