using System;
using Microsoft.AspNetCore.Builder;
using Criteo.Profiling.Tracing;

namespace Criteo.Profiling.Tracing.Middleware
{
    public static class TracingMiddleware
    {
        public static void UseTracing(this IApplicationBuilder app, string serviceName)
        {
            app.Use(async (context, next) => {
                var trace = Trace.Create();
                Trace.Current = trace;
                trace.Record(Annotations.ServerRecv());
                trace.Record(Annotations.ServiceName(serviceName));
                trace.Record(Annotations.Rpc(context.Request.Method));
                await next.Invoke();
                trace.Record(Annotations.ServerSend());
            });
        }
    }
}
using System;
using Microsoft.AspNetCore.Builder;
using Criteo.Profiling.Tracing;

namespace Criteo.Profiling.Tracing.Middleware
{
    public static class TracingMiddleware
    {
        public static void UseTracing(this IApplicationBuilder app, string serviceName)
        {
            var extractor = new ZipkinHttpTraceExtractor();
            app.Use(async (context, next) => {
                Trace trace;
                if (!extractor.TryExtract(context.Request.Headers, out trace))
                {
                    trace = Trace.Create();
                }
                Trace.Current = trace;
                trace.Record(Annotations.ServerRecv());
                trace.Record(Annotations.ServiceName(serviceName));
                trace.Record(Annotations.Rpc(context.Request.Method));
                await next.Invoke();
                trace.Record(Annotations.ServerSend());
            });
        }
    }
}
Enable middleware to extract trace from incoming request headers.
Enable middleware to extract trace from incoming request headers.

If such headers exist, the trace is created from these values.
