Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent add_message metric duplicate #124

Merged
merged 13 commits into from
Mar 24, 2024
23 changes: 19 additions & 4 deletions .github/workflows/quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ name: Format check on push
on:
pull_request:
types: [opened, reopened, edited, synchronize, ready_for_review]
# branches:
# - feature/software-quality-tools

jobs:
dotnet-format:
runs-on: ubuntu-latest
Expand All @@ -25,10 +24,26 @@ jobs:
- name: Format
run: dotnet format ./csharp-minitwit/csharp-minitwit.sln --verbosity diagnostic

- name: Lint Dockerfile
uses: hadolint/hadolint-action@master
with:
dockerfile: ./csharp-minitwit/Dockerfile

# - name: Lint C# code
# run: dotnet build

- name: Push changes
run: |
git config user.name 'github-actions[bot]'
git config user.email 'github-actions[bot]@users.noreply.github.com'
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY
git commit -am "Automated formatting"
git push
git fetch --all
git checkout 'origin/${{ github.head_ref }}'
git add .
# Check if there are any changes to commit
if git diff --staged --quiet; then
echo "No changes to commit."
else
git commit -am "Automated formatting"
git push
fi
4 changes: 2 additions & 2 deletions csharp-minitwit/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
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;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query;

namespace csharp_minitwit.Controllers;
Expand Down
7 changes: 3 additions & 4 deletions csharp-minitwit/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ WORKDIR /app

COPY . .

RUN dotnet restore

RUN dotnet publish -c Release -o out
RUN dotnet restore && \
dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/aspnet:8.0
WORKDIR /app
Expand All @@ -17,7 +16,7 @@ WORKDIR /app
ARG ENVIRONMENT
ENV ASPNETCORE_ENVIRONMENT=${ENVIRONMENT}

COPY --from=build /app/out .
COPY --from=build /app/out .
COPY ./Databases/schema.sql ./schema.sql
COPY ./Databases/volume/minitwit.db ./Databases/volume/minitwit.db
COPY ./Services/latest_processed_sim_action_id.txt ./Services/latest_processed_sim_action_id.txt
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" }
});
}
77 changes: 39 additions & 38 deletions csharp-minitwit/Middlewares/CatchAllMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
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