@@ -445,7 +445,9 @@ func Test_executeStartQuery(t *testing.T) {
445445
446446 t .Run ("attaches logGroupIdentifiers if the crossAccount feature is enabled" , func (t * testing.T ) {
447447 cli = fakeCWLogsClient {}
448- ds := newTestDatasource ()
448+ ds := newTestDatasource (func (ds * DataSource ) {
449+ ds .monitoringAccountCache .Store ("us-east-1" , true )
450+ })
449451
450452 _ , err := ds .QueryData (contextWithFeaturesEnabled (features .FlagCloudWatchCrossAccountQuerying ), & backend.QueryDataRequest {
451453 PluginContext : backend.PluginContext {DataSourceInstanceSettings : & backend.DataSourceInstanceSettings {}},
@@ -459,7 +461,8 @@ func Test_executeStartQuery(t *testing.T) {
459461 "limit": 12,
460462 "queryLanguage": "CWLI",
461463 "queryString":"fields @message",
462- "logGroups":[{"arn": "fakeARN"}]
464+ "logGroups":[{"arn": "fakeARN"}],
465+ "region": "us-east-1"
463466 }` ),
464467 },
465468 },
@@ -480,7 +483,9 @@ func Test_executeStartQuery(t *testing.T) {
480483
481484 t .Run ("attaches logGroupIdentifiers if the crossAccount feature is enabled and strips out trailing *" , func (t * testing.T ) {
482485 cli = fakeCWLogsClient {}
483- ds := newTestDatasource ()
486+ ds := newTestDatasource (func (ds * DataSource ) {
487+ ds .monitoringAccountCache .Store ("us-east-1" , true )
488+ })
484489
485490 _ , err := ds .QueryData (contextWithFeaturesEnabled (features .FlagCloudWatchCrossAccountQuerying ), & backend.QueryDataRequest {
486491 PluginContext : backend.PluginContext {DataSourceInstanceSettings : & backend.DataSourceInstanceSettings {}},
@@ -493,7 +498,8 @@ func Test_executeStartQuery(t *testing.T) {
493498 "subtype": "StartQuery",
494499 "limit": 12,
495500 "queryString":"fields @message",
496- "logGroups":[{"arn": "*fake**ARN*"}]
501+ "logGroups":[{"arn": "*fake**ARN*"}],
502+ "region": "us-east-1"
497503 }` ),
498504 },
499505 },
@@ -512,6 +518,44 @@ func Test_executeStartQuery(t *testing.T) {
512518 }, cli .calls .startQuery )
513519 })
514520
521+ t .Run ("queries by LogGroupNames on StartQueryInput when queried region is not a monitoring account region for the data source" , func (t * testing.T ) {
522+ cli = fakeCWLogsClient {}
523+ ds := newTestDatasource (func (ds * DataSource ) {
524+ // note that the query's region is set to us-east-2, but the data source is only a monitoring account in us-east-1 so it should query by LogGroupNames
525+ ds .monitoringAccountCache .Store ("us-east-1" , true )
526+ })
527+
528+ _ , err := ds .QueryData (contextWithFeaturesEnabled (features .FlagCloudWatchCrossAccountQuerying ), & backend.QueryDataRequest {
529+ PluginContext : backend.PluginContext {DataSourceInstanceSettings : & backend.DataSourceInstanceSettings {}},
530+ Queries : []backend.DataQuery {
531+ {
532+ RefID : "A" ,
533+ TimeRange : backend.TimeRange {From : time .Unix (0 , 0 ), To : time .Unix (1 , 0 )},
534+ JSON : json .RawMessage (`{
535+ "type": "logAction",
536+ "subtype": "StartQuery",
537+ "limit": 12,
538+ "queryString":"fields @message",
539+ "logGroups":[{"arn": "arn:aws:logs:us-east-1:123456789012:log-group:group","name":"/log-group"}],
540+ "region": "us-east-2"
541+ }` ),
542+ },
543+ },
544+ })
545+
546+ assert .NoError (t , err )
547+ assert .Equal (t , []* cloudwatchlogs.StartQueryInput {
548+ {
549+ StartTime : aws .Int64 (0 ),
550+ EndTime : aws .Int64 (1 ),
551+ Limit : aws .Int32 (12 ),
552+ QueryString : aws .String ("fields @timestamp,ltrim(@log) as __log__grafana_internal__,ltrim(@logStream) as __logstream__grafana_internal__|fields @message" ),
553+ LogGroupNames : []string {"/log-group" },
554+ QueryLanguage : cloudwatchlogstypes .QueryLanguageCwli ,
555+ },
556+ }, cli .calls .startQuery )
557+ })
558+
515559 t .Run ("uses LogGroupNames if the cross account feature flag is not enabled, and log group names is present" , func (t * testing.T ) {
516560 cli = fakeCWLogsClient {}
517561 ds := newTestDatasource ()
@@ -545,6 +589,42 @@ func Test_executeStartQuery(t *testing.T) {
545589 }, cli .calls .startQuery )
546590 })
547591
592+ t .Run ("deduplicates log group names when derived from logGroups" , func (t * testing.T ) {
593+ cli = fakeCWLogsClient {}
594+ ds := newTestDatasource ()
595+
596+ _ , err := ds .QueryData (context .Background (), & backend.QueryDataRequest {
597+ PluginContext : backend.PluginContext {DataSourceInstanceSettings : & backend.DataSourceInstanceSettings {}},
598+ Queries : []backend.DataQuery {
599+ {
600+ RefID : "A" ,
601+ TimeRange : backend.TimeRange {From : time .Unix (0 , 0 ), To : time .Unix (1 , 0 )},
602+ JSON : json .RawMessage (`{
603+ "type": "logAction",
604+ "subtype": "StartQuery",
605+ "limit": 12,
606+ "queryString":"fields @message",
607+ "logGroups":[
608+ {"arn": "arn:aws:logs:us-east-1:123456789012:log-group:group1","name":"/log-group"},
609+ {"arn": "arn:aws:logs:us-east-1:123456789012:log-group:group2","name":"/log-group"}
610+ ]
611+ }` ),
612+ },
613+ },
614+ })
615+ assert .NoError (t , err )
616+ assert .Equal (t , []* cloudwatchlogs.StartQueryInput {
617+ {
618+ StartTime : aws .Int64 (0 ),
619+ EndTime : aws .Int64 (1 ),
620+ Limit : aws .Int32 (12 ),
621+ QueryString : aws .String ("fields @timestamp,ltrim(@log) as __log__grafana_internal__,ltrim(@logStream) as __logstream__grafana_internal__|fields @message" ),
622+ LogGroupNames : []string {"/log-group" },
623+ QueryLanguage : cloudwatchlogstypes .QueryLanguageCwli ,
624+ },
625+ }, cli .calls .startQuery )
626+ })
627+
548628 t .Run ("ignores logGroups if feature flag is disabled even if logGroupNames is not present" , func (t * testing.T ) {
549629 cli = fakeCWLogsClient {}
550630 ds := newTestDatasource ()
@@ -600,12 +680,12 @@ func Test_executeStartQuery(t *testing.T) {
600680 assert .NoError (t , err )
601681 assert .Equal (t , []* cloudwatchlogs.StartQueryInput {
602682 {
603- StartTime : aws .Int64 (0 ),
604- EndTime : aws .Int64 (1 ),
605- Limit : aws .Int32 (12 ),
606- QueryString : aws .String ("fields @timestamp,ltrim(@log) as __log__grafana_internal__,ltrim(@logStream) as __logstream__grafana_internal__|fields @message" ),
607- LogGroupIdentifiers : []string {"*fake**ARN " },
608- QueryLanguage : cloudwatchlogstypes .QueryLanguageCwli ,
683+ StartTime : aws .Int64 (0 ),
684+ EndTime : aws .Int64 (1 ),
685+ Limit : aws .Int32 (12 ),
686+ QueryString : aws .String ("fields @timestamp,ltrim(@log) as __log__grafana_internal__,ltrim(@logStream) as __logstream__grafana_internal__|fields @message" ),
687+ LogGroupNames : []string {"/log-group " },
688+ QueryLanguage : cloudwatchlogstypes .QueryLanguageCwli ,
609689 },
610690 }, cli .calls .startQuery )
611691 })
0 commit comments