Skip to content
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 5 commits into from Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 30 additions & 9 deletions src/Aspire.Dashboard/Components/Pages/TraceDetail.razor.cs
Expand Up @@ -16,6 +16,7 @@ public partial class TraceDetail
private OtlpTrace? _trace;
private OtlpSpan? _span;
private Subscription? _tracesSubscription;
private IDisposable? _peerChangesSubscription;
private List<SpanWaterfallViewModel>? _spanWaterfallViewModels;
private int _maxDepth;

Expand All @@ -28,6 +29,18 @@ public partial class TraceDetail
[Inject]
public required TelemetryRepository TelemetryRepository { get; set; }

[Inject]
public required IOutgoingPeerResolver OutgoingPeerResolver { get; set; }

protected override void OnInitialized()
{
_peerChangesSubscription = OutgoingPeerResolver.OnPeerChanges(async () =>
{
UpdateDetailViewData();
await InvokeAsync(StateHasChanged);
});
}

private ValueTask<GridItemsProviderResult<SpanWaterfallViewModel>> GetData(GridItemsProviderRequest<SpanWaterfallViewModel> request)
{
Debug.Assert(_spanWaterfallViewModels != null);
Expand All @@ -39,34 +52,34 @@ private ValueTask<GridItemsProviderResult<SpanWaterfallViewModel>> GetData(GridI
});
}

