Skip to content

Commit cb42c42

Browse files
resource association logic tries both with and without fixes
1 parent 5d9a043 commit cb42c42

File tree

2 files changed

+73
-32
lines changed

2 files changed

+73
-32
lines changed

pkg/job/maxdimassociator/associator.go

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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

pkg/job/maxdimassociator/associator_mq_test.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ var rabbitMQBroker = &model.TaggedResource{
2727
Namespace: "AWS/AmazonMQ",
2828
}
2929

30+
var rabbitMQBrokerWithActiveStyleName = &model.TaggedResource{
31+
ARN: "arn:aws:mq:us-east-2:123456789012:broker:rabbitmq-broker-0:b-000-111-222-333",
32+
Namespace: "AWS/AmazonMQ",
33+
}
34+
3035
var activeMQBroker = &model.TaggedResource{
3136
ARN: "arn:aws:mq:us-east-2:123456789012:broker:activemq-broker:b-000-111-222-333",
3237
Namespace: "AWS/AmazonMQ",
@@ -63,11 +68,27 @@ func TestAssociatorMQ(t *testing.T) {
6368
expectedSkip: false,
6469
expectedResource: rabbitMQBroker,
6570
},
71+
{
72+
name: "should match with Broker dimension when broker name has a number suffix and does match ARN",
73+
args: args{
74+
dimensionRegexps: config.SupportedServices.GetService("AWS/AmazonMQ").ToModelDimensionsRegexp(),
75+
resources: []*model.TaggedResource{rabbitMQBrokerWithActiveStyleName},
76+
metric: &model.Metric{
77+
MetricName: "ProducerCount",
78+
Namespace: "AWS/AmazonMQ",
79+
Dimensions: []model.Dimension{
80+
{Name: "Broker", Value: "rabbitmq-broker-0"},
81+
},
82+
},
83+
},
84+
expectedSkip: false,
85+
expectedResource: rabbitMQBrokerWithActiveStyleName,
86+
},
6687
{
6788
// ActiveMQ allows active/standby modes where the `Broker` dimension has values
6889
// like `brokername-1` and `brokername-2` which don't match the ARN (the dimension
6990
// regex will extract `Broker` as `brokername` from ARN)
70-
name: "should match with Broker dimension when broker name has a number suffix",
91+
name: "should match with Broker dimension when broker name has a number suffix and doesn't match ARN",
7192
args: args{
7293
dimensionRegexps: config.SupportedServices.GetService("AWS/AmazonMQ").ToModelDimensionsRegexp(),
7394
resources: []*model.TaggedResource{activeMQBroker},

0 commit comments

Comments
 (0)