Skip to content

Commit 0d22bbf

Browse files
simplify loop and add comment
Signed-off-by: Tristan Burgess <[email protected]>
1 parent ad742e4 commit 0d22bbf

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

pkg/job/maxdimassociator/associator.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,17 +174,25 @@ func (assoc Associator) AssociateMetricToResource(cwMetric *model.Metric) (*mode
174174

175175
// A regex mapping has been found. The metric has all (and possibly more)
176176
// the dimensions computed for the mapping. Now compute a signature
177-
// of the labels (names and values) of the dimensions of this mapping.
177+
// of the labels (names and values) of the dimensions of this mapping, and try to
178+
// find a resource match.
179+
// This loop can run up to two times:
180+
// On the first iteration, special-case dimension value
181+
// fixes to match the value up with the resource ARN are applied to particular namespaces.
182+
// The second iteration will only run if a fix was applied for one of the special-case
183+
// namespaces and no match was found. It will try to find a match without applying the fixes.
184+
// This covers cases where the dimension value does line up with the resource ARN.
178185
mappingFound = true
179186
dimFixApplied := false
180-
for _, fun := range []func(string, model.Dimension) (model.Dimension, bool){fixDimension, nil} {
181-
if !dimFixApplied && fun == nil {
187+
shouldTryFixDimension := true
188+
for {
189+
if !dimFixApplied && !shouldTryFixDimension {
182190
// If no dimension fixes were applied, no need to try running again without the fixer
183191
break
184192
}
185193

186194
var labels map[string]string
187-
labels, dimFixApplied = buildLabelsMap(cwMetric, regexpMapping, fun)
195+
labels, dimFixApplied = buildLabelsMap(cwMetric, regexpMapping, shouldTryFixDimension)
188196
signature := prom_model.LabelsToSignature(labels)
189197

190198
// Check if there's an entry for the labels (names and values) of the metric,
@@ -196,6 +204,7 @@ func (assoc Associator) AssociateMetricToResource(cwMetric *model.Metric) (*mode
196204

197205
// No resource was matched for the current signature.
198206
logger.Debug("resource signature attempt not matched", "signature", signature)
207+
shouldTryFixDimension = false
199208
}
200209

201210
// No resource was matched for any signature, continue iterating across the
@@ -219,13 +228,13 @@ func (assoc Associator) AssociateMetricToResource(cwMetric *model.Metric) (*mode
219228
// buildLabelsMap returns a map of labels names and values, as well as whether the dimension fixer was applied.
220229
// For some namespaces, values might need to be modified in order
221230
// to match the dimension value extracted from ARN.
222-
func buildLabelsMap(cwMetric *model.Metric, regexpMapping *dimensionsRegexpMapping, dimFixer func(string, model.Dimension) (model.Dimension, bool)) (map[string]string, bool) {
231+
func buildLabelsMap(cwMetric *model.Metric, regexpMapping *dimensionsRegexpMapping, shouldTryFixDimension bool) (map[string]string, bool) {
223232
labels := make(map[string]string, len(cwMetric.Dimensions))
224233
dimFixApplied := false
225234
for _, rDimension := range regexpMapping.dimensions {
226235
for _, mDimension := range cwMetric.Dimensions {
227-
if dimFixer != nil {
228-
mDimension, dimFixApplied = dimFixer(cwMetric.Namespace, mDimension)
236+
if shouldTryFixDimension {
237+
mDimension, dimFixApplied = fixDimension(cwMetric.Namespace, mDimension)
229238
}
230239

231240
if rDimension == mDimension.Name {
@@ -236,6 +245,8 @@ func buildLabelsMap(cwMetric *model.Metric, regexpMapping *dimensionsRegexpMappi
236245
return labels, dimFixApplied
237246
}
238247

248+
// fixDimension modifies the dimension value to accommodate special cases where
249+
// the dimension value doesn't match the resource ARN.
239250
func fixDimension(namespace string, dim model.Dimension) (model.Dimension, bool) {
240251
// AmazonMQ is special - for active/standby ActiveMQ brokers,
241252
// the value of the "Broker" dimension contains a number suffix

0 commit comments

Comments
 (0)