Skip to content

Commit

Permalink
Migrate to connection string
Browse files Browse the repository at this point in the history
  • Loading branch information
nyanhp committed Feb 21, 2023
1 parent 61d60dc commit 3292dc3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased (yyyy-MM-dd)

### Enhancements

- Migrated AppInsights from Instrumentation Key to Connection String (#1483)

## 5.47.0 (2022-11-24)

### Enhancements
Expand Down
61 changes: 34 additions & 27 deletions LabXml/Telemetry/LabTelemetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ public class LabTelemetry
private static volatile LabTelemetry instance;
private static object syncRoot = new Object();
private TelemetryClient telemetryClient = null;
private const string telemetryKey = "fbff0c1a-4f7b-4b90-b74d-8370a38fd213";
private const string telemetryConnectionString = "InstrumentationKey=fbff0c1a-4f7b-4b90-b74d-8370a38fd213;IngestionEndpoint=https://westeurope-5.in.applicationinsights.azure.com/;LiveEndpoint=https://westeurope.livediagnostics.monitor.azure.com/";
private DateTime labStarted;
private const string _telemetryOptInVar = "AUTOMATEDLAB_TELEMETRY_OPTIN";
private const string _nixLogPath = "/var/log/automatedlab/telemetry.log";
public bool TelemetryEnabled { get; private set; }

private LabTelemetry()
{
var config = TelemetryConfiguration.CreateDefault();
config.InstrumentationKey = telemetryKey;
config.ConnectionString = telemetryConnectionString;
config.TelemetryChannel.DeveloperMode = false;
config.TelemetryInitializers.Add(new LabTelemetryInitializer());

Expand Down Expand Up @@ -66,6 +67,29 @@ private static bool GetEnvironmentVariableAsBool(string name, bool defaultValue)
}
}

private void WriteTelemetryEvent(string message, int id)
{
// Separate method in case we find a reliable way to log on Linux
if (Environment.OSVersion.Platform == PlatformID.Unix || Environment.OSVersion.Platform == PlatformID.MacOSX)
{
try
{
System.IO.File.AppendAllText(_nixLogPath, $"{DateTime.Now.ToString("u")}<{id}>{message}");
}
catch { }

}

if (Environment.OSVersion.Platform.Equals(PlatformID.Win32NT))
{
try
{
EventLog.WriteEntry("AutomatedLab", message, EventLogEntryType.Information, id);
}
catch { }
}
}

public static LabTelemetry Instance
{
get
Expand Down Expand Up @@ -113,11 +137,8 @@ public void LabStarted(byte[] labData, string version, string osVersion, string
$"\r\nosversion = {osVersion}" +
$"\r\npsversion = {psVersion}" +
$"\r\nmachineCount = {lab.Machines.Count}";
try
{
EventLog.WriteEntry("AutomatedLab", eventMessage, EventLogEntryType.Information, 101);
}
catch { }
WriteTelemetryEvent(eventMessage, 101);

try
{
telemetryClient.TrackEvent("LabStarted", properties, metrics);
Expand Down Expand Up @@ -149,11 +170,8 @@ public void LabFinished(byte[] labData)
var eventMessage = "Lab finished - Transmitting the following:" +
$"\r\ndayOfWeek = {labStarted.DayOfWeek.ToString()}" +
$"\r\ntimeTakenSeconds = {labDuration.TotalSeconds}";
try
{
EventLog.WriteEntry("AutomatedLab", eventMessage, EventLogEntryType.Information, 102);
}
catch { }
WriteTelemetryEvent(eventMessage, 102);

try
{
telemetryClient.TrackEvent("LabFinished", properties, metrics);
Expand All @@ -179,11 +197,7 @@ public void LabRemoved(byte[] labData)

var eventMessage = "Lab removed - Transmitting the following:" +
$"\r\nlabRunningTicks = {duration.Ticks}";
try
{
EventLog.WriteEntry("AutomatedLab", eventMessage, EventLogEntryType.Information, 103);
}
catch { }
WriteTelemetryEvent(eventMessage, 103);

try
{
Expand All @@ -207,11 +221,7 @@ public void FunctionCalled(string functionName)

var eventMessage = "Function called - Transmitting the following:" +
$"\r\nfunction = {functionName}";
try
{
EventLog.WriteEntry("AutomatedLab", eventMessage, EventLogEntryType.Information, 101);
}
catch { }
WriteTelemetryEvent(eventMessage, 105);

try
{
Expand All @@ -227,6 +237,7 @@ public void FunctionCalled(string functionName)
private void SendUsedRole(List<string> roleName, bool isCustomRole = false)
{
if (!GetEnvironmentVariableAsBool(_telemetryOptInVar, false)) return;
if (roleName.Count == 0) return;
var eventMessage = "Sending role infos - Transmitting the following:";

roleName.ForEach(name =>
Expand All @@ -248,11 +259,7 @@ private void SendUsedRole(List<string> roleName, bool isCustomRole = false)
}
});

try
{
EventLog.WriteEntry("AutomatedLab", eventMessage, EventLogEntryType.Information, 104);
}
catch { }
WriteTelemetryEvent(eventMessage, 104);
try
{

Expand Down

0 comments on commit 3292dc3

Please sign in to comment.