|
1 | 1 | // Copyright (c) .NET Foundation. All rights reserved. |
2 | 2 | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
3 | 3 |
|
| 4 | +using System; |
| 5 | +using System.Diagnostics; |
4 | 6 | using Microsoft.ApplicationInsights.Extensibility; |
| 7 | +using Microsoft.ApplicationInsights.Extensibility.Implementation; |
| 8 | +using Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing; |
5 | 9 |
|
6 | 10 | namespace NuGet.Services.Logging |
7 | 11 | { |
8 | 12 | public static class ApplicationInsights |
9 | 13 | { |
| 14 | + public static IHeartbeatPropertyManager HeartbeatManager { get; private set; } |
| 15 | + |
10 | 16 | public static bool Initialized { get; private set; } |
11 | 17 |
|
12 | 18 | public static void Initialize(string instrumentationKey) |
| 19 | + { |
| 20 | + InitializeTelemetryConfiguration(instrumentationKey, heartbeatInterval: null); |
| 21 | + } |
| 22 | + |
| 23 | + public static void Initialize(string instrumentationKey, TimeSpan heartbeatInterval) |
| 24 | + { |
| 25 | + InitializeTelemetryConfiguration(instrumentationKey, heartbeatInterval); |
| 26 | + } |
| 27 | + |
| 28 | + private static void InitializeTelemetryConfiguration(string instrumentationKey, TimeSpan? heartbeatInterval) |
13 | 29 | { |
14 | 30 | if (!string.IsNullOrWhiteSpace(instrumentationKey)) |
15 | 31 | { |
16 | 32 | TelemetryConfiguration.Active.InstrumentationKey = instrumentationKey; |
17 | 33 | TelemetryConfiguration.Active.TelemetryInitializers.Add(new TelemetryContextInitializer()); |
18 | 34 |
|
| 35 | + // Configure heartbeat interval if specified. |
| 36 | + // When not defined or null, the DiagnosticsTelemetryModule will use its internal defaults (heartbeat enabled, interval of 15 minutes). |
| 37 | + if (heartbeatInterval.HasValue) |
| 38 | + { |
| 39 | + var heartbeatManager = GetHeartbeatPropertyManager(); |
| 40 | + if (heartbeatManager != null) |
| 41 | + { |
| 42 | + heartbeatManager.HeartbeatInterval = heartbeatInterval.Value; |
| 43 | + } |
| 44 | + } |
| 45 | + |
19 | 46 | Initialized = true; |
20 | 47 | } |
21 | 48 | else |
22 | 49 | { |
23 | 50 | Initialized = false; |
24 | 51 | } |
25 | 52 | } |
| 53 | + |
| 54 | + private static IHeartbeatPropertyManager GetHeartbeatPropertyManager() |
| 55 | + { |
| 56 | + if (HeartbeatManager == null) |
| 57 | + { |
| 58 | + var telemetryModules = TelemetryModules.Instance; |
| 59 | + |
| 60 | + try |
| 61 | + { |
| 62 | + foreach (var module in telemetryModules.Modules) |
| 63 | + { |
| 64 | + if (module is IHeartbeatPropertyManager heartbeatManager) |
| 65 | + { |
| 66 | + HeartbeatManager = heartbeatManager; |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + catch (Exception hearbeatManagerAccessException) |
| 71 | + { |
| 72 | + // An non-critical, unexpected exception occurred trying to access the heartbeat manager. |
| 73 | + Trace.TraceError($"There was an error accessing heartbeat manager. Details: {hearbeatManagerAccessException.ToInvariantString()}"); |
| 74 | + } |
| 75 | + |
| 76 | + if (HeartbeatManager == null) |
| 77 | + { |
| 78 | + // Heartbeat manager unavailable: log warning. |
| 79 | + Trace.TraceWarning("Heartbeat manager unavailable"); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return HeartbeatManager; |
| 84 | + } |
26 | 85 | } |
27 | 86 | } |
0 commit comments