Skip to content

chore: logging messages to use consistent placeholder names for resources #9427

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/Aspire.Hosting/ApplicationModel/ResourceNotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public async Task<string> WaitForResourceAsync(string resourceName, IEnumerable<
{
if (_logger.IsEnabled(LogLevel.Debug))
{
_logger.LogDebug("Waiting for resource '{Name}' to enter one of the target state: {TargetStates}", resourceName, string.Join(", ", targetStates));
_logger.LogDebug("Waiting for resource '{ResourceName}' to enter one of the target state: {TargetStates}", resourceName, string.Join(", ", targetStates));
}

using var watchCts = CancellationTokenSource.CreateLinkedTokenSource(_disposing.Token, cancellationToken);
Expand All @@ -131,7 +131,7 @@ public async Task<string> WaitForResourceAsync(string resourceName, IEnumerable<
&& resourceEvent.Snapshot.State?.Text is { Length: > 0 } stateText
&& targetStates.Contains(stateText, StringComparers.ResourceState))
{
_logger.LogDebug("Finished waiting for resource '{Name}'. Resource state is '{State}'.", resourceName, stateText);
_logger.LogDebug("Finished waiting for resource '{ResourceName}'. Resource state is '{State}'.", resourceName, stateText);
return stateText;
}
}
Expand All @@ -142,7 +142,7 @@ public async Task<string> WaitForResourceAsync(string resourceName, IEnumerable<
private async Task WaitUntilHealthyAsync(IResource resource, IResource dependency, WaitBehavior waitBehavior, CancellationToken cancellationToken)
{
var resourceLogger = _resourceLoggerService.GetLogger(resource);
resourceLogger.LogInformation("Waiting for resource '{Name}' to enter the '{State}' state.", dependency.Name, KnownResourceStates.Running);
resourceLogger.LogInformation("Waiting for resource '{ResourceName}' to enter the '{State}' state.", dependency.Name, KnownResourceStates.Running);
await PublishUpdateAsync(resource, s => s with { State = KnownResourceStates.Waiting }).ConfigureAwait(false);

var names = dependency.GetResolvedResourceNames();
Expand Down Expand Up @@ -192,18 +192,18 @@ async Task Core(string displayName, string resourceId)
// otherwise we don't care about their health status.
if (dependency.TryGetAnnotationsOfType<HealthCheckAnnotation>(out var _))
{
resourceLogger.LogInformation("Waiting for resource '{Name}' to become healthy.", displayName);
resourceLogger.LogInformation("Waiting for resource '{ResourceName}' to become healthy.", displayName);
await WaitForResourceCoreAsync(dependency.Name, re => re.ResourceId == resourceId && re.Snapshot.HealthStatus == HealthStatus.Healthy, cancellationToken).ConfigureAwait(false);
}

// Now wait for the resource ready event to be executed.
resourceLogger.LogInformation("Waiting for resource ready to execute for '{Name}'.", displayName);
resourceLogger.LogInformation("Waiting for resource ready to execute for '{ResourceName}'.", displayName);
resourceEvent = await WaitForResourceCoreAsync(dependency.Name, re => re.ResourceId == resourceId && re.Snapshot.ResourceReadyEvent is not null, cancellationToken: cancellationToken).ConfigureAwait(false);

// Observe the result of the resource ready event task
await resourceEvent.Snapshot.ResourceReadyEvent!.EventTask.WaitAsync(cancellationToken).ConfigureAwait(false);

resourceLogger.LogInformation("Finished waiting for resource '{Name}'.", displayName);
resourceLogger.LogInformation("Finished waiting for resource '{ResourceName}'.", displayName);

static bool IsContinuableState(WaitBehavior waitBehavior, CustomResourceSnapshot snapshot) =>
waitBehavior switch
Expand Down Expand Up @@ -273,7 +273,7 @@ public async Task<ResourceEvent> WaitForResourceHealthyAsync(string resourceName
/// </remarks>
public async Task<ResourceEvent> WaitForResourceHealthyAsync(string resourceName, WaitBehavior waitBehavior, CancellationToken cancellationToken = default)
{
_logger.LogDebug("Waiting for resource '{Name}' to enter the '{State}' state.", resourceName, HealthStatus.Healthy);
_logger.LogDebug("Waiting for resource '{ResourceName}' to enter the '{State}' state.", resourceName, HealthStatus.Healthy);
var resourceEvent = await WaitForResourceCoreAsync(resourceName, re => ShouldYield(waitBehavior, re.Snapshot), cancellationToken: cancellationToken).ConfigureAwait(false);

if (resourceEvent.Snapshot.HealthStatus != HealthStatus.Healthy)
Expand All @@ -282,7 +282,7 @@ public async Task<ResourceEvent> WaitForResourceHealthyAsync(string resourceName
throw new DistributedApplicationException($"Stopped waiting for resource '{resourceName}' to become healthy because it failed to start.");
}

_logger.LogDebug("Finished waiting for resource '{Name}'.", resourceName);
_logger.LogDebug("Finished waiting for resource '{ResourceName}'.", resourceName);

return resourceEvent;

Expand All @@ -306,7 +306,7 @@ private async Task WaitUntilCompletionAsync(IResource resource, IResource depend
var tasks = new Task[names.Length];

var resourceLogger = _resourceLoggerService.GetLogger(resource);
resourceLogger.LogInformation("Waiting for resource '{Name}' to complete.", dependency.Name);
resourceLogger.LogInformation("Waiting for resource '{ResourceName}' to complete.", dependency.Name);

await PublishUpdateAsync(resource, s => s with { State = KnownResourceStates.Waiting }).ConfigureAwait(false);

Expand Down Expand Up @@ -347,7 +347,7 @@ async Task Core(string displayName, string resourceId)
);
}

resourceLogger.LogInformation("Finished waiting for resource '{Name}'.", displayName);
resourceLogger.LogInformation("Finished waiting for resource '{ResourceName}'.", displayName);

static bool IsKnownTerminalState(CustomResourceSnapshot snapshot) =>
KnownResourceStates.TerminalStates.Contains(snapshot.State?.Text) ||
Expand Down Expand Up @@ -406,9 +406,9 @@ public async Task WaitForDependenciesAsync(IResource resource, CancellationToken
Justification = "predicate and targetState(s) parameters are mutually exclusive.")]
public async Task<ResourceEvent> WaitForResourceAsync(string resourceName, Func<ResourceEvent, bool> predicate, CancellationToken cancellationToken = default)
{
_logger.LogDebug("Waiting for resource '{Name}' to match predicate.", resourceName);
_logger.LogDebug("Waiting for resource '{ResourceName}' to match predicate.", resourceName);
var resourceEvent = await WaitForResourceCoreAsync(resourceName, predicate, cancellationToken).ConfigureAwait(false);
_logger.LogDebug("Finished waiting for resource '{Name}'.", resourceName);
_logger.LogDebug("Finished waiting for resource '{ResourceName}'.", resourceName);

return resourceEvent;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Aspire.Hosting/Backchannel/AppHostRpcTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal class AppHostRpcTarget(
PublishingActivityProgressReporter activityReporter,
IHostApplicationLifetime lifetime,
DistributedApplicationOptions options
)
)
{
public async IAsyncEnumerable<(string Id, string StatusText, bool IsComplete, bool IsError)> GetPublishingActivitiesAsync([EnumeratorCancellation]CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -66,18 +66,18 @@ public async IAsyncEnumerable<RpcResourceState> GetResourceStatesAsync([Enumerat

if (!resourceEvent.Resource.TryGetEndpoints(out var endpoints))
{
logger.LogTrace("Resource {Resource} does not have endpoints.", resourceEvent.Resource.Name);
logger.LogTrace("Resource {ResourceName} does not have endpoints.", resourceEvent.Resource.Name);
endpoints = Enumerable.Empty<EndpointAnnotation>();
}

var endpointUris = endpoints
.Where(e => e.AllocatedEndpoint != null)
.Select(e => e.AllocatedEndpoint!.UriString)
.ToArray();

// Compute health status
var healthStatus = CustomResourceSnapshot.ComputeHealthStatus(resourceEvent.Snapshot.HealthReports, resourceEvent.Snapshot.State?.Text);

yield return new RpcResourceState
{
Resource = resourceEvent.Resource.Name,
Expand Down Expand Up @@ -134,7 +134,7 @@ await resourceNotificationService.WaitForResourceHealthyAsync(
if (!StringUtils.TryGetUriFromDelimitedString(dashboardOptions.Value.DashboardUrl, ";", out var dashboardUri))
{
logger.LogWarning("Dashboard URL could not be parsed from dashboard options.");
throw new InvalidOperationException("Dashboard URL could not be parsed from dashboard options.");
throw new InvalidOperationException("Dashboard URL could not be parsed from dashboard options.");
}

var codespacesUrlRewriter = serviceProvider.GetService<CodespacesUrlRewriter>();
Expand Down Expand Up @@ -178,4 +178,4 @@ public Task<string[]> GetCapabilitiesAsync(CancellationToken cancellationToken)
});
}
#pragma warning restore CA1822
}
}
30 changes: 15 additions & 15 deletions src/Aspire.Hosting/Health/ResourceHealthCheckService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
return;
}

logger.LogError(ex, "Unexpected error ended health monitoring for resource '{Resource}'.", resourceName);
logger.LogError(ex, "Unexpected error ended health monitoring for resource '{ResourceName}'.", resourceName);
}
}, state.CancellationToken);
}
Expand Down Expand Up @@ -112,14 +112,14 @@ private async Task MonitorResourceHealthAsync(ResourceMonitorState state)
// dynamically add health checks at runtime. If this changes then we
// would need to revisit this and scan for transitive health checks
// on a periodic basis (you wouldn't want to do it on every pass.
logger.LogDebug("Resource '{Resource}' has no health checks to monitor.", resource.Name);
logger.LogDebug("Resource '{ResourceName}' has no health checks to monitor.", resource.Name);
FireResourceReadyEvent(resource, cancellationToken);

