Skip to content

Instantly share code, notes, and snippets.

@dcomartin dcomartin/CloudWatch.cs
Last active Aug 9, 2019

Embed
What would you like to do?
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using Amazon.CloudWatch;
using Amazon.CloudWatch.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
namespace AspNetCore.Aws.Demo
{
public class CloudWatchExecutionTimeMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger _logger;
private readonly IAmazonCloudWatch _amazonCloudWatch;
public CloudWatchExecutionTimeMiddleware(RequestDelegate next, ILogger<CloudWatchExecutionTimeMiddleware> logger, IAmazonCloudWatch amazonCloudWatch)
{
_next = next;
_logger = logger;
_amazonCloudWatch = amazonCloudWatch;
}
public async Task InvokeAsync(HttpContext context)
{
var stopWatch = new Stopwatch();
stopWatch.Start();
await _next(context);
stopWatch.Stop();
try
{
await _amazonCloudWatch.PutMetricDataAsync(new PutMetricDataRequest
{
Namespace = "Demo",
MetricData = new List<MetricDatum>
{
new MetricDatum
{
MetricName = "AspNetExecutionTime",
Value = stopWatch.ElapsedMilliseconds,
Unit = StandardUnit.Milliseconds,
TimestampUtc = DateTime.UtcNow,
Dimensions = new List<Dimension>
{
new Dimension
{
Name = "Method",
Value = context.Request.Method
},
new Dimension
{
Name = "Path",
Value = context.Request.Path
}
}
}
}
});
}
catch (Exception ex)
{
_logger.LogCritical(ex, "Failed to send CloudWatch Metric");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.