diff --git a/MetricFlow.sln b/MetricFlow.sln index c940a58..8780ec0 100644 --- a/MetricFlow.sln +++ b/MetricFlow.sln @@ -1,4 +1,4 @@ -Microsoft Visual Studio Solution File, Format Version 12.00 +Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.5.2.0 MinimumVisualStudioVersion = 10.0.40219.1 @@ -14,6 +14,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MetricFlow.Tests", "tests\M EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotnetKit.MetricFlow.Tracker", "src\DotnetKit.MetricFlow.Tracker\DotnetKit.MetricFlow.Tracker.csproj", "{60112D90-095C-44EA-90E9-846B4EDBF758}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApiExample", "examples\WebApiExample\WebApiExample.csproj", "{62617707-AE70-492E-B36D-63D0108EC269}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -32,6 +34,10 @@ Global {60112D90-095C-44EA-90E9-846B4EDBF758}.Debug|Any CPU.Build.0 = Debug|Any CPU {60112D90-095C-44EA-90E9-846B4EDBF758}.Release|Any CPU.ActiveCfg = Release|Any CPU {60112D90-095C-44EA-90E9-846B4EDBF758}.Release|Any CPU.Build.0 = Release|Any CPU + {62617707-AE70-492E-B36D-63D0108EC269}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {62617707-AE70-492E-B36D-63D0108EC269}.Debug|Any CPU.Build.0 = Debug|Any CPU + {62617707-AE70-492E-B36D-63D0108EC269}.Release|Any CPU.ActiveCfg = Release|Any CPU + {62617707-AE70-492E-B36D-63D0108EC269}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -40,6 +46,7 @@ Global {78058871-2DC0-6810-ED9E-588E6E3A1265} = {B36A84DF-456D-A817-6EDD-3EC3E7F6E11F} {3897CD1F-6CA5-41EC-9845-1979426CD7B8} = {01644C81-1609-407B-A964-A15BFA59E822} {60112D90-095C-44EA-90E9-846B4EDBF758} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B} + {62617707-AE70-492E-B36D-63D0108EC269} = {B36A84DF-456D-A817-6EDD-3EC3E7F6E11F} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {7CE4E738-181F-42CA-B63C-365DBD1BC7B9} diff --git a/examples/WebApiExample/Program.cs b/examples/WebApiExample/Program.cs new file mode 100644 index 0000000..703956f --- /dev/null +++ b/examples/WebApiExample/Program.cs @@ -0,0 +1,86 @@ +using DotnetKit.MetricFlow.Tracker; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); +builder.Services.AddSingleton(sp => new MetricTracker("WebApiExample")); +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); + + //add a middleware to track API calls + app.Use(async (context, next) => + { + //skip tracking metrics for the metrics endpoint + if (context.Request.Path == "/metrics") + { + await next(); + return; + } + var tracker = context.RequestServices.GetRequiredService(); + tracker.In(context.Request.Path); + //tracking requests with errors + try + { + await next(); + } + catch (Exception e) + { + tracker.Out(context.Request.Path, new Dictionary() { ["error_message"] = e.Message }, failed: true); + throw; + } + tracker.Out(context.Request.Path); + }); +} + +app.UseHttpsRedirection(); + +var summaries = new[] +{ + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" +}; + +app.MapGet("/weatherforecast", () => +{ + var forecast = Enumerable.Range(1, 5).Select(index => + new WeatherForecast + ( + DateOnly.FromDateTime(DateTime.Now.AddDays(index)), + Random.Shared.Next(-20, 55), + summaries[Random.Shared.Next(summaries.Length)] + )) + .ToArray(); + return forecast; +}) +.WithName("GetWeatherForecast") +.WithOpenApi(); + +app.MapGet("/throw_exception", () => +{ + throw new Exception("This is an exception"); +}) +.WithName("Exception") +.WithOpenApi(); + +app.MapGet("/metrics", () => +{ + var tracker = app.Services.GetRequiredService(); + return tracker.ToString(); + +}) +.WithName("Metrics") +.WithOpenApi(); + +app.Run(); + +record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) +{ + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); +} diff --git a/examples/WebApiExample/Properties/launchSettings.json b/examples/WebApiExample/Properties/launchSettings.json new file mode 100644 index 0000000..9ec0e90 --- /dev/null +++ b/examples/WebApiExample/Properties/launchSettings.json @@ -0,0 +1,41 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:64475", + "sslPort": 44327 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5045", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7286;http://localhost:5045", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/examples/WebApiExample/README.md b/examples/WebApiExample/README.md new file mode 100644 index 0000000..c9cc2e1 --- /dev/null +++ b/examples/WebApiExample/README.md @@ -0,0 +1,156 @@ +# Web API Example with MetricFlow + +This example demonstrates how to use the `MetricFlow` library to track and measure metrics in a .NET Web API application. The example includes a `Program` class that sets up the web application, configures middleware to track API calls, and exposes metrics via an endpoint. + +## Goal + +The goal of this example is to showcase how to integrate `MetricFlow` into a .NET Web API application to track various metrics such as request counts and durations for different API endpoints and indicate failed request count. + +This helps in monitoring the performance and usage of the API. + +## Overview of Implementation + +### Key Components + +- **MetricTracker:** Tracks metrics for different API endpoints. +- **Middleware:** Automatically tracks metrics for incoming requests. +- **Endpoints:** Demonstrates how to track metrics for specific API endpoints and expose metrics. + +### Implementation Details + +1. **Service Registration:** + - The `MetricTracker` service is registered as a global service in the dependency injection container. + + ```csharp + builder.Services.AddSingleton(sp => new MetricTracker("WebApiExample")); + ``` + +2. **Middleware Configuration:** + - Middleware is added to track metrics for incoming requests, excluding the `/metrics` endpoint. + - This oveview doesn't track failed requests, please look source code of this example for complete implementation. + + ```csharp + app.Use(async (context, next) => + { + if (context.Request.Path != "/metrics") + { + var tracker = context.RequestServices.GetRequiredService(); + tracker.In(context.Request.Path); + await next(); + tracker.Out(context.Request.Path); + } + else + { + await next(); + } + }); + ``` + +3. **Endpoints:** + - The `/weatherforecast` endpoint simulates a weather forecast API and tracks metrics for the endpoint. + - The `/metrics` endpoint exposes the tracked metrics. + + ```csharp + app.MapGet("/weatherforecast", (MetricTracker tracker) => + { + tracker.In("GetWeatherForecast"); + var forecast = Enumerable.Range(1, 5).Select(index => + new WeatherForecast + ( + DateOnly.FromDateTime(DateTime.Now.AddDays(index)), + Random.Shared.Next(-20, 55), + summaries[Random.Shared.Next(summaries.Length)] + )) + .ToArray(); + tracker.Out("GetWeatherForecast"); + return forecast; + }) + .WithName("GetWeatherForecast") + .WithOpenApi(); + + app.MapGet("/metrics", (MetricTracker tracker) => + { + return tracker.ToString(); + }) + .WithName("Metrics") + .WithOpenApi(); + ``` + +### Code Example + +```csharp +using System.Diagnostics; +using DotnetKit.MetricFlow.Tracker; +using DotnetKit.MetricFlow.Tracker.Abstractions; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); +builder.Services.AddSinglton(sp => new MetricTracker("WebApiExample")); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseHttpsRedirection(); + +app.Use(async (context, next) => +{ + if (context.Request.Path != "/metrics") + { + var tracker = context.RequestServices.GetRequiredService(); + tracker.In(context.Request.Path); + await next(); + tracker.Out(context.Request.Path); + } + else + { + await next(); + } +}); + +var summaries = new[] +{ + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" +}; + +app.MapGet("/weatherforecast", (MetricTracker tracker) => +{ + tracker.In("GetWeatherForecast"); + var forecast = Enumerable.Range(1, 5).Select(index => + new WeatherForecast + ( + DateOnly.FromDateTime(DateTime.Now.AddDays(index)), + Random.Shared.Next(-20, 55), + summaries[Random.Shared.Next(summaries.Length)] + )) + .ToArray(); + tracker.Out("GetWeatherForecast"); + return forecast; +}) +.WithName("GetWeatherForecast") +.WithOpenApi(); + +app.MapGet("/metrics", (MetricTracker tracker) => +{ + return tracker.ToString(); +}) +.WithName("Metrics") +.WithOpenApi(); + +app.Run(); + +record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) +{ + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); +} +``` + +![alt text](image.png) diff --git a/examples/WebApiExample/WebApiExample.csproj b/examples/WebApiExample/WebApiExample.csproj new file mode 100644 index 0000000..ecb258c --- /dev/null +++ b/examples/WebApiExample/WebApiExample.csproj @@ -0,0 +1,18 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + diff --git a/examples/WebApiExample/WebApiExample.http b/examples/WebApiExample/WebApiExample.http new file mode 100644 index 0000000..47fa571 --- /dev/null +++ b/examples/WebApiExample/WebApiExample.http @@ -0,0 +1,6 @@ +@WebApiExample_HostAddress = http://localhost:5045 + +GET {{WebApiExample_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/examples/WebApiExample/appsettings.Development.json b/examples/WebApiExample/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/examples/WebApiExample/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/examples/WebApiExample/appsettings.json b/examples/WebApiExample/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/examples/WebApiExample/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/examples/WebApiExample/image.png b/examples/WebApiExample/image.png new file mode 100644 index 0000000..b6c6598 Binary files /dev/null and b/examples/WebApiExample/image.png differ diff --git a/src/DotnetKit.MetricFlow.Tracker/Abstractions/CounterBase.cs b/src/DotnetKit.MetricFlow.Tracker/Abstractions/CounterBase.cs index 128d887..a66c421 100644 --- a/src/DotnetKit.MetricFlow.Tracker/Abstractions/CounterBase.cs +++ b/src/DotnetKit.MetricFlow.Tracker/Abstractions/CounterBase.cs @@ -8,6 +8,7 @@ public abstract class CounterBase(string name, Dictionary? metri private DateTime? _startedAt = null; private DateTime? _endedAt = null; private long _inCount = 0; + private long _failedCount = 0; private long _outCount = 0; private long _totalDuration = 0; @@ -21,6 +22,7 @@ public abstract class CounterBase(string name, Dictionary? metri public CounterValues Values => new CounterValues( Interlocked.Read(ref _inCount), Interlocked.Read(ref _outCount), + Interlocked.Read(ref _failedCount), TimeSpan.FromMilliseconds(Interlocked.Read(ref _totalDuration)), TimeSpan.FromMilliseconds(Interlocked.Read(ref _averageDuration)), TimeSpan.FromMilliseconds(Interlocked.Read(ref _minDuration)), @@ -39,9 +41,14 @@ public long Inc() return Interlocked.Increment(ref _inCount); } - public long Dec() + public long Dec(bool? failed = false) { FinalizeState(Stop()); + + if (failed == true) + { + Interlocked.Increment(ref _failedCount); + } return Interlocked.Increment(ref _outCount); } diff --git a/src/DotnetKit.MetricFlow.Tracker/Abstractions/ICounter.cs b/src/DotnetKit.MetricFlow.Tracker/Abstractions/ICounter.cs index defad20..eb60bc9 100644 --- a/src/DotnetKit.MetricFlow.Tracker/Abstractions/ICounter.cs +++ b/src/DotnetKit.MetricFlow.Tracker/Abstractions/ICounter.cs @@ -6,6 +6,6 @@ public interface ICounter DateTime TimeStamp { get; } CounterValues Values { get; } long Inc(); - long Dec(); + long Dec(bool? failed = false); } } \ No newline at end of file diff --git a/src/DotnetKit.MetricFlow.Tracker/Abstractions/IMetricTracker.cs b/src/DotnetKit.MetricFlow.Tracker/Abstractions/IMetricTracker.cs index ffd9d87..5d62886 100644 --- a/src/DotnetKit.MetricFlow.Tracker/Abstractions/IMetricTracker.cs +++ b/src/DotnetKit.MetricFlow.Tracker/Abstractions/IMetricTracker.cs @@ -12,7 +12,7 @@ public interface IMetricTracker IDisposable Track(string counterName, Dictionary? topicTags = null); - long? Out(string counterName, Dictionary? topicTags = null); + long? Out(string counterName, Dictionary? topicTags = null, bool? failed = false); void Clear(); } diff --git a/src/DotnetKit.MetricFlow.Tracker/Abstractions/MetricTrackerBase.cs b/src/DotnetKit.MetricFlow.Tracker/Abstractions/MetricTrackerBase.cs index 5b37c8f..bfcdb96 100644 --- a/src/DotnetKit.MetricFlow.Tracker/Abstractions/MetricTrackerBase.cs +++ b/src/DotnetKit.MetricFlow.Tracker/Abstractions/MetricTrackerBase.cs @@ -27,7 +27,7 @@ public abstract class MetricTrackerBase(string topic, return counter.Inc(); } - public long? Out(string metricName, Dictionary? metricMetadata = null) + public long? Out(string metricName, Dictionary? metricMetadata = null, bool? failed = false) { if (IsSampled(samplingRate, _randomizer)) { @@ -35,7 +35,7 @@ public abstract class MetricTrackerBase(string topic, } var counter = _blockCounters.GetOrAdd(metricName, counterFactory(metricName, metricMetadata)); // Handle metadata as needed - return counter.Dec(); + return counter.Dec(failed); } public IDisposable Track(string metricName, Dictionary? metricMetadata = null) diff --git a/src/DotnetKit.MetricFlow.Tracker/CounterValues.cs b/src/DotnetKit.MetricFlow.Tracker/CounterValues.cs index 9a952db..57e6d61 100644 --- a/src/DotnetKit.MetricFlow.Tracker/CounterValues.cs +++ b/src/DotnetKit.MetricFlow.Tracker/CounterValues.cs @@ -5,6 +5,7 @@ namespace DotnetKit.MetricFlow.Tracker.Abstractions public record CounterValues( long InCount, long OutCount, + long FailedCount, TimeSpan TotalDuration, TimeSpan AverageDuration, TimeSpan MinDuration, @@ -18,7 +19,7 @@ TimeSpan MaxDuration public override string ToString() { var sb = new StringBuilder(); - sb.AppendLine($"Count (in, out): {InCount} / {OutCount}"); + sb.AppendLine($"Count (in, out, failed): {InCount} / {OutCount} / {FailedCount}"); sb.AppendLine($"Avg duration: {AverageDuration.TotalMilliseconds} ms"); sb.AppendLine($"Duration (min, max) : {MinDuration.TotalMilliseconds} ms / {MaxDuration.TotalMilliseconds} ms"); sb.AppendLine($"Total duration: {TotalDuration.TotalMilliseconds} ms"); diff --git a/tests/MetricFlow.Tests/MetricTrackerTests.cs b/tests/MetricFlow.Tests/MetricTrackerTests.cs index 7c2393c..de69107 100644 --- a/tests/MetricFlow.Tests/MetricTrackerTests.cs +++ b/tests/MetricFlow.Tests/MetricTrackerTests.cs @@ -72,5 +72,25 @@ public async Task MetricTracker_ShouldTrackUsingDisposablePattern() values?.InCount.Should().Be(1); values?.OutCount.Should().Be(1); } + [Fact] + public void MetricTracker_ShouldTrackFailedMetrics() + { + // Arrange + var tracker = new MetricTracker("TestTopic"); + + // Act + tracker.In("TestMetric"); + tracker.Out("TestMetric"); + + tracker.In("TestMetric"); + tracker.Out("TestMetric",failed:true); + // Assert + var values = tracker.GetValues("TestMetric"); + values.Should().NotBeNull(); + values!.InCount.Should().Be(1); + values.OutCount.Should().Be(1); + values.FailedCount.Should().Be(1); + } + } } \ No newline at end of file