CloudWatch: Create deeplinks from log group ARNs (#59646)

This commit is contained in:
Shirley 2022-12-02 11:25:34 +01:00 committed by GitHub
parent 37c118b7dd
commit 1c5a3a7d2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 132 additions and 8 deletions

View File

@ -1,10 +1,16 @@
import { DataQueryResponse, dateMath } from '@grafana/data';
import { DataQueryRequest, DataQueryResponse, dateMath } from '@grafana/data';
import { setDataSourceSrv } from '@grafana/runtime';
import { CloudWatchQuery } from '../types';
import { addDataLinksToLogsResponse } from './datalinks';
describe('addDataLinksToLogsResponse', () => {
it('should add data links to response', async () => {
const time = {
from: dateMath.parse('2016-12-31 15:00:00Z', false)!,
to: dateMath.parse('2016-12-31 16:00:00Z', false)!,
};
it('should add data links to response from log group names', async () => {
const mockResponse: DataQueryResponse = {
data: [
{
@ -34,11 +40,6 @@ describe('addDataLinksToLogsResponse', () => {
],
};
const time = {
from: dateMath.parse('2016-12-31 15:00:00Z', false)!,
to: dateMath.parse('2016-12-31 16:00:00Z', false)!,
};
setDataSourceSrv({
async get() {
return {
@ -93,4 +94,119 @@ describe('addDataLinksToLogsResponse', () => {
],
});
});
it('should add data links to response from log groups, trimming :*', async () => {
const mockResponse: DataQueryResponse = {
data: [
{
fields: [
{
name: '@message',
config: {},
},
],
refId: 'A',
},
],
};
const mockOptions = {
targets: [
{
refId: 'A',
expression: 'stats count(@message) by bin(1h)',
logGroups: [
{ value: 'arn:aws:logs:us-east-1:111111111111:log-group:/aws/lambda/test:*' },
{ value: 'arn:aws:logs:us-east-2:222222222222:log-group:/ecs/prometheus:*' },
],
region: 'us-east-1',
} as CloudWatchQuery,
],
} as DataQueryRequest<CloudWatchQuery>;
await addDataLinksToLogsResponse(
mockResponse,
mockOptions,
{ ...time, raw: time },
(s) => s ?? '',
(v) => [v] ?? [],
(r) => r
);
expect(mockResponse).toMatchObject({
data: [
{
fields: [
{
name: '@message',
config: {
links: [
{
url: "https://us-east-1.console.aws.amazon.com/cloudwatch/home?region=us-east-1#logs-insights:queryDetail=~(end~'2016-12-31T16*3a00*3a00.000Z~start~'2016-12-31T15*3a00*3a00.000Z~timeType~'ABSOLUTE~tz~'UTC~editorString~'stats*20count*28*40message*29*20by*20bin*281h*29~isLiveTail~false~source~(~'arn*3aaws*3alogs*3aus-east-1*3a111111111111*3alog-group*3a*2faws*2flambda*2ftest~'arn*3aaws*3alogs*3aus-east-2*3a222222222222*3alog-group*3a*2fecs*2fprometheus))",
title: 'View in CloudWatch console',
},
],
},
},
],
refId: 'A',
},
],
});
});
it('should add data links to response from log groups, even without trimming :*', async () => {
const mockResponse: DataQueryResponse = {
data: [
{
fields: [
{
name: '@message',
config: {},
},
],
refId: 'A',
},
],
};
const mockOptions = {
targets: [
{
refId: 'A',
expression: 'stats count(@message) by bin(1h)',
logGroups: [{ value: 'arn:aws:logs:us-east-1:111111111111:log-group:/aws/lambda/test' }],
region: 'us-east-1',
} as CloudWatchQuery,
],
} as DataQueryRequest<CloudWatchQuery>;
await addDataLinksToLogsResponse(
mockResponse,
mockOptions,
{ ...time, raw: time },
(s) => s ?? '',
(v) => [v] ?? [],
(r) => r
);
expect(mockResponse).toMatchObject({
data: [
{
fields: [
{
name: '@message',
config: {
links: [
{
url: "https://us-east-1.console.aws.amazon.com/cloudwatch/home?region=us-east-1#logs-insights:queryDetail=~(end~'2016-12-31T16*3a00*3a00.000Z~start~'2016-12-31T15*3a00*3a00.000Z~timeType~'ABSOLUTE~tz~'UTC~editorString~'stats*20count*28*40message*29*20by*20bin*281h*29~isLiveTail~false~source~(~'arn*3aaws*3alogs*3aus-east-1*3a111111111111*3alog-group*3a*2faws*2flambda*2ftest))",
title: 'View in CloudWatch console',
},
],
},
},
],
refId: 'A',
},
],
});
});
});

View File

@ -70,8 +70,16 @@ function createAwsConsoleLink(
replace: (target: string, fieldName?: string) => string,
getVariableValue: (value: string) => string[]
) {
const arns = target.logGroups?.flatMap((group) => {
if (group.value === undefined) {
return [];
}
return [group.value.replace(/:\*$/, '')]; // remove `:*` from end of arn
});
const logGroupNames = target.logGroupNames;
const sources = arns ?? logGroupNames;
const interpolatedExpression = target.expression ? replace(target.expression) : '';
const interpolatedGroups = target.logGroupNames?.flatMap(getVariableValue) ?? [];
const interpolatedGroups = sources?.flatMap(getVariableValue) ?? [];
const urlProps: AwsUrl = {
end: range.to.toISOString(),