/// <summary> /// Initializes a new instance of the <see cref="PerformanceCollectorModule"/> class. /// </summary> public PerformanceCollectorModule() { this.Counters = new List <PerformanceCounterCollectionRequest>(); this.collector = this.collector ?? (PerformanceCounterUtility.IsWebAppRunningInAzure() ? (IPerformanceCollector) new WebAppPerformanceCollector() : (IPerformanceCollector) new StandardPerformanceCollector()); }
private void InitializeServiceClient(TelemetryConfiguration configuration) { if (this.serviceClient != null) { // service client has been passed through a constructor, we don't need to do anything return; } Uri serviceEndpointUri; if (string.IsNullOrWhiteSpace(this.QuickPulseServiceEndpoint)) { // endpoint is not specified in configuration, use the default one serviceEndpointUri = QuickPulseDefaults.ServiceEndpoint; } else { // endpoint appears to have been specified in configuration, try using it try { serviceEndpointUri = new Uri(this.QuickPulseServiceEndpoint); } catch (Exception e) { throw new ArgumentException( string.Format( CultureInfo.InvariantCulture, "Error initializing QuickPulse module. QPS endpoint is not a correct URI: '{0}'", this.QuickPulseServiceEndpoint), e); } } // create the default production implementation of the service client with the best service endpoint we could get string instanceName = GetInstanceName(configuration); string streamId = GetStreamId(); string machineName = Environment.MachineName; var assemblyVersion = SdkVersionUtils.GetSdkVersion(null); bool isWebApp = PerformanceCounterUtility.IsWebAppRunningInAzure(); int? processorCount = PerformanceCounterUtility.GetProcessorCount(isWebApp); this.serviceClient = new QuickPulseServiceClient( serviceEndpointUri, instanceName, streamId, machineName, assemblyVersion, this.timeProvider, isWebApp, processorCount ?? 0); QuickPulseEventSource.Log.TroubleshootingMessageEvent( string.Format( CultureInfo.InvariantCulture, "Service client initialized. Endpoint: '{0}', instance name: '{1}', assembly version: '{2}'", serviceEndpointUri, instanceName, assemblyVersion)); }
/// <summary> /// Initialize method is called after all configuration properties have been loaded from the configuration. /// </summary> public void Initialize(TelemetryConfiguration configuration) { if (!this.isInitialized) { lock (this.lockObject) { if (!this.isInitialized) { PerformanceCollectorEventSource.Log.ModuleIsBeingInitializedEvent( string.Format( CultureInfo.InvariantCulture, "Custom counters count: '{0}'", Counters?.Count ?? 0)); if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if (!this.defaultCountersInitialized) { this.DefaultCounters.Add(new PerformanceCounterCollectionRequest(@"\Process(??APP_WIN32_PROC??)\% Processor Time", @"\Process(??APP_WIN32_PROC??)\% Processor Time")); this.DefaultCounters.Add(new PerformanceCounterCollectionRequest(@"\Process(??APP_WIN32_PROC??)\% Processor Time Normalized", @"\Process(??APP_WIN32_PROC??)\% Processor Time Normalized")); this.DefaultCounters.Add(new PerformanceCounterCollectionRequest(@"\Memory\Available Bytes", @"\Memory\Available Bytes")); #if !NETSTANDARD2_0 // Exclude those counters which don't exist for .netcore this.DefaultCounters.Add(new PerformanceCounterCollectionRequest(@"\ASP.NET Applications(??APP_W3SVC_PROC??)\Requests/Sec", @"\ASP.NET Applications(??APP_W3SVC_PROC??)\Requests/Sec")); this.DefaultCounters.Add(new PerformanceCounterCollectionRequest(@"\.NET CLR Exceptions(??APP_CLR_PROC??)\# of Exceps Thrown / sec", @"\.NET CLR Exceptions(??APP_CLR_PROC??)\# of Exceps Thrown / sec")); this.DefaultCounters.Add(new PerformanceCounterCollectionRequest(@"\ASP.NET Applications(??APP_W3SVC_PROC??)\Request Execution Time", @"\ASP.NET Applications(??APP_W3SVC_PROC??)\Request Execution Time")); 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")); #endif 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")); if (!PerformanceCounterUtility.IsWebAppRunningInAzure()) { this.DefaultCounters.Add(new PerformanceCounterCollectionRequest(@"\Processor(_Total)\% Processor Time", @"\Processor(_Total)\% Processor Time")); } } if (!this.EnableIISExpressPerformanceCounters && IsRunningUnderIisExpress()) { PerformanceCollectorEventSource.Log.RunningUnderIisExpress(); return; } this.telemetryConfiguration = configuration; this.client = new TelemetryClient(configuration); this.client.Context.GetInternalContext().SdkVersion = SdkVersionUtils.GetSdkVersion(PerformanceCounterUtility.SDKVersionPrefix()); this.lastRefreshTimestamp = DateTime.MinValue; this.timer = new Timer(this.TimerCallback); // schedule the first tick this.timer.ScheduleNextTick(this.collectionPeriod); this.isInitialized = true; } } } }
/// <summary> /// Initialize method is called after all configuration properties have been loaded from the configuration. /// </summary> /// <param name="configuration">TelemetryConfiguration passed to the module.</param> public void Initialize(TelemetryConfiguration configuration) { if (!this.isInitialized) { lock (this.moduleInitializationLock) { if (!this.isInitialized) { QuickPulseEventSource.Log.ModuleIsBeingInitializedEvent( this.QuickPulseServiceEndpoint, this.DisableFullTelemetryItems, this.DisableTopCpuProcesses, this.AuthenticationApiKey); QuickPulseEventSource.Log.TroubleshootingMessageEvent("Validating configuration..."); ValidateConfiguration(configuration); this.config = configuration; QuickPulseEventSource.Log.TroubleshootingMessageEvent("Initializing members..."); this.collectionTimeSlotManager = this.collectionTimeSlotManager ?? new QuickPulseCollectionTimeSlotManager(); this.performanceCollector = this.performanceCollector ?? (PerformanceCounterUtility.IsWebAppRunningInAzure() ? new WebAppPerformanceCollector() : (IPerformanceCollector) new StandardPerformanceCollector()); this.timeProvider = this.timeProvider ?? new Clock(); this.topCpuCollector = this.topCpuCollector ?? new QuickPulseTopCpuCollector(this.timeProvider, new QuickPulseProcessProvider(PerfLib.GetPerfLib())); this.timings = timings ?? QuickPulseTimings.Default; CollectionConfigurationError[] errors; this.collectionConfiguration = new CollectionConfiguration( new CollectionConfigurationInfo() { ETag = string.Empty }, out errors, this.timeProvider); this.dataAccumulatorManager = this.dataAccumulatorManager ?? new QuickPulseDataAccumulatorManager(this.collectionConfiguration); this.InitializeServiceClient(configuration); this.stateManager = new QuickPulseCollectionStateManager( this.serviceClient, this.timeProvider, this.timings, this.OnStartCollection, this.OnStopCollection, this.OnSubmitSamples, this.OnReturnFailedSamples, this.OnUpdatedConfiguration); this.CreateStateThread(); this.isInitialized = true; } } } }
/// <summary> /// Initialize method is called after all configuration properties have been loaded from the configuration. /// </summary> /// <param name="configuration">TelemetryConfiguration passed to the module.</param> public void Initialize(TelemetryConfiguration configuration) { if (!this.isInitialized) { lock (this.lockObject) { if (!this.isInitialized) { QuickPulseEventSource.Log.ModuleIsBeingInitializedEvent( string.Format( CultureInfo.InvariantCulture, "QuickPulseServiceEndpoint: '{0}', DisableFullTelemetryItems: '{1}'", this.QuickPulseServiceEndpoint, this.DisableFullTelemetryItems)); QuickPulseEventSource.Log.TroubleshootingMessageEvent("Validating configuration..."); this.ValidateConfiguration(configuration); this.config = configuration; QuickPulseEventSource.Log.TroubleshootingMessageEvent("Initializing members..."); this.collectionTimeSlotManager = this.collectionTimeSlotManager ?? new QuickPulseCollectionTimeSlotManager(); this.dataAccumulatorManager = this.dataAccumulatorManager ?? new QuickPulseDataAccumulatorManager(); this.performanceCollector = this.performanceCollector ?? (PerformanceCounterUtility.IsWebAppRunningInAzure() ? (IPerformanceCollector) new WebAppPerformanceCollector() : (IPerformanceCollector) new StandardPerformanceCollector()); this.timeProvider = this.timeProvider ?? new Clock(); this.timings = timings ?? QuickPulseTimings.Default; this.InitializeServiceClient(configuration); this.stateManager = new QuickPulseCollectionStateManager( this.serviceClient, this.timeProvider, this.timings, this.OnStartCollection, this.OnStopCollection, this.OnSubmitSamples, this.OnReturnFailedSamples); this.CreateStateThread(); this.isInitialized = true; } } } }
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(); } }
private void InitializeServiceClient(TelemetryConfiguration configuration) { if (this.ServiceClient != null) { // service client has been passed through a constructor, we don't need to do anything return; } Uri serviceEndpointUri; if (string.IsNullOrWhiteSpace(this.QuickPulseServiceEndpoint)) { // endpoint is not explicitly specified, use the Endpoint from the TelemetryConfiguration (ex: https://rt.services.visualstudio.com/QuickPulseService.svc) serviceEndpointUri = new Uri(configuration.EndpointContainer.Live, "QuickPulseService.svc"); } else { // endpoint appears to have been specified in configuration, try using it try { serviceEndpointUri = new Uri(this.QuickPulseServiceEndpoint); } catch (Exception e) { throw new ArgumentException( string.Format( CultureInfo.InvariantCulture, "Error initializing QuickPulse module. QPS endpoint is not a correct URI: '{0}'", this.QuickPulseServiceEndpoint), e); } } // create the default production implementation of the service client with the best service endpoint we could get string instanceName = GetInstanceName(configuration); string streamId = GetStreamId(); var assemblyVersion = SdkVersionUtils.GetSdkVersion(null); bool isWebApp = PerformanceCounterUtility.IsWebAppRunningInAzure(); int? processorCount = PerformanceCounterUtility.GetProcessorCount(); this.ServiceClient = new QuickPulseServiceClient( serviceEndpointUri, instanceName, streamId, ServerId, assemblyVersion, this.timeProvider, isWebApp, processorCount ?? 0); // TelemetryConfigurationFactory will initialize Modules after Processors. Need to update the processor with the correct service endpoint. foreach (var processor in this.TelemetryProcessors) { processor.ServiceEndpoint = serviceEndpointUri; } QuickPulseEventSource.Log.TroubleshootingMessageEvent( string.Format( CultureInfo.InvariantCulture, "Service client initialized. Endpoint: '{0}', instance name: '{1}', assembly version: '{2}'", serviceEndpointUri, instanceName, assemblyVersion)); }