Do you know what is going on with your API? Collect ASP.NET Core API metrics using AppMetrics.

Miłosz Wojarski
4 min readApr 24, 2023

--

Photo by Luke Chesser on Unsplash

App Metrics is an open-source and cross-platform .NET library used to record metrics within an application. App Metrics can run on .NET Core or on the full .NET framework also supporting .NET 4.5.2.

It doesn’t matter if you want to measure how your app is used, see the most faulty endpoints, or just know the health status of your app. AppMetrics can cover many other use cases when talking about application monitoring. It runs in the background of .NET applications with minimal performance and memory overhead.

Benefits

As an app owner who needs to constantly monitor my app, I list the most useful metrics and how they help me identify current and incoming issues with my app.

  1. Errors per endpoint — It helps me identify which particular endpoint is least stable and requires enhancements or refactoring.
  2. Request / response times — I can find out all the variations in response times, to track potential- unstable endpoints and further investigate them.
  3. Number of requests to each endpoint — it helps me detect all potential misuse of my app and which parts of the app are used more often — so need more attention. It also highlights those parts of the code that are not used at all (we can reduce maintenance costs by removing unused code).
  4. When endpoints / services are used — It gives me hints on how and when my application should be scaled.
  5. System usage — How much resources my app is using — helps me identify any memory leaks or inefficient processing in my app.
  6. ApDex Score — Measures users satisfaction of using an API.
  7. Contexts — AppMetrics supports multiple contexts, so it is easy to distinct metrics in one process from another.
  8. Custom metrics — as AppMetrics supports custom metrics, so I can measure almost anything that is going on in my application like for the execution time of a particular part of the code, all sql queries time or how much memory is used by a particular part of the code. What is more- I can measure business metric with the same code as technical metrics.
  9. Historical data — for all the above I can track past values and how did it change, which gives me a great overview of trends. I can act reactively on all potential problems and challenges before they actually come.

Types of metrics

  • Gauge — works best to measure values like CPU, Memory usage, Percentage value, Temperature, etc.
  • Counter — The simplest metric that counts a number of events.
  • Meter — Similar to counter, however better for measuring trends rather than knowing exact value. It is based on Exponential Weighted Moving Average so the trends are more smooth.
  • Histogram — Measures statistical distrbituion of a set of values. Useful for metrics like size of POST and PIT requests.
  • Timers — Measures execution time of given part of the code. Very useful for measuring database queries and distinct them from the whole API call response time.
  • Apdex — Measures end-user satisfaction, based on Application Performance Index.

Proponents of the Apdex standard believe that it offers a better way to “measure what matters”. The Apdex method converts many measurements into one number on a uniform scale of 0 to 1 (0 = no users satisfied, 1 = all users satisfied). The resulting Apdex score is a numerical measure of user satisfaction with the performance of enterprise applications.

https://en.wikipedia.org/wiki/Apdex

Metrics reporters

AppMetrics only collects data, by itself it cannot display data in a human-readable or graphical format. That is why it is advised to use cooperating data aggregator to store and analyze the data.

As it supports:

  • Prometheus
  • DataDog
  • InfluxDb
  • Microsoft HealthChecks
  • Azure insights
  • Custom http integration

it doesn’t matter what metrics aggregator you choose, both pull and push methods are supported.

Metrics push & pull support

101 Setup

Add nugets to your .csproj file

  <PackageReference Include="App.Metrics.App.All" Version="4.3.0" />
<PackageReference Include="App.Metrics.AspNetCore.Tracking" Version="4.3.0" />
<PackageReference Include="App.Metrics.AspNetCore.Mvc" Version="4.3.0" />
<PackageReference Include="App.Metrics.Reporting.Console" Version="4.3.0" />

Register metrics in your Program.cs file.

builder.Services.AddMvcCore().AddMetricsCore();
builder.Services.AddMetricsReportingHostedService();
builder.Services.AddMetricsEndpoints();

//...

app.UseMetricsAllMiddleware();
app.UseMetricsAllEndpoints();

Voila!

Build & run it and access /metrics endpoint. It should return you most basic (but still very usefull) set of metrics.

Metrics endpoint

--

--

Miłosz Wojarski

.NET Tech Lead | Software developer | I write about software development, architecture, DevOps.