Enabled PerfCounterModule for NetCore apps. The perf counter will only collect counters if app is deployed to Azure Web Apps.

This commit is contained in:
Cijo Thomas 2018-04-23 14:30:13 -07:00
Родитель eda8dbd3e8
Коммит 1599917a86
23 изменённых файлов: 60 добавлений и 93 удалений

Просмотреть файл

@ -227,7 +227,7 @@
MachineName = this.machineName,
Timestamp = timestamp.UtcDateTime,
IsWebApp = this.isWebApp,
PerformanceCollectionSupported = false,
PerformanceCollectionSupported = this.isWebApp,
ProcessorCount = this.processorCount
};
@ -265,7 +265,7 @@
MachineName = this.machineName,
Timestamp = sample.EndTimestamp.UtcDateTime,
IsWebApp = this.isWebApp,
PerformanceCollectionSupported = false,
PerformanceCollectionSupported = this.isWebApp,
ProcessorCount = this.processorCount,
Metrics = metricPoints.ToArray(),
Documents = documents,

Просмотреть файл

@ -41,8 +41,10 @@
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.6.0-beta4" />
<PackageReference Include="MicroBuild.Core" Version="0.2.0" developmentDependency="true" />
<PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="1.1.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="1.0.0" />
<PackageReference Include="System.Runtime.Serialization.Json" Version="4.3.0" />
<PackageReference Include="System.Runtime.Serialization.Primitives" Version="4.3.0" />
<PackageReference Include="System.Threading.Thread" Version="4.3.0" />
</ItemGroup>
<ItemGroup>

Просмотреть файл

@ -1,65 +0,0 @@
namespace Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.Implementation.WebAppPerformanceCollector
{
using System;
using System.Collections.Generic;
internal class WebAppPerformanceCollector : IPerformanceCollector
{
private static readonly Tuple<PerformanceCounterData, double>[] emptyCollectResult = new Tuple<PerformanceCounterData, double>[0];
/// <summary>
/// Gets a collection of registered performance counters.
/// </summary>
public IEnumerable<PerformanceCounterData> PerformanceCounters { get; } = new PerformanceCounterData[0];
/// <summary>
/// Performs collection for all registered counters.
/// </summary>
/// <param name="onReadingFailure">Invoked when an individual counter fails to be read.</param>
public IEnumerable<Tuple<PerformanceCounterData, double>> Collect(Action<string, Exception> onReadingFailure = null)
{
return WebAppPerformanceCollector.emptyCollectResult;
}
/// <summary>
/// Refreshes counters.
/// </summary>
public void RefreshCounters()
{
}
/// <summary>
/// Registers a counter using the counter name and reportAs value to the total list of counters.
/// </summary>
/// <param name="perfCounter">Name of the performance counter.</param>
/// <param name="reportAs">Report as name for the performance counter.</param>
/// <param name="isCustomCounter">Boolean to check if the performance counter is custom defined.</param>
/// <param name="error">Captures the error logged.</param>
/// <param name="blockCounterWithInstancePlaceHolder">Boolean that controls the registry of the counter based on the availability of instance place holder.</param>
public void RegisterCounter(
string perfCounter,
string reportAs,
bool isCustomCounter,
out string error,
bool blockCounterWithInstancePlaceHolder)
{
error = string.Empty;
}
/// <summary>
/// Removes a counter.
/// </summary>
/// <param name="perfCounter">Name of the performance counter to remove.</param>
/// <param name="reportAs">ReportAs value of the performance counter to remove.</param>
public void RemoveCounter(string perfCounter, string reportAs)
{
}
/// <summary>
/// Rebinds performance counters to Windows resources.
/// </summary>
public void RefreshPerformanceCounter(PerformanceCounterData pcd)
{
}
}
}

Просмотреть файл

