Skip to content

Commit 38b594a

Browse files
committed
Initial test at stopping direct log shipping in func
1 parent ac5ed02 commit 38b594a

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

tracer/missing-nullability-files.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ 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
488489
src/Datadog.Trace/ClrProfiler/AutoInstrumentation/MongoDb/IBsonDocumentProxy.cs
489490
src/Datadog.Trace/ClrProfiler/AutoInstrumentation/MongoDb/MongoDbTags.cs
490491
src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Msmq/MessageQueueTransactionType.cs
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 System.Linq;
9+
using Datadog.Trace.Logging;
10+
11+
internal static class AzureFunctionsHostDetector
12+
{
13+
private static readonly IDatadogLogger Log = DatadogLogging.GetLoggerFor(typeof(AzureFunctionsHostDetector));
14+
15+
public static readonly bool IsRunningInFunctionsHost;
16+
17+
static AzureFunctionsHostDetector()
18+
{
19+
IsRunningInFunctionsHost = DetectIfFunctionsHost();
20+
21+
if (IsRunningInFunctionsHost)
22+
{
23+
Log.Information("Detected Azure Functions host process. Direct log submission will be disabled for ILogger to prevent duplicate logs.");
24+
}
25+
}
26+
27+
private static bool DetectIfFunctionsHost()
28+
{
29+
try
30+
{
31+
var process = Process.GetCurrentProcess();
32+
var processName = process.ProcessName.ToLowerInvariant();
33+
34+
// Check if we're in a known Azure Functions host process
35+
if (processName == "func" ||
36+
processName.Contains("microsoft.azure.webjobs.script"))
37+
{
38+
var functionsRuntime = Environment.GetEnvironmentVariable("FUNCTIONS_WORKER_RUNTIME");
39+
if (string.IsNullOrEmpty(functionsRuntime))
40+
{
41+
return false; // Not Azure Functions
42+
}
43+
44+
// The host process won't have this, but the worker will
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+
// function host will have these loaded (I think)
53+
var hasHostAssemblies = AppDomain.CurrentDomain.GetAssemblies()
54+
.Any(a => a.GetName().Name == "Microsoft.Azure.WebJobs.Script.WebHost" ||
55+
a.GetName().Name == "Microsoft.Azure.WebJobs.Script");
56+
57+
return hasHostAssemblies;
58+
}
59+
60+
return false;
61+
}
62+
catch (Exception ex)
63+
{
64+
Log.Error(ex, "Error detecting Azure Functions host process");
65+
return false;
66+
}
67+
}
68+
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// <copyright file="LoggerFactoryConstructorIntegration.cs" company="Datadog">
1+
// <copyright file="LoggerFactoryConstructorIntegration.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>
@@ -51,6 +51,11 @@ public static CallTargetReturn OnMethodEnd<TTarget>(TTarget instance, Exception?
5151
return CallTargetReturn.GetDefault();
5252
}
5353

54+
if (AzureFunctionsHostDetector.IsRunningInFunctionsHost)
55+
{
56+
return CallTargetReturn.GetDefault();
57+
}
58+
5459
if (exception is not null)
5560
{
5661
// If there's an exception during the constructor, things aren't going to work anyway

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ internal static bool TryAddDirectSubmissionLoggerProvider(TLoggerFactory loggerF
7676
return false;
7777
}
7878

79+
if (AzureFunctionsHostDetector.IsRunningInFunctionsHost)
80+
{
81+
// We're in the host process, skip adding the provider entirely
82+
Log.Debug("Skipping DirectSubmissionLoggerProvider creation in Azure Functions host process");
83+
return false;
84+
}
85+
7986
var provider = new DirectSubmissionLoggerProvider(
8087
TracerManager.Instance.DirectLogSubmission.Sink,
8188
TracerManager.Instance.DirectLogSubmission.Settings.MinimumLevel,

0 commit comments

Comments
 (0)