|
1 | 1 | // Copyright The OpenTelemetry Authors |
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
4 | | -using System.Collections; |
5 | | -using System.Collections.Specialized; |
6 | 4 | using System.Diagnostics; |
7 | 5 | using System.Web; |
8 | 6 | using OpenTelemetry.Context.Propagation; |
@@ -56,6 +54,16 @@ public void Has_Started_Returns_Correctly() |
56 | 54 | Assert.Equal(activity, aspNetActivity); |
57 | 55 | } |
58 | 56 |
|
| 57 | + [Fact] |
| 58 | + public void Baggage_Is_Restored_Before_Starting_Activity() |
| 59 | + { |
| 60 | + this.EnableListener(); |
| 61 | + var propagator = new MockTextMapPropagator(); |
| 62 | + var context = HttpContextHelper.GetFakeHttpContextBase(); |
| 63 | + using var activity = ActivityHelper.StartAspNetActivity(propagator, context, this.StartTestActivityWithBaggageAttribute); |
| 64 | + Assert.Equal(MockTextMapPropagator.BaggageValue, activity?.Tags.FirstOrDefault(kv => kv.Key == MockTextMapPropagator.BaggageKey).Value); |
| 65 | + } |
| 66 | + |
59 | 67 | [Fact] |
60 | 68 | public async Task Can_Restore_Activity() |
61 | 69 | { |
@@ -507,72 +515,45 @@ private void EnableListener(Action<Activity>? onStarted = null, Action<Activity> |
507 | 515 | return this.testActivitySource.StartActivity(ActivityKind.Server, activityContext); |
508 | 516 | } |
509 | 517 |
|
510 | | - private class TestHttpRequest : HttpRequestBase |
| 518 | + private Activity? StartTestActivityWithBaggageAttribute(HttpContextBase httpContext, ActivityContext activityContext) |
511 | 519 | { |
512 | | - private readonly NameValueCollection headers = []; |
513 | | - |
514 | | - public override NameValueCollection Headers => this.headers; |
515 | | - |
516 | | - public override UnvalidatedRequestValuesBase Unvalidated => new TestUnvalidatedRequestValues(this.headers); |
| 520 | + var baggageValue = Baggage.Current.GetBaggage(MockTextMapPropagator.BaggageKey); |
| 521 | + var activity = this.testActivitySource.StartActivity(ActivityKind.Server, activityContext); |
| 522 | + activity?.AddTag(MockTextMapPropagator.BaggageKey, baggageValue); |
| 523 | + return activity; |
517 | 524 | } |
518 | 525 |
|
519 | | - private class TestUnvalidatedRequestValues : UnvalidatedRequestValuesBase |
520 | | - { |
521 | | - public TestUnvalidatedRequestValues(NameValueCollection headers) |
522 | | - { |
523 | | - this.Headers = headers; |
524 | | - } |
525 | | - |
526 | | - public override NameValueCollection Headers { get; } |
527 | | - } |
528 | | - |
529 | | - private class TestHttpResponse : HttpResponseBase |
530 | | - { |
531 | | - } |
532 | | - |
533 | | - private class TestHttpServerUtility : HttpServerUtilityBase |
| 526 | + private class NoopTextMapPropagator : TextMapPropagator |
534 | 527 | { |
535 | | - private readonly HttpContextBase context; |
| 528 | + public override ISet<string>? Fields => null; |
536 | 529 |
|
537 | | - public TestHttpServerUtility(HttpContextBase context) |
| 530 | + public override PropagationContext Extract<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>?> getter) |
538 | 531 | { |
539 | | - this.context = context; |
| 532 | + return default; |
540 | 533 | } |
541 | 534 |
|
542 | | - public override Exception GetLastError() |
| 535 | + public override void Inject<T>(PropagationContext context, T carrier, Action<T, string, string> setter) |
543 | 536 | { |
544 | | - return this.context.Error; |
545 | 537 | } |
546 | 538 | } |
547 | 539 |
|
548 | | - private class TestHttpContext : HttpContextBase |
| 540 | + private class MockTextMapPropagator : TextMapPropagator |
549 | 541 | { |
550 | | - private readonly Hashtable items; |
551 | | - |
552 | | - public TestHttpContext(Exception? error = null) |
553 | | - { |
554 | | - this.Server = new TestHttpServerUtility(this); |
555 | | - this.items = []; |
556 | | - this.Error = error; |
557 | | - } |
558 | | - |
559 | | - public override HttpRequestBase Request { get; } = new TestHttpRequest(); |
560 | | - |
561 | | - /// <inheritdoc /> |
562 | | - public override IDictionary Items => this.items; |
563 | | - |
564 | | - public override Exception? Error { get; } |
| 542 | + internal const string BaggageKey = "TestKey1"; |
| 543 | + internal const string BaggageValue = "TestValue1"; |
565 | 544 |
|
566 | | - public override HttpServerUtilityBase Server { get; } |
567 | | - } |
568 | | - |
569 | | - private class NoopTextMapPropagator : TextMapPropagator |
570 | | - { |
571 | 545 | public override ISet<string>? Fields => null; |
572 | 546 |
|
573 | 547 | public override PropagationContext Extract<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>?> getter) |
574 | 548 | { |
575 | | - return default; |
| 549 | + var baggage = Baggage.Create(new Dictionary<string, string> |
| 550 | + { |
| 551 | + { BaggageKey, BaggageValue }, |
| 552 | + }); |
| 553 | + |
| 554 | + var activityContext = new ActivityContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.Recorded); |
| 555 | + |
| 556 | + return new PropagationContext(activityContext, baggage); |
576 | 557 | } |
577 | 558 |
|
578 | 559 | public override void Inject<T>(PropagationContext context, T carrier, Action<T, string, string> setter) |
|
0 commit comments