Skip to content

Commit 10b3f96

Browse files
Add machine ID and exception logging (#202)
* Add machine ID * Add exception tracking
1 parent ca0e899 commit 10b3f96

File tree

3 files changed

+82
-4
lines changed

3 files changed

+82
-4
lines changed

src/Telemetry/Telemetry.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,12 @@ private void InitializeTelemetry(string productVersion)
186186
this._client.Context.Session.Id = CurrentSessionId;
187187
this._client.Context.Device.OperatingSystem = RuntimeInformation.OSDescription;
188188

189-
this._commonProperties = new TelemetryCommonProperties(productVersion).GetTelemetryCommonProperties();
189+
this._commonProperties = new TelemetryCommonProperties(productVersion, this._client).GetTelemetryCommonProperties();
190190
this._commonMeasurements = new Dictionary<string, double>();
191191
}
192192
catch (Exception e)
193193
{
194+
this._client.TrackException(e);
194195
this._client = null;
195196
// we don't want to fail the tool if telemetry fails.
196197
Debug.Fail(e.ToString());
Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,48 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.Runtime.InteropServices;
7+
using Microsoft.ApplicationInsights;
68

79
namespace Microsoft.Azure.WebJobs.Extensions.Sql.Telemetry
810
{
911
public class TelemetryCommonProperties
1012
{
1113
private readonly string _productVersion;
1214

13-
public TelemetryCommonProperties(
14-
string productVersion)
15+
public TelemetryCommonProperties(string productVersion, TelemetryClient telemetryClient)
1516
{
1617
this._productVersion = productVersion;
18+
this._userLevelCacheWriter = new UserLevelCacheWriter(telemetryClient);
1719
}
1820

21+
private readonly UserLevelCacheWriter _userLevelCacheWriter;
22+
1923
private const string OSVersion = "OSVersion";
2024
private const string ProductVersion = "ProductVersion";
25+
private const string MachineId = "MachineId";
2126

2227
public Dictionary<string, string> GetTelemetryCommonProperties()
2328
{
2429
return new Dictionary<string, string>
2530
{
2631
{OSVersion, RuntimeInformation.OSDescription},
27-
{ProductVersion, this._productVersion}
32+
{ProductVersion, this._productVersion},
33+
{MachineId, this.GetMachineId()},
2834
};
2935
}
36+
37+
private string GetMachineId()
38+
{
39+
return this._userLevelCacheWriter.RunWithCache(MachineId, () =>
40+
{
41+
// For now just use random GUID. Typically hashed MAC address is used but that's not
42+
// something we need currently - a GUID per user is sufficient.
43+
return Guid.NewGuid().ToString();
44+
});
45+
}
3046
}
3147
}
3248

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.IO;
6+
using Microsoft.ApplicationInsights;
7+
8+
namespace Microsoft.Azure.WebJobs.Extensions.Sql.Telemetry
9+
{
10+
public sealed class UserLevelCacheWriter
11+
{
12+
private const string AzureFunctionsSqlBindingsProfileDirectoryName = ".azurefunctions-sqlbindings";
13+
private readonly string _azureFunctionsSqlBindingsTryUserProfileFolderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), AzureFunctionsSqlBindingsProfileDirectoryName);
14+
private readonly TelemetryClient _telemetryClient;
15+
16+
public UserLevelCacheWriter(TelemetryClient telemetryClient)
17+
{
18+
this._telemetryClient = telemetryClient;
19+
}
20+
21+
public string RunWithCache(string cacheKey, Func<string> getValueToCache)
22+
{
23+
string cacheFilepath = this.GetCacheFilePath(cacheKey);
24+
try
25+
{
26+
if (!File.Exists(cacheFilepath))
27+
{
28+
if (!Directory.Exists(this._azureFunctionsSqlBindingsTryUserProfileFolderPath))
29+
{
30+
Directory.CreateDirectory(this._azureFunctionsSqlBindingsTryUserProfileFolderPath);
31+
}
32+
33+
string runResult = getValueToCache();
34+
35+
File.WriteAllText(cacheFilepath, runResult);
36+
return runResult;
37+
}
38+
else
39+
{
40+
return File.ReadAllText(cacheFilepath);
41+
}
42+
}
43+
catch (Exception ex)
44+
{
45+
if (ex is UnauthorizedAccessException
46+
|| ex is PathTooLongException
47+
|| ex is IOException)
48+
{
49+
this._telemetryClient.TrackException(ex);
50+
return getValueToCache();
51+
}
52+
53+
throw;
54+
}
55+
}
56+
private string GetCacheFilePath(string cacheKey)
57+
{
58+
return Path.Combine(this._azureFunctionsSqlBindingsTryUserProfileFolderPath, $"{cacheKey}.azureFunctionsSqlBindingsTryUserLevelCache");
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)