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 1 commit
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
28 changes: 19 additions & 9 deletions src/Aspire.Dashboard/Components/Pages/TraceDetail.razor.cs
Expand Up @@ -28,6 +28,9 @@ public partial class TraceDetail
[Inject]
public required TelemetryRepository TelemetryRepository { get; set; }

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

private ValueTask<GridItemsProviderResult<SpanWaterfallViewModel>> GetData(GridItemsProviderRequest<SpanWaterfallViewModel> request)
{
Debug.Assert(_spanWaterfallViewModels != null);
Expand All @@ -39,34 +42,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 +85,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 +100,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 +121,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
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.AddSingleton<IOutgoingPeerResolver, ResourceOutgoingPeerResolver>();
builder.Services.AddTransient<StructuredLogsViewModel>();
builder.Services.AddTransient<TracesViewModel>();

Expand Down
9 changes: 9 additions & 0 deletions src/Aspire.Dashboard/Model/IOutgoingPeerResolver.cs
@@ -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 src/Aspire.Dashboard/Model/ResourceOutgoingPeerResolver.cs
@@ -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 () =>
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))
{
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)
{
}
}
}
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);
25 changes: 25 additions & 0 deletions src/Aspire.Hosting/Dashboard/DashboardViewModelService.cs
Expand Up @@ -473,6 +473,31 @@ private async Task WriteExecutableChange(ExecutableViewModel executableViewModel
resourceViewModel.Endpoints.Add(endpointString);
}
}

var resourceApplicationModel = applicationModel.Resources.FirstOrDefault(r => r.Name == resourceViewModel.Name);
if (resourceApplicationModel?.TryGetAllocatedEndPoints(out var allocatedEndPoints) ?? false)
{
if (allocatedEndPoints.Count() == 1)
{
var service = services.FirstOrDefault(s => s.Metadata.Name == resourceViewModel.Name);
if (service != null)
{
resourceViewModel.Services.Add(new ResourceService(service.Metadata.Name, service.AllocatedAddress, service.AllocatedPort));
}
}
else
{
foreach (var allocatedEndPoint in allocatedEndPoints)
{
var serviceName = resourceApplicationModel.Name + "_" + allocatedEndPoint.Name;
JamesNK marked this conversation as resolved.
Show resolved Hide resolved
var service = services.FirstOrDefault(s => s.Metadata.Name == serviceName);
if (service != null)
{
resourceViewModel.Services.Add(new ResourceService(service.Metadata.Name, service.AllocatedAddress, service.AllocatedPort));
}
}
}
}
}

private static int? GetExpectedEndpointsCount(IEnumerable<Service> services, CustomResource resource)
Expand Down
2 changes: 1 addition & 1 deletion src/Aspire.Hosting/Dcp/DcpHostService.cs
Expand Up @@ -62,7 +62,7 @@ internal sealed partial class DcpHostService : IHostedLifecycleService, IAsyncDi
{
serviceCollection.AddSingleton(_applicationModel);
serviceCollection.AddSingleton(kubernetesService);
serviceCollection.AddScoped<IDashboardViewModelService, DashboardViewModelService>();
serviceCollection.AddSingleton<IDashboardViewModelService, DashboardViewModelService>();
JamesNK marked this conversation as resolved.
Show resolved Hide resolved
});
}
}
Expand Down