/*
* Copyright 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 Amazon.Util;
using Amazon.Runtime.Internal.Util;
using System.Globalization;
using System;
namespace Amazon.Runtime.Internal
{
///
/// This handler manages the metrics used to time the complete call and
/// logs the final metrics.
///
public class MetricsHandler : PipelineHandler
{
///
/// Captures the overall execution time and logs final metrics.
///
/// The execution context which contains both the
/// requests and response context.
public override void InvokeSync(IExecutionContext executionContext)
{
executionContext.RequestContext.Metrics.AddProperty(Metric.AsyncCall, false);
try
{
executionContext.RequestContext.Metrics.StartEvent(Metric.ClientExecuteTime);
base.InvokeSync(executionContext);
}
finally
{
executionContext.RequestContext.Metrics.StopEvent(Metric.ClientExecuteTime);
this.LogMetrics(executionContext);
}
}
#if AWS_ASYNC_API
///
/// Captures the overall execution time and logs final metrics.
///
/// The response type for the current request.
///
/// The execution context, it contains the request and response context.
///
/// A task that represents the asynchronous operation.
public override async System.Threading.Tasks.Task InvokeAsync(IExecutionContext executionContext)
{
executionContext.RequestContext.Metrics.AddProperty(Metric.AsyncCall, true);
try
{
executionContext.RequestContext.Metrics.StartEvent(Metric.ClientExecuteTime);
var response = await base.InvokeAsync(executionContext).ConfigureAwait(false);
return response;
}
finally
{
executionContext.RequestContext.Metrics.StopEvent(Metric.ClientExecuteTime);
this.LogMetrics(executionContext);
}
}
#elif AWS_APM_API
///
/// Captures the overall execution time.
///
/// The execution context which contains both the
/// requests and response context.
/// IAsyncResult which represent an async operation.
public override IAsyncResult InvokeAsync(IAsyncExecutionContext executionContext)
{
executionContext.RequestContext.Metrics.AddProperty(Metric.AsyncCall, true);
executionContext.RequestContext.Metrics.StartEvent(Metric.ClientExecuteTime);
return base.InvokeAsync(executionContext);
}
///
/// Captures the overall execution time and logs final metrics.
///
/// The execution context, it contains the
/// request and response context.
protected override void InvokeAsyncCallback(IAsyncExecutionContext executionContext)
{
executionContext.RequestContext.Metrics.StopEvent(Metric.ClientExecuteTime);
this.LogMetrics(ExecutionContext.CreateFromAsyncContext(executionContext));
base.InvokeAsyncCallback(executionContext);
}
#endif
}
}