<commit_before>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 Middleware.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());
            });
        }
    }
}<commit_msg>Add exception handling in middleware<commit_after>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 Middleware.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));
                try
                {
                    await next.Invoke();
                }
                catch (System.Exception e)
                {
                    trace.Record(Annotations.Tag("error", e.Message));
                    throw;
                }
                finally
                {
                    trace.Record(Annotations.ServerSend());
                }
            });
        }
    }
}
{'fraction_non_alphanumeric': 0.09054770318021202, 'fraction_numerical': 0.0, 'mean_word_length': 1.565118912797282, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6746731', 'n_tokens_mistral': 565, 'n_tokens_neox': 537, 'n_words': 98}