-
Notifications
You must be signed in to change notification settings - Fork 183
Use WithHttpsCertificateConfiguration to configure tls cert for Otel Collector
#1115
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
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -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 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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) | ||||||||||
| { | ||||||||||
|
|
@@ -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
|
||||||||||
| 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
AI
Jan 20, 2026
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.
Default 'ToString()': ReferenceExpression inherits 'ToString()' from 'Object', and so is not suitable for printing.
Copilot
AI
Jan 20, 2026
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.
Remove trailing whitespace at the end of this line. The coding style guidelines require clean formatting without unnecessary whitespace.
| } | |
| } |
This file was deleted.
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.
The variable 'collector' is assigned but never used. If this is meant to demonstrate capturing the resource builder, consider adding a comment explaining its purpose. Otherwise, remove the variable assignment and keep the fluent chain without assignment.