Skip to content

Initialize telemetry context in UpdateTelemetryProperties if not already initialized #9553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

adamint
Copy link
Member

@adamint adamint commented May 29, 2025

Description

Kusto exception data shows that we are missing data on several pages' component renders due to a OnParamsSet being called before we have reached telemetry context initialization in OnInitializedAsync. We can get around this by moving telemetry initialization to the top of each OnInitializedAsync. This problem only affects async component initialization but I moved every call for consistency.

Fixes part of #9534

Checklist

  • Is this feature complete?
    • Yes. Ready to ship.
    • No. Follow-up changes expected.
  • Are you including unit tests for the changes and scenario tests if relevant?
    • Yes
    • No
  • Did you add public API?
    • Yes
      • If yes, did you have an API Review for it?
        • Yes
        • No
      • Did you add <remarks /> and <code /> elements on your triple slash comments?
        • Yes
        • No
    • No
  • Does the change make any security assumptions or guarantees?
    • Yes
      • If yes, have you done a threat model and had a security review?
        • Yes
        • No
    • No
  • Does the change require an update in our Aspire docs?

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR ensures the telemetry context is initialized at the very start of each component’s lifecycle method to prevent missing telemetry when async parameter sets occur before initialization. The key changes are:

  • Add TelemetryContextProvider.Initialize(TelemetryContext) at the top of each OnInitialized/OnInitializedAsync.
  • Remove duplicate initialization calls at the end of those methods.
  • Apply the pattern consistently across all dashboard page and control components.

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/Aspire.Dashboard/Components/Pages/Traces.razor.cs Moved telemetry initialization to top of OnInitialized and removed duplicate.
src/Aspire.Dashboard/Components/Pages/TraceDetail.razor.cs Same change in TraceDetail component.
src/Aspire.Dashboard/Components/Pages/StructuredLogs.razor.cs Same change in StructuredLogs component.
src/Aspire.Dashboard/Components/Pages/Resources.razor.cs Same change in Resources component’s OnInitializedAsync.
src/Aspire.Dashboard/Components/Pages/Metrics.razor.cs Same change in Metrics component.
src/Aspire.Dashboard/Components/Pages/Login.razor.cs Same change in Login component’s async init.
src/Aspire.Dashboard/Components/Pages/Error.razor.cs Moved RequestId assignment after telemetry init for Error component.
src/Aspire.Dashboard/Components/Pages/ConsoleLogs.razor.cs Same change in ConsoleLogs component.
src/Aspire.Dashboard/Components/Controls/ResourceDetails.razor.cs Reordered telemetry init before grid label setup.
Comments suppressed due to low confidence (1)

src/Aspire.Dashboard/Components/Pages/Traces.razor.cs:152

  • Consider adding a unit test to verify that TelemetryContextProvider.Initialize is called before any asynchronous lifecycle methods to ensure telemetry is always initialized in time.
TelemetryContextProvider.Initialize(TelemetryContext);

@@ -160,6 +160,7 @@ private async Task HandleSearchFilterChangedAsync()

protected override async Task OnInitializedAsync()
{
TelemetryContextProvider.Initialize(TelemetryContext);
Copy link
Preview

Copilot AI May 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Add a comment explaining why telemetry initialization is placed at the top of OnInitializedAsync, so future maintainers understand its importance before any awaits or parameter setup.

Copilot uses AI. Check for mistakes.

@JamesNK
Copy link
Member

JamesNK commented May 29, 2025

I'd like to understand the problem some more.

Is the problem that TelemetryContextProvider.UpdateTelemetryProperties is getting called on some pages before TelemetryContextProvider.Initialize is called? If that's the case, shouldn't we have seen errors from here when testing and using the app?

I like it would be a good idea to make PostProperties safer. If telemetry service isn't set then log a warning and skip calling PostOperation.

@adamint
Copy link
Member Author

adamint commented May 29, 2025

Yes, that’s exactly what’s happening (sometimes) in real dashboard use. To clarify, you would suggest also logging a warning instead of throwing ?

@JamesNK
Copy link
Member

JamesNK commented May 29, 2025

logging a warning instead of throwing ?

Yes

adamint added 2 commits May 29, 2025 09:41
…ized

# Conflicts:
#	src/Aspire.Dashboard/Components/Controls/ResourceDetails.razor.cs
@JamesNK JamesNK merged commit 2eb107d into dotnet:main May 31, 2025
254 checks passed
@JamesNK
Copy link
Member

JamesNK commented May 31, 2025

/backport to release/9.3

Copy link
Contributor

Started backporting to release/9.3: https://github.com/dotnet/aspire/actions/runs/15359291327

Copy link
Contributor

@JamesNK backporting to "release/9.3" failed, the patch most likely resulted in conflicts:

$ git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch

Applying: Initialize telemetry before anything else is done
Applying: Log warning instead of throwing an exception if telemetry service is null in PostProperties
error: sha1 information is lacking or useless (src/Aspire.Dashboard/Components/Pages/ConsoleLogs.razor.cs).
error: could not build fake ancestor
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0002 Log warning instead of throwing an exception if telemetry service is null in PostProperties
Error: The process '/usr/bin/git' failed with exit code 128

Please backport manually!

JamesNK added a commit that referenced this pull request May 31, 2025
…ady initialized (#9553)

Co-authored-by: James Newton-King <james@newtonking.com>
danmoseley pushed a commit that referenced this pull request Jun 2, 2025
…ady initialized (#9553) (#9602)

Co-authored-by: Adam Ratzman <adam@adamratzman.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants