Skip to content

Commit 1f10419

Browse files
authored
Rework telemetry client (#253)
***NO_CI***
1 parent 5707a07 commit 1f10419

File tree

8 files changed

+91
-47
lines changed

8 files changed

+91
-47
lines changed

azure-pipelines.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,8 @@ jobs:
330330
# Replace the intrument key
331331
- powershell: |
332332
. ./common.ps1
333-
$fileToReplace = "$(System.DefaultWorkingDirectory)/nanoFirmwareFlasher.Library/appsettings.json"
334-
$sourceString = "INTRUMENT_KEY"
333+
$fileToReplace = "$(System.DefaultWorkingDirectory)/nanoFirmwareFlasher.Tool\appsettings.json"
334+
$sourceString = "INSTRUMENT_KEY"
335335
$instrumentKey = "$(InstrumentKey)"
336336
337337
Find-ReplaceInFile -filePath $fileToReplace -sourceString $sourceString -targetString $instrumentKey

nanoFirmwareFlasher.Library/FirmwarePackage.cs

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,20 @@
33
// See LICENSE file in the project root for full license information.
44
//
55

6+
using Microsoft.ApplicationInsights;
7+
using Microsoft.ApplicationInsights.DataContracts;
8+
using Microsoft.Extensions.Configuration;
69
using nanoFramework.Tools.Debugger;
7-
using nanoFramework.Tools.FirmwareFlasher;
810
using Newtonsoft.Json;
911
using System;
1012
using System.Collections.Generic;
1113
using System.IO;
1214
using System.IO.Compression;
1315
using System.Linq;
1416
using System.Net.Http;
17+
using System.Reflection;
1518
using System.Text.RegularExpressions;
1619
using System.Threading.Tasks;
17-
using Microsoft.ApplicationInsights;
18-
using Microsoft.ApplicationInsights.DataContracts;
19-
using Microsoft.Extensions.Configuration;
20-
using Microsoft.Extensions.Configuration.Json;
21-
using System.Reflection;
2220

2321
namespace nanoFramework.Tools.FirmwareFlasher
2422
{
@@ -36,11 +34,8 @@ public abstract class FirmwarePackage : IDisposable
3634

3735
private readonly string _targetName;
3836
private readonly bool _preview;
39-
4037
private const string _readmeContent = "This folder contains nanoFramework firmware files. Can safely be removed.";
4138

42-
private static IConfigurationRoot _configuration;
43-
4439
/// <summary>
4540
/// Path with the base location for firmware packages.
4641
/// </summary>
@@ -113,16 +108,6 @@ static FirmwarePackage()
113108
BaseAddress = new Uri("https://api.cloudsmith.io/v1/packages/net-nanoframework/")
114109
};
115110
_cloudsmithClient.DefaultRequestHeaders.Add("Accept", "*/*");
116-
117-
if (_configuration == null)
118-
{
119-
120-
var builder = new ConfigurationBuilder()
121-
.SetBasePath(Directory.GetCurrentDirectory())
122-
.AddJsonFile("appsettings.json");
123-
124-
_configuration = builder.Build();
125-
}
126111
}
127112

128113
/// <summary>
@@ -361,19 +346,11 @@ internal async Task<ExitCodes> DownloadAndExtractAsync()
361346
Console.ForegroundColor = ConsoleColor.White;
362347
}
363348

364-
//send app insight on successful download
365-
366-
string insightConnectionString = _configuration["iConnectionString"];
367-
string optOut = Environment.GetEnvironmentVariable("NANOFRAMEWORK_TELEMETRY_OPTOUT");
368-
369349
stepSuccessful = true;
370350

371-
if (!string.IsNullOrEmpty(insightConnectionString) && optOut != "1")
351+
// send telemetry data on successful download
352+
if (NanoTelemetryClient.TelemetryClient is not null)
372353
{
373-
TelemetryClient telemetryClient = new TelemetryClient(new Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration()
374-
{
375-
ConnectionString = insightConnectionString
376-
});
377354
AssemblyInformationalVersionAttribute nanoffVersion = null;
378355

379356
try
@@ -385,18 +362,17 @@ internal async Task<ExitCodes> DownloadAndExtractAsync()
385362
}
386363
catch
387364
{
388-
365+
// OK to fail here, just telemetry
389366
}
367+
390368
var packageTelemetry = new EventTelemetry("PackageDownloaded");
391369
packageTelemetry.Properties.Add("TargetName", _targetName);
392370
packageTelemetry.Properties.Add("Version", Version);
393371
packageTelemetry.Properties.Add("nanoffVersion", nanoffVersion == null ? "unknown" : nanoffVersion.InformationalVersion);
394-
telemetryClient.TrackEvent(packageTelemetry);
395-
telemetryClient.Flush();
396372

