Skip to content

Commit a1b948b

Browse files
authored
Distributed tracing in networking (#44063)
Introduce conceptual docs on distributed tracing in System.Net. Leverage Aspire Dashboards to demo the traces.
1 parent e6f894d commit a1b948b

23 files changed

+417
-108
lines changed

docs/core/diagnostics/distributed-tracing-builtin-activities.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ This is a reference for distributed tracing [activities](xref:System.Diagnostics
1111

1212
## System.Net activities
1313

14+
> [!TIP]
15+
> For a comprehensive guide about collecting and reporting `System.Net` traces, see [Networking distributed traces in .NET](../../fundamentals/networking/telemetry/metrics.md).
16+
1417
### HTTP client request
1518

1619
<xref:System.Net.Http.SocketsHttpHandler> and <xref:System.Net.Http.HttpClientHandler> report the HTTP client request activity following the recommendations defined in OpenTelemetry [HTTP Client Semantic Conventions](https://opentelemetry.io/docs/specs/semconv/http/http-spans/#http-client). It describes the HTTP request sent by <xref:System.Net.Http.HttpClient>'s send overloads during the time interval the underlying handler completes the request. Completing the request includes the time up to reading response headers from the network stream. It doesn't include the time spent reading the response body. <xref:System.Net.Http.SocketsHttpHandler> may retry requests, for example, on connection failures or HTTP version downgrades. Retries are not reported as separate *HTTP client request* activities.
38.1 KB
Loading
31.4 KB
Loading

docs/core/diagnostics/snippets/OTel-Prometheus-Grafana-Jaeger/csharp/Program.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
// Metrics provides by ASP.NET Core in .NET 8
4242
.AddMeter("Microsoft.AspNetCore.Hosting")
4343
.AddMeter("Microsoft.AspNetCore.Server.Kestrel")
44+
// Metrics provided by System.Net libraries
45+
.AddMeter("System.Net.Http")
46+
.AddMeter("System.Net.NameResolution")
4447
.AddPrometheusExporter());
4548

4649
// Add Tracing for ASP.NET Core and our custom ActivitySource and export to Jaeger
@@ -88,6 +91,24 @@
8891
app.MapGet("/NestedGreeting", SendNestedGreeting);
8992
//</Snippet_MapNested>
9093

94+
//<Snippet_ClientStress>
95+
app.MapGet("/ClientStress", async Task<string> (ILogger<Program> logger, HttpClient client) =>
96+
{
97+
string[] uris = ["http://example.com", "http://httpbin.org/get", "https://example.com", "https://httpbin.org/get"];
98+
await Parallel.ForAsync(0, 50, async (_, ct) =>
99+
{
100+
string uri = uris[Random.Shared.Next(uris.Length)];
101+
102+
try
103+
{
104+
await client.GetAsync(uri, ct);
105+
logger.LogInformation($"{uri} - done.");
106+
}
107+
catch { logger.LogInformation($"{uri} - failed."); }
108+
});
109+
return "Sent 50 requests to example.com and httpbin.org.";
110+
});
111+
//</Snippet_ClientStress>
91112

92113
#if !AZURE_MONITOR
93114
//<Snippet_Prometheus>
@@ -99,7 +120,7 @@
99120
app.Run();
100121

101122
//<Snippet_SendGreeting>
102-
async Task<String> SendGreeting(ILogger<Program> logger)
123+
async Task<string> SendGreeting(ILogger<Program> logger)
103124
{
104125
// Create a new Activity scoped to the method
105126
using var activity = greeterActivitySource.StartActivity("GreeterActivity");
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The Aspire Service Defaults project provides an easy way to configure OTel for ASP.NET projects, *even if not using the rest of .NET Aspire* such as the AppHost for orchestration. The Service Defaults project is available as a project template via Visual Studio or `dotnet new`. It configures OTel and sets up the OTLP exporter. You can then use the [OTel environment variables](https://github.com/open-telemetry/opentelemetry-dotnet/tree/c94c422e31b2a5181a97b2dcf4bdc984f37ac1ff/src/OpenTelemetry.Exporter.OpenTelemetryProtocol#exporter-configuration) to configure the OTLP endpoint to send telemetry to, and provide the resource properties for the application.
2+
3+
The steps to use *ServiceDefaults* outside .NET Aspire are:
4+
5+
1. Add the *ServiceDefaults* project to the solution using Add New Project in Visual Studio, or use `dotnet new`:
6+
7+
```dotnetcli
8+
dotnet new aspire-servicedefaults --output ServiceDefaults
9+
```
10+
11+
1. Reference the *ServiceDefaults* project from your ASP.NET application. In Visual Studio, select **Add** > **Project Reference** and select the **ServiceDefaults** project"
12+
1. Call the OpenTelemetry setup function `ConfigureOpenTelemetry()` as part of your application builder initialization.
13+
14+
``` csharp
15+
var builder = WebApplication.CreateBuilder(args)
16+
builder.ConfigureOpenTelemetry(); // Extension method from ServiceDefaults.
17+
var app = builder.Build();
18+
app.MapGet("/", () => "Hello World!");
19+
app.Run();
20+
```
21+
22+
For a full walkthrough, see [Example: Use OpenTelemetry with OTLP and the standalone Aspire Dashboard](../../../../core/diagnostics/observability-otlp-example.md).
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
A simple way to collect traces and metrics in ASP.NET applications is to use [.NET Aspire](/dotnet/aspire/get-started/aspire-overview). .NET Aspire is a set of extensions to .NET to make it easy to create and work with distributed applications. One of the benefits of using .NET Aspire is that telemetry is built in, using the OpenTelemetry libraries for .NET.
2+
3+
The default project templates for .NET Aspire contain a `ServiceDefaults` project. Each service in the .NET Aspire solution has a reference to the Service Defaults project. The services use it to set up and configure OTel.
4+
5+
The Service Defaults project template includes the OTel SDK, ASP.NET, HttpClient, and Runtime Instrumentation packages. These instrumentation components are configured in the [Extensions.cs](https://github.com/dotnet/aspire/blob/main/src/Aspire.ProjectTemplates/templates/aspire-servicedefaults/Extensions.cs) file. To support telemetry visualization in Aspire Dashboard, the Service Defaults project also includes the OTLP exporter by default.
6+
7+
Aspire Dashboard is designed to bring telemetry observation to the local debug cycle, which enables developers to ensure that the applications are producing telemetry. The telemetry visualization also helps to diagnose those applications locally. Being able to observe the calls between services is as useful at debug time as in production. The .NET Aspire dashboard is launched automatically when you <kbd>F5</kbd> the `AppHost` Project from Visual Studio or `dotnet run` the `AppHost` project from command line.
39.2 KB
Loading
43.6 KB
Loading
37.3 KB
Loading
95.1 KB
Loading

0 commit comments

Comments
 (0)