Пример #1
0
        public void Event(HttpContext httpContext, string @event)
        {
            // todo - if the user isnt using tracing the code gets here and will blow up on
            // _tracer.Tracer.TryExtract..
            if (_tracer == null)
            {
                return;
            }

            var span = httpContext.GetSpan();

            if (span == null)
            {
                var spanBuilder = new SpanBuilder($"server {httpContext.Request.Method} {httpContext.Request.Path}");
                if (_tracer.Tracer.TryExtract(out var spanContext, httpContext.Request.Headers, (c, k) => c[k].GetValue(),
                                              c => c.Select(x => new KeyValuePair <string, string>(x.Key, x.Value.GetValue())).GetEnumerator()))
                {
                    spanBuilder.AsChildOf(spanContext);
                }

                span = _tracer.Start(spanBuilder);
                httpContext.SetSpan(span);
            }

            span?.Log(LogField.CreateNew().Event(@event));
        }
        public void BeginRequest([Property] HttpContext httpContext)
        {
            var patterns = _options.IgnoredRoutesRegexPatterns;

            if (patterns == null || patterns.Any(x => Regex.IsMatch(httpContext.Request.Path, x)))
            {
                return;
            }
            var spanBuilder = new SpanBuilder($"server {httpContext.Request.Method} {httpContext.Request.Path}");

            if (_tracer.Tracer.TryExtract(out var spanContext, httpContext.Request.Headers, (c, k) => c[k].GetValue(),
                                          c => c.Select(x => new KeyValuePair <string, string>(x.Key, x.Value.GetValue())).GetEnumerator()))
            {
                spanBuilder.AsChildOf(spanContext);
            }
            var span = _tracer.Start(spanBuilder);

            span.Log(LogField.CreateNew().ServerReceive());
            span.Log(LogField.CreateNew().Event("AspNetCore BeginRequest"));
            span.Tags
            .Server().Component("AspNetCore")
            .HttpMethod(httpContext.Request.Method)
            .HttpUrl($"{httpContext.Request.Scheme}://{httpContext.Request.Host.ToUriComponent()}{httpContext.Request.Path}{httpContext.Request.QueryString}")
            .HttpHost(httpContext.Request.Host.ToUriComponent())
            .HttpPath(httpContext.Request.Path)
            .HttpStatusCode(httpContext.Response.StatusCode)
            .PeerAddress(httpContext.Connection.RemoteIpAddress.ToString())
            .PeerPort(httpContext.Connection.RemotePort)
            .UserIp(httpContext.GetUserIp());

            _tracer.Tracer.SetEntrySpan(span);
        }
Пример #3
0
        private void Event(HttpContext httpContext, string @event)
        {
            // todo - if the user isnt using tracing the code gets here and will blow up on
            // _tracer.Tracer.TryExtract. We already use the fake tracer for another scenario
            // so sticking it here as well..I guess we need a factory for this but cba to do it at
            // the moment
            if (_tracer.GetType() == typeof(FakeServiceTracer))
            {
                return;
            }

            var span = httpContext.GetSpan();

            if (span == null)
            {
                var spanBuilder = new SpanBuilder($"server {httpContext.Request.Method} {httpContext.Request.Path}");
                if (_tracer.Tracer.TryExtract(out var spanContext, httpContext.Request.Headers, (c, k) => c[k].GetValue(),
                                              c => c.Select(x => new KeyValuePair <string, string>(x.Key, x.Value.GetValue())).GetEnumerator()))
                {
                    spanBuilder.AsChildOf(spanContext);
                }

                span = _tracer.Start(spanBuilder);
                httpContext.SetSpan(span);
            }

            span?.Log(LogField.CreateNew().Event(@event));
        }
        public void HttpRequest([Property(Name = "Request")] HttpRequestMessage request)
        {
            var patterns = _options.IgnoredRoutesRegexPatterns;

            if (patterns == null || patterns.Any(x => Regex.IsMatch(request.RequestUri.AbsolutePath, x)))
            {
                return;
            }
            var spanBuilder = new SpanBuilder($"httpclient {request.Method}");
            var spanContext = _tracer.Tracer.GetEntrySpan()?.SpanContext;

            if (spanContext != null)
            {
                spanBuilder.AsChildOf(spanContext);
            }
            var span = _tracer.Start(spanBuilder);

            span.Tags.Client().Component("HttpClient")
            .HttpMethod(request.Method.Method)
            .HttpUrl(request.RequestUri.OriginalString)
            .HttpHost(request.RequestUri.Host)
            .HttpPath(request.RequestUri.PathAndQuery)
            .PeerAddress(request.RequestUri.OriginalString)
            .PeerHostName(request.RequestUri.Host)
            .PeerPort(request.RequestUri.Port);

            _tracer.Tracer.Inject(span.SpanContext, request.Headers, (c, k, v) => c.Add(k, v));
            span.Log(LogField.CreateNew().ClientSend());
            if (request.Method == HttpMethod.Post)
            {
                span.Tags.Add("request", request.Content.ReadAsStringAsync().Result);
            }

            _tracer.Tracer.SetExitSpan(span);
        }
        public static TResult Trace <TResult>(this IServiceTracer tracer, ISpanBuilder spanBuilder, Func <ISpan, TResult> operation)
        {
            if (tracer == null)
            {
                throw new ArgumentNullException(nameof(tracer));
            }

            using (var span = tracer.Start(spanBuilder))
            {
                try
                {
                    if (operation == null)
                    {
                        return(default(TResult));
                    }

                    return(operation(span));
                }
                catch (Exception exception)
                {
                    span.Exception(exception);
                    throw;
                }
            }
        }
