Skip to content

Commit

Permalink
signalfxexporter: Update exclude_metrics option to use dpfilters.Metr…
Browse files Browse the repository at this point in the history
…icFilter
  • Loading branch information
asuresh4 committed Jan 15, 2021
1 parent ebd62bd commit 9169c99
Show file tree
Hide file tree
Showing 15 changed files with 202 additions and 126 deletions.
7 changes: 4 additions & 3 deletions exporter/signalfxexporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ The following configuration options can also be configured:
configuration option for [SignalFx
receiver](../../receiver/signalfxreceiver/README.md) to preserve datapoint
origin.
- `exclude_metrics`: metric names that will be excluded from sending
to Signalfx backend. If `send_compatible_metrics` or `translation_rules`
options are enabled, the exclusion will be applied on translated metrics.
- `exclude_metrics`: List of metric filters that will determine metrics to be
excluded from sending to Signalfx backend. If `send_compatible_metrics`
or `translation_rules` options are enabled, the exclusion will be applied
on translated metrics. See [here](./testdata/config.yaml) for examples.
- `headers` (no default): Headers to pass in the payload.
- `log_dimension_updates` (default = `false`): Whether or not to log dimension
updates.
Expand Down
10 changes: 6 additions & 4 deletions exporter/signalfxexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/correlation"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/translation"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/translation/dpfilters"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk"
)

Expand Down Expand Up @@ -85,10 +86,11 @@ type Config struct {
// And keep `override=true` in resourcedetection config.
SyncHostMetadata bool `mapstructure:"sync_host_metadata"`

// ExcludeMetrics defines metrics that will be excluded from sending to Signalfx
// backend. If translations enabled with SendCompatibleMetrics or TranslationRules
// options, the exclusion will be applied on translated metrics.
ExcludeMetrics []string `mapstructure:"exclude_metrics"`
// ExcludeMetrics defines dpfilter.MetricFilters that will determine metrics to be
// excluded from sending to SignalFx backend. If translations enabled with
// SendCompatibleMetrics or TranslationRules options, the exclusion will be applied
// on translated metrics.
ExcludeMetrics []dpfilters.MetricFilter `mapstructure:"exclude_metrics"`

// Correlation configuration for syncing traces service and environment to metrics.
Correlation *correlation.Config `mapstructure:"correlation"`
Expand Down
21 changes: 21 additions & 0 deletions exporter/signalfxexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/correlation"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/translation"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/translation/dpfilters"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk"
)

