예제 #1
0
        // See https://github.com/aspnet/AspNetCore/blob/master/src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs
        public static void InitFromMessageHeaders(this Activity activity, MessageHeaderCollection headers)
        {
            var traceId = headers.GetValue(DiagnosticsConstants.TraceIdHeaderKey);

            if (!string.IsNullOrEmpty(traceId))
            {
                // This will reflect, that the current activity is a child of the activity
                // which is represented in the message.
                activity.SetParentId(traceId);

                var traceState = headers.GetValue(DiagnosticsConstants.TraceStateHeaderKey);
                if (!string.IsNullOrEmpty(traceState))
                {
                    activity.TraceStateString = traceState;
                }

                // We expect baggage to be empty by default.
                // Only very advanced users will be using it in near future, we encourage them to keep baggage small (few items).
                var baggage = headers.GetValue(DiagnosticsConstants.TraceBaggageHeaderKey);
                if (BaggageConverter.TryDeserialize(baggage, out var baggageItems))
                {
                    activity.AddBaggageRange(baggageItems);
                }
            }
        }
예제 #2
0
        public void AddBaggageRange_SomeNewItems_ItemsAreAppended()
        {
            var activity = new Activity("test");
            IList <KeyValuePair <string, string> > itemsToAdd = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("key1", "value1"),
                new KeyValuePair <string, string>("key2", "value2"),
                new KeyValuePair <string, string>("key3", "value3")
            };

            activity.AddBaggage("key0", "value0");

            activity.AddBaggageRange(itemsToAdd);

            activity.Baggage.Should().BeEquivalentTo(
                new KeyValuePair <string, string>("key0", "value0"),
                new KeyValuePair <string, string>("key1", "value1"),
                new KeyValuePair <string, string>("key2", "value2"),
                new KeyValuePair <string, string>("key3", "value3"));
        }