4.6 KiB
Customizing OpenTelemetry .NET SDK for Metrics
MeterProvider
As shown in the getting-started doc, a valid
MeterProvider
must be configured and built to collect metrics with OpenTelemetry .NET Sdk.
MeterProvider holds all the configuration for metrics like MetricReaders,
Views, etc. Naturally, almost all the customizations must be done on the
MeterProvider.
Building a MeterProvider
Building a MeterProvider is done using MeterProviderBuilder which must be
obtained by calling Sdk.CreateMeterProviderBuilder(). MeterProviderBuilder
exposes various methods which configure the provider it is going to build.
These include methods like AddMeter, AddView etc, and are explained in
subsequent sections of this document. Once configuration is done, calling
Build() on the MeterProviderBuilder builds the MeterProvider instance.
Once built, changes to its configuration is not allowed. In most cases, a single
MeterProvider is created at the application startup, and is disposed when
application shuts down.
The snippet below shows how to build a basic MeterProvider. This will create a
provider with default configuration, and is not particularly useful. The
subsequent sections show how to build a more useful provider.
using OpenTelemetry;
using OpenTelemetry.Metrics;
using var meterProvider = Sdk.CreateMeterProviderBuilder().Build();
MeterProvider configuration
MeterProvider holds the metrics configuration, which includes the following:
- The list of
Meters from which instruments are created to report measurements. - The list of instrumentations enabled via Instrumentation Library.
- The list of MetricReaders, including exporting readers which exports metrics to Exporters
- The Resource associated with the metrics.
- The list of Views to be used.
Meter
Meter
is used for creating
Instruments,
which are then used to report
Measurements.
The SDK follows an explicit opt-in model for listening to meters. i.e, by
default, it listens to no meters. Every meter which is used to create
instruments must be explicitly added to the meter provider.
AddMeter method on MeterProviderBuilder can be used to add a Meter to the
provider. The name of the Meter (case-insensitive) must be provided as an
argument to this method. AddMeter can be called multiple times to add more
than one meters. It also supports wild-card subscription model.
It is not possible to add meters once the provider is built by the
Build() method on the MeterProviderBuilder.
The snippet below shows how to add meters to the provider.
using OpenTelemetry;
using OpenTelemetry.Metrics;
using var meterProvider = Sdk.CreateMeterProviderBuilder()
// The following enables instruments from Meter
// named "MyCompany.MyProduct.MyLibrary" only.
.AddMeter("MyCompany.MyProduct.MyLibrary")
// The following enables instruments from all Meters
// whose name starts with "AbcCompany.XyzProduct.".
.AddMeter("AbcCompany.XyzProduct.*")
.Build();
See Program.cs for complete example.
Note:
A common mistake while configuring MeterProvider is forgetting to add the
required Meters to the provider. It is recommended to leverage the wildcard
subscription model where it makes sense. For example, if your application is
expecting to enable instruments from a number of libraries from a company "Abc",
the you can use AddMeter("Abc.*") to enable all meters whose name starts with
"Abc.".
View
// TODO
Instrumentation
// TODO
MetricReader
// TODO
Resource
// TODO