@@ -176,19 +176,31 @@ func (assoc Associator) AssociateMetricToResource(cwMetric *model.Metric) (*mode
176176 // the dimensions computed for the mapping. Now compute a signature
177177 // of the labels (names and values) of the dimensions of this mapping.
178178 mappingFound = true
179- labels := buildLabelsMap (cwMetric , regexpMapping )
180- signature := prom_model .LabelsToSignature (labels )
179+ dimFixApplied := false
180+ for _ , fun := range []func (string , model.Dimension ) (model.Dimension , bool ){fixDimension , nil } {
181+ if ! dimFixApplied && fun == nil {
182+ // If no dimension fixes were applied, no need to try running again without the fixer
183+ break
184+ }
185+
186+ var labels map [string ]string
187+ labels , dimFixApplied = buildLabelsMap (cwMetric , regexpMapping , fun )
188+ signature := prom_model .LabelsToSignature (labels )
189+
190+ // Check if there's an entry for the labels (names and values) of the metric,
191+ // and return the resource in case.
192+ if resource , ok := regexpMapping .dimensionsMapping [signature ]; ok {
193+ logger .Debug ("resource matched" , "signature" , signature )
194+ return resource , false
195+ }
181196
182- // Check if there's an entry for the labels (names and values) of the metric,
183- // and return the resource in case.
184- if resource , ok := regexpMapping .dimensionsMapping [signature ]; ok {
185- logger .Debug ("resource matched" , "signature" , signature )
186- return resource , false
197+ // No resource was matched for the current signature.
198+ logger .Debug ("resource signature attempt not matched" , "signature" , signature )
187199 }
188200
189- // Otherwise , continue iterating across the rest of regex mappings
190- // to attempt to find another one with fewer dimensions.
191- logger .Debug ("resource not matched" , "signature" , signature )
201+ // No resource was matched for any signature , continue iterating across the
202+ // rest of regex mappings to attempt to find another one with fewer dimensions.
203+ logger .Debug ("resource not matched" )
192204 }
193205 }
194206
@@ -204,38 +216,46 @@ func (assoc Associator) AssociateMetricToResource(cwMetric *model.Metric) (*mode
204216 return nil , mappingFound
205217}
206218
207- // buildLabelsMap returns a map of labels names and values.
219+ // buildLabelsMap returns a map of labels names and values, as well as whether the dimension fixer was applied .
208220// For some namespaces, values might need to be modified in order
209221// to match the dimension value extracted from ARN.
210- func buildLabelsMap (cwMetric * model.Metric , regexpMapping * dimensionsRegexpMapping ) map [string ]string {
222+ func buildLabelsMap (cwMetric * model.Metric , regexpMapping * dimensionsRegexpMapping , dimFixer func ( string , model. Dimension ) (model. Dimension , bool )) ( map [string ]string , bool ) {
211223 labels := make (map [string ]string , len (cwMetric .Dimensions ))
224+ dimFixApplied := false
212225 for _ , rDimension := range regexpMapping .dimensions {
213226 for _ , mDimension := range cwMetric .Dimensions {
214- name := mDimension .Name
215- value := mDimension .Value
216-
217- // AmazonMQ is special - for active/standby ActiveMQ brokers,
218- // the value of the "Broker" dimension contains a number suffix
219- // that is not part of the resource ARN
220- if cwMetric .Namespace == "AWS/AmazonMQ" && name == "Broker" {
221- if amazonMQBrokerSuffix .MatchString (value ) {
222- value = amazonMQBrokerSuffix .ReplaceAllString (value , "" )
223- }
224- }
225-
226- // AWS Sagemaker endpoint name may have upper case characters
227- // Resource ARN is only in lower case, hence transforming
228- // endpoint name value to be able to match the resource ARN
229- if cwMetric .Namespace == "AWS/SageMaker" && name == "EndpointName" {
230- value = strings .ToLower (value )
227+ if dimFixer != nil {
228+ mDimension , dimFixApplied = dimFixer (cwMetric .Namespace , mDimension )
231229 }
232230
233231 if rDimension == mDimension .Name {
234- labels [name ] = value
232+ labels [mDimension . Name ] = mDimension . Value
235233 }
236234 }
237235 }
238- return labels
236+ return labels , dimFixApplied
237+ }
238+
239+ func fixDimension (namespace string , dim model.Dimension ) (model.Dimension , bool ) {
240+ // AmazonMQ is special - for active/standby ActiveMQ brokers,
241+ // the value of the "Broker" dimension contains a number suffix
242+ // that is not part of the resource ARN
243+ if namespace == "AWS/AmazonMQ" && dim .Name == "Broker" {
244+ if amazonMQBrokerSuffix .MatchString (dim .Value ) {
245+ dim .Value = amazonMQBrokerSuffix .ReplaceAllString (dim .Value , "" )
246+ return dim , true
247+ }
248+ }
249+
250+ // AWS Sagemaker endpoint name may have upper case characters
251+ // Resource ARN is only in lower case, hence transforming
252+ // endpoint name value to be able to match the resource ARN
253+ if namespace == "AWS/SageMaker" && dim .Name == "EndpointName" {
254+ dim .Value = strings .ToLower (dim .Value )
255+ return dim , true
256+ }
257+
258+ return dim , false
239259}
240260
241261// containsAll returns true if a contains all elements of b
0 commit comments