Add ASP.NET example for metrics (#4381)

This commit is contained in:
Reiley Yang 2023-04-13 13:48:43 -07:00 committed by GitHub
parent 7db694157f
commit 7958763a1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 197 additions and 11 deletions

View File

@ -193,7 +193,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "customizing-the-sdk", "docs
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "learning-more-instruments", "docs\metrics\learning-more-instruments\learning-more-instruments.csproj", "{E7F491CC-C37E-4A56-9CA7-8F77F59E0614}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "getting-started", "docs\metrics\getting-started\getting-started.csproj", "{EA60B549-F712-4ABE-8E44-FCA83B78C06E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "getting-started-console", "docs\metrics\getting-started-console\getting-started-console.csproj", "{EA60B549-F712-4ABE-8E44-FCA83B78C06E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "getting-started-aspnetcore", "docs\metrics\getting-started-aspnetcore\getting-started-aspnetcore.csproj", "{0C3E7D40-E0B3-4B77-8139-0E85C3600688}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "extending-the-sdk", "docs\metrics\extending-the-sdk\extending-the-sdk.csproj", "{1F9D7748-D099-4E25-97F5-9C969D6FF969}"
EndProject
@ -421,6 +423,10 @@ Global
{EA60B549-F712-4ABE-8E44-FCA83B78C06E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EA60B549-F712-4ABE-8E44-FCA83B78C06E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EA60B549-F712-4ABE-8E44-FCA83B78C06E}.Release|Any CPU.Build.0 = Release|Any CPU
{0C3E7D40-E0B3-4B77-8139-0E85C3600688}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0C3E7D40-E0B3-4B77-8139-0E85C3600688}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0C3E7D40-E0B3-4B77-8139-0E85C3600688}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0C3E7D40-E0B3-4B77-8139-0E85C3600688}.Release|Any CPU.Build.0 = Release|Any CPU
{1F9D7748-D099-4E25-97F5-9C969D6FF969}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1F9D7748-D099-4E25-97F5-9C969D6FF969}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1F9D7748-D099-4E25-97F5-9C969D6FF969}.Release|Any CPU.ActiveCfg = Release|Any CPU
@ -569,6 +575,7 @@ Global
{64E3D8BB-93AB-4571-93F7-ED8D64DFFD06} = {5B7FB835-3FFF-4BC2-99C5-A5B5FAE3C818}
{E7F491CC-C37E-4A56-9CA7-8F77F59E0614} = {3277B1C0-BDFE-4460-9B0D-D9A661FB48DB}
{EA60B549-F712-4ABE-8E44-FCA83B78C06E} = {3277B1C0-BDFE-4460-9B0D-D9A661FB48DB}
{0C3E7D40-E0B3-4B77-8139-0E85C3600688} = {3277B1C0-BDFE-4460-9B0D-D9A661FB48DB}
{1F9D7748-D099-4E25-97F5-9C969D6FF969} = {3277B1C0-BDFE-4460-9B0D-D9A661FB48DB}
{81234AFA-B4E7-4D0D-AB97-FD559C78EDA2} = {3277B1C0-BDFE-4460-9B0D-D9A661FB48DB}
{1F6CC903-04C9-4E7C-B388-C215C467BFB9} = {3862190B-E2C5-418E-AFDC-DB281FB5C705}

View File

@ -40,7 +40,8 @@ repo.
If you are new here, please read the getting started docs:
* [logs](./docs/logs/getting-started/README.md)
* [metrics](./docs/metrics/getting-started/README.md)
* metrics: [ASP.NET Core](./docs/metrics/getting-started-aspnetcore/README.md) |
[Console](./docs/metrics/getting-started-console/README.md)
* traces: [ASP.NET Core](./docs/trace/getting-started-aspnetcore/README.md) |
[Console](./docs/trace/getting-started-console/README.md)

View File

