Skip to content
This repository was archived by the owner on Aug 3, 2024. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/NuGet.Services.Logging/ApplicationInsights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Diagnostics;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights.Extensibility.Implementation;
using Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing;
Expand Down Expand Up @@ -32,21 +33,28 @@ private static void InitializeTelemetryConfiguration(string instrumentationKey,
TelemetryConfiguration.Active.InstrumentationKey = instrumentationKey;
TelemetryConfiguration.Active.TelemetryInitializers.Add(new TelemetryContextInitializer());

// Construct a TelemetryClient to emit traces so we can track and debug AI initialization.
var telemetryClient = new TelemetryClient();

// Configure heartbeat interval if specified.
// When not defined or null, the DiagnosticsTelemetryModule will use its internal defaults (heartbeat enabled, interval of 15 minutes).
if (heartbeatInterval.HasValue)
{
var heartbeatManager = GetHeartbeatPropertyManager();
var heartbeatManager = GetHeartbeatPropertyManager(telemetryClient);
if (heartbeatManager != null)
{
heartbeatManager.HeartbeatInterval = heartbeatInterval.Value;

Trace.TraceInformation($"Telemetry initialized using configured heartbeat interval: {heartbeatInterval.Value}.");
telemetryClient.TrackTrace(
$"Telemetry initialized using configured heartbeat interval: {heartbeatInterval.Value}.",
SeverityLevel.Information);
}
}
else
{
Trace.TraceInformation($"Telemetry initialized using default heartbeat interval.");
telemetryClient.TrackTrace(
"Telemetry initialized using default heartbeat interval.",
SeverityLevel.Information);
}

Initialized = true;
Expand All @@ -57,7 +65,7 @@ private static void InitializeTelemetryConfiguration(string instrumentationKey,
}
}

private static IHeartbeatPropertyManager GetHeartbeatPropertyManager()
private static IHeartbeatPropertyManager GetHeartbeatPropertyManager(TelemetryClient telemetryClient)
{
if (HeartbeatManager == null)
{
Expand All @@ -76,13 +84,15 @@ private static IHeartbeatPropertyManager GetHeartbeatPropertyManager()
catch (Exception hearbeatManagerAccessException)
{
// An non-critical, unexpected exception occurred trying to access the heartbeat manager.
Trace.TraceError($"There was an error accessing heartbeat manager. Details: {hearbeatManagerAccessException.ToInvariantString()}");
telemetryClient.TrackTrace(
$"There was an error accessing heartbeat manager. Details: {hearbeatManagerAccessException.ToInvariantString()}",
SeverityLevel.Error);
}

if (HeartbeatManager == null)
{
// Heartbeat manager unavailable: log warning.
Trace.TraceWarning("Heartbeat manager unavailable");
telemetryClient.TrackTrace("Heartbeat manager unavailable", SeverityLevel.Warning);
}
}

Expand Down