@@ -95,6 +95,7 @@ type namespaceDetail struct {
9595// New creates a new instance of the MetricSet. New is responsible for unpacking
9696// any MetricSet specific configuration options if there are any.
9797func New (base mb.BaseMetricSet ) (mb.MetricSet , error ) {
98+ logger := logp .NewLogger (metricsetName )
9899 metricSet , err := aws .NewMetricSet (base )
99100 if err != nil {
100101 return nil , errors .Wrap (err , "error creating aws metricset" )
@@ -109,13 +110,14 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
109110 return nil , errors .Wrap (err , "error unpack raw module config using UnpackConfig" )
110111 }
111112
113+ logger .Debugf ("cloudwatch config = %s" , config )
112114 if len (config .CloudwatchMetrics ) == 0 {
113115 return nil , errors .New ("metrics in config is missing" )
114116 }
115117
116118 return & MetricSet {
117119 MetricSet : metricSet ,
118- logger : logp . NewLogger ( metricsetName ) ,
120+ logger : logger ,
119121 CloudwatchConfigs : config .CloudwatchMetrics ,
120122 }, nil
121123}
@@ -288,6 +290,12 @@ func (m *MetricSet) readCloudwatchConfig() (listMetricWithDetail, map[string][]n
288290 resourceTypesWithTags := map [string ][]aws.Tag {}
289291
290292 for _ , config := range m .CloudwatchConfigs {
293+ // If tags_filter on metricset level is given, overwrite tags in
294+ // cloudwatch metrics with tags_filter.
295+ if m .MetricSet .TagsFilter != nil {
296+ config .Tags = m .MetricSet .TagsFilter
297+ }
298+
291299 // If there is no statistic method specified, then use the default.
292300 if config .Statistic == nil {
293301 config .Statistic = defaultStatistics
@@ -461,6 +469,8 @@ func (m *MetricSet) createEvents(svcCloudwatch cloudwatchiface.ClientAPI, svcRes
461469
462470 // Find a timestamp for all metrics in output
463471 timestamp := aws .FindTimestamp (metricDataResults )
472+
473+ // Create events when there is no tags_filter or tags.resource_type_filter specified.
464474 if len (resourceTypeTagFilters ) == 0 {
465475 if ! timestamp .IsZero () {
466476 for _ , output := range metricDataResults {
@@ -492,8 +502,10 @@ func (m *MetricSet) createEvents(svcCloudwatch cloudwatchiface.ClientAPI, svcRes
492502 return events , nil
493503 }
494504
495- // Get tags
505+ // Create events with tags
496506 for resourceType , tagsFilter := range resourceTypeTagFilters {
507+ m .logger .Debugf ("resourceType = %s" , resourceType )
508+ m .logger .Debugf ("tagsFilter = %s" , tagsFilter )
497509 resourceTagMap , err := aws .GetResourcesTags (svcResourceAPI , []string {resourceType })
498510 if err != nil {
499511 // If GetResourcesTags failed, continue report event just without tags.
@@ -507,8 +519,11 @@ func (m *MetricSet) createEvents(svcCloudwatch cloudwatchiface.ClientAPI, svcRes
507519 // filter resourceTagMap
508520 for identifier , tags := range resourceTagMap {
509521 if exists := aws .CheckTagFiltersExist (tagsFilter , tags ); ! exists {
522+ m .logger .Debugf ("In region %s, service %s tags does not match tags_filter" , regionName , identifier )
510523 delete (resourceTagMap , identifier )
524+ continue
511525 }
526+ m .logger .Debugf ("In region %s, service %s tags match tags_filter" , regionName , identifier )
512527 }
513528
514529 if ! timestamp .IsZero () {
@@ -537,6 +552,12 @@ func (m *MetricSet) createEvents(svcCloudwatch cloudwatchiface.ClientAPI, svcRes
537552
538553 identifierValue := labels [identifierValueIdx ]
539554 if _ , ok := events [identifierValue ]; ! ok {
555+ // when tagsFilter is not empty but no entry in
556+ // resourceTagMap for this identifier, do not initialize
557+ // an event for this identifier.
558+ if len (tagsFilter ) != 0 && resourceTagMap [identifierValue ] == nil {
559+ continue
560+ }
540561 events [identifierValue ] = aws .InitEvent (regionName , m .AccountName , m .AccountID )
541562 }
542563 events [identifierValue ] = insertRootFields (events [identifierValue ], output .Values [timestampIdx ], labels )
0 commit comments