Expand Down Expand Up @@ -94,6 +95,26 @@ func TestLoadConfig(t *testing.T) {
},
},
},
ExcludeMetrics: []dpfilters.MetricFilter{
{
MetricName: "metric1",
},
{
MetricNames: []string{"metric2", "metric3"},
},
{
MetricName: "metric4",
Dimensions: map[string]interface{}{
"dimension_key": "dimension_val",
},
},
{
MetricName: "metric5",
Dimensions: map[string]interface{}{
"dimension_key": []interface{}{"dimension_val1", "dimension_val2"},
},
},
},
DeltaTranslationTTL: 3600,
Correlation: &correlation.Config{
HTTPClientSettings: confighttp.HTTPClientSettings{
Expand Down
7 changes: 6 additions & 1 deletion exporter/signalfxexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ func newSignalFxExporter(

headers := buildHeaders(config)

converter, err := translation.NewMetricsConverter(logger, options.metricTranslator, config.ExcludeMetrics)
if err != nil {
return nil, fmt.Errorf("failed to create metric converter: %v", err)
}

dpClient := &sfxDPClient{
sfxClientBase: sfxClientBase{
ingestURL: options.ingestURL,
Expand All @@ -103,7 +108,7 @@ func newSignalFxExporter(
},
logger: logger,
accessTokenPassthrough: config.AccessTokenPassthrough,
converter: translation.NewMetricsConverter(logger, options.metricTranslator),
converter: converter,
}

dimClient := dimensions.NewDimensionClient(
Expand Down
20 changes: 18 additions & 2 deletions exporter/signalfxexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/dimensions"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/translation"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/translation/dpfilters"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/metrics"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk"
)
Expand All @@ -64,6 +65,15 @@ func TestNew(t *testing.T) {
},
wantErr: true,
},
{
name: "fails to create metrics converter",
config: &Config{
AccessToken: "test",
Realm: "realm",
ExcludeMetrics: []dpfilters.MetricFilter{{}},
},
wantErr: true,
},
{
name: "successfully create exporter",
config: &Config{
Expand Down Expand Up @@ -184,6 +194,9 @@ func TestConsumeMetrics(t *testing.T) {
serverURL, err := url.Parse(server.URL)
assert.NoError(t, err)

c, err := translation.NewMetricsConverter(zap.NewNop(), nil, nil)
require.NoError(t, err)
require.NotNil(t, c)
dpClient := &sfxDPClient{
sfxClientBase: sfxClientBase{
ingestURL: serverURL,
Expand All @@ -196,7 +209,7 @@ func TestConsumeMetrics(t *testing.T) {
}},
},
logger: zap.NewNop(),
converter: translation.NewMetricsConverter(zap.NewNop(), nil),
converter: c,
}

numDroppedTimeSeries, err := dpClient.pushMetricsData(context.Background(), tt.md)
Expand Down Expand Up @@ -961,6 +974,9 @@ func BenchmarkExporterConsumeData(b *testing.B) {
serverURL, err := url.Parse(server.URL)
assert.NoError(b, err)

c, err := translation.NewMetricsConverter(zap.NewNop(), nil, nil)
require.NoError(b, err)
require.NotNil(b, c)
dpClient := &sfxDPClient{
sfxClientBase: sfxClientBase{
ingestURL: serverURL,
Expand All @@ -972,7 +988,7 @@ func BenchmarkExporterConsumeData(b *testing.B) {
}},
},
logger: zap.NewNop(),
converter: translation.NewMetricsConverter(zap.NewNop(), nil),
converter: c,
}

for i := 0; i < b.N; i++ {
Expand Down
4 changes: 0 additions & 4 deletions exporter/signalfxexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,6 @@ func setTranslationRules(cfg *Config) error {
}
cfg.TranslationRules = defaultRules
}
if len(cfg.ExcludeMetrics) > 0 {
cfg.TranslationRules = append(cfg.TranslationRules,
translation.GetExcludeMetricsRule(cfg.ExcludeMetrics))
}
return nil
}

Expand Down
47 changes: 11 additions & 36 deletions exporter/signalfxexporter/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/translation"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/translation/dpfilters"
)

func TestCreateDefaultConfig(t *testing.T) {
Expand Down Expand Up @@ -237,54 +238,27 @@ func TestCreateMetricsExporterWithSpecifiedTranslaitonRules(t *testing.T) {
}

func TestCreateMetricsExporterWithExcludedMetrics(t *testing.T) {
config := &Config{
ExporterSettings: configmodels.ExporterSettings{
TypeVal: configmodels.Type(typeStr),
NameVal: typeStr,
},
AccessToken: "testToken",
Realm: "us1",
ExcludeMetrics: []string{"metric1"},
}

te, err := createMetricsExporter(context.Background(), component.ExporterCreateParams{Logger: zap.NewNop()}, config)
require.NoError(t, err)
require.NotNil(t, te)

assert.Equal(t, 1, len(config.TranslationRules))
assert.Equal(t, translation.ActionDropMetrics, config.TranslationRules[0].Action)
assert.Equal(t, 1, len(config.TranslationRules[0].MetricNames))
assert.True(t, config.TranslationRules[0].MetricNames["metric1"])
}

func TestCreateMetricsExporterWithDefinedRulesAndExcludedMetrics(t *testing.T) {
config := &Config{
ExporterSettings: configmodels.ExporterSettings{
TypeVal: configmodels.Type(typeStr),
NameVal: typeStr,
},
AccessToken: "testToken",
Realm: "us1",
TranslationRules: []translation.Rule{
{
Action: translation.ActionRenameDimensionKeys,
Mapping: map[string]string{
"old_dimension": "new_dimension",
},
},
},
ExcludeMetrics: []string{"metric1"},
ExcludeMetrics: []dpfilters.MetricFilter{{
MetricName: "metric1",
}},
}

te, err := createMetricsExporter(context.Background(), component.ExporterCreateParams{Logger: zap.NewNop()}, config)
require.NoError(t, err)
require.NotNil(t, te)

assert.Equal(t, 2, len(config.TranslationRules))
assert.Equal(t, translation.ActionRenameDimensionKeys, config.TranslationRules[0].Action)
assert.Equal(t, translation.ActionDropMetrics, config.TranslationRules[1].Action)
assert.Equal(t, 1, len(config.TranslationRules[1].MetricNames))
assert.True(t, config.TranslationRules[1].MetricNames["metric1"])
// TODO: Assert exclude metrics are correctly configured.
//assert.Equal(t, 1, len(config.TranslationRules))
//assert.Equal(t, translation.ActionDropMetrics, config.TranslationRules[0].Action)
//require.Equal(t, 1, len(config.TranslationRules[0].MetricFilters))
//assert.Equal(t, "metric1", config.TranslationRules[0].MetricFilters[0].MetricName)
}

func TestDefaultTranslationRules(t *testing.T) {
Expand All @@ -295,7 +269,8 @@ func TestDefaultTranslationRules(t *testing.T) {
require.NoError(t, err)
data := testMetricsData()

c := translation.NewMetricsConverter(zap.NewNop(), tr)
c, err := translation.NewMetricsConverter(zap.NewNop(), tr, nil)
require.NoError(t, err)
translated := c.MetricDataToSignalFxV2(data)
require.NotNil(t, translated)

Expand Down
17 changes: 14 additions & 3 deletions exporter/signalfxexporter/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,20 @@ exporters:
access_token_passthrough: false
send_compatible_metrics: true
translation_rules:
- action: rename_dimension_keys
mapping:
k8s.cluster.name: kubernetes_cluster
- action: rename_dimension_keys
mapping:
k8s.cluster.name: kubernetes_cluster
exclude_metrics:
- metric_name: metric1
- metric_names: [metric2, metric3]
- metric_name: metric4
dimensions:
dimension_key: dimension_val
- metric_name: metric5
dimensions:
dimension_key: [dimension_val1, dimension_val2]



service:
pipelines:
Expand Down
14 changes: 12 additions & 2 deletions exporter/signalfxexporter/translation/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
tracetranslator "go.opentelemetry.io/collector/translator/trace"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/translation/dpfilters"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk"
)

Expand All @@ -51,13 +52,18 @@ var (
type MetricsConverter struct {
logger *zap.Logger
metricTranslator *MetricTranslator
filterSet *dpfilters.FilterSet
}

// NewMetricsConverter creates a MetricsConverter from the passed in logger and
// MetricTranslator. Pass in a nil MetricTranslator to not use translation
// rules.
func NewMetricsConverter(logger *zap.Logger, t *MetricTranslator) *MetricsConverter {
return &MetricsConverter{logger: logger, metricTranslator: t}
func NewMetricsConverter(logger *zap.Logger, t *MetricTranslator, excludes []dpfilters.MetricFilter) (*MetricsConverter, error) {
fs, err := dpfilters.NewFilterSet(excludes)
if err != nil {
return nil, err
}
return &MetricsConverter{logger: logger, metricTranslator: t, filterSet: fs}, nil
}

// MetricDataToSignalFxV2 converts the passed in MetricsData to SFx datapoints,
Expand Down Expand Up @@ -111,6 +117,10 @@ func (c *MetricsConverter) metricToSfxDataPoints(metric pdata.Metric, extraDimen
dps = c.metricTranslator.TranslateDataPoints(c.logger, dps)
}

dps = filterDataPoints(dps, func(dp *sfxpb.DataPoint) bool {
return c.filterSet.Matches(dp)
})

return dps
}

Expand Down

0 comments on commit 9169c99

Please sign in to comment.