Skip to content

Commit 17c3637

Browse files
WN Update: SignalR ActivitySource (#34183)
* WN Update SignalR ActivitySource Review suggestion by tdykstra Co-authored-by: Tom Dykstra <[email protected]> * Removed dupe code in program.cs * Updated code highlight to match code change --------- Co-authored-by: Tom Dykstra <[email protected]>
1 parent fcbf8d0 commit 17c3637

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+86178
-80
lines changed

aspnetcore/release-notes/aspnetcore-9.0.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ author: rick-anderson
44
description: Learn about the new features in ASP.NET Core 9.0.
55
ms.author: riande
66
ms.custom: mvc
7-
ms.date: 10/07/2024
7+
ms.date: 11/19/2024
88
uid: aspnetcore-9
99
---
1010
# What's new in ASP.NET Core 9.0

aspnetcore/release-notes/aspnetcore-9/includes/signalr-distributed-tracing-improvements.md

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
### Improved Activities for SignalR
22

3-
SignalR now has an ActivitySource named `Microsoft.AspNetCore.SignalR.Server` that emits events for hub method calls:
3+
SignalR now has an ActivitySource for both the hub server and client.
4+
5+
#### .NET SignalR server ActivitySource
6+
7+
The SignalR ActivitySource named `Microsoft.AspNetCore.SignalR.Server` emits events for hub method calls:
48

59
* Every method is its own activity, so anything that emits an activity during the hub method call is under the hub method activity.
610
* Hub method activities don't have a parent. This means they are not bundled under the long-running SignalR connection.
@@ -15,29 +19,19 @@ The following example uses the [.NET Aspire dashboard](/dotnet/aspire/fundamenta
1519

1620
Add the following startup code to the `Program.cs` file:
1721

18-
```csharp
19-
// Set OTEL_EXPORTER_OTLP_ENDPOINT environment variable depending on where your OTEL endpoint is
20-
var builder = WebApplication.CreateBuilder(args);
22+
[!code-csharp[](~/release-notes/aspnetcore-9/samples/SignalRChatTraceExample/Program.cs?name=snippet_trace_signalr_server&highlight=1,10-23)]
23+
24+
The following is example output from the Aspire Dashboard:
2125

22-
builder.Services.AddRazorPages();
23-
builder.Services.AddSignalR();
26+
:::image type="content" source="~/release-notes/aspnetcore-9/_static/signalr-activities-events.png" alt-text="Activity list for SignalR Hub method call events":::
2427

25-
builder.Services.AddOpenTelemetry()
26-
.WithTracing(tracing =>
27-
{
28-
if (builder.Environment.IsDevelopment())
29-
{
30-
// We want to view all traces in development
31-
tracing.SetSampler(new AlwaysOnSampler());
32-
}
28+
#### .NET SignalR client ActivitySource
3329

34-
tracing.AddAspNetCoreInstrumentation();
35-
tracing.AddSource("Microsoft.AspNetCore.SignalR.Server");
36-
});
30+
The SignalR ActivitySource named `Microsoft.AspNetCore.SignalR.Client` emits events for a SignalR client:
3731

38-
builder.Services.ConfigureOpenTelemetryTracerProvider(tracing => tracing.AddOtlpExporter());
39-
```
32+
* The .NET SignalR client has an `ActivitySource` named `Microsoft.AspNetCore.SignalR.Client`. Hub invocations now create a client span. Note that other SignalR clients, such as the JavaScript client, don't support tracing. This feature will be added to more clients in future releases.
33+
* Hub invocations on the client and server support [context propagation](https://opentelemetry.io/docs/concepts/context-propagation/). Propagating the trace context enables true distributed tracing. It's now possible to see invocations flow from the client to the server and back.
4034

41-
The following is example output from the Aspire Dashboard:
35+
Here's how these new activities look in the [.NET Aspire dashboard](https://learn.microsoft.com/dotnet/aspire/fundamentals/dashboard/overview?tabs=bash#standalone-mode):
4236

43-
:::image type="content" source="~/release-notes/aspnetcore-9/_static/signalr-activites-events.png" alt-text="Activity list for SignalR Hub method call events":::
37+
![SignalR distributed tracing in Aspire dashboard](~/release-notes/aspnetcore-9/_static/signalr-distributed-tracing-aspire-dashboard.png)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Microsoft.AspNetCore.SignalR;
2+
3+
namespace SignalRChat.Hubs
4+
{
5+
public class ChatHub : Hub
6+
{
7+
public async Task SendMessage(string user, string message)
8+
{
9+
await Clients.All.SendAsync("ReceiveMessage", user, message);
10+
}
11+
}
12+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@page
2+
@model ErrorModel
3+
@{
4+
ViewData["Title"] = "Error";
5+
}
6+
7+
<h1 class="text-danger">Error.</h1>
8+
<h2 class="text-danger">An error occurred while processing your request.</h2>
9+
10+
@if (Model.ShowRequestId)
11+
{
12+
<p>
13+
<strong>Request ID:</strong> <code>@Model.RequestId</code>
14+
</p>
15+
}
16+
17+
<h3>Development Mode</h3>
18+
<p>
19+
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred.
20+
</p>
21+
<p>
22+
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
23+
It can result in displaying sensitive information from exceptions to end users.
24+
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
25+
and restarting the app.
26+
</p>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Diagnostics;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.AspNetCore.Mvc.RazorPages;
4+
5+
namespace SignalRChat.Pages
6+
{
7+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
8+
[IgnoreAntiforgeryToken]
9+
public class ErrorModel : PageModel
10+
{
11+
public string? RequestId { get; set; }
12+
13+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
14+
15+
private readonly ILogger<ErrorModel> _logger;
16+
17+
public ErrorModel(ILogger<ErrorModel> logger)
18+
{
19+
_logger = logger;
20+
}
21+
22+
public void OnGet()
23+
{
24+
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
25+
}
26+
}
27+
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
@page
2+
<div class="container">
3+
<div class="row p-1">
4+
<div class="col-1">User</div>
5+
<div class="col-5"><input type="text" id="userInput" /></div>
6+
</div>
7+
<div class="row p-1">
8+
<div class="col-1">Message</div>
9+
<div class="col-5"><input type="text" class="w-100" id="messageInput" /></div>
10+
</div>
11+
<div class="row p-1">
12+
<div class="col-6 text-end">
13+
<input type="button" id="sendButton" value="Send Message" />
14+
</div>
15+
</div>
16+
<div class="row p-1">
17+
<div class="col-6">
18+
<hr />
19+
</div>
20+
</div>
21+
<div class="row p-1">
22+
<div class="col-6">
23+
<ul id="messagesList"></ul>
24+
</div>
25+
</div>
26+
</div>
27+
<script src="~/js/signalr/dist/browser/signalr.js"></script>
28+
<script src="~/js/chat.js"></script>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Microsoft.AspNetCore.Mvc.RazorPages;
3+
4+
namespace SignalRChat.Pages
5+
{
6+
public class IndexModel : PageModel
7+
{
8+
private readonly ILogger<IndexModel> _logger;
9+
10+
public IndexModel(ILogger<IndexModel> logger)
11+
{
12+
_logger = logger;
13+
}
14+
15+
public void OnGet()
16+
{
17+
18+
}
19+
}
20+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@page
2+
@model PrivacyModel
3+
@{
4+
ViewData["Title"] = "Privacy Policy";
5+
}
6+
<h1>@ViewData["Title"]</h1>
7+
8+
<p>Use this page to detail your site's privacy policy.</p>

0 commit comments

Comments
 (0)