Skip to content

SF-6804: StackifyLib: Frequent Calls to GetEC2InstanceId #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 20, 2017
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
13 changes: 12 additions & 1 deletion Src/StackifyLib/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,16 @@ public static void LoadSettings()
{
ErrorSessionGoodKeys = CaptureErrorSessionWhitelist.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList();
}


// SF-6804: Frequent Calls to GetEC2InstanceId
var captureEc2InstanceMetadataUpdateThresholdMinutes = Get("Stackify.Ec2InstanceMetadataUpdateThresholdMinutes", "");
if (string.IsNullOrWhiteSpace(captureEc2InstanceMetadataUpdateThresholdMinutes) == false)
{
if (int.TryParse(captureEc2InstanceMetadataUpdateThresholdMinutes, out int minutes) && minutes > 0)
{
Ec2InstanceMetadataUpdateThresholdMinutes = minutes;
}
}
}
catch (Exception ex)
{
Expand Down Expand Up @@ -109,6 +118,8 @@ public static void LoadSettings()

public static string CaptureErrorCookiesBlacklist { get; set; } = ".ASPXAUTH";

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


/// <summary>
/// Attempts to fetch a setting value given the key.
Expand Down
63 changes: 55 additions & 8 deletions Src/StackifyLib/Models/EnvironmentDetail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,33 @@ private void GetAzureInfo()
}

// http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html#d0e30002
const string EC2InstanceIdUrl = "http://169.254.169.254/latest/meta-data/instance-id";
private const string EC2InstanceIdUrl = "http://169.254.169.254/latest/meta-data/instance-id";
public static readonly object ec2InstanceLock = new object();
private static DateTimeOffset? ec2InstanceIdLastUpdate = null;
private static string ec2InstanceId = null;

/// <summary>
/// Get the EC2 Instance name if it exists else null
/// </summary>
#if NET451 || NET45 || NET40
public static string GetEC2InstanceId()
{
string r = null;

// SF-6804: Frequent Calls to GetEC2InstanceId
bool skipEc2InstanceIdUpdate = false;
var threshold = TimeSpan.FromMinutes(Config.Ec2InstanceMetadataUpdateThresholdMinutes);
lock (ec2InstanceLock)
{
skipEc2InstanceIdUpdate = ec2InstanceIdLastUpdate != null && ec2InstanceIdLastUpdate < DateTimeOffset.UtcNow.Subtract(threshold);
r = string.IsNullOrWhiteSpace(ec2InstanceId) ? null : ec2InstanceId;
}

if (skipEc2InstanceIdUpdate)
{
return r;
}

try
{
var request = (HttpWebRequest)WebRequest.Create(EC2InstanceIdUrl);
Expand All @@ -123,22 +142,44 @@ public static string GetEC2InstanceId()
using (var reader = new StreamReader(responseStream, encoding))
{
var id = reader.ReadToEnd();
return string.IsNullOrWhiteSpace(id) ? null : id;
r = string.IsNullOrWhiteSpace(id) ? null : id;
}
}
}
return null;
}
}
catch // if not in aws this will timeout
{
return null;
r = null;
}

lock (ec2InstanceLock)
{
ec2InstanceId = r;
ec2InstanceIdLastUpdate = DateTimeOffset.UtcNow;
}

return r;
}
#else
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)
{
skipEc2InstanceIdUpdate = ec2InstanceIdLastUpdate != null && ec2InstanceIdLastUpdate < DateTimeOffset.UtcNow.Subtract(threshold);
r = string.IsNullOrWhiteSpace(ec2InstanceId) ? null : ec2InstanceId;
}

if (skipEc2InstanceIdUpdate)
{
return r;
}

try
{

Expand All @@ -152,17 +193,23 @@ public static async Task<string> GetEC2InstanceId()
if (statusCode >= 200 && statusCode < 300)
{
string id = await content.Content.ReadAsStringAsync();
return string.IsNullOrWhiteSpace(id) ? null : id;
r = string.IsNullOrWhiteSpace(id) ? null : id;
}
}

}
catch // if not in aws this will timeout
{
return null;
r = null;
}

return null;
lock (ec2InstanceLock)
{
ec2InstanceId = r;
ec2InstanceIdLastUpdate = DateTimeOffset.UtcNow;
}

return r;
}
#endif
/// <summary>
Expand Down Expand Up @@ -269,7 +316,7 @@ public EnvironmentDetail(bool loadDetails)
this.IsAzureWorkerRole = false;

//Logger global properties would override everything

if (!string.IsNullOrEmpty(Config.AppName))
{
ConfiguredAppName = Config.AppName;
Expand Down