Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

builder.AddProject<Projects.CommunityToolkit_Aspire_Hosting_OpenTelemetryCollector_Api>("api");

builder.AddOpenTelemetryCollector("opentelemetry-collector")
var collector = builder.AddOpenTelemetryCollector("opentelemetry-collector")
.WithAppForwarding()
.WithConfig("./config.yaml");

builder.Build().Run();

Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,4 @@
<PackageReference Include="Aspire.Hosting" />
</ItemGroup>

<ItemGroup>
<Compile Include="$(SharedDir)\DevCertHostingExtensions.cs" Link="Utils\DevCertHostingExtensions.cs" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -34,42 +34,20 @@ public static IResourceBuilder<OpenTelemetryCollectorResource> AddOpenTelemetryC
var settings = new OpenTelemetryCollectorSettings();
configureSettings?.Invoke(settings);

var isHttpsEnabled = !settings.ForceNonSecureReceiver && url.StartsWith("https", StringComparison.OrdinalIgnoreCase);

var resource = new OpenTelemetryCollectorResource(name);
var resourceBuilder = builder.AddResource(resource)
.WithImage(settings.CollectorImage, settings.CollectorTag)
.WithEnvironment("ASPIRE_ENDPOINT", new HostUrl(url))
.WithEnvironment("ASPIRE_API_KEY", builder.Configuration[DashboardOtlpApiKeyVariableName])
.WithIconName("DesktopPulse");

if (settings.EnableGrpcEndpoint)
resourceBuilder.WithEndpoint(targetPort: 4317, name: OpenTelemetryCollectorResource.GrpcEndpointName, scheme: isHttpsEnabled ? "https" : "http");
if (settings.EnableHttpEndpoint)
resourceBuilder.WithEndpoint(targetPort: 4318, name: OpenTelemetryCollectorResource.HttpEndpointName, scheme: isHttpsEnabled ? "https" : "http");


if (!settings.ForceNonSecureReceiver && isHttpsEnabled && builder.ExecutionContext.IsRunMode)
{
resourceBuilder.RunWithHttpsDevCertificate();
var useHttpsForReceivers = !settings.ForceNonSecureReceiver && url.StartsWith("https", StringComparison.OrdinalIgnoreCase);

// Not using `Path.Combine` as we MUST use unix style paths in the container
var certFilePath = $"{DevCertHostingExtensions.DEV_CERT_BIND_MOUNT_DEST_DIR}/{DevCertHostingExtensions.CERT_FILE_NAME}";
var certKeyPath = $"{DevCertHostingExtensions.DEV_CERT_BIND_MOUNT_DEST_DIR}/{DevCertHostingExtensions.CERT_KEY_FILE_NAME}";
if (settings.EnableGrpcEndpoint)
ConfigureReceiver(4317, OpenTelemetryCollectorResource.GrpcEndpointName);

if (settings.EnableHttpEndpoint)
{
resourceBuilder.WithArgs(
$@"--config=yaml:receivers::otlp::protocols::http::tls::cert_file: ""{certFilePath}""",
$@"--config=yaml:receivers::otlp::protocols::http::tls::key_file: ""{certKeyPath}""");
}
if (settings.EnableGrpcEndpoint)
{
resourceBuilder.WithArgs(
$@"--config=yaml:receivers::otlp::protocols::grpc::tls::cert_file: ""{certFilePath}""",
$@"--config=yaml:receivers::otlp::protocols::grpc::tls::key_file: ""{certKeyPath}""");
}
}
if (settings.EnableHttpEndpoint)
ConfigureReceiver(4318, OpenTelemetryCollectorResource.HttpEndpointName);

if (!settings.DisableHealthcheck)
{
Expand All @@ -83,6 +61,26 @@ public static IResourceBuilder<OpenTelemetryCollectorResource> AddOpenTelemetryC
);
}
return resourceBuilder;

void ConfigureReceiver(int port, string protocol)
{
var scheme = useHttpsForReceivers ? "https" : "http";
resourceBuilder.WithEndpoint(targetPort: port, name: protocol, scheme: scheme);

if (!useHttpsForReceivers)
{
return;
}

#pragma warning disable ASPIRECERTIFICATES001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
resourceBuilder.WithHttpsCertificateConfiguration(ctx =>
{
ctx.Arguments.Add(ReferenceExpression.Create($@"--config=yaml:receivers::otlp::protocols::{protocol}::tls::cert_file: ""{ctx.CertificatePath}"""));
ctx.Arguments.Add(ReferenceExpression.Create($@"--config=yaml:receivers::otlp::protocols::{protocol}::tls::key_file: ""{ctx.KeyPath}"""));
Comment on lines +78 to +79
Copy link

Copilot AI Dec 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default 'ToString()': ReferenceExpression inherits 'ToString()' from 'Object', and so is not suitable for printing.

Suggested change
ctx.Arguments.Add(ReferenceExpression.Create($@"--config=yaml:receivers::otlp::protocols::{protocol}::tls::cert_file: ""{ctx.CertificatePath}"""));
ctx.Arguments.Add(ReferenceExpression.Create($@"--config=yaml:receivers::otlp::protocols::{protocol}::tls::key_file: ""{ctx.KeyPath}"""));
ctx.Arguments.Add($@"--config=yaml:receivers::otlp::protocols::{protocol}::tls::cert_file: ""{ctx.CertificatePath}""");
ctx.Arguments.Add($@"--config=yaml:receivers::otlp::protocols::{protocol}::tls::key_file: ""{ctx.KeyPath}""");

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReferenceExpression is absolutely required here as certificate and key paths cannot be resolve until after the app host starts.

Comment on lines +78 to +79
Copy link

Copilot AI Dec 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default 'ToString()': ReferenceExpression inherits 'ToString()' from 'Object', and so is not suitable for printing.

Suggested change
ctx.Arguments.Add(ReferenceExpression.Create($@"--config=yaml:receivers::otlp::protocols::{protocol}::tls::cert_file: ""{ctx.CertificatePath}"""));
ctx.Arguments.Add(ReferenceExpression.Create($@"--config=yaml:receivers::otlp::protocols::{protocol}::tls::key_file: ""{ctx.KeyPath}"""));
ctx.Arguments.Add($@"--config=yaml:receivers::otlp::protocols::{protocol}::tls::cert_file: ""{ctx.CertificatePath}""");
ctx.Arguments.Add($@"--config=yaml:receivers::otlp::protocols::{protocol}::tls::key_file: ""{ctx.KeyPath}""");

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReferenceExpression is absolutely required here as certificate and key paths cannot be resolve until after the app host starts.

return Task.CompletedTask;
});
#pragma warning restore ASPIRECERTIFICATES001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
}
Copy link

Copilot AI Dec 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is trailing whitespace after the closing brace. This should be removed for consistent code formatting.

Suggested change
}
}

Copilot uses AI. Check for mistakes.
}

/// <summary>
Expand Down Expand Up @@ -123,5 +121,4 @@ public static IResourceBuilder<OpenTelemetryCollectorResource> WithConfig(this I
return builder.WithBindMount(configPath, $"/config/{configFileInfo.Name}")
.WithArgs($"--config=/config/{configFileInfo.Name}");
}

}
152 changes: 0 additions & 152 deletions src/Shared/DevCertHostingExtensions.cs

This file was deleted.

Loading
Loading