Пример #6
0
        public ISpan OnBeginRequest(HttpContext httpContext)
        {
            var spanBuilder = new SpanBuilder($"server {httpContext.Request.Method} {httpContext.Request.Path}");

            if (_tracer.Tracer.TryExtract(out var spanContext, httpContext.Request.Headers, (c, k) => c[k].GetValue(),
                                          c => c.Select(x => new KeyValuePair <string, string>(x.Key, x.Value.GetValue())).GetEnumerator()))
            {
                spanBuilder.AsChildOf(spanContext);
            }
            var span = _tracer.Start(spanBuilder);

            httpContext.SetSpan(span);
            span.Log(LogField.CreateNew().ServerReceive());
            span.Log(LogField.CreateNew().Event("AspNetCore BeginRequest"));
            span.Tags
            .Server().Component("AspNetCore")
            .HttpMethod(httpContext.Request.Method)
            .HttpUrl($"{httpContext.Request.Scheme}://{httpContext.Request.Host.ToUriComponent()}{httpContext.Request.Path}{httpContext.Request.QueryString}")
            .HttpHost(httpContext.Request.Host.ToUriComponent())
            .HttpPath(httpContext.Request.Path)
            .HttpStatusCode(httpContext.Response.StatusCode)
            .PeerAddress(httpContext.Connection.RemoteIpAddress.ToString())
            .PeerPort(httpContext.Connection.RemotePort);
            _tracer.Tracer.SetCurrentSpan(span);
            return(span);
        }
Пример #7
0
        public static TResult Trace <TResult>(this IServiceTracer tracer, ISpanBuilder spanBuilder, Func <ISpan, TResult> operation)
        {
            if (tracer == null)
            {
                throw new ArgumentNullException(nameof(tracer));
            }

            var span = tracer.Start(spanBuilder);

            var curr = tracer.Tracer.GetCurrentSpan();

            try
            {
                tracer.Tracer.SetCurrentSpan(span);

                if (operation == null)
                {
                    return(default(TResult));
                }

                return(operation(span));
            }
            catch (Exception exception)
            {
                span.Exception(exception);
                throw;
            }
            finally
            {
                span.Finish(DateTimeOffset.UtcNow);
                tracer.Tracer.SetCurrentSpan(curr);
            }
        }
Пример #8
0
        public static void Trace(this IServiceTracer tracer, ISpanBuilder spanBuilder, Action <ISpan> operation)
        {
            if (tracer == null)
            {
                throw new ArgumentNullException(nameof(tracer));
            }

            var span = tracer.Start(spanBuilder);

            var curr = tracer.Tracer.GetCurrentSpan();

            try
            {
                tracer.Tracer.SetCurrentSpan(span);
                operation?.Invoke(span);
            }
            catch (Exception exception)
            {
                span.Exception(exception);
                throw;
            }
            finally
            {
                span.Finish(DateTimeOffset.UtcNow);
                tracer.Tracer.SetCurrentSpan(curr);
            }
        }
Пример #9
0
        public static async Task <TResult> TraceAsync <TResult>(this IServiceTracer tracer, ISpanBuilder spanBuilder, Func <ISpan, Task <TResult> > operation)
        {
            if (tracer == null)
            {
                throw new ArgumentNullException(nameof(tracer));
            }

            var span = tracer.Start(spanBuilder);

            var curr = tracer.Tracer.GetCurrentSpan();

            try
            {
                tracer.Tracer.SetCurrentSpan(span);
                return(await operation?.Invoke(span));
            }
            catch (Exception exception)
            {
                span.Exception(exception);
                throw;
            }
            finally
            {
                span.Finish(DateTimeOffset.UtcNow);
                tracer.Tracer.SetCurrentSpan(curr);
            }
        }
