Skip to content
This repository has been archived by the owner on Jul 5, 2020. It is now read-only.

Commit

Permalink
Modifies check for IsAzureWebApp to exclude WebApp for PremiumContain…
Browse files Browse the repository at this point in the history
…ers as they can use regular perf counter (#1218)
  • Loading branch information
cijothomas authored Jun 14, 2019
1 parent 66a8883 commit bb0064a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

## Version 2.11.0-beta1
- [Defer populating RequestTelemetry properties.](https://github.com/Microsoft/ApplicationInsights-dotnet-server/issues/1173)
- [Azure Web App for Windows Containers to use regular PerfCounter mechanism.](https://github.com/microsoft/ApplicationInsights-dotnet-server/pull/1167)
- [Support for Process CPU and Process Memory perf counters in all platforms including Linux.](https://github.com/microsoft/ApplicationInsights-dotnet-server/issues/1189)

## Version 2.10.0
- Updated Base SDK to 2.10.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,45 @@ public void GetCollectorReturnsXPlatformCollectorForNonWindows()
}
#endif
}

[TestMethod]
public void IsWebAppReturnsTrueOnRegularWebApp()
{
try
{
PerformanceCounterUtility.isAzureWebApp = null;
Environment.SetEnvironmentVariable("WEBSITE_SITE_NAME", "something");
Environment.SetEnvironmentVariable("WEBSITE_ISOLATION", "nothyperv");
var actual = PerformanceCounterUtility.IsWebAppRunningInAzure();
Assert.IsTrue(actual);
}
finally
{
PerformanceCounterUtility.isAzureWebApp = null;
Environment.SetEnvironmentVariable("WEBSITE_SITE_NAME", string.Empty);
Environment.SetEnvironmentVariable("WEBSITE_ISOLATION", string.Empty);
Task.Delay(1000).Wait();
}
}

[TestMethod]
public void IsWebAppReturnsFalseOnPremiumContainerWebApp()
{
try
{
PerformanceCounterUtility.isAzureWebApp = null;
Environment.SetEnvironmentVariable("WEBSITE_SITE_NAME", "something");
Environment.SetEnvironmentVariable("WEBSITE_ISOLATION", "hyperv");
var actual = PerformanceCounterUtility.IsWebAppRunningInAzure();
Assert.IsFalse(actual);
}
finally
{
PerformanceCounterUtility.isAzureWebApp = null;
Environment.SetEnvironmentVariable("WEBSITE_SITE_NAME", string.Empty);
Environment.SetEnvironmentVariable("WEBSITE_ISOLATION", string.Empty);
Task.Delay(1000).Wait();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ internal static class PerformanceCounterUtility
private const string AzureWebAppCoreSdkVersionPrefix = "azwapccore:";

private const string WebSiteEnvironmentVariable = "WEBSITE_SITE_NAME";
private const string ProcessorsCountEnvironmentVariable = "NUMBER_OF_PROCESSORS";
private const string WebSiteIsolationEnvironmentVariable = "WEBSITE_ISOLATION";
private const string WebSiteIsolationHyperV = "hyperv";

private static readonly ConcurrentDictionary<string, string> PlaceholderCache =
new ConcurrentDictionary<string, string>();
Expand Down Expand Up @@ -165,16 +166,21 @@ public static string FormatPerformanceCounter(PerformanceCounterStructure pc)
}

/// <summary>
/// Searches for the environment variable specific to Azure web applications and confirms if the current application is a web application or not.
/// Searches for the environment variable specific to Azure Web App.
/// </summary>
/// <returns>Boolean, which is true if the current application is an Azure web application.</returns>
/// <returns>Boolean, which is true if the current application is an Azure Web App.</returns>
public static bool IsWebAppRunningInAzure()
{
if (!isAzureWebApp.HasValue)
{
try
{
isAzureWebApp = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable(WebSiteEnvironmentVariable));
// Presence of "WEBSITE_SITE_NAME" indicate web apps.
// "WEBSITE_ISOLATION"!="hyperv" indicate premium containers. In this case, perf counters
// can be read using regular mechanism and hence this method retuns false for
// premium containers.
isAzureWebApp = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable(WebSiteEnvironmentVariable)) &&
Environment.GetEnvironmentVariable(WebSiteIsolationEnvironmentVariable) != WebSiteIsolationHyperV;
}
catch (Exception ex)
{
Expand Down

0 comments on commit bb0064a

Please sign in to comment.