|
5 | 5 | using Microsoft.ApplicationInsights; |
6 | 6 | using Microsoft.ApplicationInsights.DataContracts; |
7 | 7 | using Microsoft.ApplicationInsights.Extensibility; |
8 | | -using Microsoft.ApplicationInsights.Extensibility.Implementation; |
9 | 8 | using Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing; |
10 | 9 |
|
11 | 10 | namespace NuGet.Services.Logging |
12 | 11 | { |
| 12 | + /// <summary> |
| 13 | + /// Utility class to initialize an <see cref="ApplicationInsightsConfiguration"/> instance |
| 14 | + /// using provided instrumentation key, optional heartbeat interval, |
| 15 | + /// and, if detected, taking into account an optional ApplicationInsights.config file. |
| 16 | + /// </summary> |
| 17 | + /// <remarks> |
| 18 | + /// Calling <see cref="Initialize(string)"/> or <see cref="Initialize(string, TimeSpan)"/> returns the |
| 19 | + /// initialized <see cref="ApplicationInsightsConfiguration"/> object; |
| 20 | + /// it does not set the obsolete <see cref="TelemetryClient.Active"/> component. |
| 21 | + /// |
| 22 | + /// It is the caller's responsibility to ensure the returned configuration is used |
| 23 | + /// when creating new <see cref="TelemetryClient"/> instances. |
| 24 | + /// </remarks> |
13 | 25 | public static class ApplicationInsights |
14 | 26 | { |
15 | | - public static IHeartbeatPropertyManager HeartbeatManager { get; private set; } |
16 | | - |
17 | | - public static bool Initialized { get; private set; } |
18 | | - |
19 | | - public static void Initialize(string instrumentationKey) |
| 27 | + /// <summary> |
| 28 | + /// Initializes an <see cref="ApplicationInsightsConfiguration"/> using the provided |
| 29 | + /// <paramref name="instrumentationKey"/>, taking into account the <c>ApplicationInsights.config</c> file if present. |
| 30 | + /// </summary> |
| 31 | + /// <param name="instrumentationKey">The instrumentation key to use.</param> |
| 32 | + public static ApplicationInsightsConfiguration Initialize(string instrumentationKey) |
20 | 33 | { |
21 | | - InitializeTelemetryConfiguration(instrumentationKey, heartbeatInterval: null); |
| 34 | + return InitializeApplicationInsightsConfiguration(instrumentationKey, heartbeatInterval: null); |
22 | 35 | } |
23 | 36 |
|
24 | | - public static void Initialize(string instrumentationKey, TimeSpan heartbeatInterval) |
| 37 | + /// <summary> |
| 38 | + /// Initializes an <see cref="ApplicationInsightsConfiguration"/> using the provided |
| 39 | + /// <paramref name="instrumentationKey"/> and <paramref name="heartbeatInterval"/>, |
| 40 | + /// taking into account the <c>ApplicationInsights.config</c> file if present. |
| 41 | + /// </summary> |
| 42 | + /// <param name="instrumentationKey">The instrumentation key to use.</param> |
| 43 | + /// <param name="heartbeatInterval">The heartbeat interval to use.</param> |
| 44 | + public static ApplicationInsightsConfiguration Initialize( |
| 45 | + string instrumentationKey, |
| 46 | + TimeSpan heartbeatInterval) |
25 | 47 | { |
26 | | - InitializeTelemetryConfiguration(instrumentationKey, heartbeatInterval); |
| 48 | + return InitializeApplicationInsightsConfiguration(instrumentationKey, heartbeatInterval); |
27 | 49 | } |
28 | 50 |
|
29 | | - private static void InitializeTelemetryConfiguration(string instrumentationKey, TimeSpan? heartbeatInterval) |
| 51 | + private static ApplicationInsightsConfiguration InitializeApplicationInsightsConfiguration( |
| 52 | + string instrumentationKey, |
| 53 | + TimeSpan? heartbeatInterval) |
30 | 54 | { |
| 55 | + // Note: TelemetryConfiguration.Active is being deprecated |
| 56 | + // https://github.com/microsoft/ApplicationInsights-dotnet/issues/1152 |
| 57 | + // We use TelemetryConfiguration.CreateDefault() as opposed to instantiating a new TelemetryConfiguration() |
| 58 | + // to take into account the ApplicationInsights.config file (if detected). |
| 59 | + var telemetryConfiguration = TelemetryConfiguration.CreateDefault(); |
| 60 | + |
31 | 61 | if (!string.IsNullOrWhiteSpace(instrumentationKey)) |
32 | 62 | { |
33 | | - TelemetryConfiguration.Active.InstrumentationKey = instrumentationKey; |
34 | | - TelemetryConfiguration.Active.TelemetryInitializers.Add(new TelemetryContextInitializer()); |
| 63 | + telemetryConfiguration.InstrumentationKey = instrumentationKey; |
| 64 | + } |
35 | 65 |
|
36 | | - // Construct a TelemetryClient to emit traces so we can track and debug AI initialization. |
37 | | - var telemetryClient = new TelemetryClient(); |
| 66 | + telemetryConfiguration.TelemetryInitializers.Add(new TelemetryContextInitializer()); |
38 | 67 |
|
39 | | - // Configure heartbeat interval if specified. |
40 | | - // When not defined or null, the DiagnosticsTelemetryModule will use its internal defaults (heartbeat enabled, interval of 15 minutes). |
41 | | - if (heartbeatInterval.HasValue) |
42 | | - { |
43 | | - var heartbeatManager = GetHeartbeatPropertyManager(telemetryClient); |
44 | | - if (heartbeatManager != null) |
45 | | - { |
46 | | - heartbeatManager.HeartbeatInterval = heartbeatInterval.Value; |
| 68 | + // Construct a TelemetryClient to emit traces so we can track and debug AI initialization. |
| 69 | + var telemetryClient = new TelemetryClient(telemetryConfiguration); |
47 | 70 |
|
48 | | - telemetryClient.TrackTrace( |
49 | | - $"Telemetry initialized using configured heartbeat interval: {heartbeatInterval.Value}.", |
50 | | - SeverityLevel.Information); |
51 | | - } |
52 | | - } |
53 | | - else |
54 | | - { |
55 | | - telemetryClient.TrackTrace( |
56 | | - "Telemetry initialized using default heartbeat interval.", |
| 71 | + telemetryClient.TrackTrace( |
| 72 | + $"TelemetryConfiguration initialized using instrumentation key: {instrumentationKey ?? "EMPTY"}.", |
57 | 73 | SeverityLevel.Information); |
58 | | - } |
59 | 74 |
|
60 | | - Initialized = true; |
61 | | - } |
62 | | - else |
63 | | - { |
64 | | - Initialized = false; |
65 | | - } |
66 | | - } |
| 75 | + var diagnosticsTelemetryModule = new DiagnosticsTelemetryModule(); |
67 | 76 |
|
68 | | - private static IHeartbeatPropertyManager GetHeartbeatPropertyManager(TelemetryClient telemetryClient) |
69 | | - { |
70 | | - if (HeartbeatManager == null) |
| 77 | + // Configure heartbeat interval if specified. |
| 78 | + // When not defined, the DiagnosticsTelemetryModule will use its internal defaults (heartbeat enabled, interval of 15 minutes). |
| 79 | + var traceMessage = "DiagnosticsTelemetryModule initialized using default heartbeat interval."; |
| 80 | + if (heartbeatInterval.HasValue) |
71 | 81 | { |
72 | | - var telemetryModules = TelemetryModules.Instance; |
| 82 | + diagnosticsTelemetryModule.HeartbeatInterval = heartbeatInterval.Value; |
| 83 | + traceMessage = $"DiagnosticsTelemetryModule initialized using configured heartbeat interval: {heartbeatInterval.Value}."; |
| 84 | + } |
73 | 85 |
|
74 | | - try |
75 | | - { |
76 | | - foreach (var module in telemetryModules.Modules) |
77 | | - { |
78 | | - if (module is IHeartbeatPropertyManager heartbeatManager) |
79 | | - { |
80 | | - HeartbeatManager = heartbeatManager; |
81 | | - } |
82 | | - } |
83 | | - } |
84 | | - catch (Exception hearbeatManagerAccessException) |
85 | | - { |
86 | | - // An non-critical, unexpected exception occurred trying to access the heartbeat manager. |
87 | | - telemetryClient.TrackTrace( |
88 | | - $"There was an error accessing heartbeat manager. Details: {hearbeatManagerAccessException.ToInvariantString()}", |
89 | | - SeverityLevel.Error); |
90 | | - } |
| 86 | + diagnosticsTelemetryModule.Initialize(telemetryConfiguration); |
91 | 87 |
|
92 | | - if (HeartbeatManager == null) |
93 | | - { |
94 | | - // Heartbeat manager unavailable: log warning. |
95 | | - telemetryClient.TrackTrace("Heartbeat manager unavailable", SeverityLevel.Warning); |
96 | | - } |
97 | | - } |
| 88 | + telemetryClient.TrackTrace(traceMessage, SeverityLevel.Information); |
98 | 89 |
|
99 | | - return HeartbeatManager; |
| 90 | + return new ApplicationInsightsConfiguration(telemetryConfiguration, diagnosticsTelemetryModule); |
100 | 91 | } |
101 | 92 | } |
102 | 93 | } |
0 commit comments