Esempio n. 1
0
        private IHeartbeatPropertyManager GetHeartbeatPropertyManager()
        {
            if (this.HeartbeatManager == null)
            {
                var telemetryModules = TelemetryModules.Instance;

                try
                {
                    foreach (var module in telemetryModules.Modules)
                    {
                        if (module is IHeartbeatPropertyManager hman)
                        {
                            this.HeartbeatManager = hman;
                        }
                    }
                }
                catch (Exception hearbeatManagerAccessException)
                {
                    WindowsServerEventSource.Log.AppServiceHeartbeatManagerAccessFailure(hearbeatManagerAccessException.ToInvariantString());
                }

                if (this.HeartbeatManager == null)
                {
                    WindowsServerEventSource.Log.AppServiceHeartbeatManagerNotAvailable();
                }
            }

            return(this.HeartbeatManager);
        }
Esempio n. 2
0
        public HeartbeatCustomizer()
        {
            var telemetryModules = TelemetryModules.Instance;

            this.heartbeatModule = telemetryModules.Modules.OfType <IHeartbeatPropertyManager>().FirstOrDefault();
            Diagnostics.LogInfo("HeartbeatCustomizer initialized");
        }
        public void IsHeartbeatEnabledByDefault()
        {
            using (var initializedModule = new DiagnosticsTelemetryModule())
            {
                // test that the heartbeat is enabled by default
                IHeartbeatPropertyManager hbeat = initializedModule;
                Assert.IsTrue(hbeat.IsHeartbeatEnabled);

                // initialize the DiagnosticsTelemetryModule, and check that heartbeats are still enabled
                initializedModule.Initialize(new TelemetryConfiguration());
                Assert.IsTrue(hbeat.IsHeartbeatEnabled);
            }
        }
        public void InitializeHealthHeartbeatWithNonDefaultInterval()
        {
            TimeSpan userSetInterval = TimeSpan.FromMilliseconds(HeartbeatProvider.MinimumHeartbeatInterval.TotalMilliseconds + 7852.0);

            using (var initializedModule = new DiagnosticsTelemetryModule())
            {
                // set the interval via the IHeartbeatPropertyManager interface
                IHeartbeatPropertyManager hbeat = initializedModule;
                Assert.AreNotEqual(userSetInterval, hbeat.HeartbeatInterval);
                hbeat.HeartbeatInterval = userSetInterval;

                // initialize the DiagnosticsTelemetryModule, and check that the interval is still intact
                initializedModule.Initialize(new TelemetryConfiguration());
                Assert.AreEqual(userSetInterval, hbeat.HeartbeatInterval);
            }
        }
        public void CanDisableHeartbeatPriorToInitialize()
        {
            using (var initializedModule = new DiagnosticsTelemetryModule())
            {
                // disable the heartbeat at construction time but before initialize
                // (this simulates the flow of disabling the heartbeat via config)
                IHeartbeatPropertyManager hbeat = initializedModule;
                hbeat.IsHeartbeatEnabled = false;

                // initialize the DiagnosticsTelemetryModule, and check that heartbeats are still disabled
                initializedModule.Initialize(new TelemetryConfiguration());
                Assert.IsFalse(hbeat.IsHeartbeatEnabled);

                // dig into the heartbeat provider itself to ensure this is indeed disabled
                Assert.IsFalse(initializedModule.HeartbeatProvider.IsHeartbeatEnabled);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Add all enabled, present Azure IMS fields to the heartbeat properties.
        /// </summary>
        /// <param name="provider">Heartbeat provider to set the properties on.</param>
        /// <returns>True if any property values were successfully set, false if none were set.</returns>
        public async Task <bool> SetDefaultPayloadAsync(IHeartbeatPropertyManager provider)
        {
            bool hasSetFields = false;

            try
            {
                if (!this.isAzureMetadataCheckCompleted)
                {
                    this.isAzureMetadataCheckCompleted = true;

                    var azureComputeMetadata = await this.azureInstanceMetadataRequestor.GetAzureComputeMetadataAsync()
                                               .ConfigureAwait(false);

                    if (azureComputeMetadata != null)
                    {
                        var enabledImdsFields = this.ExpectedAzureImsFields.Except(provider.ExcludedHeartbeatProperties);
                        foreach (string field in enabledImdsFields)
                        {
                            string verifiedValue = azureComputeMetadata.VerifyExpectedValue(field);

                            bool addedProperty = provider.AddHeartbeatProperty(
                                propertyName: string.Concat(AzureComputeMetadataHeartbeatPropertyProvider.HeartbeatPropertyPrefix, field),
                                propertyValue: verifiedValue,
                                isHealthy: true);
                            if (!addedProperty)
                            {
                                WindowsServerEventSource.Log.AzureInstanceMetadataWasntAddedToHeartbeatProperties(field, verifiedValue);
                            }

                            hasSetFields = hasSetFields || addedProperty;
                        }
                    }
                    else
                    {
                        WindowsServerEventSource.Log.AzureInstanceMetadataNotAdded();
                    }
                }
            }
            catch (Exception setPayloadException)
            {
                WindowsServerEventSource.Log.AzureInstanceMetadataFailureSettingDefaultPayload(setPayloadException.Message, setPayloadException.InnerException?.Message);
            }

            return(hasSetFields);
        }
        public void CanDisableHeartbeatPropertyProviderPriorToInitialize()
        {
            using (var initializedModule = new DiagnosticsTelemetryModule())
            {
                // disable the heartbeat property provider 'Base' at construction time but before initialize
                // (this simulates the flow of disabling the heartbeat via config)
                IHeartbeatPropertyManager        hbeat             = initializedModule;
                IHeartbeatDefaultPayloadProvider hbeatPropProvider = HeartbeatDefaultPayload.DefaultPayloadProviders.First();
                hbeat.ExcludedHeartbeatPropertyProviders.Add(hbeatPropProvider.Name);

                // dig into the heartbeat provider itself to ensure this is indeed disabled
                Assert.IsTrue(initializedModule.HeartbeatProvider.ExcludedHeartbeatPropertyProviders.Contains(hbeatPropProvider.Name));

                // initialize the DiagnosticsTelemetryModule, and check that heartbeats are still disabled
                initializedModule.Initialize(new TelemetryConfiguration());

                // ensure this heartbeat property provider is still disabled after init
                Assert.IsTrue(initializedModule.HeartbeatProvider.ExcludedHeartbeatPropertyProviders.Contains(hbeatPropProvider.Name));
            }
        }
        // Gather system data that we want to add to the heartbeat
        public bool Initialize(TimeSpan hbeatInterval)
        {
            foreach (var md in TelemetryModules.Instance.Modules)
            {
                if (md is IHeartbeatPropertyManager heartbeatPropertyMan)
                {
                    this.hbeatPropertyManager = heartbeatPropertyMan;
                    this.hbeatPropertyManager.IsHeartbeatEnabled = false;
                    this.hbeatPropertyManager.HeartbeatInterval  = hbeatInterval;
                    this.hbeatPropertyManager.IsHeartbeatEnabled = true;
                }
            }

            if (this.hbeatPropertyManager == null)
            {
                System.Console.WriteLine("Could not find the Heartbeat Property Manager. Heartbeat properties will not be added.");
                return(false);
            }

            return(true);
        }
Esempio n. 9
0
        private bool AddAppServiceEnvironmentVariablesToHeartbeat(IHeartbeatPropertyManager hbeatManager, bool isUpdateOperation = false)
        {
            bool hasBeenUpdated = false;

            if (hbeatManager == null)
            {
                WindowsServerEventSource.Log.AppServiceHeartbeatSetCalledWithNullManager();
            }
            else
            {
                foreach (var kvp in this.WebHeartbeatPropertyNameEnvVarMap)
                {
                    try
                    {
                        string hbeatKey   = kvp.Key.ToString();
                        string hbeatValue = string.Empty;
                        AppServiceEnvironmentVariableMonitor.Instance.GetCurrentEnvironmentVariableValue(kvp.Value, ref hbeatValue);
                        if (isUpdateOperation)
                        {
                            hbeatManager.SetHeartbeatProperty(hbeatKey, hbeatValue);
                        }
                        else
                        {
                            hbeatManager.AddHeartbeatProperty(hbeatKey, hbeatValue, true);
                        }

                        hasBeenUpdated = true;
                    }
                    catch (Exception heartbeatValueException)
                    {
                        WindowsServerEventSource.Log.AppServiceHeartbeatPropertyAquisitionFailed(kvp.Value, heartbeatValueException.ToInvariantString());
                    }
                }
            }

            return(hasBeenUpdated);
        }
Esempio n. 10
0
 internal AzureInstanceMetadataTelemetryModule(IHeartbeatPropertyManager heartbeatPropertyManager) => this.HeartbeatPropertyManager = heartbeatPropertyManager;
 /// <summary>
 /// Initializes a new instance of the <see cref="AppServicesHeartbeatTelemetryModule" /> class. This is
 /// internal, and allows for overriding the Heartbeat Property Manager to test this module with.
 /// </summary>
 /// <param name="hbeatPropManager">The heartbeat property manager to use when setting/updating env var values.</param>
 internal AppServicesHeartbeatTelemetryModule(IHeartbeatPropertyManager hbeatPropManager)
 {
     this.HeartbeatPropertyManager = hbeatPropManager;
 }