Skip to content
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

Update metrics sample to use OTLP and OTEL collector #606

Merged
merged 9 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion samples/Metrics/MetricsApp.AppHost/MetricsApp.AppHost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="..\..\Shared\DevCertHostingExtensions.cs" Link="DevCertHostingExtensions.cs" />

<PackageReference Include="Aspire.Hosting.AppHost" Version="9.0.0" />

<ProjectReference Include="..\MetricsApp\MetricsApp.csproj" />
<ProjectReference Include="..\MetricsApp\MetricsApp.csproj" />
DamianEdwards marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace MetricsApp.AppHost.OpenTelemetryCollector;
using Microsoft.Extensions.Hosting;

namespace MetricsApp.AppHost.OpenTelemetryCollector;

public static class OpenTelemetryCollectorResourceBuilderExtensions
{
Expand All @@ -11,18 +13,33 @@ public static IResourceBuilder<OpenTelemetryCollectorResource> AddOpenTelemetryC
builder.AddOpenTelemetryCollectorInfrastructure();

var url = builder.Configuration[DashboardOtlpUrlVariableName] ?? DashboardOtlpUrlDefaultValue;
var isHttpsEnabled = url.StartsWith("https", StringComparison.OrdinalIgnoreCase);

var dashboardOtlpEndpoint = new HostUrl(url);
var dashboardInsecure = url.StartsWith("https", StringComparison.OrdinalIgnoreCase) ? "false" : "true";

var resource = new OpenTelemetryCollectorResource(name);
return builder.AddResource(resource)
var resourceBuilder = builder.AddResource(resource)
.WithImage("ghcr.io/open-telemetry/opentelemetry-collector-releases/opentelemetry-collector-contrib", "latest")
.WithEndpoint(targetPort: 4317, name: OpenTelemetryCollectorResource.OtlpGrpcEndpointName, scheme: "http")
.WithEndpoint(targetPort: 4318, name: OpenTelemetryCollectorResource.OtlpHttpEndpointName, scheme: "http")
.WithEndpoint(targetPort: 4317, name: OpenTelemetryCollectorResource.OtlpGrpcEndpointName, scheme: isHttpsEnabled ? "https" : "http")
.WithEndpoint(targetPort: 4318, name: OpenTelemetryCollectorResource.OtlpHttpEndpointName, scheme: isHttpsEnabled ? "https" : "http")
.WithBindMount(configFileLocation, "/etc/otelcol-contrib/config.yaml")
.WithEnvironment("ASPIRE_ENDPOINT", $"{dashboardOtlpEndpoint}")
.WithEnvironment("ASPIRE_API_KEY", builder.Configuration[DashboardOtlpApiKeyVariableName])
.WithEnvironment("ASPIRE_INSECURE", dashboardInsecure);
.WithEnvironment("ASPIRE_INSECURE", isHttpsEnabled ? "false" : "true");

if (isHttpsEnabled && builder.ExecutionContext.IsRunMode && builder.Environment.IsDevelopment())
{
DevCertHostingExtensions.RunWithHttpsDevCertificate(resourceBuilder, "HTTPS_CERT_FILE", "HTTPS_CERT_KEY_FILE", (certFilePath, certKeyPath) =>
{
// Set TLS details using YAML path via the command line. This allows the values to be added to the existing config file.
// Setting the values in the config file doesn't work because adding the "tls" section always enables TLS, even if there is no cert provided.
resourceBuilder.WithArgs(
@"--config=yaml:receivers::otlp::protocols::grpc::tls::cert_file: ""dev-certs/dev-cert.pem""",
@"--config=yaml:receivers::otlp::protocols::grpc::tls::key_file: ""dev-certs/dev-cert.key""",
@"--config=/etc/otelcol-contrib/config.yaml");
});
}

return resourceBuilder;
}
}
2 changes: 1 addition & 1 deletion samples/Metrics/otelcollector/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ exporters:
x-otlp-api-key: ${env:ASPIRE_API_KEY}
tls:
insecure: ${env:ASPIRE_INSECURE}
insecure_skip_verify: true
insecure_skip_verify: true # Required in local development because cert is localhost and the endpoint is host.docker.internal
otlphttp/prometheus:
endpoint: ${env:PROMETHEUS_ENDPOINT}
tls:
Expand Down
4 changes: 2 additions & 2 deletions samples/Shared/DevCertHostingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public static IResourceBuilder<TResource> RunWithHttpsDevCertificate<TResource>(

var bindSource = Path.GetDirectoryName(certPath) ?? throw new UnreachableException();

var certFileDest = Path.Combine(DEV_CERT_BIND_MOUNT_DEST_DIR, certFileName);
var certKeyFileDest = Path.Combine(DEV_CERT_BIND_MOUNT_DEST_DIR, certKeyFileName);
var certFileDest = $"{DEV_CERT_BIND_MOUNT_DEST_DIR}/{certFileName}";
var certKeyFileDest = $"{DEV_CERT_BIND_MOUNT_DEST_DIR}/{certKeyFileName}";
Comment on lines -49 to +50
Copy link
Member Author

@JamesNK JamesNK Dec 5, 2024

Choose a reason for hiding this comment

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

FYI Path.Combine would add backslash when app host is run on Windows.

e.g. /dev-cert\dev-cert.pem

The OpenTelemetry collector errored when given the bad path. Seems best to hardcode to forward slash.


builder.ApplicationBuilder.CreateResourceBuilder(containerResource)
.WithBindMount(bindSource, DEV_CERT_BIND_MOUNT_DEST_DIR, isReadOnly: true)
Expand Down
Loading