This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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