Configuration Options
App Metrics provides access to configuration options through the AddMetrics
extension on the IServiceCollection
.
These configuration options include:
Property | Description |
---|---|
DefaultContextLabel | Metrics recorded through the IMetrics interface are grouped into "Contexts", for example a database context or application context. Metrics names should be unique per context. If no context label is presented when recording a metric, the default of "Application" will be used. This value can be changed through the DefaultContextLabel option. |
DefaultSamplingType | Histograms track the statistical distribution of a set of values, they do this by generating values from a reservoir of values using sampling. App Metrics provides different types of sampling, the default sampling type is Exponentially Decaying, this property allows that default value to be changed. The default sampling type is only used if the Histogram being updated does not specify it's own sampling type. |
GlobalTags | All metric types can be tagged, for example we could tag all metrics by environment e.g. staging or production so when we report metrics we know the environment for which they originated. GlobalTags provide access to a Dictionary<string, string> which is used to tag all metrics. |
MetricsEnabled | Allows recording of all metrics to be disabled. |
ReportingEnabled | Allows all configured reporters to be disabled. |
AddDefaultGlobalTags | If set to true add the default global tags. |
Changing options using Action<AppMetricOptions>
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options => options.AddMetricsResourceFilter());
services.AddMetrics(options => {
options.DefaultContextLabel = "MyContext",
options.GlobalTags.Add("env", "stage");
options.MetricsEnabled = true;
options.ReportingEnabled = true;
})
.AddDefaultReservoir(() => new Lazy<IReservoir>(() => new DefaultSlidingWindowReservoir()))
.AddJsonSerialization()
.AddHealthChecks()
.AddMetricsMiddleware();
}
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
app.UseMetrics();
}
}
Changing options using Microsoft.Extensions.Configuration.IConfiguration
Below is an example Startup.cs
using appsettings.json
as a configuration source:
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; set; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMetrics(Configuration.GetSection("AppMetrics"))
.AddJsonSerialization()
.AddHealthChecks()
.AddMetricsMiddleware();
}
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
app.UseMetrics();
}
}
And the appsettings.json
file
{
"AppMetrics": {
"DefaultContextLabel": "MyContext",
"DefaultSamplingType": "SlidingWindow",
"MetricsEnabled": true,
"ReportingEnabled": true,
"GlobalTags": { "env": "stage" }
}
}
Changes options via an options delegate and configuration source
In cases where it's required to have some settings in a configuration file but others in code, it's possible to provide both an options delegate and configuration source, where options are applied in order they are registered. For example, in the following code snippet any options registered via the configuration section will override those registered in code. The reverse can also be applied by changing the order of the options delegate and configuration source on the AddMetrics
method.
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options => options.AddMetricsResourceFilter());
services.AddMetrics(options => {
options.DefaultContextLabel = "MyContext"
}, Configuration.GetSection("AppMetrics"))
.AddDefaultReservoir(() => new Lazy<IReservoir>(() => new DefaultSlidingWindowReservoir()))
.AddJsonSerialization()
.AddHealthChecks()
.AddMetricsMiddleware();
}
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
app.UseMetrics();
}
}
Note
To have routes measured a resource filter is required to extract the route template of each request, add the resource filter when configuring Mvc options i.e.
services.AddMvc(options => options.AddMetricsResourceFilter());