Skip to content

Commit 890e8fe

Browse files
Add environment and has website instance ID flag to events (#203)
1 parent 10b3f96 commit 890e8fe

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

src/Telemetry/Telemetry.cs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,32 @@ public void Initialize(IConfiguration config, ILoggerFactory loggerFactory)
5757
CurrentSessionId = Guid.NewGuid().ToString();
5858

5959
string productVersion = typeof(Telemetry).Assembly.GetName().Version.ToString();
60-
//initialize in task to offload to parallel thread
61-
this._trackEventTask = Task.Factory.StartNew(() => this.InitializeTelemetry(productVersion));
60+
// initialize in task to offload to parallel thread
61+
this._trackEventTask = Task.Factory.StartNew(() => this.InitializeTelemetry(productVersion, config));
6262
this._initialized = true;
6363
}
6464

65+
private void InitializeTelemetry(string productVersion, IConfiguration config)
66+
{
67+
try
68+
{
69+
var telemetryConfig = new TelemetryConfiguration(InstrumentationKey);
70+
this._client = new TelemetryClient(telemetryConfig);
71+
this._client.Context.Session.Id = CurrentSessionId;
72+
this._client.Context.Device.OperatingSystem = RuntimeInformation.OSDescription;
73+
74+
this._commonProperties = new TelemetryCommonProperties(productVersion, this._client, config).GetTelemetryCommonProperties();
75+
this._commonMeasurements = new Dictionary<string, double>();
76+
}
77+
catch (Exception e)
78+
{
79+
this._client.TrackException(e);
80+
this._client = null;
81+
// we don't want to fail the tool if telemetry fails.
82+
Debug.Fail(e.ToString());
83+
}
84+
}
85+
6586
public bool Enabled { get; private set; }
6687

6788
public void TrackEvent(TelemetryEventName eventName, IDictionary<string, string> properties = null,
@@ -177,27 +198,6 @@ public void TrackError(TelemetryErrorName errorName, Exception ex, IDictionary<s
177198
}
178199
}
179200

180-
private void InitializeTelemetry(string productVersion)
181-
{
182-
try
183-
{
184-
var config = new TelemetryConfiguration(InstrumentationKey);
185-
this._client = new TelemetryClient(config);
186-
this._client.Context.Session.Id = CurrentSessionId;
187-
this._client.Context.Device.OperatingSystem = RuntimeInformation.OSDescription;
188-
189-
this._commonProperties = new TelemetryCommonProperties(productVersion, this._client).GetTelemetryCommonProperties();
190-
this._commonMeasurements = new Dictionary<string, double>();
191-
}
192-
catch (Exception e)
193-
{
194-
this._client.TrackException(e);
195-
this._client = null;
196-
// we don't want to fail the tool if telemetry fails.
197-
Debug.Fail(e.ToString());
198-
}
199-
}
200-
201201
private void TrackEventTask(
202202
string eventName,
203203
IDictionary<string, string> properties,

src/Telemetry/TelemetryCommonProperties.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,34 @@
55
using System.Collections.Generic;
66
using System.Runtime.InteropServices;
77
using Microsoft.ApplicationInsights;
8+
using Microsoft.Extensions.Configuration;
89

910
namespace Microsoft.Azure.WebJobs.Extensions.Sql.Telemetry
1011
{
1112
public class TelemetryCommonProperties
1213
{
1314
private readonly string _productVersion;
15+
private readonly string _azureFunctionsEnvironment;
16+
private readonly bool _hasWebsiteInstanceId;
1417

15-
public TelemetryCommonProperties(string productVersion, TelemetryClient telemetryClient)
18+
public TelemetryCommonProperties(string productVersion, TelemetryClient telemetryClient, IConfiguration config)
1619
{
1720
this._productVersion = productVersion;
1821
this._userLevelCacheWriter = new UserLevelCacheWriter(telemetryClient);
22+
this._azureFunctionsEnvironment = config.GetValue(AZURE_FUNCTIONS_ENVIRONMENT_KEY, "");
23+
this._hasWebsiteInstanceId = config.GetValue(WEBSITE_INSTANCE_ID_KEY, "") != "";
1924
}
2025

2126
private readonly UserLevelCacheWriter _userLevelCacheWriter;
2227

2328
private const string OSVersion = "OSVersion";
2429
private const string ProductVersion = "ProductVersion";
2530
private const string MachineId = "MachineId";
31+
private const string AzureFunctionsEnvironment = "AzureFunctionsEnvironment";
32+
private const string HasWebsiteInstanceId = "HasWebsiteInstanceId";
33+
34+
private const string AZURE_FUNCTIONS_ENVIRONMENT_KEY = "AZURE_FUNCTIONS_ENVIRONMENT";
35+
private const string WEBSITE_INSTANCE_ID_KEY = "WEBSITE_INSTANCE_ID";
2636

2737
public Dictionary<string, string> GetTelemetryCommonProperties()
2838
{
@@ -31,6 +41,8 @@ public Dictionary<string, string> GetTelemetryCommonProperties()
3141
{OSVersion, RuntimeInformation.OSDescription},
3242
{ProductVersion, this._productVersion},
3343
{MachineId, this.GetMachineId()},
44+
{AzureFunctionsEnvironment, this._azureFunctionsEnvironment},
45+
{HasWebsiteInstanceId, this._hasWebsiteInstanceId.ToString()}
3446
};
3547
}
3648

0 commit comments

Comments
 (0)