@ -22,23 +22,6 @@
<Compile Include="$(MSBuildThisFileDirectory)Implementation\StandardPerformanceCollector\StandardPerformanceCounter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\StandardPerformanceCollector\ICounterValue.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\StandardPerformanceCollector\StandardPerformanceCollector.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\WebAppPerformanceCollector\AzureWebEnvironmentVariables.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\WebAppPerformanceCollector\CacheHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\WebAppPerformanceCollector\CounterFactory.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\WebAppPerformanceCollector\NormalizedCPUPercentageGauge.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\WebAppPerformanceCollector\CPUPercenageGauge.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\WebAppPerformanceCollector\ICachedEnvironmentVariableAccess.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\WebAppPerformanceCollector\ICounterValue.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\WebAppPerformanceCollector\RawCounterGauge.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\WebAppPerformanceCollector\PerformanceCounterImplementation.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\WebAppPerformanceCollector\RateCounterGauge.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\WebAppPerformanceCollector\RatioCounterGauge.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\WebAppPerformanceCollector\SumUpCountersGauge.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\WebAppPerformanceCollector\WebAppPerformanceCollector.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PerformanceCounterCollectionRequest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\Timer\Timer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\Timer\TimerInterface.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PerformanceCollectorModule.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Resources.Designer.cs" />
</ItemGroup>
<ItemGroup>

Просмотреть файл

@ -30,7 +30,7 @@
/// <param name="dueTime">The due time.</param>
public void ScheduleNextTick(TimeSpan dueTime)
{
this.timer.Change((long)dueTime.TotalMilliseconds, Timeout.Infinite);
this.timer.Change(dueTime, Timeout.InfiniteTimeSpan);
}
/// <summary>

Просмотреть файл

@ -1,7 +1,12 @@
namespace Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.Implementation.WebAppPerformanceCollector
{
using System;
using System.Collections.Generic;
#if NETSTANDARD1_6
using Microsoft.Extensions.Caching.Memory;
#else
using System.Runtime.Caching;
#endif
using System.Text;
/// <summary>
@ -14,6 +19,10 @@
/// </summary>
private static readonly CacheHelper CacheHelperInstance = new CacheHelper();
#if NETSTANDARD1_6
IMemoryCache cache = new MemoryCache(new MemoryCacheOptions());
#endif
/// <summary>
/// Prevents a default instance of the <see cref="CacheHelper"/> class from being created.
/// </summary>
@ -102,8 +111,12 @@
/// /<param name="toCache">Object to be cached.</param>
/// <param name="absoluteExpiration">DateTimeOffset until item expires from cache.</param>
public void SaveToCache(string cacheKey, object toCache, DateTimeOffset absoluteExpiration)
{
MemoryCache.Default.Add(cacheKey, toCache, absoluteExpiration);
{
#if NETSTANDARD1_6
cache.Set(cacheKey, toCache, absoluteExpiration);
#else
MemoryCache.Default.Add(cacheKey, toCache, absoluteExpiration);
#endif
}
/// <summary>
@ -113,7 +126,11 @@
/// <returns> The requested item, as object type T.</returns>
public object GetFromCache(string cacheKey)
{
#if NETSTANDARD1_6
return cache.Get(cacheKey);
#else
return MemoryCache.Default[cacheKey];
#endif
}
/// <summary>
@ -123,7 +140,12 @@
/// <returns>Boolean value for whether or not a key is in the cache.</returns>
public bool IsInCache(string cacheKey)
{
#if NETSTANDARD1_6
object output;
return cache.TryGetValue(cacheKey, out output);
#else
return MemoryCache.Default[cacheKey] != null;
#endif
}
}
}

Просмотреть файл

@ -153,7 +153,7 @@
throw new InvalidOperationException(
string.Format(
CultureInfo.CurrentCulture,
Resources.WebAppPerformanceCounterReadFailed,
"Failed to perform a read for web app performance counter {0}",
coutnerOriginalString),
e);
}
@ -190,7 +190,7 @@
throw new InvalidOperationException(
string.Format(
CultureInfo.CurrentCulture,
Resources.WebAppPerformanceCounterFirstReadFailed,
"Failed to perform the first read for web app performance counter. Please make sure it exists. Counter: {0}",
counterName),
e);
}

Просмотреть файл

