-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
161 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 5 additions & 1 deletion
6
processor/deltatocumulativeprocessor/internal/maybe/ptr_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
processor/deltatocumulativeprocessor/internal/telemetry/faults_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package telemetry_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
"go.opentelemetry.io/otel/metric/noop" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/exp/metrics/identity" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/deltatocumulativeprocessor/internal/data" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/deltatocumulativeprocessor/internal/delta" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/deltatocumulativeprocessor/internal/streams" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/deltatocumulativeprocessor/internal/telemetry" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/deltatocumulativeprocessor/internal/testdata/random" | ||
) | ||
|
||
// TestFaults verifies certain non-fatal errors are actually caused and | ||
// subsequently dropped. It does so by writing bad samples to the actual | ||
// implementation instead of fabricating errors manually. | ||
func TestFaults(t *testing.T) { | ||
type Map = streams.Map[data.Number] | ||
type Case struct { | ||
Name string | ||
Map Map | ||
Pre func(Map, identity.Stream, data.Number) error | ||
Bad func(Map, identity.Stream, data.Number) error | ||
Err error | ||
} | ||
|
||
sum := random.Sum() | ||
evid, evdp := sum.Stream() | ||
|
||
cases := []Case{ | ||
{ | ||
Name: "older-start", | ||
Pre: func(dps Map, id identity.Stream, dp data.Number) error { | ||
dp.SetStartTimestamp(ts(20)) | ||
dp.SetTimestamp(ts(30)) | ||
return dps.Store(id, dp) | ||
}, | ||
Bad: func(dps Map, id identity.Stream, dp data.Number) error { | ||
dp.SetStartTimestamp(ts(10)) | ||
dp.SetTimestamp(ts(40)) | ||
return dps.Store(id, dp) | ||
}, | ||
Err: delta.ErrOlderStart{Start: ts(20), Sample: ts(10)}, | ||
}, | ||
{ | ||
Name: "out-of-order", | ||
Pre: func(dps Map, id identity.Stream, dp data.Number) error { | ||
dp.SetTimestamp(ts(20)) | ||
return dps.Store(id, dp) | ||
}, | ||
Bad: func(dps Map, id identity.Stream, dp data.Number) error { | ||
dp.SetTimestamp(ts(10)) | ||
return dps.Store(id, dp) | ||
}, | ||
Err: delta.ErrOutOfOrder{Last: ts(20), Sample: ts(10)}, | ||
}, | ||
{ | ||
Name: "gap", | ||
Pre: func(dps Map, id identity.Stream, dp data.Number) error { | ||
dp.SetStartTimestamp(ts(10)) | ||
dp.SetTimestamp(ts(20)) | ||
return dps.Store(id, dp) | ||
}, | ||
Bad: func(dps Map, id identity.Stream, dp data.Number) error { | ||
dp.SetStartTimestamp(ts(30)) | ||
dp.SetTimestamp(ts(40)) | ||
return dps.Store(id, dp) | ||
}, | ||
Err: delta.ErrGap{From: ts(20), To: ts(30)}, | ||
}, | ||
{ | ||
Name: "limit", | ||
Map: streams.Limit(delta.New[data.Number](), 1), | ||
Pre: func(dps Map, id identity.Stream, dp data.Number) error { | ||
dp.SetTimestamp(ts(10)) | ||
return dps.Store(id, dp) | ||
}, | ||
Bad: func(dps Map, _ identity.Stream, _ data.Number) error { | ||
id, dp := sum.Stream() | ||
dp.SetTimestamp(ts(20)) | ||
return dps.Store(id, dp) | ||
}, | ||
Err: streams.ErrLimit(1), | ||
}, | ||
{ | ||
Name: "evict", | ||
Map: func() Map { | ||
ev := HeadEvictor[data.Number]{Map: delta.New[data.Number]()} | ||
lim := streams.Limit(ev, 1) | ||
lim.Evictor = ev | ||
return lim | ||
}(), | ||
Pre: func(dps Map, _ identity.Stream, _ data.Number) error { | ||
evdp.SetTimestamp(ts(10)) | ||
return dps.Store(evid, evdp) | ||
}, | ||
Bad: func(dps Map, _ identity.Stream, _ data.Number) error { | ||
id, dp := sum.Stream() | ||
dp.SetTimestamp(ts(20)) | ||
return dps.Store(id, dp) | ||
}, | ||
Err: streams.ErrEvicted{Ident: evid, ErrLimit: streams.ErrLimit(1)}, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.Name, func(t *testing.T) { | ||
id, dp := sum.Stream() | ||
tel := telemetry.New(noop.Meter{}) | ||
|
||
dps := c.Map | ||
if dps == nil { | ||
dps = delta.New[data.Number]() | ||
} | ||
onf := telemetry.ObserveNonFatal(dps, &tel.Metrics) | ||
|
||
if c.Pre != nil { | ||
err := c.Pre(onf, id, dp.Clone()) | ||
require.NoError(t, err) | ||
} | ||
|
||
err := c.Bad(dps, id, dp.Clone()) | ||
require.Equal(t, c.Err, err) | ||
|
||
err = c.Bad(onf, id, dp.Clone()) | ||
require.NoError(t, err) | ||
}) | ||
} | ||
} | ||
|
||
type ts = pcommon.Timestamp | ||
|
||
// HeadEvictor drops the first stream on Evict() | ||
type HeadEvictor[T any] struct{ streams.Map[T] } | ||
|
||
func (e HeadEvictor[T]) Evict() (evicted identity.Stream) { | ||
e.Items()(func(id identity.Stream, _ T) bool { | ||
e.Delete(id) | ||
evicted = id | ||
return false | ||
}) | ||
return evicted | ||
} |
fb77807
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice