Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
19 changes: 19 additions & 0 deletions src/Aspire.Cli/Commands/RootCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ public RootCommand(NewCommand newCommand, RunCommand runCommand, AddCommand addC
waitForDebuggerOption.Recursive = true;
waitForDebuggerOption.DefaultValueFactory = (result) => false;

var otelOption = new Option<string>("--cli-otlp-endpoint");
otelOption.Description = "Send telemetry to the OpenTelemetry collector";
otelOption.Validators.Add((result) => {
var otelUrl = result.GetValueOrDefault<string>();

if (Uri.TryCreate(otelUrl, UriKind.Absolute, out var otelUri) && (otelUri.Scheme == Uri.UriSchemeHttp || otelUri.Scheme == Uri.UriSchemeHttps))
{
// If we have a valid URL then we can just return.
return;
}
else
{
result.AddError("The OpenTelemetry URL must be an absolute URI with either HTTP or HTTPS scheme.");
}
});
otelOption.Recursive = true;
otelOption.Hidden = true; // Hiding for now since this could be confusing about what it does.
Copy link
Member

Choose a reason for hiding this comment

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

should we obfuscate the name? or --cli-otlp-endpoint-experimental-only ?

Options.Add(otelOption);

#if DEBUG
waitForDebuggerOption.Validators.Add((result) => {

Expand Down
21 changes: 16 additions & 5 deletions src/Aspire.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using Spectre.Console;
using RootCommand = Aspire.Cli.Commands.RootCommand;

namespace Aspire.Cli;
Expand Down Expand Up @@ -49,12 +50,22 @@ private static IHost BuildApplication(string[] args)
tracing.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("aspire-cli"));
});

if (builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"] is {})
// NOTE: Actual arg validation will take place elsewhere, this is just
// a quick check to make sure that DI is configured correctly.
if (args.IndexOf("--cli-otlp-endpoint") is var otelArgIndex && otelArgIndex != -1)
{
// NOTE: If we always enable the OTEL exporter it dramatically
// impacts the CLI in terms of exiting quickly because it
// has to finish sending telemetry.
otelBuilder.UseOtlpExporter();
// If we have an --cli-otlp-endpoint switch then we need to check that we
// have at least enough args to have a base URL.
if (args.Length == otelArgIndex + 2)
{
var baseUrl = args[otelArgIndex + 1];

// If we have enough arguments we need to make sure its a valid URL.
if (Uri.TryCreate(baseUrl, UriKind.Absolute, out var baseUri) && (baseUri.Scheme == Uri.UriSchemeHttp || baseUri.Scheme == Uri.UriSchemeHttps))
{
otelBuilder.UseOtlpExporter(OpenTelemetry.Exporter.OtlpExportProtocol.Grpc, baseUri);
}
}
}

var debugMode = args?.Any(a => a == "--debug" || a == "-d") ?? false;
Expand Down
Loading