2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
4
using System ;
5
+ using System . Collections . Generic ;
5
6
using System . IO ;
7
+ using System . Linq ;
6
8
using NewRelic . Agent . Core . Configuration ;
7
9
using NewRelic . Agent . Core . Utilities ;
8
10
using NewRelic . Agent . Extensions . Logging ;
9
11
using NewRelic . Agent . Core . SharedInterfaces ;
12
+ using NewRelic . Agent . Extensions . SystemExtensions . Collections . Generic ;
10
13
11
14
namespace NewRelic . Agent . Core . Config
12
15
{
@@ -21,6 +24,7 @@ public interface IBootstrapConfiguration
21
24
string ServerlessFunctionName { get ; }
22
25
string ServerlessFunctionVersion { get ; }
23
26
bool AzureFunctionModeDetected { get ; }
27
+ bool GCSamplerV2Enabled { get ; }
24
28
}
25
29
26
30
/// <summary>
@@ -64,6 +68,7 @@ public BootstrapConfiguration(configuration localConfiguration, string configura
64
68
public BootstrapConfiguration ( configuration localConfiguration , string configurationFileName , Func < string , ValueWithProvenance < string > > getWebConfigSettingWithProvenance , IConfigurationManagerStatic configurationManagerStatic , IProcessStatic processStatic , Predicate < string > checkDirectoryExists , Func < string , string > getFullPath )
65
69
{
66
70
ServerlessModeEnabled = CheckServerlessModeEnabled ( localConfiguration ) ;
71
+ GCSamplerV2Enabled = CheckGCSamplerV2Enabled ( TryGetAppSettingAsBoolWithDefault ( localConfiguration , "GCSamplerV2Enabled" , false ) ) ;
67
72
DebugStartupDelaySeconds = localConfiguration . debugStartupDelaySeconds ;
68
73
ConfigurationFileName = configurationFileName ;
69
74
LogConfig = new BootstrapLogConfig ( localConfiguration . log , processStatic , checkDirectoryExists , getFullPath ) ;
@@ -133,6 +138,8 @@ public string AgentEnabledAt
133
138
134
139
public bool AzureFunctionModeDetected => ConfigLoaderHelpers . GetEnvironmentVar ( "FUNCTIONS_WORKER_RUNTIME" ) != null ;
135
140
141
+ public bool GCSamplerV2Enabled { get ; private set ; }
142
+
136
143
private bool CheckServerlessModeEnabled ( configuration localConfiguration )
137
144
{
138
145
// We may need these later even if we don't use it now.
@@ -154,6 +161,11 @@ private bool CheckServerlessModeEnabled(configuration localConfiguration)
154
161
return localConfiguration . serverlessModeEnabled ;
155
162
}
156
163
164
+ private bool CheckGCSamplerV2Enabled ( bool localConfigurationGcSamplerV2Enabled )
165
+ {
166
+ return localConfigurationGcSamplerV2Enabled || ( ConfigLoaderHelpers . GetEnvironmentVar ( "NEW_RELIC_GC_SAMPLER_V2_ENABLED" ) . TryToBoolean ( out var enabledViaEnvVariable ) && enabledViaEnvVariable ) ;
167
+ }
168
+
157
169
private void SetAgentEnabledValues ( )
158
170
{
159
171
_agentEnabledWithProvenance = TryGetAgentEnabledFromWebConfig ( ) ;
@@ -204,6 +216,30 @@ private ValueWithProvenance<bool> TryGetAgentEnabledSetting(Func<string, ValueWi
204
216
return null ;
205
217
}
206
218
219
+ private Dictionary < string , string > TransformAppSettings ( configuration localConfiguration )
220
+ {
221
+ if ( localConfiguration . appSettings == null )
222
+ return new Dictionary < string , string > ( ) ;
223
+
224
+ return localConfiguration . appSettings
225
+ . Where ( setting => setting != null )
226
+ . Select ( setting => new KeyValuePair < string , string > ( setting . key , setting . value ) )
227
+ . ToDictionary ( IEnumerableExtensions . DuplicateKeyBehavior . KeepFirst ) ;
228
+ }
229
+
230
+ private bool TryGetAppSettingAsBoolWithDefault ( configuration localConfiguration , string key , bool defaultValue )
231
+ {
232
+ var value = TransformAppSettings ( localConfiguration ) . GetValueOrDefault ( key ) ;
233
+
234
+ bool parsedBool ;
235
+ var parsedSuccessfully = bool . TryParse ( value , out parsedBool ) ;
236
+ if ( ! parsedSuccessfully )
237
+ return defaultValue ;
238
+
239
+ return parsedBool ;
240
+ }
241
+
242
+
207
243
private class BootstrapLogConfig : ILogConfig
208
244
{
209
245
private readonly string _directoryFromLocalConfig ;
0 commit comments