private static List<SpanWaterfallViewModel> CreateSpanWaterfallViewModels(OtlpTrace trace)
private static List<SpanWaterfallViewModel> CreateSpanWaterfallViewModels(OtlpTrace trace, IOutgoingPeerResolver outgoingPeerResolver)
{
var orderedSpans = new List<SpanWaterfallViewModel>();
// There should be one root span but just in case, we'll add them all.
foreach (var rootSpan in trace.Spans.Where(s => string.IsNullOrEmpty(s.ParentSpanId)).OrderBy(s => s.StartTime))
{
AddSelfAndChildren(orderedSpans, rootSpan, depth: 1, CreateViewModel);
AddSelfAndChildren(orderedSpans, rootSpan, depth: 1, outgoingPeerResolver, CreateViewModel);
}
// Unparented spans.
foreach (var unparentedSpan in trace.Spans.Where(s => !string.IsNullOrEmpty(s.ParentSpanId) && s.GetParentSpan() == null).OrderBy(s => s.StartTime))
{
AddSelfAndChildren(orderedSpans, unparentedSpan, depth: 1, CreateViewModel);
AddSelfAndChildren(orderedSpans, unparentedSpan, depth: 1, outgoingPeerResolver, CreateViewModel);
}

return orderedSpans;

static void AddSelfAndChildren(List<SpanWaterfallViewModel> orderedSpans, OtlpSpan span, int depth, Func<OtlpSpan, int, SpanWaterfallViewModel> createViewModel)
static void AddSelfAndChildren(List<SpanWaterfallViewModel> orderedSpans, OtlpSpan span, int depth, IOutgoingPeerResolver outgoingPeerResolver, Func<OtlpSpan, int, IOutgoingPeerResolver, SpanWaterfallViewModel> createViewModel)
{
orderedSpans.Add(createViewModel(span, depth));
orderedSpans.Add(createViewModel(span, depth, outgoingPeerResolver));
depth++;

foreach (var child in span.GetChildSpans().OrderBy(s => s.StartTime))
{
AddSelfAndChildren(orderedSpans, child, depth, createViewModel);
AddSelfAndChildren(orderedSpans, child, depth, outgoingPeerResolver, createViewModel);
}
}

static SpanWaterfallViewModel CreateViewModel(OtlpSpan span, int depth)
static SpanWaterfallViewModel CreateViewModel(OtlpSpan span, int depth, IOutgoingPeerResolver outgoingPeerResolver)
{
var traceStart = span.Trace.FirstSpan.StartTime;
var relativeStart = span.StartTime - traceStart;
Expand All @@ -82,6 +95,13 @@ static SpanWaterfallViewModel CreateViewModel(OtlpSpan span, int depth)
// A span may indicate a call to another service but the service isn't instrumented.
var hasPeerService = span.Attributes.Any(a => a.Key == OtlpSpan.PeerServiceAttributeKey);
var isUninstrumentedPeer = hasPeerService && span.Kind is OtlpSpanKind.Client or OtlpSpanKind.Producer && !span.GetChildSpans().Any();
var uninstrumentedPeer = isUninstrumentedPeer
? OtlpHelpers.GetValue(span.Attributes, OtlpSpan.PeerServiceAttributeKey)
: null;
if (uninstrumentedPeer != null)
{
uninstrumentedPeer = outgoingPeerResolver.ResolvePeerName(uninstrumentedPeer);
}

var viewModel = new SpanWaterfallViewModel
{
Expand All @@ -90,7 +110,7 @@ static SpanWaterfallViewModel CreateViewModel(OtlpSpan span, int depth)
Width = width,
Depth = depth,
LabelIsRight = labelIsRight,
UninstrumentedPeer = isUninstrumentedPeer ? OtlpHelpers.GetValue(span.Attributes, OtlpSpan.PeerServiceAttributeKey) : null
UninstrumentedPeer = uninstrumentedPeer
};
return viewModel;
}
Expand All @@ -111,7 +131,7 @@ private void UpdateDetailViewData()
_trace = TelemetryRepository.GetTrace(TraceId);
if (_trace != null)
{
_spanWaterfallViewModels = CreateSpanWaterfallViewModels(_trace);
_spanWaterfallViewModels = CreateSpanWaterfallViewModels(_trace, OutgoingPeerResolver);
_maxDepth = _spanWaterfallViewModels.Max(s => s.Depth);

if (_tracesSubscription is null || _tracesSubscription.ApplicationId != _trace.FirstSpan.Source.InstanceId)
Expand Down Expand Up @@ -169,6 +189,7 @@ private void ClearSelectedSpan()

public void Dispose()
{
_peerChangesSubscription?.Dispose();
_tracesSubscription?.Dispose();
}
}
1 change: 1 addition & 0 deletions src/Aspire.Dashboard/DashboardWebApplication.cs
Expand Up @@ -86,6 +86,7 @@ public DashboardWebApplication(ILogger<DashboardWebApplication> logger, Action<I
// OTLP services.
builder.Services.AddGrpc();
builder.Services.AddSingleton<TelemetryRepository>();
builder.Services.AddScoped<IOutgoingPeerResolver, ResourceOutgoingPeerResolver>();
builder.Services.AddTransient<StructuredLogsViewModel>();
builder.Services.AddTransient<TracesViewModel>();

Expand Down
10 changes: 10 additions & 0 deletions src/Aspire.Dashboard/Model/IOutgoingPeerResolver.cs
@@ -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);
}
2 changes: 1 addition & 1 deletion src/Aspire.Dashboard/Model/Otlp/SpanWaterfallViewModel.cs
Expand Up @@ -28,7 +28,7 @@ public string GetTooltip()
}
if (HasUninstrumentedPeer)
{
tooltip += Environment.NewLine + $"Outgoing call to peer {UninstrumentedPeer}";
tooltip += Environment.NewLine + $"Outgoing call to {UninstrumentedPeer}";
}

return tooltip;
Expand Down
133 changes: 133 additions & 0 deletions src/Aspire.Dashboard/Model/ResourceOutgoingPeerResolver.cs
@@ -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 () =>
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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)?

Copy link
Member Author

Choose a reason for hiding this comment

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

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)?

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)

{
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();
}
}
9 changes: 9 additions & 0 deletions src/Aspire.Dashboard/Model/ResourceViewModel.cs
Expand Up @@ -13,8 +13,17 @@ public abstract class ResourceViewModel
public List<EnvironmentVariableViewModel> Environment { get; } = new();
public required ILogSource LogSource { get; init; }
public List<string> Endpoints { get; } = new();
public List<ResourceService> Services { get; } = new();
public int? ExpectedEndpointsCount { get; init; }
public abstract string ResourceType { get; }
}

public sealed class ResourceService(string name, string? allocatedAddress, int? allocatedPort)
{
public string Name { get; } = name;
public string? AllocatedAddress { get; } = allocatedAddress;
public int? AllocatedPort { get; } = allocatedPort;
public string AddressAndPort { get; } = $"{allocatedAddress}:{allocatedPort}";
}

public sealed record NamespacedName(string Name, string? Namespace);