Skip to content

Commit fd9864f

Browse files
committed
Add some tests and env var
Super terrible tests though very slow.
1 parent 38b594a commit fd9864f

File tree

8 files changed

+136
-74
lines changed

8 files changed

+136
-74
lines changed

tracer/missing-nullability-files.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,6 @@ src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Kafka/KafkaProduceSyncDelivery
485485
src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Kafka/KafkaProduceSyncIntegration.cs
486486
src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Kafka/Offset.cs
487487
src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Kafka/Partition.cs
488-
src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Logging/AzureFunctionsHostDetector.cs
489488
src/Datadog.Trace/ClrProfiler/AutoInstrumentation/MongoDb/IBsonDocumentProxy.cs
490489
src/Datadog.Trace/ClrProfiler/AutoInstrumentation/MongoDb/MongoDbTags.cs
491490
src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Msmq/MessageQueueTransactionType.cs
@@ -574,6 +573,7 @@ src/Datadog.Trace/ClrProfiler/AutoInstrumentation/AWS/SQS/ICreateQueueRequest.cs
574573
src/Datadog.Trace/ClrProfiler/AutoInstrumentation/AWS/SQS/ICreateQueueResponse.cs
575574
src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Azure/Functions/AzureFunctionsExecutionReason.cs
576575
src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Azure/Functions/AzureFunctionsExecutorTryExecuteAsyncIntegration.cs
576+
src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Azure/Functions/AzureFunctionsHostDetector.cs
577577
src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Azure/Functions/FunctionInvocationMiddlewareInvokeIntegration.cs
578578
src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Azure/Functions/IFunctionDescriptor.cs
579579
src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Azure/Functions/IFunctionInstance.cs
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// <copyright file="AzureFunctionsHostDetector.cs" company="Datadog">
2+
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
4+
// </copyright>
5+
6+
using System;
7+
using System.Diagnostics;
8+
using Datadog.Trace.Logging;
9+
10+
internal static class AzureFunctionsHostDetector
11+
{
12+
private static readonly IDatadogLogger Log = DatadogLogging.GetLoggerFor(typeof(AzureFunctionsHostDetector));
13+
14+
public static readonly bool IsRunningInFunctionsHost;
15+
16+
static AzureFunctionsHostDetector()
17+
{
18+
IsRunningInFunctionsHost = DetectIfFunctionsHost();
19+
20+
if (IsRunningInFunctionsHost)
21+
{
22+
Log.Information("Detected Azure Functions host process.");
23+
}
24+
}
25+
26+
private static bool DetectIfFunctionsHost()
27+
{
28+
try
29+
{
30+
var functionsRuntime = Environment.GetEnvironmentVariable("FUNCTIONS_WORKER_RUNTIME");
31+
if (string.IsNullOrEmpty(functionsRuntime))
32+
{
33+
return false; // we are the worker process, not the host
34+
}
35+
36+
// Check if we are isolated
37+
if (!string.Equals(functionsRuntime, "dotnet-isolated", StringComparison.OrdinalIgnoreCase))
38+
{
39+
return false; // yeah azure functions, but not isolated, so not an issue here
40+
}
41+
42+
// at this point we can assume that we are in Azure Functions
43+
// granted this may not always be the case though as we've basically just checked the environment variable
44+
45+
var workerId = Environment.GetEnvironmentVariable("FUNCTIONS_WORKER_ID");
46+
if (!string.IsNullOrEmpty(workerId))
47+
{
48+
// We're in the worker process, not the host
49+
return false;
50+
}
51+
52+
// at this point we can assume that we are in the host process
53+
// but again this is just us checking the environment variable(s)
54+
// Alternatively, we can check the process name and loaded assemblies if we want to be even more thorough
55+
// like so below, but unsure if it is worth it as
56+
// this is still guarded by having another env var set to disable ILogger
57+
var process = Process.GetCurrentProcess();
58+
var processName = process.ProcessName.ToLowerInvariant();
59+
var isKnownHostProcess = processName == "func" || processName.Contains("microsoft.azure.webjobs.script.webhost") || processName == "webhost";
60+
61+
return isKnownHostProcess;
62+
}
63+
catch (Exception ex)
64+
{
65+
Log.Error(ex, "Error detecting Azure Functions host process");
66+
return false; // just assume we aren't the host
67+
}
68+
}
69+
}

tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Logging/AzureFunctionsHostDetector.cs

Lines changed: 0 additions & 68 deletions
This file was deleted.

tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Logging/ILogger/DirectSubmission/LoggerFactoryConstructorIntegration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static CallTargetReturn OnMethodEnd<TTarget>(TTarget instance, Exception?
5151
return CallTargetReturn.GetDefault();
5252
}
5353

54-
if (AzureFunctionsHostDetector.IsRunningInFunctionsHost)
54+
if (TracerManager.Instance.DirectLogSubmission.Settings.DisableForAzureFunctionsHost && AzureFunctionsHostDetector.IsRunningInFunctionsHost)
5555
{
5656
return CallTargetReturn.GetDefault();
5757
}

tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Logging/ILogger/DirectSubmission/LoggerFactoryConstructorNet7Integration.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// <copyright file="LoggerFactoryConstructorNet7Integration.cs" company="Datadog">
1+
// <copyright file="LoggerFactoryConstructorNet7Integration.cs" company="Datadog">
22
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
33
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
44
// </copyright>
@@ -40,6 +40,11 @@ internal static CallTargetReturn OnMethodEnd<TTarget>(TTarget instance, Exceptio
4040
return CallTargetReturn.GetDefault();
4141
}
4242

