App Metrics provides a two ways to organize your metrics:
- Through the use of Tags when declaring your metrics or by configuring tags globally for all Metrics.
- By specifing a Context label when declaring your metrics. By default any all custom Metrics will belong the the "Application" Context.
Metrics can be tagged when they are declared, these tags can then be shipped with your metric values to your database of choice which is useful for commonly-queried metadata.
An example use case for tagging is recording an APIs response time per endpoint where a timer is recorded per endpoint but then reported as the same metric name tagged with by the endpoint. This allows us to more easily have a dynamic list of endpoints and their response times when visualizing with Grafana for example.
[!code-csharpMain]
Tags can also be defined for all Metrics globally which is useful for tagging by things like machine name, environment, ip address, application name etc.
[!code-csharpMain]
App Metrics adds a few default global tags which are commonly used. These are as follows:
Tag | Description |
---|---|
app |
The name of the application, which is set to the entry assembly name of the application i.e. Assembly.GetEntryAssembly().GetName().Name . |
server |
The name of the machine the application is currently running on i.e. Environment.MachineName . |
env |
The environment the application is running in. When running in debug mode will be set to debug otherwise release . This can be adjusted to be staging , production etc via app settings, via the tagging helpers or disabled setting the AppMetricsOptions.AddDefaultGlobalTags to false . |
App Metrics provides an extension method on the AppMetricsOptions delegate passing some environment information that can be used to set global tags such as the machine name, assembly version etc.
[!code-csharpMain]
[!code-csharpMain]
Organizing your Metrics into separate meaningful contexts is helpful for visualization and reporting. Only one level of Context categorization is supported.
The default global context can be modified using the DefaultContextLabel in the configuration options when configuring your application to use App Metrics.
To add Metrics to a particular context, the context can be specified when declaring your metrics. For example:
[!code-csharpMain]
The DefaultMetricsFilter can be used to filter Metrics by a Context. This would be useful if we only wanted to report on a particular Metrics Context.
Note
See Metrics Filtering for more details on filtering Metrics.
Below is a snippet from a /metrics
response generated by a web host using the App.Metrics.Extensions.Middleware nuget package (Note: Metric values have been omitted for brevity).
[!code-jsonMain]
Note
You'll need to install the App.Metrics.Formatters.Json nuget package and AddJsonSerialization()
in your Startup.cs
for formatting /metrics
results as json.
The DefaultMetricsFilter can be used to filter Metrics by a Tag Keys. This would be useful if we only wanted to report on a particular Metrics Tags.
Note
See Metrics Filtering for more details on filtering Metrics.