From f6164edac18dbde88bbdd95834574727bcd60e39 Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Thu, 7 Sep 2023 12:46:33 -1000 Subject: [PATCH] enrich metrics snippet to project file --- aspnetcore/log-mon/metrics/metrics.md | 30 +------------------ .../EnrichMetrics/EnrichMetrics.csproj | 9 ++++++ .../metrics/samples/EnrichMetrics/Program.cs | 27 +++++++++++++++++ .../samples/EnrichMetrics/appsettings.json | 9 ++++++ 4 files changed, 46 insertions(+), 29 deletions(-) create mode 100644 aspnetcore/log-mon/metrics/metrics/samples/EnrichMetrics/EnrichMetrics.csproj create mode 100644 aspnetcore/log-mon/metrics/metrics/samples/EnrichMetrics/Program.cs create mode 100644 aspnetcore/log-mon/metrics/metrics/samples/EnrichMetrics/appsettings.json diff --git a/aspnetcore/log-mon/metrics/metrics.md b/aspnetcore/log-mon/metrics/metrics.md index 16b9b36764..8e2e7e8b99 100644 --- a/aspnetcore/log-mon/metrics/metrics.md +++ b/aspnetcore/log-mon/metrics/metrics.md @@ -97,35 +97,7 @@ ASP.NET Core has many built-in metrics. The `http.server.request.duration` metri The `http.server.request.duration` metric supports tag enrichment using . Enrichment is when a library or app adds its own tags to a metric. This is useful if an app wants to add a custom categorization to dashboards or alerts built with metrics. -```cs -using Microsoft.AspNetCore.Http.Features; - -var builder = WebApplication.CreateBuilder(); -var app = builder.Build(); - -app.Use(async (context, next) => -{ - var tagsFeature = context.Features.Get(); - if (tagsFeature != null) - { - var source = context.Request.Query["utm_medium"].ToString() switch - { - "" => "none", - "social" => "social", - "email" => "email", - "organic" => "organic", - _ => "other" - }; - tagsFeature.Tags.Add(new KeyValuePair("mkt_medium", source)); - } - - await next.Invoke(); -}); - -app.MapGet("/", () => "Hello World!"); - -app.Run(); -``` +:::code language="csharp" source="~/log-mon/metrics/metrics/samples/EnrichMetrics/Program.cs"::: The proceeding example: diff --git a/aspnetcore/log-mon/metrics/metrics/samples/EnrichMetrics/EnrichMetrics.csproj b/aspnetcore/log-mon/metrics/metrics/samples/EnrichMetrics/EnrichMetrics.csproj new file mode 100644 index 0000000000..1b28a01c81 --- /dev/null +++ b/aspnetcore/log-mon/metrics/metrics/samples/EnrichMetrics/EnrichMetrics.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + + diff --git a/aspnetcore/log-mon/metrics/metrics/samples/EnrichMetrics/Program.cs b/aspnetcore/log-mon/metrics/metrics/samples/EnrichMetrics/Program.cs new file mode 100644 index 0000000000..2cc80bd472 --- /dev/null +++ b/aspnetcore/log-mon/metrics/metrics/samples/EnrichMetrics/Program.cs @@ -0,0 +1,27 @@ +using Microsoft.AspNetCore.Http.Features; + +var builder = WebApplication.CreateBuilder(); +var app = builder.Build(); + +app.Use(async (context, next) => +{ + var tagsFeature = context.Features.Get(); + if (tagsFeature != null) + { + var source = context.Request.Query["utm_medium"].ToString() switch + { + "" => "none", + "social" => "social", + "email" => "email", + "organic" => "organic", + _ => "other" + }; + tagsFeature.Tags.Add(new KeyValuePair("mkt_medium", source)); + } + + await next.Invoke(); +}); + +app.MapGet("/", () => "Hello World!"); + +app.Run(); diff --git a/aspnetcore/log-mon/metrics/metrics/samples/EnrichMetrics/appsettings.json b/aspnetcore/log-mon/metrics/metrics/samples/EnrichMetrics/appsettings.json new file mode 100644 index 0000000000..10f68b8c8b --- /dev/null +++ b/aspnetcore/log-mon/metrics/metrics/samples/EnrichMetrics/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}