@ -53,7 +53,24 @@
<Compile Include="$(MSBuildThisFileDirectory)Implementation\QuickPulse\Service contract\TelemetryDocument\RequestTelemetryDocument.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\QuickPulse\Service contract\TelemetryDocument\TelemetryDocumentType.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\QuickPulse\Service contract\TelemetryDocument\TraceTelemetryDocument.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\Timer\Timer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\Timer\TimerInterface.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\WebAppPerformanceCollector\AzureWebEnvironmentVariables.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\WebAppPerformanceCollector\CacheHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\WebAppPerformanceCollector\CounterFactory.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\WebAppPerformanceCollector\CPUPercenageGauge.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\WebAppPerformanceCollector\ICachedEnvironmentVariableAccess.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\WebAppPerformanceCollector\ICounterValue.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\WebAppPerformanceCollector\NormalizedCPUPercentageGauge.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\WebAppPerformanceCollector\PerformanceCounterImplementation.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\WebAppPerformanceCollector\RateCounterGauge.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\WebAppPerformanceCollector\RatioCounterGauge.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\WebAppPerformanceCollector\RawCounterGauge.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\WebAppPerformanceCollector\SumUpCountersGauge.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Implementation\WebAppPerformanceCollector\WebAppPerformanceCollector.cs" />
<Compile Include="$(MSBuildThisFileDirectory)IQuickPulseTelemetryProcessor.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PerformanceCollectorModule.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PerformanceCounterCollectionRequest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)QuickPulseTelemetryModule.cs">
<ExcludeFromStyleCop>True</ExcludeFromStyleCop>
</Compile>

Просмотреть файл

@ -162,7 +162,10 @@
this.DefaultCounters.Add(new PerformanceCounterCollectionRequest(@"\Process(??APP_WIN32_PROC??)\Private Bytes", @"\Process(??APP_WIN32_PROC??)\Private Bytes"));
this.DefaultCounters.Add(new PerformanceCounterCollectionRequest(@"\Process(??APP_WIN32_PROC??)\IO Data Bytes/sec", @"\Process(??APP_WIN32_PROC??)\IO Data Bytes/sec"));
this.DefaultCounters.Add(new PerformanceCounterCollectionRequest(@"\ASP.NET Applications(??APP_W3SVC_PROC??)\Requests In Application Queue", @"\ASP.NET Applications(??APP_W3SVC_PROC??)\Requests In Application Queue"));
this.DefaultCounters.Add(new PerformanceCounterCollectionRequest(@"\Processor(_Total)\% Processor Time", @"\Processor(_Total)\% Processor Time"));
if (!PerformanceCounterUtility.IsWebAppRunningInAzure())
{
this.DefaultCounters.Add(new PerformanceCounterCollectionRequest(@"\Processor(_Total)\% Processor Time", @"\Processor(_Total)\% Processor Time"));
}
}
if (!this.EnableIISExpressPerformanceCounters && IsRunningUnderIisExpress())
@ -198,6 +201,10 @@
private static bool IsRunningUnderIisExpress()
{
#if NETSTANDARD1_6
// For netstandard target, only time perfcounter is active is if running as Azure WebApp
return false;
#else
var iisExpressProcessName = "iisexpress";
try
@ -212,6 +219,7 @@
return false;
}
#endif
}
/// <summary>
@ -324,7 +332,7 @@
errors.Add(
string.Format(
CultureInfo.InvariantCulture,
Resources.PerformanceCounterCheckConfigurationEntry,
"Counter {0}: {1}",
req.PerformanceCounter,
error));
}

Просмотреть файл

@ -1,11 +1,11 @@
@echo off
@setlocal enabledelayedexpansion enableextensions
CALL buildReleaseFull.cmd
CALL buildReleaseFull.cmd
set BuildRoot=%~dp0..\bin\Release\
set VSTestPath=%PROGRAMFILES(x86)%\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe
CALL "%VSTestPath%" /UseVsixExtensions:true "%BuildRoot%Test\PerformanceCollector\FunctionalTests\PerfCollector.FunctionalTests.dll" /logger:trx
CALL "%VSTestPath%" "%BuildRoot%Test\PerformanceCollector\FunctionalTests\PerfCollector.FunctionalTests.dll" /logger:trx
PAUSE