@ -2,7 +2,8 @@
## MeterProvider
As shown in the [getting-started](../getting-started/README.md) doc, a valid
As shown in the [getting-startedgetting started in 5 minutes - Console
Application](../getting-started-console/README.md) doc, a valid
[`MeterProvider`](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#meterprovider)
must be configured and built to collect metrics with OpenTelemetry .NET Sdk.
`MeterProvider` holds all the configuration for metrics like MetricReaders,

View File

@ -0,0 +1,38 @@
// <copyright file="Program.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System.Diagnostics;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
var appBuilder = WebApplication.CreateBuilder(args);
// Configure OpenTelemetry with metrics and auto-start.
appBuilder.Services.AddOpenTelemetry()
.ConfigureResource(builder => builder
.AddService(serviceName: "OTel.NET Getting Started"))
.WithMetrics(builder => builder
.AddAspNetCoreInstrumentation()
.AddConsoleExporter((exporterOptions, metricReaderOptions) =>
{
metricReaderOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = 1000;
}));
var app = appBuilder.Build();
app.MapGet("/", () => $"Hello from OpenTelemetry Metrics!");
app.Run();

View File

@ -0,0 +1,102 @@
# Getting Started with OpenTelemetry .NET in 5 Minutes - ASP.NET Core Application
First, download and install the [.NET
SDK](https://dotnet.microsoft.com/download) on your computer.
Create a new web application:
```sh
dotnet new web -o aspnetcoreapp
cd aspnetcoreapp
```
Install the
[OpenTelemetry.Exporter.Console](../../../src/OpenTelemetry.Exporter.Console/README.md),
[OpenTelemetry.Extensions.Hosting](../../../src/OpenTelemetry.Extensions.Hosting/README.md),
and
[OpenTelemetry.Instrumentation.AspNetCore](../../../src/OpenTelemetry.Exporter.Console/README.md)
packages:
```sh
dotnet add package OpenTelemetry.Exporter.Console --prerelease
dotnet add package OpenTelemetry.Extensions.Hosting --prerelease
dotnet add package OpenTelemetry.Instrumentation.AspNetCore --prerelease
```
> **Note** This quickstart guide uses prerelease packages. For a quickstart
> which only relies on stable packages see: [Getting Started - Console
> Application](../getting-started-console/README.md). For more information about
> when instrumentation will be marked as stable see: [Instrumentation-1.0.0
> milestone](https://github.com/open-telemetry/opentelemetry-dotnet/milestone/23).
Update the `Program.cs` file with the code from [Program.cs](./Program.cs).
Run the application again (using `dotnet run`) and then browse to the url shown
in the console for your application (ex `http://localhost:5154`). You should see
the trace output from the console.
```text
Export http.server.duration, Measures the duration of inbound HTTP requests., Unit: ms, Meter: OpenTelemetry.Instrumentation.AspNetCore/1.0.0.0
(2023-04-11T21:49:43.6915232Z, 2023-04-11T21:50:50.6564690Z] http.flavor: 1.1 http.method: GET http.route: / http.scheme: http http.status_code: 200 net.host.name: localhost net.host.port: 5000 Histogram
Value: Sum: 3.5967 Count: 11 Min: 0.073 Max: 2.5539
(-Infinity,0]:0
(0,5]:11
(5,10]:0
(10,25]:0
(25,50]:0
(50,75]:0
(75,100]:0
(100,250]:0
(250,500]:0
(500,750]:0
(750,1000]:0
(1000,2500]:0
(2500,5000]:0
(5000,7500]:0
(7500,10000]:0
(10000,+Infinity]:0
```
Congratulations! You are now collecting metrics using OpenTelemetry.
What does the above program do?
The program uses the
[OpenTelemetry.Instrumentation.AspNetCore](../../../src/OpenTelemetry.Instrumentation.AspNetCore/README.md)
package to automatically create metrics for incoming ASP.NET Core requests, uses
the
[OpenTelemetry.Exporter.Console](../../../src/OpenTelemetry.Exporter.Console/README.md)
package to write metrics to the console every 1000 milliseconds. This is done by
configuring an OpenTelemetry
[MeterProvider](../customizing-the-sdk/README.MD#meterprovider) using extension
methods and setting it to auto-start when the host is started:
```csharp
appBuilder.Services.AddOpenTelemetry()
.ConfigureResource(builder => builder
.AddService(serviceName: "OTel.NET Getting Started"))
.WithMetrics(builder => builder
.AddAspNetCoreInstrumentation()
.AddConsoleExporter((exporterOptions, metricReaderOptions) =>
{
metricReaderOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = 1000;
})
);
```
> **Note**
> The `AddOpenTelemetry` extension is part of the
[OpenTelemetry.Extensions.Hosting](../../../src/OpenTelemetry.Extensions.Hosting/README.md)
package.
The index route ("/") is set up to write out a greeting message on the response:
```csharp
app.MapGet("/", () => $"Hello from OpenTelemetry Metrics!");
```
## Learn more
* [Getting Started with Prometheus and Grafana](../getting-started-prometheus-grafana/README.md)
* [Customizing OpenTelemetry .NET SDK](../customizing-the-sdk/README.md)
* [Extending the OpenTelemetry .NET SDK](../extending-the-sdk/README.md)

View File

@ -0,0 +1,9 @@
{
"DetailedErrors": true,
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

View File

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Console\OpenTelemetry.Exporter.Console.csproj" />
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Extensions.Hosting\OpenTelemetry.Extensions.Hosting.csproj" />
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.AspNetCore\OpenTelemetry.Instrumentation.AspNetCore.csproj" />
</ItemGroup>
</Project>

View File

@ -11,8 +11,9 @@
## Export metrics from the application
It is highly recommended to go over the [getting-started](../getting-started/README.md)
doc before following along this document.
It is highly recommended to go over the [getting started in 5 minutes - Console
Application](../getting-started-console/README.md) doc before following along
this document.
Create a new console application and run it:
@ -33,8 +34,8 @@ Now, we are going to make some small tweaks to the example in the
getting-started metrics `Program.cs` to make the metrics available via
OpenTelemetry Prometheus Exporter.
First, copy and paste everything from getting-started
metrics [example](../getting-started/Program.cs) to the Program.cs file of the
First, copy and paste everything from getting-started metrics
[example](../getting-started-console/Program.cs) to the Program.cs file of the
new console application (getting-started-prometheus) we've created.
And replace the below line:

View File

@ -21,7 +21,8 @@ See the individual "getting started" examples depending on the signal being
used:
* [Logs](../../docs/logs/getting-started/Program.cs)
* [Metrics](../../docs/metrics/getting-started/Program.cs)
* Metrics: [ASP.NET Core](../../docs/metrics/getting-started-aspnetcore/README.md)
| [Console](../../docs/metrics/getting-started-console/README.md)
* Traces: [ASP.NET Core](../../docs/trace/getting-started-aspnetcore/README.md)
| [Console](../../docs/trace/getting-started-console/README.md)

View File

@ -53,9 +53,11 @@ OpenTelemetry logging.
If you are new to
[metrics](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md),
it is first recommended to follow the [getting started in 5
minutes](../../docs/metrics/getting-started/README.md) guide to get up and
running.
it is recommended to first follow the [getting started in 5 minutes - ASP.NET
Core Application](../../docs/metrics/getting-started-aspnetcore/README.md) guide
or the [getting started in 5 minutes - Console
Application](../../docs/metrics/getting-started-console/README.md) guide to get up
and running.
For a more detailed explanation of SDK metric features see [Customizing
OpenTelemetry .NET SDK for