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 environment variable handling #128

Merged
merged 5 commits into from
Apr 26, 2024
Merged
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
Prev Previous commit
Next Next commit
Add more options
rafaelldi committed Apr 26, 2024

Verified

This commit was signed with the committer’s verified signature.
commit b8ccd65bfacbc1874570f7054aeb8d74d5fe0e48
4 changes: 1 addition & 3 deletions src/dotnet/aspire-session-host/Connection.cs
Original file line number Diff line number Diff line change
@@ -16,9 +16,7 @@ internal sealed class Connection : IDisposable

internal Connection(ConfigurationManager configuration)
{
var connectionOptions = configuration
.GetSection(ConnectionOptions.SectionName)
.Get<ConnectionOptions>();
var connectionOptions = configuration.GetSection(ConnectionOptions.SectionName).Get<ConnectionOptions>();
if (connectionOptions?.RdPort == null)
throw new ApplicationException("Unable to find RD port environment variable");

17 changes: 1 addition & 16 deletions src/dotnet/aspire-session-host/EnvironmentVariables.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,16 @@
using System.Globalization;

namespace AspireSessionHost;
namespace AspireSessionHost;

internal static class EnvironmentVariables
{
//This variable is used to configure the endpoint to interact with DCP to create, delete session, etc.
internal const string AspNetCoreUrls = "ASPNETCORE_URLS";
internal static Uri? GetAspNetCoreUrls() => GetUrlFromEnvironment(AspNetCoreUrls);

//This variable is used to configure OpenTelemetry Protocol gRPC endpoint to receive telemetry data from the child
//projects.
private const string OtlpServerPort = "RIDER_OTLP_SERVER_PORT";
internal static int? GetOtlpServerPort() => GetPortFromEnvironment(OtlpServerPort);

private static Uri? GetUrlFromEnvironment(string variableName)
{
var variable = Environment.GetEnvironmentVariable(variableName);
if (string.IsNullOrEmpty(variable)) return null;
Uri.TryCreate(variable, UriKind.Absolute, out var url);
return url;
}

private static int? GetPortFromEnvironment(string variableName)
{
var variable = Environment.GetEnvironmentVariable(variableName);
if (string.IsNullOrEmpty(variable)) return null;
if (!int.TryParse(variable, CultureInfo.InvariantCulture, out var port)) return null;
return port;
}
}
1 change: 1 addition & 0 deletions src/dotnet/aspire-session-host/OTel/OTelServiceOptions.cs
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ namespace AspireSessionHost.OTel;
public class OTelServiceOptions
{
public string? EndpointUrl { get; set; }
public int? ServerPort { get; set; }
}

internal sealed class ConfigureOTelServiceOptions(IConfiguration configuration) : IConfigureOptions<OTelServiceOptions>
14 changes: 6 additions & 8 deletions src/dotnet/aspire-session-host/Program.cs
Original file line number Diff line number Diff line change
@@ -6,12 +6,10 @@
using Microsoft.AspNetCore.Server.Kestrel.Core;
using static AspireSessionHost.EnvironmentVariables;

ParentProcessWatchdog.StartNewIfAvailable();

var aspNetCoreUrl = GetAspNetCoreUrls();
if (aspNetCoreUrl is null) throw new ApplicationException($"Unable to find {AspNetCoreUrls} variable");

var otlpServerPort = GetOtlpServerPort();
ParentProcessWatchdog.StartNewIfAvailable();

var builder = WebApplication.CreateBuilder(args);

@@ -33,15 +31,15 @@
it.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower;
});

var otelServiceOptions =
builder.Configuration.GetSection(ConfigureOTelServiceOptions.SectionName).Get<OTelServiceOptions>();
var otlpServerPort = otelServiceOptions?.ServerPort;
builder.WebHost.ConfigureKestrel(it =>
{
it.ListenLocalhost(aspNetCoreUrl.Port);
if (otlpServerPort.HasValue && otlpServerPort != 0)
if (otlpServerPort != null && otlpServerPort != 0)
{
it.ListenLocalhost(otlpServerPort.Value, options =>
{
options.Protocols = HttpProtocols.Http2;
});
it.ListenLocalhost(otlpServerPort.Value, options => { options.Protocols = HttpProtocols.Http2; });
}
});

Original file line number Diff line number Diff line change
@@ -32,11 +32,11 @@ class AspireSessionHostLauncher(private val project: Project) {
private val LOG = logger<AspireSessionHostLauncher>()

private const val ASPNETCORE_URLS = "ASPNETCORE_URLS"
private const val RIDER_RD_PORT = "Rider_Connection__RdPort"
private const val RIDER_PARENT_PROCESS_ID = "RIDER_PARENT_PROCESS_ID"
private const val RIDER_RD_PORT = "Rider_Connection__RdPort"
private const val RIDER_RESOURCE_SERVICE_ENDPOINT_URL = "Rider_ResourceService__EndpointUrl"
private const val RIDER_RESOURCE_SERVICE_API_KEY = "Rider_ResourceService__ApiKey"
private const val RIDER_OTLP_SERVER_PORT = "RIDER_OTLP_SERVER_PORT"
private const val RIDER_OTEL_SERVICE_SERVER_PORT = "Rider_OtelService__ServerPort"
private const val RIDER_OTEL_SERVICE_ENDPOINT_URL = "Rider_OtelService__EndpointUrl"
}

@@ -111,7 +111,7 @@ class AspireSessionHostLauncher(private val project: Project) {
if (aspireHostConfig.resourceServiceApiKey != null)
put(RIDER_RESOURCE_SERVICE_API_KEY, aspireHostConfig.resourceServiceApiKey)
if (settings.collectTelemetry && aspireHostConfig.openTelemetryProtocolEndpointUrl != null) {
put(RIDER_OTLP_SERVER_PORT, aspireHostConfig.openTelemetryProtocolServerPort.toString())
put(RIDER_OTEL_SERVICE_SERVER_PORT, aspireHostConfig.openTelemetryProtocolServerPort.toString())
put(RIDER_OTEL_SERVICE_ENDPOINT_URL, aspireHostConfig.openTelemetryProtocolEndpointUrl)
}
}