66#nullable enable
77
88using System ;
9+ using System . Runtime . CompilerServices ;
910using Datadog . Trace . ClrProfiler ;
10- using Datadog . Trace . ClrProfiler . ServerlessInstrumentation ;
1111using Datadog . Trace . Configuration ;
1212
1313namespace Datadog . Trace . Util ;
@@ -22,18 +22,18 @@ internal static bool IsServerlessEnvironment(out Exception? exceptionInReading)
2222 // Track the first exception encountered while reading env vars
2323 Exception ? firstException = null ;
2424
25- var isServerless = TryCheckEnvVar ( PlatformKeys . Aws . FunctionName , ref firstException )
26- || ( TryCheckEnvVar ( PlatformKeys . AzureAppService . SiteNameKey , ref firstException )
27- && ! TryCheckEnvVar ( ConfigurationKeys . AzureAppService . AzureAppServicesContextKey , ref firstException ) )
28- || ( TryCheckEnvVar ( PlatformKeys . GcpFunction . FunctionNameKey , ref firstException )
29- && TryCheckEnvVar ( PlatformKeys . GcpFunction . FunctionTargetKey , ref firstException ) )
30- || ( TryCheckEnvVar ( PlatformKeys . GcpFunction . DeprecatedFunctionNameKey , ref firstException )
31- && TryCheckEnvVar ( PlatformKeys . GcpFunction . DeprecatedProjectKey , ref firstException ) ) ;
25+ var isServerless = EnvironmentVariableExists ( PlatformKeys . Aws . FunctionName , ref firstException )
26+ || ( EnvironmentVariableExists ( PlatformKeys . AzureAppService . SiteNameKey , ref firstException )
27+ && ! EnvironmentVariableExists ( ConfigurationKeys . AzureAppService . AzureAppServicesContextKey , ref firstException ) )
28+ || ( EnvironmentVariableExists ( PlatformKeys . GcpFunction . FunctionNameKey , ref firstException )
29+ && EnvironmentVariableExists ( PlatformKeys . GcpFunction . FunctionTargetKey , ref firstException ) )
30+ || ( EnvironmentVariableExists ( PlatformKeys . GcpFunction . DeprecatedFunctionNameKey , ref firstException )
31+ && EnvironmentVariableExists ( PlatformKeys . GcpFunction . DeprecatedProjectKey , ref firstException ) ) ;
3232 exceptionInReading = firstException ;
3333 return isServerless ;
3434 }
3535
36- private static bool TryCheckEnvVar ( string key , ref Exception ? storedException )
36+ private static bool EnvironmentVariableExists ( string key , ref Exception ? storedException )
3737 {
3838 try
3939 {
@@ -63,11 +63,13 @@ public static bool IsClrProfilerAttachedSafe()
6363 }
6464 }
6565
66- public static string ? InjectionEnabled ( ) => GetEnvironmentVariable ( ConfigurationKeys . SsiDeployed ) ;
66+ public static string ? SsiDeployedEnvVar ( ) => GetEnvironmentVariable ( ConfigurationKeys . SsiDeployed ) ;
6767
6868 public static string ? ProgramData ( ) => GetEnvironmentVariable ( PlatformKeys . ProgramData ) ;
6969
7070#pragma warning disable RS0030
71+ // this access is allowed here as it's controlled by analyzer EnvironmentGetEnvironmentVariableAnalyzer making sure it's using a key from ConfigurationKeys/PlatformKeys
72+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
7173 private static string ? GetEnvironmentVariable ( string key ) => Environment . GetEnvironmentVariable ( key ) ;
7274#pragma warning restore RS0030
7375}
0 commit comments