Example #1
0
        public static TraceContext New()
        {
            var context = new TraceContext
            {
                TraceId  = SodiumCore.GetRandomBytes(16),
                ParentId = SodiumCore.GetRandomBytes(8),
                Flags    = TraceFlags.None
            };

            return(context);
        }
        //
        // See: https://www.w3.org/TR/trace-context/#problem-statement
        public static IApplicationBuilder UseTraceContext(this IApplicationBuilder app)
        {
            app.Use(async(context, next) =>
            {
                if (!context.Request.Headers.TryGetValue(HttpHeaders.TraceParent, out var traceContext))
                {
                    context.Request.Headers.Add(HttpHeaders.TraceParent,
                                                traceContext = TraceContext.New().Header);
                }

                context.TraceIdentifier = traceContext;

                if (app.ApplicationServices.GetService(typeof(IHttpContextAccessor)) is IHttpContextAccessor accessor)
                {
                    accessor.HttpContext = context;
                }

                await next();
            });

            return(app);
        }