//----------------------------------------------------------------------------- // // Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"). // You may not use this file except in compliance with the License. // A copy of the License is located at // // http://aws.amazon.com/apache2.0 // // or in the "license" file accompanying this file. This file is distributed // on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either // express or implied. See the License for the specific language governing // permissions and limitations under the License. // //----------------------------------------------------------------------------- using System; using System.Net; using System.Threading.Tasks; using Amazon.XRay.Recorder.Core; using Amazon.XRay.Recorder.Handlers.System.Net.Utils; namespace Amazon.XRay.Recorder.Handlers.System.Net { /// /// Tracing extension methods of class. /// /// public static class HttpWebRequestTracingExtension { /// /// Wrapper of method. /// It collects information from request and response. Also, a trace header will be injected /// into the HttpWebRequest to propagate the tracing to downstream web service. This method is /// used for synchronous requests. /// Request query string can be omitted from the http tracing depending on sanitizeHttpRequestTracing flag. Defaults to false, thus tracing absolute Uri. /// /// An instance of which the method extended to /// value. /// A that contains the response from the Internet resource. public static WebResponse GetResponseTraced(this WebRequest request, bool sanitizeHttpRequestTracing = false) { RequestUtil.ProcessRequest(request, sanitizeHttpRequestTracing); try { var response = (HttpWebResponse)request.GetResponse(); RequestUtil.ProcessResponse(response); return response; } catch (Exception e) { AWSXRayRecorder.Instance.AddException(e); if (e is WebException webException) { var exceptionResponse = (HttpWebResponse)webException.Response; if (exceptionResponse != null) { RequestUtil.ProcessResponse(exceptionResponse); } } throw; } finally { AWSXRayRecorder.Instance.EndSubsegment(); } } /// /// Wrapper of method. /// It collects information from request and response. Also, a trace header will be injected /// into the HttpWebRequest to propagate the tracing to downstream web service. This method is /// used for asynchronous requests. /// Request query string can be omitted from the http tracing depending on sanitizeHttpRequestTracing flag. Defaults to false, thus tracing absolute Uri. /// /// An instance of which the method extended to /// value. /// A task of that contains the response from the Internet resource. public static async Task GetAsyncResponseTraced(this WebRequest request, bool sanitizeHttpRequestTracing = false) { RequestUtil.ProcessRequest(request, sanitizeHttpRequestTracing); try { var response = (HttpWebResponse)await request.GetResponseAsync(); RequestUtil.ProcessResponse(response); return response; } catch (Exception e) { AWSXRayRecorder.Instance.AddException(e); if (e is WebException webException) { var exceptionResponse = (HttpWebResponse)webException.Response; if (exceptionResponse != null) { RequestUtil.ProcessResponse(exceptionResponse); } } throw; } finally { AWSXRayRecorder.Instance.EndSubsegment(); } } } }