Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/ebrvc/DevOps-GroupC into…
Browse files Browse the repository at this point in the history
… feature/software-quality-tools
  • Loading branch information
ebrvc committed Mar 24, 2024
2 parents acd9b68 + 7ec7ad0 commit 96b9a6e
Show file tree
Hide file tree
Showing 9 changed files with 590 additions and 591 deletions.
2 changes: 2 additions & 0 deletions csharp-minitwit/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using csharp_minitwit.Models.ViewModels;
using csharp_minitwit.Services.Interfaces;
using csharp_minitwit.Services.Repositories;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.EntityFrameworkCore.Query;

using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
Expand Down
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: 38 additions & 40 deletions csharp-minitwit/Middlewares/CatchAllMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
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();
}
}
}
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();
}
}
}
Loading

0 comments on commit 96b9a6e

Please sign in to comment.