diff --git a/cmd/otelcontribcol/go.mod b/cmd/otelcontribcol/go.mod index c2284e11d2e2..59641d1d580e 100644 --- a/cmd/otelcontribcol/go.mod +++ b/cmd/otelcontribcol/go.mod @@ -562,6 +562,7 @@ require ( github.com/jcmturner/rpc/v2 v2.0.3 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901 // indirect + github.com/jonboulle/clockwork v0.4.0 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/jpillora/backoff v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect diff --git a/cmd/oteltestbedcol/go.mod b/cmd/oteltestbedcol/go.mod index 1166b6f0b492..b48ba55896af 100644 --- a/cmd/oteltestbedcol/go.mod +++ b/cmd/oteltestbedcol/go.mod @@ -150,6 +150,7 @@ require ( github.com/ionos-cloud/sdk-go/v6 v6.1.11 // indirect github.com/jaegertracing/jaeger v1.59.0 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect + github.com/jonboulle/clockwork v0.4.0 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/jpillora/backoff v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect diff --git a/connector/datadogconnector/go.sum b/connector/datadogconnector/go.sum index cf16dc6b5ce4..1b25e5701c77 100644 --- a/connector/datadogconnector/go.sum +++ b/connector/datadogconnector/go.sum @@ -573,6 +573,8 @@ github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHW github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= +github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4= +github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= diff --git a/exporter/datadogexporter/go.mod b/exporter/datadogexporter/go.mod index d74c988b70e7..21e6dac994b6 100644 --- a/exporter/datadogexporter/go.mod +++ b/exporter/datadogexporter/go.mod @@ -222,6 +222,7 @@ require ( github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/ionos-cloud/sdk-go/v6 v6.1.11 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect + github.com/jonboulle/clockwork v0.4.0 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/jpillora/backoff v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect diff --git a/exporter/datadogexporter/integrationtest/go.sum b/exporter/datadogexporter/integrationtest/go.sum index 9641a25b6630..7e4978036fd6 100644 --- a/exporter/datadogexporter/integrationtest/go.sum +++ b/exporter/datadogexporter/integrationtest/go.sum @@ -573,6 +573,8 @@ github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHW github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= +github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4= +github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= diff --git a/pkg/stanza/fileconsumer/internal/reader/reader_test.go b/pkg/stanza/fileconsumer/internal/reader/reader_test.go index 521aab3bf9d6..5f07b23821ee 100644 --- a/pkg/stanza/fileconsumer/internal/reader/reader_test.go +++ b/pkg/stanza/fileconsumer/internal/reader/reader_test.go @@ -10,13 +10,12 @@ import ( "testing" "time" - "github.com/jonboulle/clockwork" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/filetest" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/fingerprint" - "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/internal/now" + internaltime "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/internal/time" ) func TestFileReader_FingerprintUpdated(t *testing.T) { @@ -207,25 +206,9 @@ func TestFlushPeriodEOF(t *testing.T) { require.NoError(t, err) assert.Equal(t, int64(0), r.Offset) - now.Now = newAlwaysIncreasingClock().Now - defer func() { now.Now = time.Now }() + internaltime.Now = internaltime.NewAlwaysIncreasingClock().Now + defer func() { internaltime.Now = time.Now }() r.ReadToEnd(context.Background()) sink.ExpectTokens(t, content[0:aContentLength], []byte{'b'}) } - -// Clock where Now() always returns a greater value than the previous return value -type alwaysIncreasingClock struct { - clockwork.FakeClock -} - -func newAlwaysIncreasingClock() alwaysIncreasingClock { - return alwaysIncreasingClock{ - FakeClock: clockwork.NewFakeClock(), - } -} - -func (c alwaysIncreasingClock) Now() time.Time { - c.FakeClock.Advance(time.Nanosecond) - return c.FakeClock.Now() -} diff --git a/pkg/stanza/fileconsumer/matcher/internal/filter/exclude.go b/pkg/stanza/fileconsumer/matcher/internal/filter/exclude.go index 8e6e254f597e..de4156535cfe 100644 --- a/pkg/stanza/fileconsumer/matcher/internal/filter/exclude.go +++ b/pkg/stanza/fileconsumer/matcher/internal/filter/exclude.go @@ -7,6 +7,8 @@ import ( "time" "go.uber.org/multierr" + + internaltime "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/internal/time" ) type excludeOlderThanOption struct { @@ -25,7 +27,7 @@ func (eot excludeOlderThanOption) apply(items []*item) ([]*item, error) { // Keep (include) the file if its age (since last modification) // is the same or less than the configured age. - fileAge := time.Since(fi.ModTime()) + fileAge := internaltime.Since(fi.ModTime()) if fileAge <= eot.age { filteredItems = append(filteredItems, item) } diff --git a/pkg/stanza/fileconsumer/matcher/internal/filter/exclude_test.go b/pkg/stanza/fileconsumer/matcher/internal/filter/exclude_test.go index 436bec80987d..6eb745314c91 100644 --- a/pkg/stanza/fileconsumer/matcher/internal/filter/exclude_test.go +++ b/pkg/stanza/fileconsumer/matcher/internal/filter/exclude_test.go @@ -11,6 +11,8 @@ import ( "time" "github.com/stretchr/testify/require" + + internaltime "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/internal/time" ) func TestExcludeOlderThanFilter(t *testing.T) { @@ -25,10 +27,6 @@ func TestExcludeOlderThanFilter(t *testing.T) { expect []string expectedErr string expectedWindowsErr string - - // List of OSes on which to skip this test case - skipOS []string - skipReason string }{ "no_files": { files: []string{}, @@ -47,9 +45,6 @@ func TestExcludeOlderThanFilter(t *testing.T) { expectedErr: "", }, "exclude_some_files": { - skipOS: []string{"windows"}, - skipReason: "Flaky test case on Windows: https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/32838", - files: []string{"a.log", "b.log"}, fileMTimes: []time.Time{twoHoursAgo, threeHoursAgo}, excludeOlderThan: 3 * time.Hour, @@ -78,11 +73,8 @@ func TestExcludeOlderThanFilter(t *testing.T) { for name, tc := range cases { t.Run(name, func(t *testing.T) { - for _, os := range tc.skipOS { - if runtime.GOOS == os { - t.Skipf("Skipping test case: %s", tc.skipReason) - } - } + internaltime.Since = internaltime.NewAlwaysIncreasingClock().Since + defer func() { internaltime.Since = time.Since }() tmpDir := t.TempDir() var items []*item diff --git a/pkg/stanza/flush/flush.go b/pkg/stanza/flush/flush.go index 9c3c4926e902..b4d700795898 100644 --- a/pkg/stanza/flush/flush.go +++ b/pkg/stanza/flush/flush.go @@ -7,7 +7,7 @@ import ( "bufio" "time" - "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/internal/now" + internaltime "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/internal/time" ) type State struct { @@ -42,7 +42,7 @@ func (s *State) Func(splitFunc bufio.SplitFunc, period time.Duration) bufio.Spli // If there's a token, return it if token != nil { - s.LastDataChange = now.Now() + s.LastDataChange = internaltime.Now() s.LastDataLength = 0 return advance, token, err } @@ -55,14 +55,14 @@ func (s *State) Func(splitFunc bufio.SplitFunc, period time.Duration) bufio.Spli // We're seeing new data so postpone the next flush if len(data) > s.LastDataLength { - s.LastDataChange = now.Now() + s.LastDataChange = internaltime.Now() s.LastDataLength = len(data) return 0, nil, nil } // Flush timed out if time.Since(s.LastDataChange) > period { - s.LastDataChange = now.Now() + s.LastDataChange = internaltime.Now() s.LastDataLength = 0 return len(data), data, nil } @@ -74,6 +74,6 @@ func (s *State) Func(splitFunc bufio.SplitFunc, period time.Duration) bufio.Spli // Deprecated: [v0.88.0] Use WithFunc instead. func WithPeriod(splitFunc bufio.SplitFunc, period time.Duration) bufio.SplitFunc { - s := &State{LastDataChange: now.Now()} + s := &State{LastDataChange: internaltime.Now()} return s.Func(splitFunc, period) } diff --git a/pkg/stanza/internal/now/now.go b/pkg/stanza/internal/now/now.go deleted file mode 100644 index 17de420da57a..000000000000 --- a/pkg/stanza/internal/now/now.go +++ /dev/null @@ -1,8 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -package now // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/internal/now" - -import "time" - -var Now = time.Now diff --git a/pkg/stanza/internal/time/time.go b/pkg/stanza/internal/time/time.go new file mode 100644 index 000000000000..79562212c861 --- /dev/null +++ b/pkg/stanza/internal/time/time.go @@ -0,0 +1,35 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package time // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/internal/time" + +import ( + "time" + + "github.com/jonboulle/clockwork" +) + +var Now = time.Now +var Since = time.Since + +// Clock where Now() always returns a greater value than the previous return value +type AlwaysIncreasingClock struct { + clockwork.FakeClock +} + +func NewAlwaysIncreasingClock() AlwaysIncreasingClock { + return AlwaysIncreasingClock{ + FakeClock: clockwork.NewFakeClock(), + } +} + +func (c AlwaysIncreasingClock) Now() time.Time { + c.FakeClock.Advance(time.Nanosecond) + return c.FakeClock.Now() +} + +func (c AlwaysIncreasingClock) Since(t time.Time) time.Duration { + // ensure that internal c.FakeClock.Now() will return a greater value + c.FakeClock.Advance(time.Nanosecond) + return c.FakeClock.Since(t) +} diff --git a/receiver/filelogreceiver/go.mod b/receiver/filelogreceiver/go.mod index d51139fc8ea2..9ba1b528fc2b 100644 --- a/receiver/filelogreceiver/go.mod +++ b/receiver/filelogreceiver/go.mod @@ -32,6 +32,7 @@ require ( github.com/gogo/protobuf v1.3.2 // indirect github.com/google/uuid v1.6.0 // indirect github.com/hashicorp/go-version v1.7.0 // indirect + github.com/jonboulle/clockwork v0.4.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/knadh/koanf/maps v0.1.1 // indirect github.com/knadh/koanf/providers/confmap v0.1.0 // indirect diff --git a/receiver/otlpjsonfilereceiver/go.mod b/receiver/otlpjsonfilereceiver/go.mod index 1a3ac315d7c7..4243513829d1 100644 --- a/receiver/otlpjsonfilereceiver/go.mod +++ b/receiver/otlpjsonfilereceiver/go.mod @@ -31,6 +31,7 @@ require ( github.com/gogo/protobuf v1.3.2 // indirect github.com/google/uuid v1.6.0 // indirect github.com/hashicorp/go-version v1.7.0 // indirect + github.com/jonboulle/clockwork v0.4.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/knadh/koanf/maps v0.1.1 // indirect github.com/knadh/koanf/providers/confmap v0.1.0 // indirect