return;
}

var registrationKeysToCheck = annotations.DistinctBy(a => a.Key).Select(a => a.Key).ToFrozenSet();
logger.LogDebug("Resource '{Resource}' health checks to monitor: {HeathCheckKeys}", resource.Name, string.Join(", ", registrationKeysToCheck));
logger.LogDebug("Resource '{ResourceName}' health checks to monitor: {HeathCheckKeys}", resource.Name, string.Join(", ", registrationKeysToCheck));

var lastHealthCheckTimestamp = 0L;
var lastDelayInterrupted = false;
Expand Down Expand Up @@ -156,7 +156,7 @@ private async Task MonitorResourceHealthAsync(ResourceMonitorState state)
report = new HealthReport(registrationKeysToCheck.ToDictionary(k => k, k => new HealthReportEntry(HealthStatus.Unhealthy, "Error calling HealthCheckService.", TimeSpan.Zero, ex, data: null)), TimeSpan.Zero);
}

logger.LogTrace("Health report status for '{Resource}' is {HealthReportStatus}.", resource.Name, report.Status);
logger.LogTrace("Health report status for '{ResourceName}' is {HealthReportStatus}.", resource.Name, report.Status);

if (report.Status == HealthStatus.Healthy)
{
Expand All @@ -176,7 +176,7 @@ private async Task MonitorResourceHealthAsync(ResourceMonitorState state)

if (ContainsAnyHealthReportChange(report, currentEvent.Snapshot.HealthReports))
{
logger.LogTrace("Health reports for '{Resource}' have changed. Publishing updated reports.", resource.Name);
logger.LogTrace("Health reports for '{ResourceName}' have changed. Publishing updated reports.", resource.Name);

await resourceNotificationService.PublishUpdateAsync(resource, s =>
{
Expand All @@ -197,7 +197,7 @@ await resourceNotificationService.PublishUpdateAsync(resource, s =>
// cancellation token is signaled.
logger.LogTrace(
ex,
"Health check monitoring loop for resource '{Resource}' observed exception but was ignored.",
"Health check monitoring loop for resource '{ResourceName}' observed exception but was ignored.",
resource.Name
);
}
Expand All @@ -210,7 +210,7 @@ await resourceNotificationService.PublishUpdateAsync(resource, s =>
? HealthyHealthCheckInterval
: NonHealthyHealthCheckStepInterval * Math.Min(5, nonHealthyReportCount);

logger.LogTrace("Resource '{Resource}' health check monitoring loop starting delay of {DelayInterval}.", resource.Name, delayInterval);
logger.LogTrace("Resource '{ResourceName}' health check monitoring loop starting delay of {DelayInterval}.", resource.Name, delayInterval);

lastDelayInterrupted = await state.DelayAsync(currentEvent, delayInterval, cancellationToken).ConfigureAwait(false);
}
Expand Down Expand Up @@ -239,26 +239,26 @@ private static bool ContainsAnyHealthReportChange(HealthReport report, Immutable

private void FireResourceReadyEvent(IResource resource, CancellationToken cancellationToken)
{
logger.LogDebug("Resource '{Resource}' is ready.", resource.Name);
logger.LogDebug("Resource '{ResourceName}' is ready.", resource.Name);

// We don't want to block the monitoring loop while we fire the event.
_ = Task.Run(async () =>
{
var resourceReadyEvent = new ResourceReadyEvent(resource, services);

logger.LogDebug("Publishing ResourceReadyEvent for '{Resource}'.", resource.Name);
logger.LogDebug("Publishing ResourceReadyEvent for '{ResourceName}'.", resource.Name);

// Execute the publish and store the task so that waiters can await it and observe the result.
var task = eventing.PublishAsync(resourceReadyEvent, cancellationToken);

logger.LogDebug("Waiting for ResourceReadyEvent for '{Resource}'.", resource.Name);
logger.LogDebug("Waiting for ResourceReadyEvent for '{ResourceName}'.", resource.Name);

// Suppress exceptions, we just want to make sure that the event is completed.
await task.ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing);

logger.LogDebug("ResourceReadyEvent for '{Resource}' completed.", resource.Name);
logger.LogDebug("ResourceReadyEvent for '{ResourceName}' completed.", resource.Name);

logger.LogDebug("Publishing the result of ResourceReadyEvent for '{Resource}'.", resource.Name);
logger.LogDebug("Publishing the result of ResourceReadyEvent for '{ResourceName}'.", resource.Name);

await resourceNotificationService.PublishUpdateAsync(resource, s => s with
{
Expand Down Expand Up @@ -315,7 +315,7 @@ public ResourceMonitorState(ILogger logger, ResourceEvent initialEvent, Cancella
_resourceName = initialEvent.Resource.Name;
LatestEvent = initialEvent;

_logger.LogDebug("Starting health monitoring for resource '{Resource}'.", _resourceName);
_logger.LogDebug("Starting health monitoring for resource '{ResourceName}'.", _resourceName);
}

// Used to cancel and exit the monitoring loop for a resource.
Expand All @@ -325,7 +325,7 @@ public ResourceMonitorState(ILogger logger, ResourceEvent initialEvent, Cancella

public void StopResourceMonitor()
{
_logger.LogDebug("Stopping health monitoring for resource '{Resource}'.", _resourceName);
_logger.LogDebug("Stopping health monitoring for resource '{ResourceName}'.", _resourceName);
_cts.Cancel();
}

Expand Down Expand Up @@ -353,7 +353,7 @@ internal async Task<bool> DelayAsync(ResourceEvent? currentEvent, TimeSpan delay
// The event might have changed before delay was called. Interrupt immediately if required.
if (currentEvent != null && ShouldInterrupt(currentEvent, LatestEvent))
{
_logger.LogTrace("Health monitoring delay interrupted for resource '{Resource}'.", _resourceName);
_logger.LogTrace("Health monitoring delay interrupted for resource '{ResourceName}'.", _resourceName);
return true;
}
_delayInterruptTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal sealed class ResourceContainerImageBuilder(
{
public async Task BuildImageAsync(IResource resource, CancellationToken cancellationToken)
{
logger.LogInformation("Building container image for resource {Resource}", resource.Name);
logger.LogInformation("Building container image for resource {ResourceName}", resource.Name);

if (resource is ProjectResource)
{
Expand Down Expand Up @@ -189,4 +189,4 @@ await activityReporter.UpdateActivityStatusAsync(
}

}
}
}
Loading