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 all commits
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,10 @@ | ||
// 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); | ||
IDisposable OnPeerChanges(Func<Task> callback); | ||
} |
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
133 changes: 133 additions & 0 deletions
133
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,133 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Concurrent; | ||
|
||
namespace Aspire.Dashboard.Model; | ||
|
||
public sealed class ResourceOutgoingPeerResolver : IOutgoingPeerResolver, IAsyncDisposable | ||
{ | ||
private readonly IDashboardViewModelService _dashboardViewModelService; | ||
private readonly ConcurrentDictionary<string, ResourceViewModel> _resourceNameMapping = new(); | ||
private readonly CancellationTokenSource _watchContainersTokenSource = new(); | ||
private readonly Task _watchTask; | ||
private readonly List<Subscription> _subscriptions; | ||
private readonly object _lock = new object(); | ||
|
||
public ResourceOutgoingPeerResolver(IDashboardViewModelService dashboardViewModelService) | ||
{ | ||
_dashboardViewModelService = dashboardViewModelService; | ||
_subscriptions = new List<Subscription>(); | ||
|
||
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)) | ||
{ | ||
await OnResourceListChanged(resourceChanged.ObjectChangeType, resourceChanged.Resource).ConfigureAwait(false); | ||
} | ||
}); | ||
} | ||
|
||
private async Task OnResourceListChanged(ObjectChangeType changeType, ResourceViewModel resourceViewModel) | ||
{ | ||
if (changeType == ObjectChangeType.Added) | ||
{ | ||
_resourceNameMapping[resourceViewModel.Name] = resourceViewModel; | ||
} | ||
else if (changeType == ObjectChangeType.Modified) | ||
{ | ||
_resourceNameMapping[resourceViewModel.Name] = resourceViewModel; | ||
} | ||
else if (changeType == ObjectChangeType.Deleted) | ||
{ | ||
_resourceNameMapping.TryRemove(resourceViewModel.Name, out _); | ||
drewnoakes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
await RaisePeerChangesAsync().ConfigureAwait(false); | ||
} | ||
|
||
public string ResolvePeerName(string networkAddress) | ||
{ | ||
foreach (var (resourceName, resource) in _resourceNameMapping) | ||
{ | ||
foreach (var service in resource.Services) | ||
{ | ||
if (string.Equals(service.AddressAndPort, networkAddress, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return resource.Name; | ||
} | ||
} | ||
} | ||
|
||
return networkAddress; | ||
} | ||
|
||
public IDisposable OnPeerChanges(Func<Task> callback) | ||
{ | ||
lock (_lock) | ||
{ | ||
var subscription = new Subscription(callback, RemoveSubscription); | ||
_subscriptions.Add(subscription); | ||
return subscription; | ||
} | ||
} | ||
|
||
private void RemoveSubscription(Subscription subscription) | ||
{ | ||
lock (_lock) | ||
{ | ||
_subscriptions.Remove(subscription); | ||
} | ||
} | ||
|
||
private async Task RaisePeerChangesAsync() | ||
{ | ||
if (_subscriptions.Count == 0) | ||
{ | ||
return; | ||
} | ||
|
||
Subscription[] subscriptions; | ||
lock (_lock) | ||
{ | ||
subscriptions = _subscriptions.ToArray(); | ||
} | ||
|
||
foreach (var subscription in subscriptions) | ||
{ | ||
await subscription.ExecuteAsync().ConfigureAwait(false); | ||
} | ||
} | ||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
_watchContainersTokenSource.Cancel(); | ||
_watchContainersTokenSource.Dispose(); | ||
|
||
try | ||
{ | ||
await _watchTask.ConfigureAwait(false); | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
} | ||
} | ||
|
||
private sealed class Subscription(Func<Task> callback, Action<Subscription> onDispose) : IDisposable | ||
{ | ||
private readonly Func<Task> _callback = callback; | ||
private readonly Action<Subscription> _onDispose = onDispose; | ||
|
||
public void Dispose() => _onDispose(this); | ||
public Task ExecuteAsync() => _callback(); | ||
} | ||
} |
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
Oops, something went wrong.
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)