Skip to content

Latest commit

 

History

History
140 lines (107 loc) · 4.62 KB

File metadata and controls

140 lines (107 loc) · 4.62 KB

Getting Started with OpenTelemetry .NET Metrics in 5 Minutes - Console Application

First, download and install the .NET SDK on your computer.

Create a new console application and run it:

dotnet new console --output getting-started
cd getting-started
dotnet run

You should see the following output:

Hello World!

Install the OpenTelemetry.Exporter.Console package:

dotnet add package OpenTelemetry.Exporter.Console

Update the Program.cs file with the code from Program.cs:

Run the application again (using dotnet run) and you should see the metric output from the console (metrics will be seen once the program ends), similar to shown below:

Export MyFruitCounter, Meter: MyCompany.MyProduct.MyLibrary/1.0
(2021-09-23T22:00:08.4399776Z, 2021-09-23T22:00:08.4510115Z] color:red name:apple LongSum
Value: 6
(2021-09-23T22:00:08.4399776Z, 2021-09-23T22:00:08.4510115Z] color:yellow name:lemon LongSum
Value: 7
(2021-09-23T22:00:08.4399776Z, 2021-09-23T22:00:08.4510115Z] color:green name:apple LongSum
Value: 2

Congratulations! You are now collecting metrics using OpenTelemetry.

What does the above program do?

The program creates a Meter instance named "MyCompany.MyProduct.MyLibrary".

private static readonly Meter MyMeter = new("MyCompany.MyProduct.MyLibrary", "1.0");

Then it creates a Counter instrument from it.

private static readonly Counter<long> MyFruitCounter = MyMeter.CreateCounter<long>("MyFruitCounter");

This counter is used to report several metric measurements.

MyFruitCounter.Add(1, new("name", "apple"), new("color", "red"));
MyFruitCounter.Add(2, new("name", "lemon"), new("color", "yellow"));
MyFruitCounter.Add(1, new("name", "lemon"), new("color", "yellow"));

An OpenTelemetry MeterProvider is configured to subscribe to an instrument named "MyFruitCounter" from the Meter MyCompany.MyProduct.MyLibrary, and aggregate the measurements in-memory with a default cardinality limit of 2000. The pre-aggregated metrics are exported to a ConsoleExporter.

var meterProvider = Sdk.CreateMeterProviderBuilder()
    .AddMeter("MyCompany.MyProduct.MyLibrary")
    .AddConsoleExporter()
    .Build();

Note

If you need to collect metrics with cardinality higher than the default limit 2000, please follow the cardinality limits guidance. Here is a quick example of how to change the cardinality limit to 10 for this particular metric:

var meterProvider = Sdk.CreateMeterProviderBuilder()
    .AddMeter("MyCompany.MyProduct.MyLibrary")
    .AddView(instrumentName: "MyFruitCounter", new MetricStreamConfiguration { CardinalityLimit = 10 })
    .AddConsoleExporter()
    .Build();
graph LR

subgraph SDK
  MeterProvider
  MetricReader[BaseExportingMetricReader]
  ConsoleExporter
end

subgraph API
  Instrument["Meter(#quot;MyCompany.MyProduct.MyLibrary#quot;, #quot;1.0#quot;)<br/>Counter(#quot;MyFruitCounter#quot;)"]
end

Instrument --> | Measurements | MeterProvider

MeterProvider --> | Metrics | MetricReader --> | Push | ConsoleExporter
Loading

MeterProvider

As shown in the above program, a valid MeterProvider must be configured and built to collect metrics with OpenTelemetry .NET SDK. MeterProvider holds all the configuration for metrics like Meter names, readers, etc. and is highly customizable.

OpenTelemetry .NET special note

Metrics in OpenTelemetry .NET is a somewhat unique implementation of the OpenTelemetry project, as most of the Metrics API is implemented by the .NET runtime itself. From a high level, what this means is that you can instrument your application by simply depending on System.Diagnostics.DiagnosticSource package.

Learn more