Skip to content

Commit 9065448

Browse files
committed
Cleanup, test fixes
1 parent f78dd23 commit 9065448

2 files changed

Lines changed: 35 additions & 38 deletions

File tree

Datadog.Serverless.Compat.Tests/CompatibilityLayerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,9 @@ public EnvironmentVariableScope(params (string name, string? value)[] variables)
263263

264264
public void Dispose()
265265
{
266-
foreach (var (name, originalValue) in _originalValues)
266+
foreach (var kvp in _originalValues)
267267
{
268-
Environment.SetEnvironmentVariable(name, originalValue);
268+
Environment.SetEnvironmentVariable(kvp.Key, kvp.Value);
269269
}
270270
}
271271
}

Datadog.Serverless/CompatibilityLayer.cs

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,26 @@ private static string DeterminePipeBaseName(string windowsPipeNameKey, string pi
167167
return !string.IsNullOrEmpty(pipeName) ? pipeName : defaultName;
168168
}
169169

170+
private static string CalculatePipeName(string windowsPipeNameKey, string pipeNameKey, string defaultName, string logContext)
171+
{
172+
var pipeBase = DeterminePipeBaseName(windowsPipeNameKey, pipeNameKey, defaultName);
173+
174+
// Windows pipe max: 256 chars - "\\.\pipe\" (9) - "_" (1) - GUID (32) = 214
175+
const int maxBaseLength = 214;
176+
177+
if (pipeBase.Length > maxBaseLength)
178+
{
179+
Logger.LogWarning($"{logContext} pipe base name exceeds {maxBaseLength} characters ({pipeBase.Length}). Truncating to allow for GUID.");
180+
pipeBase = pipeBase.Substring(0, maxBaseLength);
181+
}
182+
183+
var guid = Guid.NewGuid().ToString("N");
184+
var pipeName = $"{pipeBase}_{guid}";
185+
186+
Logger.LogDebug($"CompatibilityLayer calculated {logContext} pipe name: {pipeName}");
187+
return pipeName;
188+
}
189+
170190
/// <summary>
171191
/// Calculates the trace pipe name with a unique GUID suffix.
172192
/// This method is instrumented by the Datadog tracer to override the return value.
@@ -176,27 +196,11 @@ private static string DeterminePipeBaseName(string windowsPipeNameKey, string pi
176196
/// <returns>The trace pipe name to use for communication with the agent</returns>
177197
public static string CalculateTracePipeName()
178198
{
179-
var tracePipeBase = DeterminePipeBaseName(
199+
return CalculatePipeName(
180200
"DD_TRACE_WINDOWS_PIPE_NAME",
181201
"DD_TRACE_PIPE_NAME",
182-
"dd_trace");
183-
184-
// Validate base pipe name length before appending GUID
185-
// Windows pipe path: \\.\pipe\{base}_{guid}
186-
// Max total: 256 - 9 (\\.\pipe\) - 1 (underscore) - 32 (GUID) = 214
187-
const int maxBaseLength = 214;
188-
189-
if (tracePipeBase.Length > maxBaseLength)
190-
{
191-
Logger.LogWarning($"Trace pipe base name exceeds {maxBaseLength} characters ({tracePipeBase.Length}). Truncating to allow for GUID.");
192-
tracePipeBase = tracePipeBase.Substring(0, maxBaseLength);
193-
}
194-
195-
var guid = Guid.NewGuid().ToString("N");
196-
var pipeName = $"{tracePipeBase}_{guid}";
197-
198-
Logger.LogDebug($"CompatibilityLayer calculated trace pipe name: {pipeName}");
199-
return pipeName;
202+
"dd_trace",
203+
"trace");
200204
}
201205

202206
/// <summary>
@@ -208,25 +212,11 @@ public static string CalculateTracePipeName()
208212
/// <returns>The DogStatsD pipe name to use for communication with the agent</returns>
209213
public static string CalculateDogStatsDPipeName()
210214
{
211-
var dogstatsdPipeBase = DeterminePipeBaseName(
215+
return CalculatePipeName(
212216
"DD_DOGSTATSD_WINDOWS_PIPE_NAME",
213217
"DD_DOGSTATSD_PIPE_NAME",
214-
"dd_dogstatsd");
215-
216-
// Validate base pipe name length before appending GUID
217-
const int maxBaseLength = 214;
218-
219-
if (dogstatsdPipeBase.Length > maxBaseLength)
220-
{
221-
Logger.LogWarning($"DogStatsD pipe base name exceeds {maxBaseLength} characters ({dogstatsdPipeBase.Length}). Truncating to allow for GUID.");
222-
dogstatsdPipeBase = dogstatsdPipeBase.Substring(0, maxBaseLength);
223-
}
224-
225-
var guid = Guid.NewGuid().ToString("N");
226-
var pipeName = $"{dogstatsdPipeBase}_{guid}";
227-
228-
Logger.LogDebug($"CompatibilityLayer calculated DogStatsD pipe name: {pipeName}");
229-
return pipeName;
218+
"dd_dogstatsd",
219+
"DogStatsD");
230220
}
231221

232222
internal static void ConfigureNamedPipes(ProcessStartInfo startInfo, OS os)
@@ -243,6 +233,13 @@ internal static void ConfigureNamedPipes(ProcessStartInfo startInfo, OS os)
243233
var tracePipeName = CalculateTracePipeName();
244234
var dogstatsdPipeName = CalculateDogStatsDPipeName();
245235

236+
// Ensure pipe names are not null before setting environment variables
237+
if (tracePipeName == null || dogstatsdPipeName == null)
238+
{
239+
Logger.LogError("Failed to calculate pipe names. Trace and DogStatsD pipe names cannot be null.");
240+
return;
241+
}
242+
246243
// Set environment variables for the spawned rust binary
247244
startInfo.EnvironmentVariables["DD_TRACE_WINDOWS_PIPE_NAME"] = tracePipeName;
248245
startInfo.EnvironmentVariables["DD_DOGSTATSD_WINDOWS_PIPE_NAME"] = dogstatsdPipeName;

0 commit comments

Comments
 (0)