//-----------------------------------------------------------------------------
//
// 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.Http;
using System.Threading;
using System.Threading.Tasks;
using Amazon.XRay.Recorder.Core;
using Amazon.XRay.Recorder.Handlers.System.Net.Utils;
namespace Amazon.XRay.Recorder.Handlers.System.Net
{
///
/// Wrapper around for AWS X-Ray tracing of HTTP requests
///
public class HttpClientXRayTracingHandler : DelegatingHandler
{
public HttpClientXRayTracingHandler() : base()
{
}
public HttpClientXRayTracingHandler(HttpMessageHandler innerHandler) : base(innerHandler)
{
}
///
/// 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.
///
/// An instance of
/// An instance of
/// A Task of representing the asynchronous operation
protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
RequestUtil.ProcessRequest(request);
HttpResponseMessage response;
try
{
response = await base.SendAsync(request, cancellationToken);
RequestUtil.ProcessResponse(response);
}
catch (Exception ex)
{
AWSXRayRecorder.Instance.AddException(ex);
throw;
}
finally
{
AWSXRayRecorder.Instance.EndSubsegment();
}
return response;
}
}
}