opentelemetry-dotnet/docs/metrics/getting-started-observable-.../README.md

2.4 KiB

Getting Started with OpenTelemetry .NET in 5 Minutes

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

Create a new console application and run it:

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

You should see the following output:

Hello World!

Install the OpenTelemetry.Exporter.Console package:

dotnet add package --prerelease 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, similar to shown below:

Service.Nameunknown_service:getting-started-observable-counter
Export 16:35:25.669 16:35:25.670 observable-counter [tag1=value1;tag2=value2] LongSum, Meter: MyCompany.MyProduct.MyLibrary/1.0
Value: 10
Export 16:35:25.669 16:35:26.698 observable-counter [tag1=value1;tag2=value2] LongSum, Meter: MyCompany.MyProduct.MyLibrary/1.0
Value: 20
Export 16:35:25.669 16:35:27.711 observable-counter [tag1=value1;tag2=value2] LongSum, Meter: MyCompany.MyProduct.MyLibrary/1.0
Value: 30
Export 16:35:25.669 16:35:28.729 observable-counter [tag1=value1;tag2=value2] LongSum, Meter: MyCompany.MyProduct.MyLibrary/1.0
Value: 40

Congratulations! You are now collecting metrics using OpenTelemetry.

What does the above program do?

The program creates a Meter instance named "MyCompany.MyProduct.MyLibrary" and then creates a Asynchronous Counter instrument from it. This Counter reports an ever increasing number as its measurement until exited after 10 seconds.

An OpenTelemetry MeterProvider is configured to subscribe to instruments from the Meter MyCompany.MyProduct.MyLibrary, and aggregate the measurements in-memory. The pre-aggregated metrics are exported every 1 second to a ConsoleExporter. ConsoleExporter simply displays it on the console.