43+
if (TracerManager.Instance.DirectLogSubmission.Settings.DisableForAzureFunctionsHost && AzureFunctionsHostDetector.IsRunningInFunctionsHost)
44+
{
45+
return CallTargetReturn.GetDefault();
46+
}
47+
4348
if (exception is not null)
4449
{
4550
// If there's an exception during the constructor, things aren't going to work anyway

tracer/src/Datadog.Trace/Configuration/ConfigurationKeys.DirectLogSubmission.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// <copyright file="ConfigurationKeys.DirectLogSubmission.cs" company="Datadog">
1+
// <copyright file="ConfigurationKeys.DirectLogSubmission.cs" company="Datadog">
22
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
33
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
44
// </copyright>
@@ -79,6 +79,13 @@ internal static class DirectLogSubmission
7979
/// </summary>
8080
/// <seealso cref="DirectLogSubmissionSettings.BatchPeriod"/>
8181
public const string BatchPeriodSeconds = "DD_LOGS_DIRECT_SUBMISSION_BATCH_PERIOD_SECONDS";
82+
83+
/// <summary>
84+
/// Configuration key to disable direct log submission for Azure Functions host.
85+
/// Default value is <c>false</c>.
86+
/// </summary>
87+
/// <seealso cref="DirectLogSubmissionSettings.DisableForAzureFunctionsHost"/>
88+
public const string DisableForAzureFunctionsHost = "DD_LOGS_DIRECT_SUBMISSION_DISABLE_FOR_AZURE_FUNCTIONS_HOST";
8289
}
8390
}
8491
}

tracer/src/Datadog.Trace/Logging/DirectSubmission/DirectLogSubmissionSettings.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ public DirectLogSubmissionSettings(IConfigurationSource? source, IConfigurationT
9898

9999
BatchPeriod = TimeSpan.FromSeconds(seconds);
100100

101+
DisableForAzureFunctionsHost = config
102+
.WithKeys(ConfigurationKeys.DirectLogSubmission.DisableForAzureFunctionsHost)
103+
.AsBool(false);
104+
101105
ApiKey = config.WithKeys(ConfigurationKeys.ApiKey).AsRedactedString() ?? string.Empty;
102106
bool[]? enabledIntegrations = null;
103107

@@ -230,6 +234,12 @@ public DirectLogSubmissionSettings(IConfigurationSource? source, IConfigurationT
230234
/// <seealso cref="ConfigurationKeys.DirectLogSubmission.BatchPeriodSeconds"/>
231235
internal TimeSpan BatchPeriod { get; set; }
232236

237+
/// <summary>
238+
/// Gets a value indicating whether direct log submission is disabled for Azure Functions.
239+
/// </summary>
240+
/// <seealso cref="ConfigurationKeys.DirectLogSubmission.DisableForAzureFunctionsHost"/>
241+
internal bool DisableForAzureFunctionsHost { get; } = false;
242+
233243
/// <summary>
234244
/// Gets the Datadog API key
235245
/// </summary>

tracer/test/Datadog.Trace.ClrProfiler.IntegrationTests/AzureFunctionsTests.cs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,51 @@ public async Task SubmitsTraces()
284284
var spans = await agent.WaitForSpansAsync(expectedSpanCount);
285285

286286
using var s = new AssertionScope();
287-
288-
await AssertIsolatedSpans(spans);
287+
var filteredSpans = spans.Where(s => !s.Resource.Equals("Timer ExitApp", StringComparison.OrdinalIgnoreCase)).ToImmutableList();
288+
await AssertIsolatedSpans(filteredSpans);
289289

290290
spans.Count.Should().Be(expectedSpanCount);
291291
}
292292
}
293+
294+
[SkippableTheory]
295+
[InlineData(true)] // Disable host logs enabled
296+
[InlineData(false)] // Disable host logs disabled
297+
[Trait("Category", "EndToEnd")]
298+
[Trait("Category", "AzureFunctions")]
299+
[Trait("RunOnWindows", "True")]
300+
public async Task IsolatedRuntime_DisableHostLogs_Configuration(bool disableHostLogs)
301+
{
302+
// Configure the new environment variable
303+
SetEnvironmentVariable("DD_LOGS_DIRECT_SUBMISSION_DISABLE_FOR_AZURE_FUNCTIONS_HOST", disableHostLogs.ToString());
304+
SetInstrumentationVerification();
305+
var hostName = "integration_ilogger_tests";
306+
using var logsIntake = new MockLogsIntake();
307+
EnableDirectLogSubmission(logsIntake.Port, nameof(IntegrationId.ILogger), hostName);
308+
309+
using var agent = EnvironmentHelper.GetMockAgent(useTelemetry: true);
310+
using (await RunAzureFunctionAndWaitForExit(agent, expectedExitCode: -1))
311+
{
312+
const int expectedSpanCount = 21;
313+
var spans = await agent.WaitForSpansAsync(expectedSpanCount);
314+
315+
var filteredSpans = spans.Where(s => !s.Resource.Equals("Timer ExitApp", StringComparison.OrdinalIgnoreCase)).ToImmutableList();
316+
317+
await AssertIsolatedSpans(filteredSpans);
318+
319+
var logs = logsIntake.Logs;
320+
321+
if (disableHostLogs)
322+
{
323+
// i should probably look for the log :)
324+
logs.Should().HaveCount(11);
325+
}
326+
else
327+
{
328+
logs.Should().HaveCount(14);
329+
}
330+
}
331+
}
293332
}
294333

295334
[UsesVerify]

0 commit comments

Comments
 (0)