Exemplo n.º 1
0
        private void LogStopOfRequest(HttpActionExecutedContext actionExecutedContext, Action <IApmContext, ApmWebApiFinishInformation> finishAction)
        {
            var applicationName  = ApmHttpRequestMessageParser.GetApplicationName(actionExecutedContext.Request);
            var eventName        = ApmHttpRequestMessageParser.GetEventName(actionExecutedContext.Request);
            var methodIdentifier = ApmHttpRequestMessageParser.GetMethodIdentifier(actionExecutedContext.Request);
            var traceId          = ApmHttpRequestMessageParser.GetTraceId(actionExecutedContext.Request);
            var spanId           = ApmHttpRequestMessageParser.GetSpanId(actionExecutedContext.Request);
            var parentSpanId     = ApmHttpRequestMessageParser.GetParentSpanId(actionExecutedContext.Request);
            var sampled          = ApmHttpRequestMessageParser.GetSampled(actionExecutedContext.Request);
            var flags            = ApmHttpRequestMessageParser.GetFlags(actionExecutedContext.Request);
            var responseTime     = ApmHttpRequestMessageParser.GetResponseTime(actionExecutedContext.Request);

            var apmWebApiFinishInformation = new ApmWebApiFinishInformation
            {
                ApplicationName  = applicationName,
                EventName        = eventName,
                MethodIdentifier = methodIdentifier,
                Flags            = flags,
                ParentSpanId     = parentSpanId,
                Sampled          = sampled,
                SpanId           = spanId,
                TraceId          = traceId,
                Request          = actionExecutedContext.Request,
                Response         = actionExecutedContext.Response,
                ResponseTime     = responseTime,
                Exception        = actionExecutedContext.Exception
            };

            object apmContextObject;

            if (!apmWebApiFinishInformation.Request.Properties.TryGetValue(Constants.ApmContextPropertyKey, out apmContextObject))
            {
                throw new Exception("Add global filter for ApmWebApiFilterAttributeBase");
            }

            var apmContext = (IApmContext)apmContextObject;

            if (!apmContext.ContainsKey(Constants.TimeTakeMsPropertyKey))
            {
                apmContext[Constants.TimeTakeMsPropertyKey] = apmWebApiFinishInformation.ResponseTime.ToString();
            }

            finishAction(apmContext, apmWebApiFinishInformation);
        }
        private void LogStopOfRequest(HttpRequestMessage request, HttpResponseMessage response, Action <IApmContext, ApmHttpClientFinishInformation> finishAction)
        {
            var applicationName  = _apmHttpRequestMessageParser.GetApplicationName(request);
            var eventName        = _apmHttpRequestMessageParser.GetEventName(request);
            var methodIdentifier = _apmHttpRequestMessageParser.GetMethodIdentifier(request);
            var responseTime     = _apmHttpRequestMessageParser.GetResponseTime(request);
            var clientName       = _apmHttpRequestMessageParser.GetClientName(request);

            var incomingTraceId      = _apmHttpRequestMessageParser.GetIncomingTraceId(request);
            var incomingSpanId       = _apmHttpRequestMessageParser.GetIncomingSpanId(request);
            var incomingParentSpanId = _apmHttpRequestMessageParser.GetIncomingParentSpanId(request);
            var incomingFlags        = _apmHttpRequestMessageParser.GetIncomingFlags(request);
            var incomingSampled      = _apmHttpRequestMessageParser.GetIncomingSampled(request);

            var traceId      = _apmHttpRequestMessageParser.GetTraceId(request);
            var spanId       = _apmHttpRequestMessageParser.GetSpanId(request);
            var parentSpanId = _apmHttpRequestMessageParser.GetParentSpanId(request);
            var flags        = _apmHttpRequestMessageParser.GetFlags(request);
            var sampled      = _apmHttpRequestMessageParser.GetSampled(request);

            var apmHttpClientFinishInformation = new ApmHttpClientFinishInformation
            {
                ApplicationName      = applicationName,
                EventName            = eventName,
                MethodIdentifier     = methodIdentifier,
                Request              = request,
                Response             = response,
                ResponseTime         = responseTime,
                ClientName           = clientName,
                IncomingTraceId      = incomingTraceId,
                IncomingSpanId       = incomingSpanId,
                IncomingParentSpanId = incomingParentSpanId,
                IncomingSampled      = incomingSampled,
                IncomingFlags        = incomingFlags,
                TraceId              = traceId,
                SpanId       = spanId,
                ParentSpanId = parentSpanId,
                Sampled      = sampled,
                Flags        = flags
            };

            object apmContextObject;

            if (!request.Properties.TryGetValue(Constants.ApmContextPropertyKey, out apmContextObject))
            {
                throw new Exception("Add delegating handler filter");
            }

            var apmContext = (IApmContext)apmContextObject;

            if (!apmContext.ContainsKey(Constants.TimeTakeMsPropertyKey))
            {
                apmContext[Constants.TimeTakeMsPropertyKey] = responseTime.ToString();
            }

            if (!apmContext.ContainsKey(Constants.ResponseStatusCodePropertyKey))
            {
                apmContext[Constants.ResponseStatusCodePropertyKey] = response.StatusCode.ToString();
            }

            finishAction(apmContext, apmHttpClientFinishInformation);
        }