|
22 | 22 | import co.elastic.apm.agent.common.util.WildcardMatcher;
|
23 | 23 | import co.elastic.apm.agent.configuration.AutoDetectedServiceInfo;
|
24 | 24 | import co.elastic.apm.agent.configuration.CoreConfiguration;
|
| 25 | +import co.elastic.apm.agent.impl.baggage.BaggageContext; |
25 | 26 | import co.elastic.apm.agent.tracer.service.ServiceInfo;
|
26 | 27 | import co.elastic.apm.agent.configuration.SpyConfiguration;
|
27 | 28 | import co.elastic.apm.agent.configuration.source.ConfigSources;
|
@@ -374,48 +375,61 @@ void testActivationStackOverflow() {
|
374 | 375 | .withObjectPoolFactory(objectPoolFactory)
|
375 | 376 | .buildAndStart();
|
376 | 377 |
|
377 |
| - Transaction transaction = tracer.startRootTransaction(getClass().getClassLoader()); |
378 |
| - assertThat(tracer.getActive()).isNull(); |
379 |
| - try (Scope scope = transaction.activateInScope()) { |
380 |
| - assertThat(tracer.getActive()).isEqualTo(transaction); |
381 |
| - Span child1 = transaction.createSpan(); |
382 |
| - try (Scope childScope = child1.activateInScope()) { |
383 |
| - assertThat(tracer.getActive()).isEqualTo(child1); |
384 |
| - Span grandchild1 = child1.createSpan(); |
385 |
| - try (Scope grandchildScope = grandchild1.activateInScope()) { |
386 |
| - // latter activation should not be applied due to activation stack overflow |
| 378 | + doWithNestedBaggageActivations(() -> { |
| 379 | + Transaction transaction = tracer.startRootTransaction(getClass().getClassLoader()); |
| 380 | + assertThat(tracer.getActive()).isNull(); |
| 381 | + try (Scope scope = transaction.activateInScope()) { |
| 382 | + assertThat(tracer.getActive()).isEqualTo(transaction); |
| 383 | + Span child1 = transaction.createSpan(); |
| 384 | + try (Scope childScope = child1.activateInScope()) { |
387 | 385 | assertThat(tracer.getActive()).isEqualTo(child1);
|
388 |
| - Span ggc = grandchild1.createSpan(); |
389 |
| - try (Scope ggcScope = ggc.activateInScope()) { |
| 386 | + Span grandchild1 = child1.createSpan(); |
| 387 | + try (Scope grandchildScope = grandchild1.activateInScope()) { |
| 388 | + // latter activation should not be applied due to activation stack overflow |
390 | 389 | assertThat(tracer.getActive()).isEqualTo(child1);
|
391 |
| - ggc.end(); |
| 390 | + Span ggc = grandchild1.createSpan(); |
| 391 | + try (Scope ggcScope = ggc.activateInScope()) { |
| 392 | + assertThat(tracer.getActive()).isEqualTo(child1); |
| 393 | + ggc.end(); |
| 394 | + } |
| 395 | + grandchild1.end(); |
392 | 396 | }
|
393 |
| - grandchild1.end(); |
| 397 | + assertThat(tracer.getActive()).isEqualTo(child1); |
| 398 | + child1.end(); |
394 | 399 | }
|
395 |
| - assertThat(tracer.getActive()).isEqualTo(child1); |
396 |
| - child1.end(); |
397 |
| - } |
398 |
| - assertThat(tracer.getActive()).isEqualTo(transaction); |
399 |
| - Span child2 = transaction.createSpan(); |
400 |
| - try (Scope childScope = child2.activateInScope()) { |
401 |
| - assertThat(tracer.getActive()).isEqualTo(child2); |
402 |
| - Span grandchild2 = child2.createSpan(); |
403 |
| - try (Scope grandchildScope = grandchild2.activateInScope()) { |
404 |
| - // latter activation should not be applied due to activation stack overflow |
| 400 | + assertThat(tracer.getActive()).isEqualTo(transaction); |
| 401 | + Span child2 = transaction.createSpan(); |
| 402 | + try (Scope childScope = child2.activateInScope()) { |
| 403 | + assertThat(tracer.getActive()).isEqualTo(child2); |
| 404 | + Span grandchild2 = child2.createSpan(); |
| 405 | + try (Scope grandchildScope = grandchild2.activateInScope()) { |
| 406 | + // latter activation should not be applied due to activation stack overflow |
| 407 | + assertThat(tracer.getActive()).isEqualTo(child2); |
| 408 | + grandchild2.end(); |
| 409 | + } |
405 | 410 | assertThat(tracer.getActive()).isEqualTo(child2);
|
406 |
| - grandchild2.end(); |
| 411 | + child2.end(); |
407 | 412 | }
|
408 |
| - assertThat(tracer.getActive()).isEqualTo(child2); |
409 |
| - child2.end(); |
| 413 | + assertThat(tracer.getActive()).isEqualTo(transaction); |
| 414 | + transaction.end(); |
410 | 415 | }
|
411 |
| - assertThat(tracer.getActive()).isEqualTo(transaction); |
412 |
| - transaction.end(); |
413 |
| - } |
| 416 | + }, tracer, 16); |
414 | 417 | assertThat(tracer.getActive()).isNull();
|
415 | 418 | assertThat(reporter.getTransactions()).hasSize(1);
|
416 | 419 | assertThat(reporter.getSpans()).hasSize(2);
|
417 | 420 | }
|
418 | 421 |
|
| 422 | + private void doWithNestedBaggageActivations(Runnable r, Tracer tracer, int nestedCount) { |
| 423 | + if (nestedCount == 0) { |
| 424 | + r.run(); |
| 425 | + return; |
| 426 | + } |
| 427 | + BaggageContext baggageContext = tracer.currentContext().withUpdatedBaggage().buildContext(); |
| 428 | + try (Scope scope = baggageContext.activateInScope()) { |
| 429 | + doWithNestedBaggageActivations(r, tracer, nestedCount - 1); |
| 430 | + } |
| 431 | + } |
| 432 | + |
419 | 433 | @Test
|
420 | 434 | void testPause() {
|
421 | 435 | tracerImpl.pause();
|
|
0 commit comments