373+
NanoTelemetryClient.TelemetryClient.TrackEvent(packageTelemetry);
374+
NanoTelemetryClient.TelemetryClient.Flush();
397375
}
398-
399-
400376
}
401377
catch
402378
{
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
using Microsoft.ApplicationInsights;
7+
using System;
8+
9+
namespace nanoFramework.Tools.FirmwareFlasher
10+
{
11+
/// <summary>
12+
/// Telemetry client for sending telemetry data to Application Insights.
13+
/// </summary>
14+
public class NanoTelemetryClient
15+
{
16+
private static TelemetryClient _myTelemetryClient;
17+
18+
// flag to signal that the telemetry client field initialized has been processed
19+
private static bool notProcessed = true;
20+
21+
/// <summary>
22+
/// Connection string for <see cref="TelemetryClient"/>.
23+
/// </summary>
24+
public static string ConnectionString;
25+
26+
/// <summary>
27+
/// Gets the <see cref="TelemetryClient"/> to use for sending telemetry data.
28+
/// </summary>
29+
public static TelemetryClient TelemetryClient => GetTelemetryClient();
30+
31+
private static TelemetryClient GetTelemetryClient()
32+
{
33+
if (notProcessed && _myTelemetryClient is null)
34+
{
35+
string optOutTelemetry = Environment.GetEnvironmentVariable("NANOFRAMEWORK_TELEMETRY_OPTOUT");
36+
37+
if (!string.IsNullOrEmpty(ConnectionString)
38+
&& (optOutTelemetry is null || optOutTelemetry != "1"))
39+
{
40+
// parsing the connection string could fail
41+
try
42+
{
43+
_myTelemetryClient = new TelemetryClient(new Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration()
44+
{
45+
ConnectionString = ConnectionString
46+
});
47+
}
48+
catch
49+
{
50+
// don't care, telemetry is not mandatory
51+
};
52+
}
53+
54+
// set flag to false to signal that the telemetry client field has been processed
55+
notProcessed = false;
56+
}
57+
58+
return _myTelemetryClient;
59+
}
60+
}
61+
}

nanoFirmwareFlasher.Library/appsettings.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

nanoFirmwareFlasher.Library/nanoFirmwareFlasher.Library.csproj

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,5 @@
127127
<None Include="..\lib\uniflash\**" Link="uniflash\%(RecursiveDir)%(Filename)%(Extension)">
128128
</None>
129129
</ItemGroup>
130-
<ItemGroup>
131-
<None Update="appsettings.json">
132-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
133-
</None>
134-
</ItemGroup>
135130

136131
</Project>

nanoFirmwareFlasher.Tool/Program.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
using CommandLine;
77
using CommandLine.Text;
8-
using nanoFramework.Tools.Debugger;
8+
using Microsoft.Extensions.Configuration;
99
using nanoFramework.Tools.FirmwareFlasher.Extensions;
1010
using Newtonsoft.Json;
1111
using System;
@@ -17,7 +17,6 @@
1717
using System.Net.Http;
1818
using System.Net.Http.Headers;
1919
using System.Reflection;
20-
using System.Threading;
2120
using System.Threading.Tasks;
2221

2322
namespace nanoFramework.Tools.FirmwareFlasher
@@ -51,6 +50,14 @@ public static async Task<int> Main(string[] args)
5150
var fullPath = Path.GetFullPath(codeBase);
5251
ExecutingPath = Path.GetDirectoryName(fullPath);
5352

53+
// grab AppInsights connection string to setup telemetry client
54+
IConfigurationRoot appConfigurationRoot = new ConfigurationBuilder()
55+
.SetBasePath(ExecutingPath)
56+
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
57+
.Build();
58+
59+
NanoTelemetryClient.ConnectionString = appConfigurationRoot?["iConnectionString"];
60+
5461
// check for empty argument collection
5562
if (!args.Any())
5663
{
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"iConnectionString": "INSTRUMENT_KEY"
3+
}

nanoFirmwareFlasher.Tool/nanoFirmwareFlasher.Tool.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@
6969
<ProjectReference Include="..\nanoFirmwareFlasher.Library\nanoFirmwareFlasher.Library.csproj" />
7070
</ItemGroup>
7171

72+
<ItemGroup>
73+
<None Update="appsettings.json">
74+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
75+
</None>
76+
</ItemGroup>
77+
7278
<Target Name="CopyToolsContent" AfterTargets="Build">
7379
<ItemGroup>
7480
<LibSourceFiles Include="$(MSBuildThisFileDirectory)\..\lib\**\*.*" />

0 commit comments

Comments
 (0)