Skip to content

Commit a17342f

Browse files
Move default associator to its own package. (prometheus-community#908)
1 parent bb1f667 commit a17342f

File tree

3 files changed

+36
-27
lines changed

3 files changed

+36
-27
lines changed

pkg/job/associator.go renamed to pkg/job/associator/associator.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package job
1+
package associator
22

33
import (
44
"strings"
@@ -15,11 +15,11 @@ import (
1515
type valueToResource map[string]*model.TaggedResource
1616

1717
// metricsToResourceAssociator contains for each dimension, the matched values and resources.
18-
type metricsToResourceAssociator map[string]valueToResource
18+
type Associator map[string]valueToResource
1919

20-
// newMetricsToResourceAssociator creates a new metricsToResourceAssociator given a set of dimensions regexs that can extract
20+
// NewAssociator creates a new metricsToResourceAssociator given a set of dimensions regexs that can extract
2121
// dimensions from a resource ARN, and a set of resources from which to extract.
22-
func newMetricsToResourceAssociator(dimensionRegexps []*regexp.Regexp, resources []*model.TaggedResource) metricsToResourceAssociator {
22+
func NewAssociator(dimensionRegexps []*regexp.Regexp, resources []*model.TaggedResource) Associator {
2323
dimensionsFilter := make(map[string]valueToResource)
2424
for _, dimensionRegexp := range dimensionRegexps {
2525
names := dimensionRegexp.SubexpNames()
@@ -48,7 +48,7 @@ func newMetricsToResourceAssociator(dimensionRegexps []*regexp.Regexp, resources
4848
// AssociateMetricToResource finds, for a given cloudwatch.Metrics, the resource that matches the better.
4949
// If no match is found, nil is returned. Also, there are some conditions where the metric shouldn't be
5050
// considered, and that is dictated by the skip return value.
51-
func (asoc metricsToResourceAssociator) AssociateMetricToResource(cwMetric *cloudwatch.Metric) (*model.TaggedResource, bool) {
51+
func (asoc Associator) AssociateMetricToResource(cwMetric *cloudwatch.Metric) (*model.TaggedResource, bool) {
5252
var r *model.TaggedResource
5353
skip := false
5454
alreadyFound := false

pkg/job/associator_test.go renamed to pkg/job/associator/associator_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package job
1+
package associator
22

33
import (
44
"fmt"
@@ -290,7 +290,7 @@ func TestAssociator(t *testing.T) {
290290
t.Skip("failure is expected. Remove skip after https://github.com/nerdswords/yet-another-cloudwatch-exporter/issues/821 is fixed.")
291291
return
292292
}
293-
associator := newMetricsToResourceAssociator(tc.args.dimensionRegexps, tc.args.resources)
293+
associator := NewAssociator(tc.args.dimensionRegexps, tc.args.resources)
294294
res, skip := associator.AssociateMetricToResource(tc.args.metric)
295295
require.Equal(t, tc.expectedSkip, skip)
296296
require.Equal(t, tc.expectedResource, res)

pkg/job/discovery.go

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,21 @@ import (
99
"sync"
1010

1111
"github.com/aws/aws-sdk-go/service/cloudwatch"
12-
"github.com/grafana/regexp"
1312

1413
"github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/apicloudwatch"
1514
"github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/apitagging"
1615
"github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/config"
16+
"github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/job/associator"
1717
"github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/job/maxdimassociator"
1818
"github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/logging"
1919
"github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/model"
2020
"github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/session"
2121
)
2222

23+
type resourceAssociator interface {
24+
AssociateMetricToResource(cwMetric *cloudwatch.Metric) (*model.TaggedResource, bool)
25+
}
26+
2327
func runDiscoveryJob(
2428
ctx context.Context,
2529
logger logging.Logger,
@@ -199,8 +203,19 @@ func getMetricDataForQueries(
199203
for _, metric := range discoveryJob.Metrics {
200204
go func(metric *config.Metric) {
201205
defer wg.Done()
206+
207+
var assoc resourceAssociator
208+
if config.FlagsFromCtx(ctx).IsFeatureEnabled(config.MaxDimensionsAssociator) {
209+
assoc = maxdimassociator.NewAssociator(svc.DimensionRegexps, resources)
210+
} else {
211+
assoc = associator.NewAssociator(svc.DimensionRegexps, resources)
212+
}
213+
if logger.IsDebugEnabled() {
214+
logger.Debug("associator", assoc)
215+
}
216+
202217
_, err := clientCloudwatch.ListMetrics(ctx, svc.Namespace, metric, func(page *cloudwatch.ListMetricsOutput) {
203-
data := getFilteredMetricDatas(ctx, logger, region, accountID, discoveryJob.Type, discoveryJob.CustomTags, tagsOnMetrics, svc.DimensionRegexps, resources, page.Metrics, discoveryJob.DimensionNameRequirements, metric)
218+
data := getFilteredMetricDatas(ctx, logger, region, accountID, discoveryJob.Type, discoveryJob.CustomTags, tagsOnMetrics, page.Metrics, discoveryJob.DimensionNameRequirements, metric, assoc)
204219

205220
mux.Lock()
206221
getMetricDatas = append(getMetricDatas, data...)
@@ -217,13 +232,23 @@ func getMetricDataForQueries(
217232
go func(metric *config.Metric) {
218233
defer wg.Done()
219234

235+
var assoc resourceAssociator
236+
if config.FlagsFromCtx(ctx).IsFeatureEnabled(config.MaxDimensionsAssociator) {
237+
assoc = maxdimassociator.NewAssociator(svc.DimensionRegexps, resources)
238+
} else {
239+
assoc = associator.NewAssociator(svc.DimensionRegexps, resources)
240+
}
241+
if logger.IsDebugEnabled() {
242+
logger.Debug("associator", assoc)
243+
}
244+
220245
metricsList, err := clientCloudwatch.ListMetrics(ctx, svc.Namespace, metric, nil)
221246
if err != nil {
222247
logger.Error(err, "Failed to get full metric list", "metric_name", metric.Name, "namespace", svc.Namespace)
223248
return
224249
}
225250

226-
data := getFilteredMetricDatas(ctx, logger, region, accountID, discoveryJob.Type, discoveryJob.CustomTags, tagsOnMetrics, svc.DimensionRegexps, resources, metricsList.Metrics, discoveryJob.DimensionNameRequirements, metric)
251+
data := getFilteredMetricDatas(ctx, logger, region, accountID, discoveryJob.Type, discoveryJob.CustomTags, tagsOnMetrics, metricsList.Metrics, discoveryJob.DimensionNameRequirements, metric, assoc)
227252

228253
mux.Lock()
229254
getMetricDatas = append(getMetricDatas, data...)
@@ -244,27 +269,11 @@ func getFilteredMetricDatas(
244269
namespace string,
245270
customTags []model.Tag,
246271
tagsOnMetrics model.ExportedTagsOnMetrics,
247-
dimensionRegexps []*regexp.Regexp,
248-
resources []*model.TaggedResource,
249272
metricsList []*cloudwatch.Metric,
250273
dimensionNameList []string,
251274
m *config.Metric,
275+
assoc resourceAssociator,
252276
) []*model.CloudwatchData {
253-
type resourceAssociator interface {
254-
AssociateMetricToResource(cwMetric *cloudwatch.Metric) (*model.TaggedResource, bool)
255-
}
256-
257-
var assoc resourceAssociator
258-
if config.FlagsFromCtx(ctx).IsFeatureEnabled(config.MaxDimensionsAssociator) {
259-
assoc = maxdimassociator.NewAssociator(dimensionRegexps, resources)
260-
} else {
261-
assoc = newMetricsToResourceAssociator(dimensionRegexps, resources)
262-
}
263-
264-
if logger.IsDebugEnabled() {
265-
logger.Debug("FilterMetricData DimensionsFilter", "dimensionsFilter", assoc)
266-
}
267-
268277
getMetricsData := make([]*model.CloudwatchData, 0, len(metricsList))
269278
for _, cwMetric := range metricsList {
270279
if len(dimensionNameList) > 0 && !metricDimensionsMatchNames(cwMetric, dimensionNameList) {

0 commit comments

Comments
 (0)