diff --git a/docs/en/Community-Articles/2023-10-30-NET-8-ASP-NET-Core-Metrics/POST.md b/docs/en/Community-Articles/2023-10-30-NET-8-ASP-NET-Core-Metrics/POST.md index c536613429..07df71527d 100644 --- a/docs/en/Community-Articles/2023-10-30-NET-8-ASP-NET-Core-Metrics/POST.md +++ b/docs/en/Community-Articles/2023-10-30-NET-8-ASP-NET-Core-Metrics/POST.md @@ -20,7 +20,7 @@ ASP.NET Core has many built-in metrics. You can see the following figure for a l > See https://learn.microsoft.com/en-us/dotnet/core/diagnostics/built-in-metrics-aspnetcore and https://github.com/dotnet/aspnetcore/issues/47536 for all built-in metrics and their descriptions. -For example, we have the `kestrel-current-connections` metric that shows _number of connections that are currently active on the server_ and `http-server-request-duration` metric that shows _the duration of HTTP requests on the server_. +For example, we have the `kestrel-current-connections` metric that shows the _number of connections that are currently active on the server_ and the `http-server-request-duration` metric that shows _the duration of HTTP requests on the server_. All of these and other built-in metrics are produced by using the [**System.Diagnostics.Metrics**](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.metrics) API and with a small amount of code, we can use & view them. @@ -35,7 +35,7 @@ dotnet new web -o MetricsDemo cd MetricsDemo ``` -After that open the application in your favourite IDE and add the following service registration code to your application: +After that open the application in your favorite IDE and add the following service registration code to your application: ```csharp var builder = WebApplication.CreateBuilder(args); @@ -58,11 +58,11 @@ builder.Services.AddOpenTelemetry() Here, we have done the following things: -* `AddOpenTelemetry` method registers the required **OpenTelemetry** services into the DI container. +* The `AddOpenTelemetry` method registers the required **OpenTelemetry** services into the DI container. * `WithMetrics` method, we add pre-built metrics to the `OpenTelemetryBuilder` in the `AddMeter` method (_Microsoft.AspNetCore.Hosting_ and _Microsoft.AspNetCore.Server.Kestrel_ are pre-built metrics provided by ASP.NET Core). * Also, we have used the `AddView` method to customize the output of the metrics by the SDK. Also, it can be used to customize which [Instruments](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument) are to be processed or ignored. -After making the related configurations, let's add the middlewares below and view the metrics: +After making the related configurations, let's add the middleware below and view the metrics: ```csharp app.MapPrometheusScrapingEndpoint(); @@ -93,14 +93,14 @@ After the tool is installed, you can run the application and by running the `dot dotnet-counters monitor -n MetricsDemo --counters Microsoft.AspNetCore.Hosting ``` -When you run the command, it will print a message like in the below and will wait an initial request to your application: +When you run the command, it will print a message like in the one below and will wait for an initial request to your application: ```txt Press p to pause, r to resume, q to quit. Status: Waiting for initial payload... ``` -If you send a request to your application, then the related built-in metrics that you have added will be collected and will be show in the terminal instantly: +If you send a request to your application, then the related built-in metrics that you have added will be collected and will be shown in the terminal instantly: ![](built-in-metric-response.png) @@ -112,7 +112,7 @@ dotnet-counters monitor -n MetricsDemo --counters Microsoft.AspNetCore.Server.Ke ## Creating Custom Metrics in ASP.NET Core Applications -So far, we have seen what metrics are, which built-in metrics are provided by ASP.NET Core and use the built-in metrics and run the `dotnet-counters` global tool to view these metrics. At this point, let's see how we can create custom metrics and view them via `dotnet-counters` tool. +So far, we have seen what metrics are, which built-in metrics are provided by ASP.NET Core and use the built-in metrics and run the `dotnet-counters` global tool to view these metrics. At this point, let's see how we can create custom metrics and view them via the `dotnet-counters` tool. `IMeterFactory` is the recommended service to create *Meter* instances. ASP.NET Core registers `IMeterFactory` in dependency injection (DI) by default. The meter factory integrates metrics with DI, making isolating and collecting metrics easy. @@ -140,17 +140,17 @@ public class ProductMetrics } ``` -In this class, we are creating a counter and defining metrics that we want to collect and view. `MetricsDemo.ProductStore` is the **counter name** and `metricsdemo.productstore.sold_products_count` is the **metric**, in our example. We need to register this class with a `Singleton` lifetime, because we don't want to reset the counter and want to keep it during the application runtime. +In this class, we are creating a counter and defining metrics that we want to collect and view. `MetricsDemo.ProductStore` is the **counter name** and `metricsdemo.productstore.sold_products_count` is the **metric**, in our example. We need to register this class with a `Singleton` lifetime because we don't want to reset the counter and want to keep it during the application runtime. > The [System.Diagnostics.Metrics.Meter](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.metrics.meter) type is the entry point for a library to create a named group of instruments. Instruments record the numeric measurements that are needed to calculate metrics. Here we used [CreateCounter](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.metrics.meter.createcounter) to create a Counter instrument named _metricsdemo.productstore.sold_products_count_. -So, register the type with DI container in the `Program.cs` file (or you can implement the `ISingletonDependency` interface, if you are using an ABP based application): +So, register the type with the DI container in the `Program.cs` file (or you can implement the `ISingletonDependency` interface, if you are using an ABP based application): ```csharp builder.Services.AddSingleton(); ``` -Then, we can inject this class and increase sold product count whenever it's needed. Since, we have registered it in DI, we can use it in minimal APIs like in the following example: +Then, we can inject this class and increase sold product count whenever it's needed. Since we have registered it in DI, we can use it in minimal APIs like in the following example: ```csharp app.MapPost("/complete-sale", ([FromBody] ProductSoldModel model, ProductMetrics metrics) => @@ -161,13 +161,13 @@ app.MapPost("/complete-sale", ([FromBody] ProductSoldModel model, ProductMetrics }); ``` -Whenever a request made to this endpoint the metric that we defined (_metricsdemo.productstore.sold_products_count_) will be increased and we can see this metric via `dotnet-counters` global tool. You can run the following command to see the metrics: +Whenever a request is made to this endpoint the metric that we defined (_metricsdemo.productstore.sold_products_count_) will be increased and we can see this metric via the `dotnet-counters` global tool. You can run the following command to see the metrics: ```bash dotnet-counters monitor -n MetricsDemo --counters MetricsDemo.ProductStore ``` -Here is an example output that you would see if you have made a POST requests to the `/complete-sale` endpoint: +Here is an example output that you would see if you have made a POST request to the `/complete-sale` endpoint: ```txt Press p to pause, r to resume, q to quit. @@ -179,15 +179,15 @@ Press p to pause, r to resume, q to quit. ### Advanced: View Metrics in Prometheus & Grafana - Building Dashboards -It's a common requirement to build a dashboard and monitor the metrics and check the health of your applications. In that point, you can expose the metrics and scrape them using the [Prometheus](https://learn.microsoft.com/en-us/aspnet/core/log-mon/metrics/metrics?view=aspnetcore-8.0#set-up-and-configure-prometheus) and build dashboard by using the [Grafana](https://learn.microsoft.com/en-us/aspnet/core/log-mon/metrics/metrics?view=aspnetcore-8.0#show-metrics-on-a-grafana-dashboard). +It's a common requirement to build a dashboard and monitor the metrics and check the health of your applications. At that point, you can expose the metrics and scrape them using the [Prometheus](https://learn.microsoft.com/en-us/aspnet/core/log-mon/metrics/metrics?view=aspnetcore-8.0#set-up-and-configure-prometheus) and build a dashboard by using the [Grafana](https://learn.microsoft.com/en-us/aspnet/core/log-mon/metrics/metrics?view=aspnetcore-8.0#show-metrics-on-a-grafana-dashboard). -If you want to build a dashboard, you can see [this documentation of Microsoft](https://learn.microsoft.com/en-us/aspnet/core/log-mon/metrics/metrics?view=aspnetcore-8.0#view-metrics-in-grafana-with-opentelemetry-and-prometheus), which shows how to set up the **Prometheus server** and show metrics on a **Grafana** dashboard. +If you want to build a dashboard, you can see [this documentation from Microsoft](https://learn.microsoft.com/en-us/aspnet/core/log-mon/metrics/metrics?view=aspnetcore-8.0#view-metrics-in-grafana-with-opentelemetry-and-prometheus), which shows how to set up the **Prometheus server** and show metrics on a **Grafana** dashboard. Also, you can check [this video](https://www.youtube.com/watch?v=A2pKhNQoQUU) if you want to see it in action. ## Conclusion -In this article, I showed you the new built-in metrics of .NET8, describe what metrics are, which pre-built metrics are provided by ASP.NET Core, how to use & view these pre-built metrics and finally I have showed you how to create custom metrics and view it in a dashboard. If you want to learn more about the **Metrics System of .NET**, you can check out the references that I have shared below. +In this article, I showed you the new built-in metrics of .NET8, describe what metrics are, which pre-built metrics are provided by ASP.NET Core, how to use & view these pre-built metrics and finally, I have shown you how to create custom metrics and view them in a dashboard. If you want to learn more about the **Metrics System of .NET**, you can check out the references that I have shared below. ## References