dotnet / aspire

An opinionated, cloud ready stack for building observable, production ready, distributed applications in .NET
https://learn.microsoft.com/dotnet/aspire
MIT License
3.44k stars 362 forks source link

Add origin labels to containers #4425

Open afscrome opened 1 month ago

afscrome commented 1 month ago

Would it be useful to add some origin labels to containers spun up by Aspire to help identify where they came form? (e.g. the project name & the resource name). Whilst I imagine this wouldn't be of much help in regular use, it could be of help in diagnostic scenarios to know where containers came from.

These labels could also be used with the "long running" container idea been thrown around, in which you can identify if an existing isntance is running by looking for containers with these labels

For example, the following could be done in IDistributedApplicationLifecycleHook.BeforeStartAsync

var projectLabel = $"net.dot.aspire.project={Assembly.GetEntryAssembly()?.GetName().Name}";

foreach (var container in appModel.GetContainerResources())
{
    var resourceLabel = $"net.dot.aspire.resource={container.Name}";
    container.Annotations.Add(new ContainerRunArgsCallbackAnnotation(x => {
        x.Add($"--label");
        x.Add(projectLabel);
        x.Add($"--label");
        x.Add(resourceLabel);
    }));
}

(For context, this idea comes out of https://github.com/dotnet/aspire/issues/4066#issuecomment-2155877090 )

danegsta commented 4 weeks ago

Support for this in DCP has been merged to main