Skip to content
This repository was archived by the owner on Aug 3, 2024. It is now read-only.

Commit f5c510e

Browse files
Add AppInsights heartbeat support (#308)
* Upgrade Microsoft.ApplicationInsights v2.3.0 to v2.10.0 * Add AppInsights heartbeat support * PR feedback
1 parent f4a73c5 commit f5c510e

2 files changed

Lines changed: 60 additions & 1 deletion

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,86 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
5+
using System.Diagnostics;
46
using Microsoft.ApplicationInsights.Extensibility;
7+
using Microsoft.ApplicationInsights.Extensibility.Implementation;
8+
using Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing;
59

610
namespace NuGet.Services.Logging
711
{
812
public static class ApplicationInsights
913
{
14+
public static IHeartbeatPropertyManager HeartbeatManager { get; private set; }
15+
1016
public static bool Initialized { get; private set; }
1117

1218
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)
1329
{
1430
if (!string.IsNullOrWhiteSpace(instrumentationKey))
1531
{
1632
TelemetryConfiguration.Active.InstrumentationKey = instrumentationKey;
1733
TelemetryConfiguration.Active.TelemetryInitializers.Add(new TelemetryContextInitializer());
1834

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+
1946
Initialized = true;
2047
}
2148
else
2249
{
2350
Initialized = false;
2451
}
2552
}
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+
}
2685
}
2786
}

src/NuGet.Services.Logging/NuGet.Services.Logging.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
<PrivateAssets>all</PrivateAssets>
6767
</PackageReference>
6868
<PackageReference Include="Microsoft.ApplicationInsights">
69-
<Version>2.3.0</Version>
69+
<Version>2.10.0</Version>
7070
</PackageReference>
7171
<PackageReference Include="Microsoft.Extensions.Logging">
7272
<Version>1.0.0</Version>

0 commit comments

Comments
 (0)