コード例 #1
0
        private static T?ParseB3Sampling <T>(IHeadersCollection headers)
            where T : struct, Enum
        {
            string priority = string.Empty;
            var    debugged = headers.GetValues(HttpHeaderNames.B3Flags).ToList();
            var    sampled  = headers.GetValues(HttpHeaderNames.B3Sampled).ToList();

            if (debugged.Count != 0 && debugged[0] == "1")
            {
                priority = UserKeep;
            }
            else if (sampled.Count != 0)
            {
                priority = sampled.First();
            }

            if (Enum.TryParse <T>(priority, out var result) && Enum.IsDefined(typeof(T), result))
            {
                return(result);
            }

            Log.Information(
                "Could not parse headers: {0}: {1} or {2}: {3}",
                HttpHeaderNames.B3Flags,
                string.Join(",", debugged),
                HttpHeaderNames.B3Sampled,
                string.Join(",", sampled));

            return(default);
コード例 #2
0
        public void SetHeaders_InjectsTheHeadersCorrectly(IHeadersCollection headers)
        {
            var traceId     = TraceId.CreateRandom();
            var spanContext = new SpanContext(traceId, 123, SamplingPriority.AutoKeep);

            ServerTimingHeader.SetHeaders(spanContext, headers, (h, name, value) => h.Add(name, value));

            using (new AssertionScope())
            {
                headers.GetValues("Server-Timing").Should().HaveCount(1);
                headers.GetValues("Server-Timing").Should().Equal($"traceparent;desc=\"00-{traceId.ToString()}-000000000000007b-01\"");
                headers.GetValues("Access-Control-Expose-Headers").Should().HaveCount(1);
                headers.GetValues("Access-Control-Expose-Headers").Should().Equal("Server-Timing");
            }
        }
コード例 #3
0
        private static void AssertExpected(IHeadersCollection headers, string key, string expected)
        {
            var matches = headers.GetValues(key);

            Assert.Single(matches);
            matches.ToList().ForEach(x => Assert.Equal(expected, x));
        }
        internal void Inject_CreateCorrectTraceStateHeaderIfPresent(IHeadersCollection headers)
        {
            var         traceId     = TraceId.CreateFromString("0af7651916cd43dd8448eb211c80319c");
            const ulong spanId      = 67667974448284343;
            var         spanContext = new SpanContext(traceId, spanId, "state");
            var         propagator  = new W3CSpanContextPropagator(new OtelTraceIdConvention());

            propagator.Inject(spanContext, headers);

            using (new AssertionScope())
            {
                headers.GetValues(W3CHeaderNames.TraceState).Should().HaveCount(1);
                headers.GetValues(W3CHeaderNames.TraceState).Should().BeEquivalentTo(new List <string> {
                    "state"
                });
            }
        }
        internal void Inject_DoNotCreateCorrectTraceStateHeaderIfNotPresent(IHeadersCollection headers)
        {
            var         traceId     = TraceId.CreateFromString("0af7651916cd43dd8448eb211c80319c");
            const ulong spanId      = 67667974448284343;
            var         spanContext = new SpanContext(traceId, spanId, traceState: null);
            var         propagator  = new W3CSpanContextPropagator(new OtelTraceIdConvention());

            propagator.Inject(spanContext, headers);

            headers.GetValues(W3CHeaderNames.TraceState).Should().HaveCount(0);
        }
コード例 #6
0
        internal void Inject_CreateCorrectTraceStateHeaderIfPresent(IHeadersCollection headers)
        {
            var         traceId     = TraceId.CreateFromString("0af7651916cd43dd8448eb211c80319c");
            const ulong spanId      = 67667974448284343;
            var         spanContext = new SpanContext(traceId, spanId, samplingPriority: null, serviceName: null, "state");
            var         propagator  = W3CSpanContextPropagator.Instance;

            propagator.Inject(spanContext, headers);

            headers.GetValues(W3CHeaderNames.TraceState).Should().Equal("state");
        }
コード例 #7
0
        internal void Inject_CratesCorrectTraceParentHeader(IHeadersCollection headers)
        {
            var         traceId     = TraceId.CreateFromString("0af7651916cd43dd8448eb211c80319c");
            const ulong spanId      = 67667974448284343;
            var         spanContext = new SpanContext(traceId, spanId, samplingPriority: null);
            var         propagator  = W3CSpanContextPropagator.Instance;

            propagator.Inject(spanContext, headers);

            headers.GetValues(W3CHeaderNames.TraceParent).Should().Equal("00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-01");
        }
コード例 #8
0
        private static SamplingPriority?ParseB3Sampling(IHeadersCollection headers)
        {
            var debugged = headers.GetValues(HttpHeaderNames.B3Flags).ToList();
            var sampled  = headers.GetValues(HttpHeaderNames.B3Sampled).ToList();

            if (debugged.Count != 0 && (debugged[0] == "0" || debugged[0] == "1"))
            {
                return(debugged[0] == "1" ? SamplingPriority.UserKeep : (SamplingPriority?)null);
            }
            else if (sampled.Count != 0 && (sampled[0] == "0" || sampled[0] == "1"))
            {
                return(sampled[0] == "1" ? SamplingPriority.AutoKeep : SamplingPriority.AutoReject);
            }

            Log.Information(
                "Could not parse headers: {0}: {1} or {2}: {3}",
                HttpHeaderNames.B3Flags,
                string.Join(",", debugged),
                HttpHeaderNames.B3Sampled,
                string.Join(",", sampled));

            return(null);
        }
コード例 #9
0
        private static ulong ParseUInt64(IHeadersCollection headers, string headerName)
        {
            var headerValues = headers.GetValues(headerName).ToList();

            if (headerValues.Count > 0)
            {
                foreach (string headerValue in headerValues)
                {
                    if (ulong.TryParse(headerValue, NumberStyles, InvariantCulture, out var result))
                    {
                        return(result);
                    }
                }

                Log.Information("Could not parse {0} headers: {1}", headerName, string.Join(",", headerValues));
            }

            return(0);
        }
コード例 #10
0
        private static T?ParseEnum <T>(IHeadersCollection headers, string headerName)
            where T : struct, Enum
        {
            var headerValues = headers.GetValues(headerName).ToList();

            if (headerValues.Count > 0)
            {
                foreach (string headerValue in headerValues)
                {
                    if (Enum.TryParse <T>(headerValue, out var result) &&
                        Enum.IsDefined(typeof(T), result))
                    {
                        return(result);
                    }
                }

                Log.Information(
                    "Could not parse {0} headers: {1}",
                    headerName,
                    string.Join(",", headerValues));
            }

            return(default);
コード例 #11
0
        private static void AssertMissing(IHeadersCollection headers, string key)
        {
            var matches = headers.GetValues(key);

            Assert.Empty(matches);
        }
コード例 #12
0
 private static IEnumerable <string> ExtractFromHeadersCollection(IHeadersCollection carrier, string header)
 {
     return(carrier.GetValues(header));
 }