mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
AzureMonitor: Set multiple resources for Logs (#61545)
This commit is contained in:
parent
6fec8fda39
commit
7a026d90e9
@ -17,7 +17,7 @@ export default function createMockQuery(overrides?: Partial<AzureMonitorQuery>):
|
|||||||
'//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 chart’s 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',
|
'//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 chart’s 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',
|
resultFormat: 'time_series',
|
||||||
workspace: 'e3fe4fde-ad5e-4d60-9974-e2f3562ffdf2',
|
workspace: 'e3fe4fde-ad5e-4d60-9974-e2f3562ffdf2',
|
||||||
resource: 'test-resource',
|
resources: ['test-resource'],
|
||||||
...overrides?.azureLogAnalytics,
|
...overrides?.azureLogAnalytics,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
import { get, set } from 'lodash';
|
|
||||||
|
|
||||||
import { toUtc } from '@grafana/data';
|
import { toUtc } from '@grafana/data';
|
||||||
import { TemplateSrv } from 'app/features/templating/template_srv';
|
import { TemplateSrv } from 'app/features/templating/template_srv';
|
||||||
|
|
||||||
@ -237,7 +235,7 @@ describe('AzureLogAnalyticsDatasource', () => {
|
|||||||
|
|
||||||
const ds = new AzureMonitorDatasource(ctx.instanceSettings, templateSrv);
|
const ds = new AzureMonitorDatasource(ctx.instanceSettings, templateSrv);
|
||||||
query.queryType = AzureQueryType.LogAnalytics;
|
query.queryType = AzureQueryType.LogAnalytics;
|
||||||
query.azureLogAnalytics = { resource: `$${singleVariable.name}` };
|
query.azureLogAnalytics = { resources: [`$${singleVariable.name}`] };
|
||||||
expect(ds.targetContainsTemplate(query)).toEqual(true);
|
expect(ds.targetContainsTemplate(query)).toEqual(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -270,7 +268,7 @@ describe('AzureLogAnalyticsDatasource', () => {
|
|||||||
const query: AzureMonitorQuery = {
|
const query: AzureMonitorQuery = {
|
||||||
refId: 'A',
|
refId: 'A',
|
||||||
azureLogAnalytics: {
|
azureLogAnalytics: {
|
||||||
resource: '/sub/124/rg/cloud/vm/server',
|
resources: ['/sub/124/rg/cloud/vm/server'],
|
||||||
query: 'perf | take 100',
|
query: 'perf | take 100',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@ -303,7 +301,7 @@ describe('AzureLogAnalyticsDatasource', () => {
|
|||||||
refId: 'A',
|
refId: 'A',
|
||||||
hide: true,
|
hide: true,
|
||||||
azureLogAnalytics: {
|
azureLogAnalytics: {
|
||||||
resource: '/sub/124/rg/cloud/vm/server',
|
resources: ['/sub/124/rg/cloud/vm/server'],
|
||||||
query: 'perf | take 100',
|
query: 'perf | take 100',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@ -315,7 +313,7 @@ describe('AzureLogAnalyticsDatasource', () => {
|
|||||||
const query: AzureMonitorQuery = {
|
const query: AzureMonitorQuery = {
|
||||||
refId: 'A',
|
refId: 'A',
|
||||||
azureLogAnalytics: {
|
azureLogAnalytics: {
|
||||||
resource: '/sub/124/rg/cloud/vm/server',
|
resources: ['/sub/124/rg/cloud/vm/server'],
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -352,9 +350,9 @@ describe('AzureLogAnalyticsDatasource', () => {
|
|||||||
templateSrv.init(Array.from(templateVariables.values()).map((item) => item.templateVariable));
|
templateSrv.init(Array.from(templateVariables.values()).map((item) => item.templateVariable));
|
||||||
const query = createMockQuery();
|
const query = createMockQuery();
|
||||||
const azureLogAnalytics: { [index: string]: any } = {};
|
const azureLogAnalytics: { [index: string]: any } = {};
|
||||||
for (const [path, templateVariable] of templateVariables.entries()) {
|
azureLogAnalytics.query = '$query';
|
||||||
set(azureLogAnalytics, path, `$${templateVariable.variableName}`);
|
azureLogAnalytics.workspace = '$workspace';
|
||||||
}
|
azureLogAnalytics.resources = ['$resource'];
|
||||||
query.queryType = AzureQueryType.LogAnalytics;
|
query.queryType = AzureQueryType.LogAnalytics;
|
||||||
query.azureLogAnalytics = {
|
query.azureLogAnalytics = {
|
||||||
...query.azureLogAnalytics,
|
...query.azureLogAnalytics,
|
||||||
@ -362,9 +360,11 @@ describe('AzureLogAnalyticsDatasource', () => {
|
|||||||
};
|
};
|
||||||
const templatedQuery = ctx.ds.interpolateVariablesInQueries([query], {});
|
const templatedQuery = ctx.ds.interpolateVariablesInQueries([query], {});
|
||||||
expect(templatedQuery[0]).toHaveProperty('datasource');
|
expect(templatedQuery[0]).toHaveProperty('datasource');
|
||||||
for (const [path, templateVariable] of templateVariables.entries()) {
|
expect(templatedQuery[0].azureLogAnalytics).toMatchObject({
|
||||||
expect(get(templatedQuery[0].azureLogAnalytics, path)).toEqual(templateVariable.templateVariable.current.value);
|
query: templateVariables.get('query')?.templateVariable.current.value,
|
||||||
}
|
workspace: templateVariables.get('workspace')?.templateVariable.current.value,
|
||||||
|
resources: [templateVariables.get('resource')?.templateVariable.current.value],
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -66,7 +66,7 @@ export default class AzureLogAnalyticsDatasource extends DataSourceWithBackend<
|
|||||||
return (
|
return (
|
||||||
item.hide !== true &&
|
item.hide !== true &&
|
||||||
!!item.azureLogAnalytics?.query &&
|
!!item.azureLogAnalytics?.query &&
|
||||||
(!!item.azureLogAnalytics.resource || !!item.azureLogAnalytics.workspace)
|
(!!item.azureLogAnalytics.resources?.length || !!item.azureLogAnalytics.workspace)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,10 +124,10 @@ export default class AzureLogAnalyticsDatasource extends DataSourceWithBackend<
|
|||||||
}
|
}
|
||||||
|
|
||||||
const templateSrv = getTemplateSrv();
|
const templateSrv = getTemplateSrv();
|
||||||
const resource = templateSrv.replace(item.resource, scopedVars);
|
const resources = item.resources?.map((r) => templateSrv.replace(r, scopedVars));
|
||||||
let workspace = templateSrv.replace(item.workspace, scopedVars);
|
let workspace = templateSrv.replace(item.workspace, scopedVars);
|
||||||
|
|
||||||
if (!workspace && !resource && this.firstWorkspace) {
|
if (!workspace && !resources && this.firstWorkspace) {
|
||||||
workspace = this.firstWorkspace;
|
workspace = this.firstWorkspace;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,7 +140,7 @@ export default class AzureLogAnalyticsDatasource extends DataSourceWithBackend<
|
|||||||
azureLogAnalytics: {
|
azureLogAnalytics: {
|
||||||
resultFormat: item.resultFormat,
|
resultFormat: item.resultFormat,
|
||||||
query,
|
query,
|
||||||
resource,
|
resources,
|
||||||
|
|
||||||
// Workspace was removed in Grafana 8, but remains for backwards compat
|
// Workspace was removed in Grafana 8, but remains for backwards compat
|
||||||
workspace,
|
workspace,
|
||||||
|
@ -924,7 +924,7 @@ describe('AzureMonitorDatasource', () => {
|
|||||||
|
|
||||||
const ds = new AzureMonitorDatasource(ctx.instanceSettings, templateSrv);
|
const ds = new AzureMonitorDatasource(ctx.instanceSettings, templateSrv);
|
||||||
query.queryType = AzureQueryType.AzureMonitor;
|
query.queryType = AzureQueryType.AzureMonitor;
|
||||||
query.azureLogAnalytics = { resource: `$${singleVariable.name}` };
|
query.azureLogAnalytics = { resources: [`$${singleVariable.name}`] };
|
||||||
expect(ds.targetContainsTemplate(query)).toEqual(false);
|
expect(ds.targetContainsTemplate(query)).toEqual(false);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -53,7 +53,11 @@ const LogsQueryEditor: React.FC<LogsQueryEditorProps> = ({
|
|||||||
ResourceRowType.Resource,
|
ResourceRowType.Resource,
|
||||||
ResourceRowType.Variable,
|
ResourceRowType.Variable,
|
||||||
]}
|
]}
|
||||||
resource={query.azureLogAnalytics?.resource ?? ''}
|
resource={
|
||||||
|
query.azureLogAnalytics?.resources && query.azureLogAnalytics.resources.length
|
||||||
|
? query.azureLogAnalytics.resources[0]
|
||||||
|
: ''
|
||||||
|
}
|
||||||
queryType="logs"
|
queryType="logs"
|
||||||
/>
|
/>
|
||||||
</EditorFieldGroup>
|
</EditorFieldGroup>
|
||||||
|
@ -33,12 +33,12 @@ const QueryField: React.FC<AzureQueryEditorFieldProps> = ({ query, datasource, o
|
|||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!query.azureLogAnalytics?.resource) {
|
if (!query.azureLogAnalytics?.resources || !query.azureLogAnalytics.resources.length) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const promises = [
|
const promises = [
|
||||||
datasource.azureLogAnalyticsDatasource.getKustoSchema(query.azureLogAnalytics.resource),
|
datasource.azureLogAnalyticsDatasource.getKustoSchema(query.azureLogAnalytics.resources[0]),
|
||||||
getPromise(),
|
getPromise(),
|
||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
@ -56,7 +56,7 @@ const QueryField: React.FC<AzureQueryEditorFieldProps> = ({ query, datasource, o
|
|||||||
worker?.setSchema(schema, 'https://help.kusto.windows.net', 'Samples');
|
worker?.setSchema(schema, 'https://help.kusto.windows.net', 'Samples');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}, [datasource.azureLogAnalyticsDatasource, query.azureLogAnalytics?.resource]);
|
}, [datasource.azureLogAnalyticsDatasource, query.azureLogAnalytics?.resources]);
|
||||||
|
|
||||||
const handleEditorMount = useCallback((editor: MonacoEditor, monaco: Monaco) => {
|
const handleEditorMount = useCallback((editor: MonacoEditor, monaco: Monaco) => {
|
||||||
monacoPromiseRef.current?.resolve?.({ editor, monaco });
|
monacoPromiseRef.current?.resolve?.({ editor, monaco });
|
||||||
|
@ -9,7 +9,7 @@ async function migrateWorkspaceQueryToResourceQuery(
|
|||||||
query: AzureMonitorQuery,
|
query: AzureMonitorQuery,
|
||||||
onChange: (newQuery: AzureMonitorQuery) => void
|
onChange: (newQuery: AzureMonitorQuery) => void
|
||||||
) {
|
) {
|
||||||
if (query.azureLogAnalytics?.workspace !== undefined && !query.azureLogAnalytics.resource) {
|
if (query.azureLogAnalytics?.workspace !== undefined && !query.azureLogAnalytics.resources) {
|
||||||
const isWorkspaceGUID = isGUIDish(query.azureLogAnalytics.workspace);
|
const isWorkspaceGUID = isGUIDish(query.azureLogAnalytics.workspace);
|
||||||
let resource: string;
|
let resource: string;
|
||||||
|
|
||||||
|
@ -166,7 +166,7 @@ describe('AzureMonitor ResourcePicker utils', () => {
|
|||||||
describe('setResource', () => {
|
describe('setResource', () => {
|
||||||
it('updates a resource with a resource URI for Log Analytics', () => {
|
it('updates a resource with a resource URI for Log Analytics', () => {
|
||||||
expect(setResource(createMockQuery(), '/subscription/sub')).toMatchObject({
|
expect(setResource(createMockQuery(), '/subscription/sub')).toMatchObject({
|
||||||
azureLogAnalytics: { resource: '/subscription/sub' },
|
azureLogAnalytics: { resources: ['/subscription/sub'] },
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -139,7 +139,7 @@ export function setResource(query: AzureMonitorQuery, resource?: string | AzureM
|
|||||||
...query,
|
...query,
|
||||||
azureLogAnalytics: {
|
azureLogAnalytics: {
|
||||||
...query.azureLogAnalytics,
|
...query.azureLogAnalytics,
|
||||||
resource,
|
resources: [resource],
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -246,7 +246,7 @@ const createLogAnalyticsTemplateVariableQuery = async (
|
|||||||
queryType: AzureQueryType.LogAnalytics,
|
queryType: AzureQueryType.LogAnalytics,
|
||||||
azureLogAnalytics: {
|
azureLogAnalytics: {
|
||||||
query: rawQuery,
|
query: rawQuery,
|
||||||
resource,
|
resources: [resource],
|
||||||
},
|
},
|
||||||
subscription: defaultSubscriptionId,
|
subscription: defaultSubscriptionId,
|
||||||
};
|
};
|
||||||
|
@ -219,7 +219,7 @@ describe('migrateStringQueriesToObjectQueries', () => {
|
|||||||
queryType: AzureQueryType.LogAnalytics,
|
queryType: AzureQueryType.LogAnalytics,
|
||||||
azureLogAnalytics: {
|
azureLogAnalytics: {
|
||||||
query: 'some kind of kql query',
|
query: 'some kind of kql query',
|
||||||
resource: '',
|
resources: [''],
|
||||||
},
|
},
|
||||||
subscription: 'defaultSubscriptionId',
|
subscription: 'defaultSubscriptionId',
|
||||||
},
|
},
|
||||||
|
@ -92,9 +92,12 @@ export interface AzureMetricQuery {
|
|||||||
export interface AzureLogsQuery {
|
export interface AzureLogsQuery {
|
||||||
query?: string;
|
query?: string;
|
||||||
resultFormat?: string;
|
resultFormat?: string;
|
||||||
resource?: string;
|
resources?: string[];
|
||||||
|
|
||||||
workspace?: string;
|
workspace?: string;
|
||||||
|
|
||||||
|
/** @deprecated Use resources instead */
|
||||||
|
resource?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -37,8 +37,9 @@ const NEW_ANNOTATION: AnnotationQuery<AzureMonitorQuery> = {
|
|||||||
queryType: AzureQueryType.LogAnalytics,
|
queryType: AzureQueryType.LogAnalytics,
|
||||||
azureLogAnalytics: {
|
azureLogAnalytics: {
|
||||||
query: 'AzureActivity\r\n| where $__timeFilter() \r\n| project TimeGenerated, Text=OperationName',
|
query: 'AzureActivity\r\n| where $__timeFilter() \r\n| project TimeGenerated, Text=OperationName',
|
||||||
resource:
|
resources: [
|
||||||
'/subscriptions/abc-123-def-456/resourcegroups/our-datasource/providers/microsoft.operationalinsights/workspaces/azureactivitylog',
|
'/subscriptions/abc-123-def-456/resourcegroups/our-datasource/providers/microsoft.operationalinsights/workspaces/azureactivitylog',
|
||||||
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -20,7 +20,7 @@ export default function migrateAnnotation(annotation: AnnotationQuery<AzureMonit
|
|||||||
queryType: AzureQueryType.LogAnalytics,
|
queryType: AzureQueryType.LogAnalytics,
|
||||||
azureLogAnalytics: {
|
azureLogAnalytics: {
|
||||||
query: oldQuery,
|
query: oldQuery,
|
||||||
resource: oldWorkspace,
|
resources: [oldWorkspace],
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -184,4 +184,16 @@ describe('AzureMonitor: migrateQuery', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should migrate a sigle resource for Logs', () => {
|
||||||
|
const q = {
|
||||||
|
...modernMetricsQuery,
|
||||||
|
azureLogAnalytics: {
|
||||||
|
...modernMetricsQuery.azureLogAnalytics,
|
||||||
|
resource: 'foo',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const result = migrateQuery(q);
|
||||||
|
expect(result.azureLogAnalytics?.resources).toEqual(['foo']);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -32,6 +32,10 @@ export default function migrateQuery(query: AzureMonitorQuery): AzureMonitorQuer
|
|||||||
workingQuery = migrateResourceGroupAndName(workingQuery);
|
workingQuery = migrateResourceGroupAndName(workingQuery);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (workingQuery.azureLogAnalytics?.resource) {
|
||||||
|
workingQuery = migrateLogsResource(workingQuery);
|
||||||
|
}
|
||||||
|
|
||||||
return workingQuery;
|
return workingQuery;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -172,3 +176,15 @@ function migrateResourceGroupAndName(query: AzureMonitorQuery): AzureMonitorQuer
|
|||||||
|
|
||||||
return workingQuery;
|
return workingQuery;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function migrateLogsResource(query: AzureMonitorQuery): AzureMonitorQuery {
|
||||||
|
let workingQuery = query;
|
||||||
|
|
||||||
|
if (workingQuery.azureLogAnalytics && workingQuery.azureLogAnalytics.resource) {
|
||||||
|
workingQuery.azureLogAnalytics.resources = [workingQuery.azureLogAnalytics.resource];
|
||||||
|
|
||||||
|
delete workingQuery.azureLogAnalytics.resource;
|
||||||
|
}
|
||||||
|
|
||||||
|
return workingQuery;
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user