Пример #10
0
        public ISpan OnBeginRequest(HttpContext httpContext)
        {
            var spanBuilder = new SpanBuilder($"server {httpContext.Request.Method} {httpContext.Request.Path}");

            if (_tracer.Tracer.TryExtract(out var spanContext, httpContext.Request.Headers, (c, k) => c[k],
                                          c => c.Select(x => new KeyValuePair <string, string>(x.Key, x.Value)).GetEnumerator()))
            {
                spanBuilder.AsChildOf(spanContext);
            }
            var span = _tracer.Start(spanBuilder);

            httpContext.SetSpan(span);
            span.Log(LogField.CreateNew().ServerReceive());
            span.Log(LogField.CreateNew().Event("Microsoft.AspNetCore.Hosting.BeginRequest"));
            _tracer.Tracer.SetCurrentSpan(span);
            return(span);
        }
Пример #11
0
        public TraceSpan OnBeginRequest(HttpContext httpContext)
        {
            var patterns = _options.IgnoredRoutesRegexPatterns;

            if (patterns == null || patterns.Any(x => Regex.IsMatch(httpContext.Request.Path, x)))
            {
                return(null);
            }
            var span = _tracer.Start();

            span.SetOperationName(httpContext.Request.GetDisplayUrl());
            span.Tags
            .Component("AspNetCore")
            .HttpMethod(httpContext.Request.Method)
            .PeerAddress(httpContext.Connection.RemoteIpAddress.ToString())
            .PeerPort(httpContext.Connection.RemotePort);

            httpContext.SetSpan(span);
            return(span);
        }
        public static async Task TraceAsync(this IServiceTracer tracer, ISpanBuilder spanBuilder, Func <ISpan, Task> operation)
        {
            if (tracer == null)
            {
                throw new ArgumentNullException(nameof(tracer));
            }

            using (var span = tracer.Start(spanBuilder))
            {
                try
                {
                    await operation?.Invoke(span);
                }
                catch (Exception exception)
                {
                    span.Exception(exception);
                    throw;
                }
            }
        }
        public static void Trace(this IServiceTracer tracer, ISpanBuilder spanBuilder, Action <ISpan> operation)
        {
            if (tracer == null)
            {
                throw new ArgumentNullException(nameof(tracer));
            }

            using (var span = tracer.Start(spanBuilder))
            {
                try
                {
                    operation?.Invoke(span);
                }
                catch (Exception exception)
                {
                    span.Exception(exception);
                    throw;
                }
            }
        }
Пример #14
0
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var span = _tracer.Start();

            _tracer.AsChildren(span);

            span.SetOperationName(request.RequestUri.OriginalString);
            span.Tags.Component("HttpClient")
            .HttpMethod(request.Method.Method);

            if (request.Method.Method.ToLower() == "post")
            {
                span.Tags.RequstBody(request.Content.ReadAsStringAsync().Result);
            }

            request.Headers.Add(TracerKeys.TraceId, span.TraceId);
            request.Headers.Add(TracerKeys.TraceSpanId, span.SpanId);
            request.Headers.Add(TracerKeys.TraceLaunchId, span.LaunchId);

            var responseMessage = await base.SendAsync(request, cancellationToken);

            var statusCode = responseMessage.StatusCode;

            span.Tags.HttpStatusCode((int)statusCode);
            if (statusCode == System.Net.HttpStatusCode.OK)
            {
                span.Tags
                .ResponseBody(responseMessage.Content.ReadAsStringAsync().Result)
                .ContentType(responseMessage.Content.Headers.ContentType.MediaType);
            }
            span.Finish();

            await _tracer.TraceSender.SendAsync(span);

            return(responseMessage);
        }
        public static ISpan Start(this IServiceTracer tracer, string operationName, DateTimeOffset?startTimestamp = null)
        {
            var spanBuilder = new SpanBuilder(operationName, startTimestamp);

            return(tracer.Start(spanBuilder));
        }
 public static ISpan StartChild(this IServiceTracer tracer, string operationName, DateTimeOffset?startTimestamp = null)
 {
     return(tracer.Start(CreateChildSpanBuilder(tracer, operationName, startTimestamp)));
 }