Skip to content

Commit

Permalink
Update viper (#11952)
Browse files Browse the repository at this point in the history
- Update viper to include:
  - Make AllKeys and AllSettings return set-but-empty keys 
  - Add Unset method
- Use Unset method to fix tests that relied on setting a key as null to unset it
  • Loading branch information
albertvaka authored May 10, 2022
1 parent d73cde8 commit d36e269
Show file tree
Hide file tree
Showing 19 changed files with 49 additions and 35 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ require (
github.com/DataDog/gopsutil v0.0.0-20220308095538-d086941833e3
github.com/DataDog/nikos v1.7.6
github.com/DataDog/sketches-go v1.4.1
github.com/DataDog/viper v1.9.0
github.com/DataDog/viper v1.10.0
github.com/DataDog/watermarkpodautoscaler v0.3.1-logs-attributes.2.0.20211014120627-6d6a5c559fc9
github.com/DataDog/zstd v1.5.0
github.com/DataDog/zstd_0 v0.0.0-20210310093942-586c1286621f
Expand Down
4 changes: 2 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/aggregator/aggregator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ func TestTags(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer config.Datadog.Set("basic_telemetry_add_container_tags", nil)
defer config.Datadog.Unset("basic_telemetry_add_container_tags")
config.Datadog.Set("basic_telemetry_add_container_tags", tt.tlmContainerTagsEnabled)
agg := NewBufferedAggregator(nil, nil, "hostname", time.Second)
agg.agentTags = tt.agentTags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func TestProcessBundledEvents(t *testing.T) {
mockConfig.Set("cluster_name", testClusterName)
clustername.ResetClusterName() // reset state as clustername was already read
// defer a reset of the state so that future hostname fetches are not impacted
defer mockConfig.Set("cluster_name", nil)
defer mockConfig.Unset("cluster_name")
defer clustername.ResetClusterName()

modifiedNewDatadogEventsWithClusterName := metrics.Event{
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/config_change_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ func TestChangeChecker(t *testing.T) {
func assertConfigChangeDetected(r *require.Assertions, checker *ChangeChecker, key string, value interface{}) {
Datadog.Set(key, value)
r.True(checker.HasChanged())
Datadog.Set(key, nil)
Datadog.Unset(key)
r.False(checker.HasChanged())
}
6 changes: 3 additions & 3 deletions pkg/config/legacy/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,9 @@ func TestConverter(t *testing.T) {
func TestExtractURLAPIKeys(t *testing.T) {
configConverter := config.NewConfigConverter()
defer func() {
configConverter.Set("dd_url", "")
configConverter.Set("api_key", "")
configConverter.Set("additional_endpoints", nil)
configConverter.Unset("dd_url")
configConverter.Unset("api_key")
configConverter.Unset("additional_endpoints")
}()
agentConfig := make(Config)

Expand Down
1 change: 1 addition & 0 deletions pkg/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Config interface {
// API implemented by viper.Viper

Set(key string, value interface{})
Unset(key string)
SetDefault(key string, value interface{})
SetFs(fs afero.Fs)
IsSet(key string) bool
Expand Down
7 changes: 7 additions & 0 deletions pkg/config/viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ func (c *safeConfig) Set(key string, value interface{}) {
c.Viper.Set(key, value)
}

// Unset wraps Viper for concurrent access
func (c *safeConfig) Unset(key string) {
c.Lock()
defer c.Unlock()
c.Viper.Unset(key)
}

// SetDefault wraps Viper for concurrent access
func (c *safeConfig) SetDefault(key string, value interface{}) {
c.Lock()
Expand Down
2 changes: 1 addition & 1 deletion pkg/forwarder/forwarder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ func TestHighPriorityTransaction(t *testing.T) {
}))

config.Datadog.Set("forwarder_backoff_max", 0.5)
defer config.Datadog.Set("forwarder_backoff_max", nil)
defer config.Datadog.Unset("forwarder_backoff_max")

oldFlushInterval := flushInterval
flushInterval = 500 * time.Millisecond
Expand Down
2 changes: 1 addition & 1 deletion pkg/logs/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (suite *ConfigTestSuite) TestGlobalProcessingRulesShouldReturnNoRulesWithEm
err error
)

suite.config.Set("logs_config.processing_rules", nil)
suite.config.Unset("logs_config.processing_rules")

rules, err = GlobalProcessingRules()
suite.Nil(err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/logs/internal/tag/local_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestLocalProviderShouldReturnEmptyList(t *testing.T) {
tags := []string{"tag1:value1", "tag2", "tag3"}

mockConfig.Set("tags", tags)
defer mockConfig.Set("tags", nil)
defer mockConfig.Unset("tags")

mockConfig.Set("logs_config.expected_tags_duration", "0")

Expand All @@ -44,7 +44,7 @@ func TestLocalProviderExpectedTags(t *testing.T) {
tags := []string{"tag1:value1", "tag2", "tag3"}

mockConfig.Set("tags", tags)
defer mockConfig.Set("tags", nil)
defer mockConfig.Unset("tags")

expectedTagsDuration := 5 * time.Second
mockConfig.Set("logs_config.expected_tags_duration", "5s")
Expand Down
6 changes: 3 additions & 3 deletions pkg/logs/internal/tag/provider_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func BenchmarkProviderExpectedTags(b *testing.B) {
config.StartTime = start
}()

defer m.Set("tags", nil)
defer m.Unset("tags")

// Setting a test-friendly value for the deadline (test should not take 1m)
m.Set("logs_config.expected_tags_duration", "1m")
Expand Down Expand Up @@ -98,7 +98,7 @@ func BenchmarkProviderNoExpectedTags(b *testing.B) {
config.StartTime = start
}()

defer m.Set("tags", nil)
defer m.Unset("tags")

// Setting a test-friendly value for the deadline (test should not take 1m)
m.Set("logs_config.expected_tags_duration", "0")
Expand All @@ -118,7 +118,7 @@ func BenchmarkProviderNoExpectedTagsNil(b *testing.B) {
config.StartTime = start
}()

defer m.Set("tags", nil)
defer m.Unset("tags")

// Setting a test-friendly value for the deadline (test should not take 1m)
m.Set("logs_config.expected_tags_duration", "0")
Expand Down
2 changes: 1 addition & 1 deletion pkg/logs/internal/tag/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestProviderExpectedTags(t *testing.T) {

tags := []string{"tag1:value1", "tag2", "tag3"}
m.Set("tags", tags)
defer m.Set("tags", nil)
defer m.Unset("tags")

m.Set("logs_config.tagger_warmup_duration", "2")

Expand Down
7 changes: 5 additions & 2 deletions pkg/metadata/host/host_no_otlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ import (
)

func TestGetOtlpMetaWithoutOtlp(t *testing.T) {
config.Datadog.Set(config.OTLPReceiverSection+".protocols.grpc.endpoint", "localhost:9999")
meta := getOtlpMeta()
assert.Equal(t, false, meta.Enabled)

config.Datadog.Set(config.OTLPSection, nil)
config.Datadog.Set(config.OTLPReceiverSection+".protocols.grpc.endpoint", "localhost:9999")
meta = getOtlpMeta()
assert.Equal(t, false, meta.Enabled)

config.Datadog.Unset(config.OTLPSection)
meta = getOtlpMeta()
assert.Equal(t, false, meta.Enabled)
}
7 changes: 5 additions & 2 deletions pkg/metadata/host/host_otlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ import (
)

func TestGetOtlpMetaWithOtlp(t *testing.T) {
config.Datadog.Set(config.OTLPReceiverSection+".protocols.grpc.endpoint", "localhost:9999")
meta := getOtlpMeta()
assert.Equal(t, false, meta.Enabled)

config.Datadog.Set(config.OTLPReceiverSection+".protocols.grpc.endpoint", "localhost:9999")
meta = getOtlpMeta()
assert.Equal(t, true, meta.Enabled)

config.Datadog.Set(config.OTLPSection, nil)
config.Datadog.Unset(config.OTLPSection)
meta = getOtlpMeta()
assert.Equal(t, false, meta.Enabled)
}
12 changes: 6 additions & 6 deletions pkg/metadata/host/host_tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestGetHostTags(t *testing.T) {
ctx := context.Background()
mockConfig := config.Mock()
mockConfig.Set("tags", []string{"tag1:value1", "tag2", "tag3"})
defer mockConfig.Set("tags", nil)
defer mockConfig.Unset("tags")

hostTags := GetHostTags(ctx, false)
assert.NotNil(t, hostTags.System)
Expand All @@ -42,7 +42,7 @@ func TestGetHostTagsWithSplits(t *testing.T) {
mockConfig := config.Mock()
mockConfig.Set("tag_value_split_separator", map[string]string{"kafka_partition": ","})
mockConfig.Set("tags", []string{"tag1:value1", "tag2", "tag3", "kafka_partition:0,1,2"})
defer mockConfig.Set("tags", nil)
defer mockConfig.Unset("tags")

hostTags := GetHostTags(ctx, false)
assert.NotNil(t, hostTags.System)
Expand All @@ -54,7 +54,7 @@ func TestGetHostTagsWithoutSplits(t *testing.T) {
mockConfig := config.Mock()
mockConfig.Set("tag_value_split_separator", map[string]string{"kafka_partition": ";"})
mockConfig.Set("tags", []string{"tag1:value1", "tag2", "tag3", "kafka_partition:0,1,2"})
defer mockConfig.Set("tags", nil)
defer mockConfig.Unset("tags")

hostTags := GetHostTags(ctx, false)
assert.NotNil(t, hostTags.System)
Expand All @@ -66,7 +66,7 @@ func TestGetHostTagsWithEnv(t *testing.T) {
mockConfig := config.Mock()
mockConfig.Set("tags", []string{"tag1:value1", "tag2", "tag3", "env:prod"})
mockConfig.Set("env", "preprod")
defer mockConfig.Set("tags", nil)
defer mockConfig.Unset("tags")
defer mockConfig.Set("env", "")

hostTags := GetHostTags(ctx, false)
Expand All @@ -91,8 +91,8 @@ func TestCombineExtraTags(t *testing.T) {
mockConfig := config.Mock()
mockConfig.Set("tags", []string{"tag1:value1", "tag2", "tag4"})
mockConfig.Set("extra_tags", []string{"tag1:value2", "tag3", "tag4"})
defer mockConfig.Set("tags", nil)
defer mockConfig.Set("extra_tags", nil)
defer mockConfig.Unset("tags")
defer mockConfig.Unset("extra_tags")

hostTags := GetHostTags(ctx, false)
assert.NotNil(t, hostTags.System)
Expand Down
4 changes: 2 additions & 2 deletions pkg/serializer/internal/metrics/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func TestEventsSeveralPayloadsCreateSingleMarshaler(t *testing.T) {
events := createEvents("3", "3", "2", "2", "1", "1")

config.Datadog.Set("serializer_max_payload_size", 500)
defer config.Datadog.Set("serializer_max_payload_size", nil)
defer config.Datadog.Unset("serializer_max_payload_size")

expectedPayloads, err := events.MarshalJSON()
assert.NoError(t, err)
Expand All @@ -178,7 +178,7 @@ func TestEventsSeveralPayloadsCreateMarshalersBySourceType(t *testing.T) {
events := createEvents("3", "3", "2", "2", "1", "1")

config.Datadog.Set("serializer_max_payload_size", 300)
defer config.Datadog.Set("serializer_max_payload_size", nil)
defer config.Datadog.Unset("serializer_max_payload_size")

expectedPayloads, err := events.MarshalJSON()
assert.NoError(t, err)
Expand Down
10 changes: 5 additions & 5 deletions pkg/serializer/serializer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func createProtoPayloadMatcher(content []byte) interface{} {
}
func TestSendV1Events(t *testing.T) {
config.Datadog.Set("enable_events_stream_payload_serialization", false)
defer config.Datadog.Set("enable_events_stream_payload_serialization", nil)
defer config.Datadog.Unset("enable_events_stream_payload_serialization")

f := &forwarder.MockedForwarder{}

Expand All @@ -228,7 +228,7 @@ func (p *testPayloadMutipleValues) Len() int { return p.count }

func TestSendV1EventsCreateMarshalersBySourceType(t *testing.T) {
config.Datadog.Set("enable_events_stream_payload_serialization", true)
defer config.Datadog.Set("enable_events_stream_payload_serialization", nil)
defer config.Datadog.Unset("enable_events_stream_payload_serialization")
f := &forwarder.MockedForwarder{}

s := NewSerializer(f, nil, nil)
Expand All @@ -246,7 +246,7 @@ func TestSendV1EventsCreateMarshalersBySourceType(t *testing.T) {
f.AssertExpectations(t)

config.Datadog.Set("serializer_max_payload_size", 20)
defer config.Datadog.Set("serializer_max_payload_size", nil)
defer config.Datadog.Unset("serializer_max_payload_size")

f.On("SubmitV1Intake", payloadsCountMatcher(3), jsonExtraHeadersWithCompression).Return(nil)
err = s.SendEvents(events)
Expand All @@ -259,7 +259,7 @@ func TestSendV1ServiceChecks(t *testing.T) {
matcher := createJSONPayloadMatcher(`[{"check":"","host_name":"","timestamp":0,"status":0,"message":"","tags":null}]`)
f.On("SubmitV1CheckRuns", matcher, jsonExtraHeadersWithCompression).Return(nil).Times(1)
config.Datadog.Set("enable_service_checks_stream_payload_serialization", false)
defer config.Datadog.Set("enable_service_checks_stream_payload_serialization", nil)
defer config.Datadog.Unset("enable_service_checks_stream_payload_serialization")

s := NewSerializer(f, nil, nil)
err := s.SendServiceChecks(metrics.ServiceChecks{&metrics.ServiceCheck{}})
Expand All @@ -273,7 +273,7 @@ func TestSendV1Series(t *testing.T) {

f.On("SubmitV1Series", matcher, jsonExtraHeadersWithCompression).Return(nil).Times(1)
config.Datadog.Set("enable_stream_payload_serialization", false)
defer config.Datadog.Set("enable_stream_payload_serialization", nil)
defer config.Datadog.Unset("enable_stream_payload_serialization")

s := NewSerializer(f, nil, nil)

Expand Down
2 changes: 1 addition & 1 deletion pkg/util/kubernetes/clustername/clustername_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestGetClusterName(t *testing.T) {

var testClusterName = "laika"
mockConfig.Set("cluster_name", testClusterName)
defer mockConfig.Set("cluster_name", nil)
defer mockConfig.Unset("cluster_name")

assert.Equal(t, testClusterName, getClusterName(ctx, data, "hostname"))

Expand Down

0 comments on commit d36e269

Please sign in to comment.