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
40 changes: 40 additions & 0 deletions Datadog.Serverless.Compat.Tests/CompatibilityLayerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,46 @@ public void GetPackageVersion_ShouldReturnVersion_WhenAssemblyAttributeExists()
Assert.NotEqual("unknown", result);
}

[Theory]
[InlineData("dotnet", "dotnet")]
[InlineData("dotnet-isolated", "dotnet-isolated")]
[InlineData(null, null)]
[InlineData("unexpected", null)]
public void GetRuntimeName_UsesExplicitWorkerOrReturnsNull(string? workerRuntime, string? expected)
{
var envVars = new MockEnvironmentVariableProvider();
envVars.Set("FUNCTIONS_WORKER_RUNTIME", workerRuntime);

Assert.Equal(expected, CompatibilityLayer.GetRuntimeName(envVars));
}

[Theory]
[InlineData("dotnet-isolated", "8.0.12", "dotnet-isolated8.0.12")]
[InlineData("dotnet", "8.0.12", "dotnet8.0.12")]
[InlineData(null, "8.0.12", null)]
[InlineData("dotnet", null, null)]
public void ConfigureVersionMetadata_ReportsExplicitVersionsOrRemovesStaleHint(
string? runtimeName, string? runtimeVersion, string? expected)
{
var startInfo = new System.Diagnostics.ProcessStartInfo("/custom/compat");
startInfo.EnvironmentVariables["DD_SERVERLESS_COMPAT_VERSION"] = "stale";
startInfo.EnvironmentVariables["DD_SERVERLESS_COMPAT_RUNTIME"] = "stale";
startInfo.EnvironmentVariables["FUNCTIONS_WORKER_RUNTIME_VERSION"] = "stale";
startInfo.EnvironmentVariables["FUNCTIONS_WORKER_RUNTIME"] = "platform-owned";
CompatibilityLayer.ConfigureVersionMetadata(startInfo, "1.12.0", runtimeName, runtimeVersion);
Assert.Equal("1.12.0", startInfo.EnvironmentVariables["DD_SERVERLESS_COMPAT_VERSION"]);
if (expected is null)
{
Assert.False(startInfo.EnvironmentVariables.ContainsKey("DD_SERVERLESS_COMPAT_RUNTIME"));
}
else
{
Assert.Equal(expected, startInfo.EnvironmentVariables["DD_SERVERLESS_COMPAT_RUNTIME"]);
}
Assert.Equal("stale", startInfo.EnvironmentVariables["FUNCTIONS_WORKER_RUNTIME_VERSION"]);
Comment thread
nina9753 marked this conversation as resolved.
Resolved
Assert.Equal("platform-owned", startInfo.EnvironmentVariables["FUNCTIONS_WORKER_RUNTIME"]);
}

[Theory]
[InlineData("FlexConsumption", null, true)]
[InlineData("FlexConsumption", "test-rg", false)]
Expand Down
29 changes: 28 additions & 1 deletion Datadog.Serverless/CompatibilityLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
if (!string.IsNullOrEmpty(executablePath))
{
Logger.LogDebug($"Detected user-configured executable path DD_SERVERLESS_COMPAT_PATH={executablePath}");
return executablePath;

Check warning on line 74 in Datadog.Serverless/CompatibilityLayer.cs

View workflow job for this annotation

GitHub Actions / Run tests on windows-latest

Possible null reference return.

Check warning on line 74 in Datadog.Serverless/CompatibilityLayer.cs

View workflow job for this annotation

GitHub Actions / Run tests on windows-latest

Possible null reference return.
}

return environment switch
Expand Down Expand Up @@ -361,7 +361,7 @@
CreateNoWindow = true,
};

startInfo.EnvironmentVariables["DD_SERVERLESS_COMPAT_VERSION"] = packageVersion;
ConfigureVersionMetadata(startInfo, packageVersion, GetRuntimeName(envVars), GetRuntimeProductVersion());

// Configure named pipes with unique names to avoid conflicts in multi-function scenarios
ConfigureNamedPipes(startInfo, os);
Expand All @@ -374,4 +374,31 @@
Logger.LogError(ex, $"Exception when starting {executablePath}");
}
}

internal static string? GetRuntimeName(IEnvironmentVariableProvider envVars) =>
envVars.GetEnvironmentVariable("FUNCTIONS_WORKER_RUNTIME") switch
{
"dotnet" => "dotnet",
"dotnet-isolated" => "dotnet-isolated",
_ => null
};

internal static string? GetRuntimeProductVersion() =>
Environment.Version.Major >= 5 ? Environment.Version.ToString() : null;

internal static void ConfigureVersionMetadata(
ProcessStartInfo startInfo,
string packageVersion,
string? runtimeName,
string? runtimeVersion)
{
startInfo.EnvironmentVariables["DD_SERVERLESS_COMPAT_VERSION"] = packageVersion;
if (string.IsNullOrWhiteSpace(runtimeName) || string.IsNullOrWhiteSpace(runtimeVersion))
{
startInfo.EnvironmentVariables.Remove("DD_SERVERLESS_COMPAT_RUNTIME");
return;
}

startInfo.EnvironmentVariables["DD_SERVERLESS_COMPAT_RUNTIME"] = runtimeName + runtimeVersion;
}
}
Loading