2.2 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-gauge
cd getting-started-observable-gauge
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-gauge
Export 15:44:05.262 15:44:05.263 Gauge [tag1=value1;tag2=value2] LongGauge, Meter: MyCompany.MyProduct.MyLibrary/1.0
Value: 306
Export 15:44:05.262 15:44:06.290 Gauge [tag1=value1;tag2=value2] LongGauge, Meter: MyCompany.MyProduct.MyLibrary/1.0
Value: 693
Export 15:44:05.262 15:44:07.302 Gauge [tag1=value1;tag2=value2] LongGauge, Meter: MyCompany.MyProduct.MyLibrary/1.0
Value: 78
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 Gauge instrument from it. This Gauge reports a randomly generated 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.