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
Tracing UI displays resource name for outgoing resources #1040
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Aspire.Dashboard.Model; | ||
|
||
public interface IOutgoingPeerResolver | ||
{ | ||
string ResolvePeerName(string networkAddress); | ||
} |
87 changes: 87 additions & 0 deletions
87
src/Aspire.Dashboard/Model/ResourceOutgoingPeerResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Aspire.Dashboard.Model; | ||
|
||
public sealed class ResourceOutgoingPeerResolver : IOutgoingPeerResolver, IAsyncDisposable | ||
{ | ||
private readonly IDashboardViewModelService _dashboardViewModelService; | ||
private readonly Dictionary<string, ResourceViewModel> _resourceNameMapping = new(); | ||
private readonly CancellationTokenSource _watchContainersTokenSource = new(); | ||
private readonly Task _watchTask; | ||
private readonly object _lock = new object(); | ||
|
||
public ResourceOutgoingPeerResolver(IDashboardViewModelService dashboardViewModelService) | ||
{ | ||
_dashboardViewModelService = dashboardViewModelService; | ||
|
||
var viewModelMonitor = _dashboardViewModelService.GetResources(); | ||
var initialList = viewModelMonitor.Snapshot; | ||
var watch = viewModelMonitor.Watch; | ||
|
||
foreach (var result in initialList) | ||
{ | ||
_resourceNameMapping[result.Name] = result; | ||
} | ||
|
||
_watchTask = Task.Run(async () => | ||
{ | ||
await foreach (var resourceChanged in watch.WithCancellation(_watchContainersTokenSource.Token)) | ||
{ | ||
OnResourceListChanged(resourceChanged.ObjectChangeType, resourceChanged.Resource); | ||
} | ||
}); | ||
} | ||
|
||
private void OnResourceListChanged(ObjectChangeType changeType, ResourceViewModel resourceViewModel) | ||
{ | ||
lock (_lock) | ||
{ | ||
if (changeType == ObjectChangeType.Added) | ||
{ | ||
_resourceNameMapping[resourceViewModel.Name] = resourceViewModel; | ||
} | ||
else if (changeType == ObjectChangeType.Modified) | ||
{ | ||
_resourceNameMapping[resourceViewModel.Name] = resourceViewModel; | ||
} | ||
else if (changeType == ObjectChangeType.Deleted) | ||
{ | ||
_resourceNameMapping.Remove(resourceViewModel.Name); | ||
} | ||
} | ||
} | ||
|
||
public string ResolvePeerName(string networkAddress) | ||
{ | ||
lock (_lock) | ||
{ | ||
foreach (var resource in _resourceNameMapping.Values) | ||
{ | ||
foreach (var service in resource.Services) | ||
{ | ||
if (service.AddressAndPort == networkAddress) | ||
{ | ||
return resource.Name; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return networkAddress; | ||
} | ||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
_watchContainersTokenSource.Cancel(); | ||
_watchContainersTokenSource.Dispose(); | ||
|
||
try | ||
{ | ||
await _watchTask.ConfigureAwait(false); | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that the IDashboardViewModelService is already caching the changes (at least for the lifetime of that object, and we need to resolve your other comment on that matter first I suppose), I'm wondering if we need this whole extra layer of caching on top of it. I guess the call to GetResourceMonitor (inside GetResources) is too expensive to do repeatedly. Just seems like a bit much for what amounts to "iterate the current snapshot". I wonder if we could expose the current snapshot better somehow instead? Just spitballing. Nothing wrong with this class as it is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copied what the console logs page does.
I think
DashboardViewModelService
is very complex. A method to subscribe to resource updates and a method to get the resources would be less efficient (does it matter?) but much simpler.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah the console logs needs the watcher part to be able to update the list as it changes. But theoretically here you only need the current snapshot at the time of a lookup. I think we can tweak the view model service api to make it simpler. We've added/removed various methods of accessing as we've needed them/not needed them anymore. At minimum I think we could add a call to get the current snapshot. I guess for your use case though, when would you call that? On every lookup? It could theoretically get stale otherwise... but would that matter in practice (especially since this is not the page you hit when the dashboard loads)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's why there is a callback to tell you data has changed and a method to get the data. That's the pattern telemetry uses.
Debounce logic to throttle callbacks could be used if there are many changes. e.g. wait 100ms between callback notifications. (I haven't done that in telemetry yet, but I plan to)