microsoft / microsoft/aspire

OTel resources duplicated if you quickly stop then restart app host

Open
#14,393 3 comments 0 reactions 0 assignees View on GitHub
area-app-model area-dashboard
Dominant language
C#
Stars
6.3k
Forks
991
Avg merge
2d 15h
Merged PRs (30d)
196

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

When I restart my app host, I often find that resources get doubled up in the resource selector.

I think what is happening is that you're getting the final telemetry from the previous app host run, alongside the new instances. (red shows previous instances shutting down, blue shows new instances starting)

Image

### Expected Behavior

I'd expect to only see resources from my current app host run, not the previous

One solution could be to randomise the telemetry port - that way the previous instance would send the dregs of it's telemetry to a completely different endpoint.

### Steps To Reproduce

I was using the below app host, based on the starter templates.

1. `aspire run`
2. ctrl+c
3. immediately `aspire run` again
4. Check the resouces in an otel drop down

If you instead give 10-20 secs between `ctrl+c` and the second `aspire run`, you won't see the duplicate resources.

```cs
using k8s;
using Microsoft.Extensions.DependencyInjection;

var builder = DistributedApplication.CreateBuilder(args);

var cache = builder.AddRedis("cache");

var apiService = builder.AddProject("apiservice")
.WithHttpHealthCheck("/health");

var project = builder.AddProject("webfrontend")
.WithExternalHttpEndpoints()
.WithHttpHealthCheck("/health")
.WithReference(cache)
.WaitFor(cache)
.WithReference(apiService)
.WaitFor(apiService);

apiService.WithReplicas(2);

apiService.GetEndpoint("http").WithPrometheusScraping();
project.GetEndpoint("http").WithPrometheusScraping();

var otel = builder.AddOpenTelemetryCollector("otel")
.WithConfig("otel.yml");

otel.OnBeforeResourceStarted(async (resource, evt, ct) =>
{
var store = evt.Services.GetRequiredService();
var path = Path.Combine(store.BasePath, $"{resource.Name}-prometheus-sd.json");
await File.WriteAllTextAsync(path, "[]");

});

builder.Build().Run();
```

### Exceptions (if any)

_No response_

### .NET Version info

_No response_

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.