diff --git a/public/app/plugins/datasource/cloudwatch/utils/datalinks.test.ts b/public/app/plugins/datasource/cloudwatch/utils/datalinks.test.ts index bc0fa5e7380..f83c4829c5b 100644 --- a/public/app/plugins/datasource/cloudwatch/utils/datalinks.test.ts +++ b/public/app/plugins/datasource/cloudwatch/utils/datalinks.test.ts @@ -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; + + 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; + + 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', + }, + ], + }); + }); }); diff --git a/public/app/plugins/datasource/cloudwatch/utils/datalinks.ts b/public/app/plugins/datasource/cloudwatch/utils/datalinks.ts index e0f8f05c6a7..3cfcb7cd7d4 100644 --- a/public/app/plugins/datasource/cloudwatch/utils/datalinks.ts +++ b/public/app/plugins/datasource/cloudwatch/utils/datalinks.ts @@ -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(),