Metric sources are a way to consume metrics and their readings. These can be added to a MetricsCollector
by specifying in the IEnumerable<MetricSource>
exposed at MetricsCollectorOptions.Sources
.
We provide some built-in metric sources that can report basic metrics within an application. Built-in sources can be added to a collector using the MetricsCollectorOptions.Sources
property or by calling AddDefaultSources
on the IMetricsCollectorBuilder
exposed by AddMetricsCollector
on an IServiceCollection
.
Provides basic metrics for a .NET application. This set contains the following metrics:
dotnet.cpu.processortime
- total processor time in secondsdotnet.cpu.threads
- threads for the processdotnet.mem.virtual
- virtual memory for the processdotnet.mem.paged
- paged memory for the process
Provides metrics about the garbage collector (GC). This set contains the following metrics:
dotnet.mem.collections.gen0
- number of gen-0 collectionsdotnet.mem.collections.gen1
- number of gen-1 collectionsdotnet.mem.collections.gen2
- number of gen-2 collections
Provides .NET Core runtime metrics which includes:
dotnet.cpu.usage
- % CPU usagedotnet.mem.working_set
- working set of the process in bytesdotnet.mem.size.heap
- total number of bytes across all heapsdotnet.mem.size.gen0
- total number of bytes in gen-0dotnet.mem.size.gen1
- total number of bytes in gen-1dotnet.mem.size.gen2
- total number of bytes in gen-2dotnet.mem.size.loh
- total number of bytes in the LOHdotnet.mem.collections.gen0
- number of gen-0 collectionsdotnet.mem.collections.gen1
- number of gen-1 collectionsdotnet.mem.collections.gen2
- number of gen-2 collectionsdotnet.threadpool.count
- number of threads in the threadpooldotnet.threadpool.queue_length
- number of work items queued to the threadpooldotnet.timers.count
- number of active timers
Note: The dotnet.mem.
generation counters are only updated when the garbage collector runs.
Until it runs, these counters will be zero and if allocations are low, these counters will be infrequently updated.
dotnet.kestrel.requests.per_sec
- requests per seconddotnet.kestrel.requests.total
- total requestsdotnet.kestrel.requests.current
- current requestsdotnet.kestrel.requests.failed
- failed requests
Custom metric sets can be defined by instantiating MetricSource
and calling the Add*
methods or by deriving from MetricSource
.
Typically an application will define an AppMetricSource
as follows:
public class AppMetricSource : MetricSource
{
public Counter MyCounter { get; }
public SamplingGauge<string, HttpStatusCode> MyGaugeWithTags { get; }
public AppMetricSource(MetricSourceOptions options) : base(options)
{
MyCounter = AddCounter("my_counter", "units", "description");
MyGaugeWithTags = AddCounter("my_gauge", "units", "description", new MetricTag<string>("route"), new MetricTag<HttpStatusCode>("status_code"));
}
}
Custom metric sources can be configured using MetricsCollectorOptions.Sources
in .NET full framework or the AddSource
method on the IMetricsCollectorBuilder
exposed by AddMetricsCollector
on an IServiceCollection
.