-
Notifications
You must be signed in to change notification settings - Fork 871
Fix OTLP endpoint resolution in isolated mode #16367
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
Changes from all commits
b26f16a
a373d6b
8fb978c
0446e07
a934c44
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| using Aspire.Hosting.ApplicationModel; | ||
| using Aspire.Hosting.Dcp.Model; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
|
|
||
| namespace Aspire.Hosting; | ||
|
|
@@ -68,9 +69,23 @@ private static void RegisterOtlpEnvironment(IResource resource, IConfiguration c | |
| return; | ||
| } | ||
|
|
||
| var (url, protocol) = OtlpEndpointResolver.ResolveOtlpEndpoint(configuration, otlpExporterAnnotation.RequiredProtocol); | ||
| context.EnvironmentVariables[KnownOtelConfigNames.ExporterOtlpEndpoint] = new HostUrl(url); | ||
| context.EnvironmentVariables[KnownOtelConfigNames.ExporterOtlpProtocol] = protocol; | ||
| var dashboardEndpoint = ResolveOtlpEndpointFromDashboard(context, otlpExporterAnnotation.RequiredProtocol); | ||
|
|
||
| if (dashboardEndpoint is not null) | ||
| { | ||
| // Use the dashboard endpoint reference directly. This resolves to the actual allocated URL, | ||
| // including when ports are randomized (e.g. isolated mode). | ||
| context.EnvironmentVariables[KnownOtelConfigNames.ExporterOtlpEndpoint] = dashboardEndpoint.Value.Endpoint; | ||
| context.EnvironmentVariables[KnownOtelConfigNames.ExporterOtlpProtocol] = dashboardEndpoint.Value.Protocol; | ||
| } | ||
| else | ||
| { | ||
| // Fall back to resolving from configuration. This is the case when the dashboard resource | ||
| // is not in the model (e.g. in tests or publish mode). | ||
| var (url, protocol) = OtlpEndpointResolver.ResolveOtlpEndpoint(configuration, otlpExporterAnnotation.RequiredProtocol); | ||
| context.EnvironmentVariables[KnownOtelConfigNames.ExporterOtlpEndpoint] = new HostUrl(url); | ||
| context.EnvironmentVariables[KnownOtelConfigNames.ExporterOtlpProtocol] = protocol; | ||
| } | ||
|
|
||
| // Set the service name and instance id to the resource name and UID. Values are injected by DCP. | ||
| context.EnvironmentVariables[KnownOtelConfigNames.ResourceAttributes] = "service.instance.id={{- index .Annotations \"" + CustomResource.OtelServiceInstanceIdAnnotation + "\" -}}"; | ||
|
|
@@ -148,4 +163,53 @@ public static IResourceBuilder<T> WithOtlpExporter<T>(this IResourceBuilder<T> b | |
|
|
||
| return builder; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Tries to resolve the OTLP endpoint from the dashboard resource in the distributed application model. | ||
| /// This ensures that when ports are randomized (e.g. isolated mode), resources use the actual | ||
| /// allocated endpoint rather than the statically configured port. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// The returned <see cref="EndpointReference"/> has no network context baked in, so it resolves | ||
| /// using the calling resource's network at evaluation time. This means containers automatically | ||
| /// get container-network URLs and non-containers get localhost URLs. | ||
| /// </remarks> | ||
| private static (EndpointReference Endpoint, string Protocol)? ResolveOtlpEndpointFromDashboard(EnvironmentCallbackContext context, OtlpProtocol? requiredProtocol) | ||
| { | ||
| DistributedApplicationModel? model; | ||
| try | ||
| { | ||
| model = context.ExecutionContext.ServiceProvider.GetService<DistributedApplicationModel>(); | ||
| } | ||
| catch (InvalidOperationException) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ??
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Take a look at |
||
| { | ||
| // ServiceProvider may not be available if the container hasn't been built yet | ||
| // (e.g. env var evaluation during testing without a fully built host). | ||
| return null; | ||
| } | ||
|
|
||
| if (model is null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| var dashboardResource = model.Resources.SingleOrDefault(r => string.Equals(r.Name, KnownResourceNames.AspireDashboard, StringComparisons.ResourceName)) as IResourceWithEndpoints; | ||
| if (dashboardResource is null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| var grpcEndpoint = dashboardResource.GetEndpoint(KnownEndpointNames.OtlpGrpcEndpointName); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason why this works and makes me happy is that
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's clean. |
||
| var httpEndpoint = dashboardResource.GetEndpoint(KnownEndpointNames.OtlpHttpEndpointName); | ||
|
|
||
| return (requiredProtocol, grpcEndpoint.Exists, httpEndpoint.Exists) switch | ||
| { | ||
| (OtlpProtocol.Grpc, true, _) => (grpcEndpoint, "grpc"), | ||
| (OtlpProtocol.HttpProtobuf, _, true) => (httpEndpoint, "http/protobuf"), | ||
| (OtlpProtocol.HttpJson, _, true) => (httpEndpoint, "http/json"), | ||
| (_, true, _) => (grpcEndpoint, "grpc"), | ||
| (_, _, true) => (httpEndpoint, "http/protobuf"), | ||
| _ => null | ||
| }; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about the otlp collector? Does this break that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a test. No it doesn't break it.