Skip to content

Commit

Permalink
Merge branch 'feature/software-quality-tools' of https://github.com/e…
Browse files Browse the repository at this point in the history
…brvc/DevOps-GroupC into feature/software-quality-tools
  • Loading branch information
ebrvc committed Mar 24, 2024
2 parents 2a79f36 + b86ee0e commit acd9b68
Show file tree
Hide file tree
Showing 8 changed files with 594 additions and 588 deletions.
60 changes: 30 additions & 30 deletions csharp-minitwit/Metrics/ApplicationMetrics.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
using Prometheus;

public static class ApplicationMetrics
{
public static readonly Histogram HttpRequestDuration = Metrics
.CreateHistogram("minitwit_http_request_duration_seconds", "Histogram of HTTP request duration.",
new HistogramConfiguration
{
// Define buckets with appropriate ranges for your application
Buckets = Histogram.LinearBuckets(start: 0.1, width: 0.1, count: 10),
LabelNames = new[] { "endpoint" }
});

public static readonly Counter HttpResponseStatusCodeTotal = Metrics
.CreateCounter("minitwit_http_response_status_code_total", "Total number of HTTP responses sent by the application by status code.",
new CounterConfiguration
{
// Add a label for the HTTP status code
LabelNames = new[] { "status_code" }
});

public static readonly Counter HttpRequestTotal = Metrics
.CreateCounter("minitwit_http_requests_total", "Total number of HTTP requests made to the application.",
new CounterConfiguration
{
LabelNames = new[] { "endpoint" }
});


}
using Prometheus;

public static class ApplicationMetrics
{
public static readonly Histogram HttpRequestDuration = Metrics
.CreateHistogram("minitwit_http_request_duration_seconds", "Histogram of HTTP request duration.",
new HistogramConfiguration
{
// Define buckets with appropriate ranges for your application
Buckets = Histogram.LinearBuckets(start: 0.1, width: 0.1, count: 10),
LabelNames = new[] { "endpoint" }
});

public static readonly Counter HttpResponseStatusCodeTotal = Metrics
.CreateCounter("minitwit_http_response_status_code_total", "Total number of HTTP responses sent by the application by status code.",
new CounterConfiguration
{
// Add a label for the HTTP status code
LabelNames = new[] { "status_code" }
});

public static readonly Counter HttpRequestTotal = Metrics
.CreateCounter("minitwit_http_requests_total", "Total number of HTTP requests made to the application.",
new CounterConfiguration
{
LabelNames = new[] { "endpoint" }
});


}
78 changes: 40 additions & 38 deletions csharp-minitwit/Middlewares/CatchAllMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;
using csharp_minitwit.Utils;
// Assuming ApplicationMetrics is in the same namespace, or add the appropriate using statement

namespace csharp_minitwit.Middlewares
{
public class CatchAllMiddleware
{
private readonly RequestDelegate _next;

public CatchAllMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task InvokeAsync(HttpContext context)
{
var watch = Stopwatch.StartNew();

// Used to monitor total requests received grouped by endpoint
ApplicationMetrics.HttpRequestTotal.WithLabels(MetricsHelpers.SanitizePath(context.Request.Path)).Inc();

await _next(context);

watch.Stop();

// Used to monitor response delay grouped by endpoint
ApplicationMetrics.HttpRequestDuration
.WithLabels(MetricsHelpers.SanitizePath(context.Request.Path))
.Observe(watch.Elapsed.TotalSeconds);

// Used to monitor response status codes grouped by endpoint
ApplicationMetrics.HttpResponseStatusCodeTotal.WithLabels(context.Response.StatusCode.ToString()).Inc();
}
}
}
using System.Diagnostics;
using System.Threading.Tasks;

using csharp_minitwit.Utils;

using Microsoft.AspNetCore.Http;
// Assuming ApplicationMetrics is in the same namespace, or add the appropriate using statement

namespace csharp_minitwit.Middlewares
{
public class CatchAllMiddleware
{
private readonly RequestDelegate _next;

public CatchAllMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task InvokeAsync(HttpContext context)
{
var watch = Stopwatch.StartNew();

// Used to monitor total requests received grouped by endpoint
ApplicationMetrics.HttpRequestTotal.WithLabels(MetricsHelpers.SanitizePath(context.Request.Path)).Inc();

await _next(context);

watch.Stop();

// Used to monitor response delay grouped by endpoint
ApplicationMetrics.HttpRequestDuration
.WithLabels(MetricsHelpers.SanitizePath(context.Request.Path))
.Observe(watch.Elapsed.TotalSeconds);

// Used to monitor response status codes grouped by endpoint
ApplicationMetrics.HttpResponseStatusCodeTotal.WithLabels(context.Response.StatusCode.ToString()).Inc();
}
}
}
Loading

0 comments on commit acd9b68

Please sign in to comment.