| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package consumertest |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"fmt" |
| 9 |
"sync" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
"github.com/stretchr/testify/require" |
| 14 |
|
| 15 |
"go.opentelemetry.io/collector/pdata/plog" |
| 16 |
"go.opentelemetry.io/collector/pdata/pmetric" |
| 17 |
"go.opentelemetry.io/collector/pdata/pprofile" |
| 18 |
"go.opentelemetry.io/collector/pdata/ptrace" |
| 19 |
"go.opentelemetry.io/collector/pdata/testdata" |
| 20 |
) |
| 21 |
|
| 22 |
type ( |
| 23 |
ctxKey string |
| 24 |
testKey int |
| 25 |
) |
| 26 |
|
| 27 |
func TestTracesSink(t *testing.T) { |
| 28 |
sink := new(TracesSink) |
| 29 |
td := testdata.GenerateTraces(1) |
| 30 |
want := make([]ptrace.Traces, 0, 7) |
| 31 |
for range 7 { |
| 32 |
require.NoError(t, sink.ConsumeTraces(context.Background(), td)) |
| 33 |
want = append(want, td) |
| 34 |
} |
| 35 |
assert.Equal(t, want, sink.AllTraces()) |
| 36 |
assert.Equal(t, len(want), sink.SpanCount()) |
| 37 |
sink.Reset() |
| 38 |
assert.Empty(t, sink.AllTraces()) |
| 39 |
assert.Equal(t, 0, sink.SpanCount()) |
| 40 |
} |
| 41 |
|
| 42 |
func TestMetricsSink(t *testing.T) { |
| 43 |
sink := new(MetricsSink) |
| 44 |
md := testdata.GenerateMetrics(1) |
| 45 |
want := make([]pmetric.Metrics, 0, 7) |
| 46 |
for range 7 { |
| 47 |
require.NoError(t, sink.ConsumeMetrics(context.Background(), md)) |
| 48 |
want = append(want, md) |
| 49 |
} |
| 50 |
assert.Equal(t, want, sink.AllMetrics()) |
| 51 |
assert.Equal(t, 2*len(want), sink.DataPointCount()) |
| 52 |
sink.Reset() |
| 53 |
assert.Empty(t, sink.AllMetrics()) |
| 54 |
assert.Equal(t, 0, sink.DataPointCount()) |
| 55 |
} |
| 56 |
|
| 57 |
func TestLogsSink(t *testing.T) { |
| 58 |
sink := new(LogsSink) |
| 59 |
md := testdata.GenerateLogs(1) |
| 60 |
want := make([]plog.Logs, 0, 7) |
| 61 |
for range 7 { |
| 62 |
require.NoError(t, sink.ConsumeLogs(context.Background(), md)) |
| 63 |
want = append(want, md) |
| 64 |
} |
| 65 |
assert.Equal(t, want, sink.AllLogs()) |
| 66 |
assert.Equal(t, len(want), sink.LogRecordCount()) |
| 67 |
sink.Reset() |
| 68 |
assert.Empty(t, sink.AllLogs()) |
| 69 |
assert.Equal(t, 0, sink.LogRecordCount()) |
| 70 |
} |
| 71 |
|
| 72 |
func TestProfilesSink(t *testing.T) { |
| 73 |
sink := new(ProfilesSink) |
| 74 |
td := testdata.GenerateProfiles(1) |
| 75 |
want := make([]pprofile.Profiles, 0, 7) |
| 76 |
for range 7 { |
| 77 |
require.NoError(t, sink.ConsumeProfiles(context.Background(), td)) |
| 78 |
want = append(want, td) |
| 79 |
} |
| 80 |
assert.Equal(t, want, sink.AllProfiles()) |
| 81 |
assert.Equal(t, len(want), sink.SampleCount()) |
| 82 |
sink.Reset() |
| 83 |
assert.Empty(t, sink.AllProfiles()) |
| 84 |
assert.Empty(t, sink.SampleCount()) |
| 85 |
} |
| 86 |
|
| 87 |
func TestTracesSinkWithContext(t *testing.T) { |
| 88 |
sink := new(TracesSink) |
| 89 |
td := testdata.GenerateTraces(1) |
| 90 |
want := make([]ptrace.Traces, 0, 7) |
| 91 |
wantCtx := make([]context.Context, 0, 7) |
| 92 |
|
| 93 |
for i := range 7 { |
| 94 |
ctx := context.WithValue(context.Background(), testKey(i), fmt.Sprintf("value-%d", i)) |
| 95 |
require.NoError(t, sink.ConsumeTraces(ctx, td)) |
| 96 |
want = append(want, td) |
| 97 |
wantCtx = append(wantCtx, ctx) |
| 98 |
} |
| 99 |
|
| 100 |
assert.Equal(t, want, sink.AllTraces()) |
| 101 |
assert.Equal(t, len(want), sink.SpanCount()) |
| 102 |
|
| 103 |
|
| 104 |
gotCtx := sink.Contexts() |
| 105 |
assert.Len(t, gotCtx, len(wantCtx)) |
| 106 |
for i, ctx := range gotCtx { |
| 107 |
assert.Equal(t, fmt.Sprintf("value-%d", i), ctx.Value(testKey(i))) |
| 108 |
} |
| 109 |
|
| 110 |
sink.Reset() |
| 111 |
assert.Empty(t, sink.AllTraces()) |
| 112 |
assert.Empty(t, sink.Contexts()) |
| 113 |
assert.Equal(t, 0, sink.SpanCount()) |
| 114 |
} |
| 115 |
|
| 116 |
func TestMetricsSinkWithContext(t *testing.T) { |
| 117 |
sink := new(MetricsSink) |
| 118 |
md := testdata.GenerateMetrics(1) |
| 119 |
want := make([]pmetric.Metrics, 0, 7) |
| 120 |
wantCtx := make([]context.Context, 0, 7) |
| 121 |
|
| 122 |
for i := range 7 { |
| 123 |
ctx := context.WithValue(context.Background(), testKey(i), fmt.Sprintf("value-%d", i)) |
| 124 |
require.NoError(t, sink.ConsumeMetrics(ctx, md)) |
| 125 |
want = append(want, md) |
| 126 |
wantCtx = append(wantCtx, ctx) |
| 127 |
} |
| 128 |
|
| 129 |
assert.Equal(t, want, sink.AllMetrics()) |
| 130 |
assert.Equal(t, 2*len(want), sink.DataPointCount()) |
| 131 |
|
| 132 |
|
| 133 |
gotCtx := sink.Contexts() |
| 134 |
assert.Len(t, gotCtx, len(wantCtx)) |
| 135 |
for i, ctx := range gotCtx { |
| 136 |
assert.Equal(t, fmt.Sprintf("value-%d", i), ctx.Value(testKey(i))) |
| 137 |
} |
| 138 |
|
| 139 |
sink.Reset() |
| 140 |
assert.Empty(t, sink.AllMetrics()) |
| 141 |
assert.Empty(t, sink.Contexts()) |
| 142 |
assert.Equal(t, 0, sink.DataPointCount()) |
| 143 |
} |
| 144 |
|
| 145 |
func TestLogsSinkWithContext(t *testing.T) { |
| 146 |
sink := new(LogsSink) |
| 147 |
md := testdata.GenerateLogs(1) |
| 148 |
want := make([]plog.Logs, 0, 7) |
| 149 |
wantCtx := make([]context.Context, 0, 7) |
| 150 |
|
| 151 |
for i := range 7 { |
| 152 |
ctx := context.WithValue(context.Background(), testKey(i), fmt.Sprintf("value-%d", i)) |
| 153 |
require.NoError(t, sink.ConsumeLogs(ctx, md)) |
| 154 |
want = append(want, md) |
| 155 |
wantCtx = append(wantCtx, ctx) |
| 156 |
} |
| 157 |
|
| 158 |
assert.Equal(t, want, sink.AllLogs()) |
| 159 |
assert.Equal(t, len(want), sink.LogRecordCount()) |
| 160 |
|
| 161 |
|
| 162 |
gotCtx := sink.Contexts() |
| 163 |
assert.Len(t, gotCtx, len(wantCtx)) |
| 164 |
for i, ctx := range gotCtx { |
| 165 |
assert.Equal(t, fmt.Sprintf("value-%d", i), ctx.Value(testKey(i))) |
| 166 |
} |
| 167 |
|
| 168 |
sink.Reset() |
| 169 |
assert.Empty(t, sink.AllLogs()) |
| 170 |
assert.Empty(t, sink.Contexts()) |
| 171 |
assert.Equal(t, 0, sink.LogRecordCount()) |
| 172 |
} |
| 173 |
|
| 174 |
func TestProfilesSinkWithContext(t *testing.T) { |
| 175 |
sink := new(ProfilesSink) |
| 176 |
td := testdata.GenerateProfiles(1) |
| 177 |
want := make([]pprofile.Profiles, 0, 7) |
| 178 |
wantCtx := make([]context.Context, 0, 7) |
| 179 |
|
| 180 |
for i := range 7 { |
| 181 |
ctx := context.WithValue(context.Background(), testKey(i), fmt.Sprintf("value-%d", i)) |
| 182 |
require.NoError(t, sink.ConsumeProfiles(ctx, td)) |
| 183 |
want = append(want, td) |
| 184 |
wantCtx = append(wantCtx, ctx) |
| 185 |
} |
| 186 |
|
| 187 |
assert.Equal(t, want, sink.AllProfiles()) |
| 188 |
assert.Equal(t, len(want), sink.SampleCount()) |
| 189 |
|
| 190 |
|
| 191 |
gotCtx := sink.Contexts() |
| 192 |
assert.Len(t, gotCtx, len(wantCtx)) |
| 193 |
for i, ctx := range gotCtx { |
| 194 |
assert.Equal(t, fmt.Sprintf("value-%d", i), ctx.Value(testKey(i))) |
| 195 |
} |
| 196 |
|
| 197 |
sink.Reset() |
| 198 |
assert.Empty(t, sink.AllProfiles()) |
| 199 |
assert.Empty(t, sink.Contexts()) |
| 200 |
assert.Equal(t, 0, sink.SampleCount()) |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
func TestSinkContextTransformation(t *testing.T) { |
| 205 |
testCases := []struct { |
| 206 |
name string |
| 207 |
sink interface { |
| 208 |
Contexts() []context.Context |
| 209 |
} |
| 210 |
consumeFunc func(any, context.Context) error |
| 211 |
testData any |
| 212 |
}{ |
| 213 |
{ |
| 214 |
name: "TracesSink", |
| 215 |
sink: new(TracesSink), |
| 216 |
consumeFunc: func(sink any, ctx context.Context) error { |
| 217 |
return sink.(*TracesSink).ConsumeTraces(ctx, testdata.GenerateTraces(1)) |
| 218 |
}, |
| 219 |
}, |
| 220 |
{ |
| 221 |
name: "MetricsSink", |
| 222 |
sink: new(MetricsSink), |
| 223 |
consumeFunc: func(sink any, ctx context.Context) error { |
| 224 |
return sink.(*MetricsSink).ConsumeMetrics(ctx, testdata.GenerateMetrics(1)) |
| 225 |
}, |
| 226 |
}, |
| 227 |
{ |
| 228 |
name: "LogsSink", |
| 229 |
sink: new(LogsSink), |
| 230 |
consumeFunc: func(sink any, ctx context.Context) error { |
| 231 |
return sink.(*LogsSink).ConsumeLogs(ctx, testdata.GenerateLogs(1)) |
| 232 |
}, |
| 233 |
}, |
| 234 |
{ |
| 235 |
name: "ProfilesSink", |
| 236 |
sink: new(ProfilesSink), |
| 237 |
consumeFunc: func(sink any, ctx context.Context) error { |
| 238 |
return sink.(*ProfilesSink).ConsumeProfiles(ctx, testdata.GenerateProfiles(1)) |
| 239 |
}, |
| 240 |
}, |
| 241 |
} |
| 242 |
|
| 243 |
for _, tc := range testCases { |
| 244 |
t.Run(tc.name, func(t *testing.T) { |
| 245 |
|
| 246 |
initialCtx := context.WithValue(context.Background(), ctxKey("initial-key"), "initial-value") |
| 247 |
|
| 248 |
|
| 249 |
transformedCtx := context.WithValue(initialCtx, ctxKey("transformed-key"), "transformed-value") |
| 250 |
|
| 251 |
|
| 252 |
err := tc.consumeFunc(tc.sink, transformedCtx) |
| 253 |
require.NoError(t, err) |
| 254 |
|
| 255 |
|
| 256 |
storedContexts := tc.sink.Contexts() |
| 257 |
assert.Len(t, storedContexts, 1, "Should have stored exactly one context") |
| 258 |
|
| 259 |
storedCtx := storedContexts[0] |
| 260 |
|
| 261 |
assert.Equal(t, "initial-value", storedCtx.Value(ctxKey("initial-key")), |
| 262 |
"Initial context value should be preserved") |
| 263 |
assert.Equal(t, "transformed-value", storedCtx.Value(ctxKey("transformed-key")), |
| 264 |
"Transformed context value should be stored") |
| 265 |
}) |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
func TestContextTransformationChain(t *testing.T) { |
| 271 |
sink := new(TracesSink) |
| 272 |
|
| 273 |
|
| 274 |
baseCtx := context.Background() |
| 275 |
ctx1 := context.WithValue(baseCtx, ctxKey("step1"), "value1") |
| 276 |
ctx2 := context.WithValue(ctx1, ctxKey("step2"), "value2") |
| 277 |
ctx3 := context.WithValue(ctx2, ctxKey("step3"), "value3") |
| 278 |
|
| 279 |
|
| 280 |
td := testdata.GenerateTraces(1) |
| 281 |
err := sink.ConsumeTraces(ctx3, td) |
| 282 |
require.NoError(t, err) |
| 283 |
|
| 284 |
|
| 285 |
storedContexts := sink.Contexts() |
| 286 |
require.Len(t, storedContexts, 1) |
| 287 |
|
| 288 |
finalCtx := storedContexts[0] |
| 289 |
|
| 290 |
assert.Equal(t, "value1", finalCtx.Value(ctxKey("step1")), "First transformation should be preserved") |
| 291 |
assert.Equal(t, "value2", finalCtx.Value(ctxKey("step2")), "Second transformation should be preserved") |
| 292 |
assert.Equal(t, "value3", finalCtx.Value(ctxKey("step3")), "Third transformation should be preserved") |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
func TestConcurrentContextTransformations(t *testing.T) { |
| 297 |
sink := new(TracesSink) |
| 298 |
const numGoroutines = 10 |
| 299 |
errChan := make(chan error, numGoroutines) |
| 300 |
|
| 301 |
var wg sync.WaitGroup |
| 302 |
wg.Add(numGoroutines) |
| 303 |
|
| 304 |
for i := range numGoroutines { |
| 305 |
go func(idx int) { |
| 306 |
defer wg.Done() |
| 307 |
key := ctxKey(fmt.Sprintf("goroutine-%d", idx)) |
| 308 |
value := fmt.Sprintf("value-%d", idx) |
| 309 |
ctx := context.WithValue(context.Background(), key, value) |
| 310 |
|
| 311 |
td := testdata.GenerateTraces(1) |
| 312 |
if err := sink.ConsumeTraces(ctx, td); err != nil { |
| 313 |
errChan <- err |
| 314 |
} |
| 315 |
}(i) |
| 316 |
} |
| 317 |
|
| 318 |
wg.Wait() |
| 319 |
close(errChan) |
| 320 |
|
| 321 |
|
| 322 |
for err := range errChan { |
| 323 |
t.Errorf("Error in goroutine: %v", err) |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
storedContexts := sink.Contexts() |
| 328 |
assert.Len(t, storedContexts, numGoroutines) |
| 329 |
|
| 330 |
|
| 331 |
contextValues := make(map[string]bool) |
| 332 |
for _, ctx := range storedContexts { |
| 333 |
for i := range numGoroutines { |
| 334 |
key := ctxKey(fmt.Sprintf("goroutine-%d", i)) |
| 335 |
expectedValue := fmt.Sprintf("value-%d", i) |
| 336 |
if val := ctx.Value(key); val == expectedValue { |
| 337 |
contextValues[fmt.Sprintf("goroutine-%d", i)] = true |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
assert.Len(t, contextValues, numGoroutines, |
| 344 |
"Should have stored contexts from all goroutines") |
| 345 |
} |