Skip to content

Commit 29db3a3

Browse files
authored
Merge pull request #37 from stackify/bug/SF-6204+CoreUpdates
Bug/sf 6204+core updates
2 parents 8078328 + 35e40cf commit 29db3a3

File tree

2 files changed

+87
-18
lines changed

2 files changed

+87
-18
lines changed

Src/StackifyLib/Config.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,18 @@ public static void LoadSettings()
7979
var captureEc2InstanceMetadataUpdateThresholdMinutes = Get("Stackify.Ec2InstanceMetadataUpdateThresholdMinutes", "");
8080
if (string.IsNullOrWhiteSpace(captureEc2InstanceMetadataUpdateThresholdMinutes) == false)
8181
{
82-
if (int.TryParse(captureEc2InstanceMetadataUpdateThresholdMinutes, out int minutes) && minutes > 0)
82+
if (int.TryParse(captureEc2InstanceMetadataUpdateThresholdMinutes, out int minutes) && minutes != 0)
8383
{
8484
Ec2InstanceMetadataUpdateThresholdMinutes = minutes;
8585
}
8686
}
87+
88+
// SF-6204: Allow local overrides of EC2 detection
89+
var isEc2 = Get("Stackify.IsEC2", "");
90+
if (string.IsNullOrWhiteSpace(isEc2) == false)
91+
{
92+
IsEc2 = isEc2.Equals("true", StringComparison.CurrentCultureIgnoreCase);
93+
}
8794
}
8895
catch (Exception ex)
8996
{
@@ -120,6 +127,8 @@ public static void LoadSettings()
120127

121128
public static int Ec2InstanceMetadataUpdateThresholdMinutes { get; set; } = 60;
122129

130+
public static bool? IsEc2 { get; set; } = null;
131+
123132

124133
/// <summary>
125134
/// Attempts to fetch a setting value given the key.

Src/StackifyLib/Models/EnvironmentDetail.cs

Lines changed: 77 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Web.Hosting;
1313
using System.Management;
1414
#endif
15+
1516
namespace StackifyLib.Models
1617
{
1718
public class EnvironmentDetail
@@ -109,17 +110,42 @@ private void GetAzureInfo()
109110
/// Get the EC2 Instance name if it exists else null
110111
/// </summary>
111112
#if NET451 || NET45 || NET40
113+
114+
public static string GetDeviceName()
115+
{
116+
var deviceName = Environment.MachineName;
117+
var isDefaultDeviceNameEc2 = IsEc2MachineName(deviceName);
118+
119+
if (Config.IsEc2 == null || Config.IsEc2 == true || isDefaultDeviceNameEc2)
120+
{
121+
var ec2InstanceId = GetEC2InstanceId();
122+
if (string.IsNullOrWhiteSpace(ec2InstanceId) == false)
123+
{
124+
deviceName = ec2InstanceId;
125+
}
126+
}
127+
128+
return deviceName;
129+
}
130+
112131
public static string GetEC2InstanceId()
113132
{
114133
string r = null;
115134

116135
// SF-6804: Frequent Calls to GetEC2InstanceId
117136
bool skipEc2InstanceIdUpdate = false;
118-
var threshold = TimeSpan.FromMinutes(Config.Ec2InstanceMetadataUpdateThresholdMinutes);
119-
lock (ec2InstanceLock)
137+
if (Config.Ec2InstanceMetadataUpdateThresholdMinutes > 0)
120138
{
121-
skipEc2InstanceIdUpdate = ec2InstanceIdLastUpdate != null && ec2InstanceIdLastUpdate < DateTimeOffset.UtcNow.Subtract(threshold);
122-
r = string.IsNullOrWhiteSpace(ec2InstanceId) ? null : ec2InstanceId;
139+
var threshold = TimeSpan.FromMinutes(Config.Ec2InstanceMetadataUpdateThresholdMinutes);
140+
lock (ec2InstanceLock)
141+
{
142+
skipEc2InstanceIdUpdate = ec2InstanceIdLastUpdate != null && ec2InstanceIdLastUpdate < DateTimeOffset.UtcNow.Subtract(threshold);
143+
r = string.IsNullOrWhiteSpace(ec2InstanceId) ? null : ec2InstanceId;
144+
}
145+
}
146+
else
147+
{
148+
skipEc2InstanceIdUpdate = true;
123149
}
124150

125151
if (skipEc2InstanceIdUpdate)
@@ -162,17 +188,43 @@ public static string GetEC2InstanceId()
162188
return r;
163189
}
164190
#else
191+
public static string GetDeviceName()
192+
{
193+
var deviceName = Process.GetCurrentProcess().MachineName;
194+
var isDefaultDeviceNameEc2 = IsEc2MachineName(deviceName);
195+
196+
if (Config.IsEc2 == null || Config.IsEc2 == true || isDefaultDeviceNameEc2)
197+
{
198+
var instanceID_task = GetEC2InstanceId();
199+
instanceID_task.Wait();
200+
if (string.IsNullOrWhiteSpace(instanceID_task.Result) == false)
201+
{
202+
deviceName = instanceID_task.Result;
203+
}
204+
}
205+
206+
return deviceName;
207+
}
208+
165209
public static async Task<string> GetEC2InstanceId()
166210
{
167211
string r = null;
168212

169213
// SF-6804: Frequent Calls to GetEC2InstanceId
170214
bool skipEc2InstanceIdUpdate = false;
171-
var threshold = TimeSpan.FromMinutes(Config.Ec2InstanceMetadataUpdateThresholdMinutes);
172-
lock (ec2InstanceLock)
215+
216+
if (Config.Ec2InstanceMetadataUpdateThresholdMinutes > 0)
173217
{
174-
skipEc2InstanceIdUpdate = ec2InstanceIdLastUpdate != null && ec2InstanceIdLastUpdate < DateTimeOffset.UtcNow.Subtract(threshold);
175-
r = string.IsNullOrWhiteSpace(ec2InstanceId) ? null : ec2InstanceId;
218+
var threshold = TimeSpan.FromMinutes(Config.Ec2InstanceMetadataUpdateThresholdMinutes);
219+
lock (ec2InstanceLock)
220+
{
221+
skipEc2InstanceIdUpdate = ec2InstanceIdLastUpdate != null && ec2InstanceIdLastUpdate < DateTimeOffset.UtcNow.Subtract(threshold);
222+
r = string.IsNullOrWhiteSpace(ec2InstanceId) ? null : ec2InstanceId;
223+
}
224+
}
225+
else
226+
{
227+
skipEc2InstanceIdUpdate = true;
176228
}
177229

178230
if (skipEc2InstanceIdUpdate)
@@ -211,7 +263,23 @@ public static async Task<string> GetEC2InstanceId()
211263

212264
return r;
213265
}
266+
214267
#endif
268+
private static bool IsEc2MachineName(string machineName)
269+
{
270+
if (string.IsNullOrWhiteSpace(machineName))
271+
{
272+
return false;
273+
}
274+
275+
if (machineName.StartsWith("EC2") && machineName.Contains("-"))
276+
{
277+
return true;
278+
}
279+
280+
return false;
281+
}
282+
215283
/// <summary>
216284
/// Get the display name of the windows service if it is a windows service
217285
/// </summary>
@@ -348,15 +416,7 @@ public EnvironmentDetail(bool loadDetails)
348416
}
349417
#endif
350418

351-
352-
#if NET451 || NET45 || NET40
353-
354-
DeviceName = GetEC2InstanceId() ?? Environment.MachineName;
355-
#else
356-
var instanceID_task = GetEC2InstanceId();
357-
instanceID_task.Wait();
358-
DeviceName = instanceID_task.Result ?? Process.GetCurrentProcess().MachineName;
359-
#endif
419+
DeviceName = GetDeviceName();
360420

361421
#if NET451 || NET45 || NET40
362422
if (string.IsNullOrEmpty(AppName) && !isWebRequest)

0 commit comments

Comments
 (0)