/// <summary> /// Gets default instance of <see cref="ITraceContext"/>. /// </summary> /// <returns>default instance of <see cref="ITraceContext"/></returns> public static ITraceContext GetTraceContext() { #if NET45 return(new CallContextContainer()); #else if (AWSXRayRecorder.IsLambda()) { return(new LambdaContextContainer()); } else { return(new AsyncLocalContextContainer()); } #endif }
/// <summary> /// Process http response. /// </summary> internal static void ProcessResponse(HttpContext httpContext) { HttpResponse response = httpContext.Response; if (!AWSXRayRecorder.Instance.IsTracingDisabled()) { var responseAttributes = PopulateResponseAttributes(response); _recorder.AddHttpInformation("response", responseAttributes); } if (AWSXRayRecorder.IsLambda()) { _recorder.EndSubsegment(); } else { _recorder.EndSegment(); } }
/// <summary> /// Processes HTTP response /// </summary> private void ProcessHTTPResponse(HttpContext context) { HttpResponse response = context.Response; if (!AWSXRayRecorder.Instance.IsTracingDisabled()) { var responseAttributes = new Dictionary <string, object>(); PopulateResponseAttributes(response, responseAttributes); _recorder.AddHttpInformation("response", responseAttributes); } if (AWSXRayRecorder.IsLambda()) { _recorder.EndSubsegment(); } else { _recorder.EndSegment(); } }
/// <summary> /// Processes HTTP request. /// </summary> private void ProcessHTTPRequest(HttpContext context) { HttpRequest request = context.Request; string headerString = null; if (request.Headers.TryGetValue(TraceHeader.HeaderKey, out StringValues headerValue)) { if (headerValue.ToArray().Length >= 1) { headerString = headerValue.ToArray()[0]; } } // Trace header doesn't exist, which means this is the root node. Create a new traceId and inject the trace header. if (!TraceHeader.TryParse(headerString, out TraceHeader traceHeader)) { _logger.DebugFormat("Trace header doesn't exist or not valid : ({0}). Injecting a new one.", headerString); traceHeader = new TraceHeader { RootTraceId = TraceId.NewId(), ParentId = null, Sampled = SampleDecision.Unknown }; } var segmentName = SegmentNamingStrategy.GetSegmentName(request); bool isSampleDecisionRequested = traceHeader.Sampled == SampleDecision.Requested; string ruleName = null; // Make sample decision if (traceHeader.Sampled == SampleDecision.Unknown || traceHeader.Sampled == SampleDecision.Requested) { string host = request.Host.Host; string url = request.Path; string method = request.Method; SamplingInput samplingInput = new SamplingInput(host, url, method, segmentName, _recorder.Origin); SamplingResponse sampleResponse = _recorder.SamplingStrategy.ShouldTrace(samplingInput); traceHeader.Sampled = sampleResponse.SampleDecision; ruleName = sampleResponse.RuleName; } if (AWSXRayRecorder.IsLambda()) { _recorder.BeginSubsegment(segmentName); } else { SamplingResponse samplingResponse = new SamplingResponse(ruleName, traceHeader.Sampled); // get final ruleName and SampleDecision _recorder.BeginSegment(SegmentNamingStrategy.GetSegmentName(request), traceHeader.RootTraceId, traceHeader.ParentId, samplingResponse); } if (!AWSXRayRecorder.Instance.IsTracingDisabled()) { var requestAttributes = new Dictionary <string, object>(); PopulateRequestAttributes(request, requestAttributes); _recorder.AddHttpInformation("request", requestAttributes); } if (isSampleDecisionRequested) { context.Response.Headers.Add(TraceHeader.HeaderKey, traceHeader.ToString()); // Its recommended not to modify response header after _next.Invoke() call } }