Skip to content

Commit ed2383a

Browse files
authored
[signalfxexporter] move initialization of the config defaults in Unmarshal. (#18207)
Signed-off-by: Bogdan Drutu <[email protected]>
1 parent d2fd181 commit ed2383a

File tree

8 files changed

+101
-117
lines changed

8 files changed

+101
-117
lines changed

.chloggen/signalfxexporterconfig.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: enhancement
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
5+
component: signalfxexporter
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: Move initialization of the config defaults in Unmarshal.
9+
10+
# One or more tracking issues related to the change
11+
issues: [18207]

exporter/signalfxexporter/config.go

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ import (
1818
"errors"
1919
"fmt"
2020
"net/url"
21-
"time"
2221

2322
"go.opentelemetry.io/collector/config/confighttp"
2423
"go.opentelemetry.io/collector/config/configopaque"
2524
"go.opentelemetry.io/collector/config/configtls"
2625
"go.opentelemetry.io/collector/confmap"
2726
"go.opentelemetry.io/collector/exporter/exporterhelper"
27+
"gopkg.in/yaml.v3"
2828

2929
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/internal/correlation"
3030
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/internal/translation"
@@ -34,6 +34,7 @@ import (
3434

3535
const (
3636
translationRulesConfigKey = "translation_rules"
37+
excludeMetricsConfigKey = "exclude_metrics"
3738
)
3839

3940
var _ confmap.Unmarshaler = (*Config)(nil)
@@ -151,27 +152,26 @@ func (cfg *Config) getAPIURL() (*url.URL, error) {
151152
return apiURL, nil
152153
}
153154

154-
func (cfg *Config) Unmarshal(componentParser *confmap.Conf) (err error) {
155+
func (cfg *Config) Unmarshal(componentParser *confmap.Conf) error {
155156
if componentParser == nil {
156157
// Nothing to do if there is no config given.
157158
return nil
158159
}
159160

160-
if err = componentParser.Unmarshal(cfg); err != nil {
161+
if err := componentParser.Unmarshal(cfg); err != nil {
161162
return err
162163
}
163164

164-
// If translations_config is not set in the config, set it to the defaults and return.
165+
// If translations_config is not set in the config, set it to the default.
165166
if !componentParser.IsSet(translationRulesConfigKey) {
167+
var err error
166168
cfg.TranslationRules, err = loadDefaultTranslationRules()
167-
return err
168-
}
169-
170-
if cfg.HTTPClientSettings.Timeout == 0 {
171-
cfg.HTTPClientSettings.Timeout = 5 * time.Second
169+
if err != nil {
170+
return err
171+
}
172172
}
173173

174-
return nil
174+
return setDefaultExcludes(cfg)
175175
}
176176

177177
// Validate checks if the exporter configuration is valid.
@@ -195,3 +195,35 @@ func (cfg *Config) Validate() error {
195195

196196
return nil
197197
}
198+
199+
func setDefaultExcludes(cfg *Config) error {
200+
exCfg, err := loadConfig([]byte(translation.DefaultExcludeMetricsYaml))
201+
if err != nil {
202+
return err
203+
}
204+
205+
// If ExcludeMetrics is not set to empty, append defaults.
206+
if cfg.ExcludeMetrics == nil || len(cfg.ExcludeMetrics) > 0 {
207+
cfg.ExcludeMetrics = append(cfg.ExcludeMetrics, exCfg.ExcludeMetrics...)
208+
}
209+
return nil
210+
}
211+
212+
func loadDefaultTranslationRules() ([]translation.Rule, error) {
213+
cfg, err := loadConfig([]byte(translation.DefaultTranslationRulesYaml))
214+
return cfg.TranslationRules, err
215+
}
216+
217+
func loadConfig(bytes []byte) (Config, error) {
218+
var cfg Config
219+
var data map[string]interface{}
220+
if err := yaml.Unmarshal(bytes, &data); err != nil {
221+
return cfg, err
222+
}
223+
224+
if err := confmap.NewFromStringMap(data).Unmarshal(&cfg, confmap.WithErrorUnused()); err != nil {
225+
return cfg, fmt.Errorf("failed to load default exclude metrics: %w", err)
226+
}
227+
228+
return cfg, nil
229+
}

exporter/signalfxexporter/config_test.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"go.opentelemetry.io/collector/component"
2727
"go.opentelemetry.io/collector/config/confighttp"
2828
"go.opentelemetry.io/collector/config/configopaque"
29+
"go.opentelemetry.io/collector/confmap"
2930
"go.opentelemetry.io/collector/confmap/confmaptest"
3031
"go.opentelemetry.io/collector/exporter/exporterhelper"
3132

@@ -53,7 +54,7 @@ func TestLoadConfig(t *testing.T) {
5354

5455
tests := []struct {
5556
id component.ID
56-
expected component.Config
57+
expected *Config
5758
}{
5859
{
5960
id: component.NewIDWithName(typeStr, ""),
@@ -194,6 +195,8 @@ func TestLoadConfig(t *testing.T) {
194195
require.NoError(t, component.UnmarshalConfig(sub, cfg))
195196

196197
assert.NoError(t, component.ValidateConfig(cfg))
198+
// We need to add the default exclude rules.
199+
assert.NoError(t, setDefaultExcludes(tt.expected))
197200
assert.Equal(t, tt.expected, cfg)
198201
})
199202
}
@@ -404,3 +407,41 @@ func TestConfigValidateErrors(t *testing.T) {
404407
})
405408
}
406409
}
410+
411+
func TestUnmarshalExcludeMetrics(t *testing.T) {
412+
tests := []struct {
413+
name string
414+
cfg *Config
415+
excludeMetricsLen int
416+
}{
417+
{
418+
name: "empty config",
419+
cfg: &Config{},
420+
excludeMetricsLen: 12,
421+
},
422+
{
423+
name: "existing exclude config",
424+
cfg: &Config{
425+
ExcludeMetrics: []dpfilters.MetricFilter{
426+
{
427+
MetricNames: []string{"metric1"},
428+
},
429+
},
430+
},
431+
excludeMetricsLen: 13,
432+
},
433+
{
434+
name: "existing empty exclude config",
435+
cfg: &Config{
436+
ExcludeMetrics: []dpfilters.MetricFilter{},
437+
},
438+
excludeMetricsLen: 0,
439+
},
440+
}
441+
for _, tt := range tests {
442+
t.Run(tt.name, func(t *testing.T) {
443+
require.NoError(t, tt.cfg.Unmarshal(confmap.NewFromStringMap(map[string]interface{}{})))
444+
assert.Len(t, tt.cfg.ExcludeMetrics, tt.excludeMetricsLen)
445+
})
446+
}
447+
}

exporter/signalfxexporter/factory.go

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,11 @@ import (
2222

2323
"go.opentelemetry.io/collector/component"
2424
"go.opentelemetry.io/collector/config/confighttp"
25-
"go.opentelemetry.io/collector/confmap"
2625
"go.opentelemetry.io/collector/exporter"
2726
"go.opentelemetry.io/collector/exporter/exporterhelper"
2827
"go.uber.org/zap"
29-
"gopkg.in/yaml.v2"
3028

3129
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/internal/correlation"
32-
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/internal/translation"
33-
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/internal/translation/dpfilters"
3430
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk"
3531
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/batchperresourceattr"
3632
)
@@ -111,14 +107,8 @@ func createMetricsExporter(
111107
set exporter.CreateSettings,
112108
config component.Config,
113109
) (exporter.Metrics, error) {
114-
115110
cfg := config.(*Config)
116111

117-
err := setDefaultExcludes(cfg)
118-
if err != nil {
119-
return nil, err
120-
}
121-
122112
exp, err := newSignalFxExporter(cfg, set)
123113
if err != nil {
124114
return nil, err
@@ -155,42 +145,6 @@ func createMetricsExporter(
155145
}, nil
156146
}
157147

158-
func loadDefaultTranslationRules() ([]translation.Rule, error) {
159-
cfg, err := loadConfig([]byte(translation.DefaultTranslationRulesYaml))
160-
return cfg.TranslationRules, err
161-
}
162-
163-
// setDefaultExcludes appends default metrics to be excluded to the exclude_metrics option.
164-
func setDefaultExcludes(cfg *Config) error {
165-
defaultExcludeMetrics, err := loadDefaultExcludes()
166-
if err != nil {
167-
return err
168-
}
169-
if cfg.ExcludeMetrics == nil || len(cfg.ExcludeMetrics) > 0 {
170-
cfg.ExcludeMetrics = append(cfg.ExcludeMetrics, defaultExcludeMetrics...)
171-
}
172-
return nil
173-
}
174-
175-
func loadDefaultExcludes() ([]dpfilters.MetricFilter, error) {
176-
cfg, err := loadConfig([]byte(translation.DefaultExcludeMetricsYaml))
177-
return cfg.ExcludeMetrics, err
178-
}
179-
180-
func loadConfig(bytes []byte) (Config, error) {
181-
var cfg Config
182-
var data map[string]interface{}
183-
if err := yaml.Unmarshal(bytes, &data); err != nil {
184-
return cfg, err
185-
}
186-
187-
if err := confmap.NewFromStringMap(data).Unmarshal(&cfg, confmap.WithErrorUnused()); err != nil {
188-
return cfg, fmt.Errorf("failed to load default exclude metrics: %w", err)
189-
}
190-
191-
return cfg, nil
192-
}
193-
194148
func createLogsExporter(
195149
ctx context.Context,
196150
set exporter.CreateSettings,

exporter/signalfxexporter/factory_test.go

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
"go.uber.org/zap"
3636

3737
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/internal/translation"
38-
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/internal/translation/dpfilters"
3938
)
4039

4140
func TestCreateDefaultConfig(t *testing.T) {
@@ -222,55 +221,6 @@ func requireDimension(t *testing.T, dims []*sfxpb.Dimension, key, val string) {
222221
require.True(t, found, `missing dimension: %s`, key)
223222
}
224223

225-
func TestCreateMetricsExporterWithDefaultExcludeMetrics(t *testing.T) {
226-
config := &Config{
227-
AccessToken: "testToken",
228-
Realm: "us1",
229-
}
230-
231-
te, err := createMetricsExporter(context.Background(), exportertest.NewNopCreateSettings(), config)
232-
require.NoError(t, err)
233-
require.NotNil(t, te)
234-
235-
// Validate that default excludes are always loaded.
236-
assert.Equal(t, 12, len(config.ExcludeMetrics))
237-
}
238-
239-
func TestCreateMetricsExporterWithExcludeMetrics(t *testing.T) {
240-
config := &Config{
241-
AccessToken: "testToken",
242-
Realm: "us1",
243-
ExcludeMetrics: []dpfilters.MetricFilter{
244-
{
245-
MetricNames: []string{"metric1"},
246-
},
247-
},
248-
}
249-
250-
te, err := createMetricsExporter(context.Background(), exportertest.NewNopCreateSettings(), config)
251-
require.NoError(t, err)
252-
require.NotNil(t, te)
253-
254-
// Validate that default excludes are always loaded.
255-
assert.Equal(t, 13, len(config.ExcludeMetrics))
256-
}
257-
258-
func TestCreateMetricsExporterWithEmptyExcludeMetrics(t *testing.T) {
259-
config := &Config{
260-
AccessToken: "testToken",
261-
Realm: "us1",
262-
ExcludeMetrics: []dpfilters.MetricFilter{},
263-
}
264-
265-
te, err := createMetricsExporter(context.Background(), exportertest.NewNopCreateSettings(), config)
266-
require.NoError(t, err)
267-
require.NotNil(t, te)
268-
269-
// Validate that default excludes are overridden when exclude metrics
270-
// is explicitly set to an empty slice.
271-
assert.Equal(t, 0, len(config.ExcludeMetrics))
272-
}
273-
274224
func testMetricsData() pmetric.Metrics {
275225
md := pmetric.NewMetrics()
276226

@@ -596,7 +546,7 @@ func TestHostmetricsCPUTranslations(t *testing.T) {
596546
require.Equal(t, 590, int(*cpuIdle[0].Value.IntValue))
597547
}
598548

599-
func TestDefaultExcludes_translated(t *testing.T) {
549+
func TestDefaultExcludesTranslated(t *testing.T) {
600550
f := NewFactory()
601551
cfg := f.CreateDefaultConfig().(*Config)
602552
require.NoError(t, setDefaultExcludes(cfg))

exporter/signalfxexporter/go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ require (
2525
go.uber.org/multierr v1.9.0
2626
go.uber.org/zap v1.24.0
2727
golang.org/x/sys v0.4.0
28-
gopkg.in/yaml.v2 v2.4.0
28+
gopkg.in/yaml.v3 v3.0.1
2929
)
3030

3131
require (
@@ -74,7 +74,6 @@ require (
7474
google.golang.org/genproto v0.0.0-20221118155620-16455021b5e6 // indirect
7575
google.golang.org/grpc v1.52.3 // indirect
7676
google.golang.org/protobuf v1.28.1 // indirect
77-
gopkg.in/yaml.v3 v3.0.1 // indirect
7877
)
7978

8079
replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/common => ../../internal/common

exporter/signalfxexporter/internal/translation/constants.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414

1515
package translation // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/internal/translation"
1616

17-
const (
18-
// DefaultTranslationRulesYaml defines default translation rules that will be applied to metrics if
19-
// config.TranslationRules not specified explicitly.
20-
// Keep it in YAML format to be able to easily copy and paste it in config if modifications needed.
21-
DefaultTranslationRulesYaml = `
17+
// DefaultTranslationRulesYaml defines default translation rules that will be applied to metrics if
18+
// config.TranslationRules not specified explicitly.
19+
// Keep it in YAML format to be able to easily copy and paste it in config if modifications needed.
20+
const DefaultTranslationRulesYaml = `
2221
translation_rules:
2322
# drops opencensus.resourcetype dimension from metrics generated by receivers written
2423
# using OC data structures. This rule can be removed once the k8s_cluster and kubeletstats
@@ -455,4 +454,3 @@ translation_rules:
455454
sf_temp.system.paging.operations.page_in: true
456455
sf_temp.system.paging.operations.page_out: true
457456
`
458-
)

receiver/signalfxreceiver/go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ require (
7979
google.golang.org/genproto v0.0.0-20221118155620-16455021b5e6 // indirect
8080
google.golang.org/grpc v1.52.3 // indirect
8181
google.golang.org/protobuf v1.28.1 // indirect
82-
gopkg.in/yaml.v2 v2.4.0 // indirect
8382
gopkg.in/yaml.v3 v3.0.1 // indirect
8483
)
8584

0 commit comments

Comments
 (0)