diff --git a/Src/StackifyLib/Config.cs b/Src/StackifyLib/Config.cs
index 8085894..e2c28ca 100644
--- a/Src/StackifyLib/Config.cs
+++ b/Src/StackifyLib/Config.cs
@@ -79,11 +79,18 @@ public static void LoadSettings()
var captureEc2InstanceMetadataUpdateThresholdMinutes = Get("Stackify.Ec2InstanceMetadataUpdateThresholdMinutes", "");
if (string.IsNullOrWhiteSpace(captureEc2InstanceMetadataUpdateThresholdMinutes) == false)
{
- if (int.TryParse(captureEc2InstanceMetadataUpdateThresholdMinutes, out int minutes) && minutes > 0)
+ if (int.TryParse(captureEc2InstanceMetadataUpdateThresholdMinutes, out int minutes) && minutes != 0)
{
Ec2InstanceMetadataUpdateThresholdMinutes = minutes;
}
}
+
+ // SF-6204: Allow local overrides of EC2 detection
+ var isEc2 = Get("Stackify.IsEC2", "");
+ if (string.IsNullOrWhiteSpace(isEc2) == false)
+ {
+ IsEc2 = isEc2.Equals("true", StringComparison.CurrentCultureIgnoreCase);
+ }
}
catch (Exception ex)
{
@@ -120,6 +127,8 @@ public static void LoadSettings()
public static int Ec2InstanceMetadataUpdateThresholdMinutes { get; set; } = 60;
+ public static bool? IsEc2 { get; set; } = null;
+
///
/// Attempts to fetch a setting value given the key.
diff --git a/Src/StackifyLib/Models/EnvironmentDetail.cs b/Src/StackifyLib/Models/EnvironmentDetail.cs
index 68fb481..87ea7f3 100644
--- a/Src/StackifyLib/Models/EnvironmentDetail.cs
+++ b/Src/StackifyLib/Models/EnvironmentDetail.cs
@@ -12,6 +12,7 @@
using System.Web.Hosting;
using System.Management;
#endif
+
namespace StackifyLib.Models
{
public class EnvironmentDetail
@@ -109,17 +110,42 @@ private void GetAzureInfo()
/// Get the EC2 Instance name if it exists else null
///
#if NET451 || NET45 || NET40
+
+ public static string GetDeviceName()
+ {
+ var deviceName = Environment.MachineName;
+ var isDefaultDeviceNameEc2 = IsEc2MachineName(deviceName);
+
+ if (Config.IsEc2 == null || Config.IsEc2 == true || isDefaultDeviceNameEc2)
+ {
+ var ec2InstanceId = GetEC2InstanceId();
+ if (string.IsNullOrWhiteSpace(ec2InstanceId) == false)
+ {
+ deviceName = ec2InstanceId;
+ }
+ }
+
+ return deviceName;
+ }
+
public static string GetEC2InstanceId()
{
string r = null;
// SF-6804: Frequent Calls to GetEC2InstanceId
bool skipEc2InstanceIdUpdate = false;
- var threshold = TimeSpan.FromMinutes(Config.Ec2InstanceMetadataUpdateThresholdMinutes);
- lock (ec2InstanceLock)
+ if (Config.Ec2InstanceMetadataUpdateThresholdMinutes > 0)
{
- skipEc2InstanceIdUpdate = ec2InstanceIdLastUpdate != null && ec2InstanceIdLastUpdate < DateTimeOffset.UtcNow.Subtract(threshold);
- r = string.IsNullOrWhiteSpace(ec2InstanceId) ? null : ec2InstanceId;
+ var threshold = TimeSpan.FromMinutes(Config.Ec2InstanceMetadataUpdateThresholdMinutes);
+ lock (ec2InstanceLock)
+ {
+ skipEc2InstanceIdUpdate = ec2InstanceIdLastUpdate != null && ec2InstanceIdLastUpdate < DateTimeOffset.UtcNow.Subtract(threshold);
+ r = string.IsNullOrWhiteSpace(ec2InstanceId) ? null : ec2InstanceId;
+ }
+ }
+ else
+ {
+ skipEc2InstanceIdUpdate = true;
}
if (skipEc2InstanceIdUpdate)
@@ -162,17 +188,43 @@ public static string GetEC2InstanceId()
return r;
}
#else
+ public static string GetDeviceName()
+ {
+ var deviceName = Process.GetCurrentProcess().MachineName;
+ var isDefaultDeviceNameEc2 = IsEc2MachineName(deviceName);
+
+ if (Config.IsEc2 == null || Config.IsEc2 == true || isDefaultDeviceNameEc2)
+ {
+ var instanceID_task = GetEC2InstanceId();
+ instanceID_task.Wait();
+ if (string.IsNullOrWhiteSpace(instanceID_task.Result) == false)
+ {
+ deviceName = instanceID_task.Result;
+ }
+ }
+
+ return deviceName;
+ }
+
public static async Task GetEC2InstanceId()
{
string r = null;
// SF-6804: Frequent Calls to GetEC2InstanceId
bool skipEc2InstanceIdUpdate = false;
- var threshold = TimeSpan.FromMinutes(Config.Ec2InstanceMetadataUpdateThresholdMinutes);
- lock (ec2InstanceLock)
+
+ if (Config.Ec2InstanceMetadataUpdateThresholdMinutes > 0)
{
- skipEc2InstanceIdUpdate = ec2InstanceIdLastUpdate != null && ec2InstanceIdLastUpdate < DateTimeOffset.UtcNow.Subtract(threshold);
- r = string.IsNullOrWhiteSpace(ec2InstanceId) ? null : ec2InstanceId;
+ var threshold = TimeSpan.FromMinutes(Config.Ec2InstanceMetadataUpdateThresholdMinutes);
+ lock (ec2InstanceLock)
+ {
+ skipEc2InstanceIdUpdate = ec2InstanceIdLastUpdate != null && ec2InstanceIdLastUpdate < DateTimeOffset.UtcNow.Subtract(threshold);
+ r = string.IsNullOrWhiteSpace(ec2InstanceId) ? null : ec2InstanceId;
+ }
+ }
+ else
+ {
+ skipEc2InstanceIdUpdate = true;
}
if (skipEc2InstanceIdUpdate)
@@ -211,7 +263,23 @@ public static async Task GetEC2InstanceId()
return r;
}
+
#endif
+ private static bool IsEc2MachineName(string machineName)
+ {
+ if (string.IsNullOrWhiteSpace(machineName))
+ {
+ return false;
+ }
+
+ if (machineName.StartsWith("EC2") && machineName.Contains("-"))
+ {
+ return true;
+ }
+
+ return false;
+ }
+
///
/// Get the display name of the windows service if it is a windows service
///
@@ -348,15 +416,7 @@ public EnvironmentDetail(bool loadDetails)
}
#endif
-
-#if NET451 || NET45 || NET40
-
- DeviceName = GetEC2InstanceId() ?? Environment.MachineName;
-#else
- var instanceID_task = GetEC2InstanceId();
- instanceID_task.Wait();
- DeviceName = instanceID_task.Result ?? Process.GetCurrentProcess().MachineName;
-#endif
+ DeviceName = GetDeviceName();
#if NET451 || NET45 || NET40
if (string.IsNullOrEmpty(AppName) && !isWebRequest)