Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Src/StackifyLib/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -120,6 +127,8 @@ public static void LoadSettings()

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

public static bool? IsEc2 { get; set; } = null;


/// <summary>
/// Attempts to fetch a setting value given the key.
Expand Down
94 changes: 77 additions & 17 deletions Src/StackifyLib/Models/EnvironmentDetail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Web.Hosting;
using System.Management;
#endif

namespace StackifyLib.Models
{
public class EnvironmentDetail
Expand Down Expand Up @@ -109,17 +110,42 @@ private void GetAzureInfo()
/// Get the EC2 Instance name if it exists else null
/// </summary>
#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)
Expand Down Expand Up @@ -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<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)
Expand Down Expand Up @@ -211,7 +263,23 @@ public static async Task<string> 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;
}

/// <summary>
/// Get the display name of the windows service if it is a windows service
/// </summary>
Expand Down Expand Up @@ -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)
Expand Down