|
9 | 9 |
|
10 | 10 | "github.com/stretchr/testify/assert"
|
11 | 11 | "github.com/stretchr/testify/require"
|
| 12 | + "go.opentelemetry.io/collector/component" |
12 | 13 | "go.opentelemetry.io/collector/component/componenttest"
|
13 | 14 | "go.opentelemetry.io/collector/consumer/consumertest"
|
14 | 15 | "go.opentelemetry.io/collector/featuregate"
|
@@ -37,7 +38,7 @@ func TestMetricsAfterOneEvaluation(t *testing.T) {
|
37 | 38 | },
|
38 | 39 | }
|
39 | 40 | cs := &consumertest.TracesSink{}
|
40 |
| - ct := s.NewSettings().TelemetrySettings |
| 41 | + ct := s.NewSettings() |
41 | 42 | proc, err := newTracesProcessor(context.Background(), ct, cs, cfg, withDecisionBatcher(syncBatcher))
|
42 | 43 | require.NoError(t, err)
|
43 | 44 | defer func() {
|
@@ -211,6 +212,102 @@ func TestMetricsAfterOneEvaluation(t *testing.T) {
|
211 | 212 | assert.Len(t, cs.AllTraces(), 1)
|
212 | 213 | }
|
213 | 214 |
|
| 215 | +func TestMetricsWithComponentID(t *testing.T) { |
| 216 | + // prepare |
| 217 | + s := setupTestTelemetry() |
| 218 | + b := newSyncIDBatcher() |
| 219 | + syncBatcher := b.(*syncIDBatcher) |
| 220 | + |
| 221 | + cfg := Config{ |
| 222 | + DecisionWait: 1, |
| 223 | + NumTraces: 100, |
| 224 | + PolicyCfgs: []PolicyCfg{ |
| 225 | + { |
| 226 | + sharedPolicyCfg: sharedPolicyCfg{ |
| 227 | + Name: "always", |
| 228 | + Type: AlwaysSample, |
| 229 | + }, |
| 230 | + }, |
| 231 | + }, |
| 232 | + } |
| 233 | + cs := &consumertest.TracesSink{} |
| 234 | + ct := s.NewSettings() |
| 235 | + ct.ID = component.MustNewIDWithName("tail_sampling", "unique_id") // e.g tail_sampling/unique_id |
| 236 | + proc, err := newTracesProcessor(context.Background(), ct, cs, cfg, withDecisionBatcher(syncBatcher)) |
| 237 | + require.NoError(t, err) |
| 238 | + defer func() { |
| 239 | + err = proc.Shutdown(context.Background()) |
| 240 | + require.NoError(t, err) |
| 241 | + }() |
| 242 | + |
| 243 | + err = proc.Start(context.Background(), componenttest.NewNopHost()) |
| 244 | + require.NoError(t, err) |
| 245 | + |
| 246 | + // test |
| 247 | + err = proc.ConsumeTraces(context.Background(), simpleTraces()) |
| 248 | + require.NoError(t, err) |
| 249 | + |
| 250 | + tsp := proc.(*tailSamplingSpanProcessor) |
| 251 | + tsp.policyTicker.OnTick() // the first tick always gets an empty batch |
| 252 | + tsp.policyTicker.OnTick() |
| 253 | + |
| 254 | + // verify |
| 255 | + var md metricdata.ResourceMetrics |
| 256 | + require.NoError(t, s.reader.Collect(context.Background(), &md)) |
| 257 | + require.Equal(t, 8, s.len(md)) |
| 258 | + |
| 259 | + for _, tt := range []struct { |
| 260 | + opts []metricdatatest.Option |
| 261 | + m metricdata.Metrics |
| 262 | + }{ |
| 263 | + { |
| 264 | + opts: []metricdatatest.Option{metricdatatest.IgnoreTimestamp()}, |
| 265 | + m: metricdata.Metrics{ |
| 266 | + Name: "otelcol_processor_tail_sampling_count_traces_sampled", |
| 267 | + Description: "Count of traces that were sampled or not per sampling policy", |
| 268 | + Unit: "{traces}", |
| 269 | + Data: metricdata.Sum[int64]{ |
| 270 | + IsMonotonic: true, |
| 271 | + Temporality: metricdata.CumulativeTemporality, |
| 272 | + DataPoints: []metricdata.DataPoint[int64]{ |
| 273 | + { |
| 274 | + Attributes: attribute.NewSet( |
| 275 | + attribute.String("policy", "unique_id.always"), |
| 276 | + attribute.String("sampled", "true"), |
| 277 | + ), |
| 278 | + Value: 1, |
| 279 | + }, |
| 280 | + }, |
| 281 | + }, |
| 282 | + }, |
| 283 | + }, |
| 284 | + { |
| 285 | + opts: []metricdatatest.Option{metricdatatest.IgnoreTimestamp(), metricdatatest.IgnoreValue()}, |
| 286 | + m: metricdata.Metrics{ |
| 287 | + Name: "otelcol_processor_tail_sampling_sampling_decision_latency", |
| 288 | + Description: "Latency (in microseconds) of a given sampling policy", |
| 289 | + Unit: "µs", |
| 290 | + Data: metricdata.Histogram[int64]{ |
| 291 | + Temporality: metricdata.CumulativeTemporality, |
| 292 | + DataPoints: []metricdata.HistogramDataPoint[int64]{ |
| 293 | + { |
| 294 | + Attributes: attribute.NewSet( |
| 295 | + attribute.String("policy", "unique_id.always"), |
| 296 | + ), |
| 297 | + }, |
| 298 | + }, |
| 299 | + }, |
| 300 | + }, |
| 301 | + }, |
| 302 | + } { |
| 303 | + got := s.getMetric(tt.m.Name, md) |
| 304 | + metricdatatest.AssertEqual(t, tt.m, got, tt.opts...) |
| 305 | + } |
| 306 | + |
| 307 | + // sanity check |
| 308 | + assert.Len(t, cs.AllTraces(), 1) |
| 309 | +} |
| 310 | + |
214 | 311 | func TestProcessorTailSamplingCountSpansSampled(t *testing.T) {
|
215 | 312 | err := featuregate.GlobalRegistry().Set("processor.tailsamplingprocessor.metricstatcountspanssampled", true)
|
216 | 313 | require.NoError(t, err)
|
@@ -238,7 +335,7 @@ func TestProcessorTailSamplingCountSpansSampled(t *testing.T) {
|
238 | 335 | },
|
239 | 336 | }
|
240 | 337 | cs := &consumertest.TracesSink{}
|
241 |
| - ct := s.NewSettings().TelemetrySettings |
| 338 | + ct := s.NewSettings() |
242 | 339 | proc, err := newTracesProcessor(context.Background(), ct, cs, cfg, withDecisionBatcher(syncBatcher))
|
243 | 340 | require.NoError(t, err)
|
244 | 341 | defer func() {
|
@@ -303,7 +400,7 @@ func TestProcessorTailSamplingSamplingTraceRemovalAge(t *testing.T) {
|
303 | 400 | },
|
304 | 401 | }
|
305 | 402 | cs := &consumertest.TracesSink{}
|
306 |
| - ct := s.NewSettings().TelemetrySettings |
| 403 | + ct := s.NewSettings() |
307 | 404 | proc, err := newTracesProcessor(context.Background(), ct, cs, cfg, withDecisionBatcher(syncBatcher))
|
308 | 405 | require.NoError(t, err)
|
309 | 406 | defer func() {
|
@@ -364,7 +461,7 @@ func TestProcessorTailSamplingSamplingLateSpanAge(t *testing.T) {
|
364 | 461 | },
|
365 | 462 | }
|
366 | 463 | cs := &consumertest.TracesSink{}
|
367 |
| - ct := s.NewSettings().TelemetrySettings |
| 464 | + ct := s.NewSettings() |
368 | 465 | proc, err := newTracesProcessor(context.Background(), ct, cs, cfg, withDecisionBatcher(syncBatcher))
|
369 | 466 | require.NoError(t, err)
|
370 | 467 | defer func() {
|
|
0 commit comments