|
| 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.Collections.Generic; |
| 6 | +using System.Diagnostics; |
| 7 | +using System.Runtime.InteropServices; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Microsoft.Extensions.Logging; |
| 10 | +using Microsoft.ApplicationInsights; |
| 11 | +using Microsoft.ApplicationInsights.Extensibility; |
| 12 | +using Microsoft.Extensions.Configuration; |
| 13 | +using Microsoft.Azure.WebJobs.Logging; |
| 14 | + |
| 15 | +namespace Microsoft.Azure.WebJobs.Extensions.Sql.Telemetry |
| 16 | +{ |
| 17 | + public sealed class Telemetry |
| 18 | + { |
| 19 | + internal static Telemetry Instance = new Telemetry(); |
| 20 | + |
| 21 | + private const string EventsNamespace = "azure-functions-sql-bindings"; |
| 22 | + internal static string CurrentSessionId; |
| 23 | + private TelemetryClient _client; |
| 24 | + private Dictionary<string, string> _commonProperties; |
| 25 | + private Dictionary<string, double> _commonMeasurements; |
| 26 | + private Task _trackEventTask; |
| 27 | + private ILogger _logger; |
| 28 | + private bool _initialized; |
| 29 | + private const string InstrumentationKey = "98697a1c-1416-486a-99ac-c6c74ebe5ebd"; |
| 30 | + /// <summary> |
| 31 | + /// The environment variable used for opting out of telemetry |
| 32 | + /// </summary> |
| 33 | + public const string TelemetryOptoutEnvVar = "AZUREFUNCTIONS_SQLBINDINGS_TELEMETRY_OPTOUT"; |
| 34 | + /// <summary> |
| 35 | + /// The app setting used for opting out of telemetry |
| 36 | + /// </summary> |
| 37 | + public const string TelemetryOptoutSetting = "AzureFunctionsSqlBindingsTelemetryOptOut"; |
| 38 | + |
| 39 | + public const string WelcomeMessage = @"Azure SQL binding for Azure Functions |
| 40 | +--------------------- |
| 41 | +Telemetry |
| 42 | +--------- |
| 43 | +This extension collect usage data in order to help us improve your experience. The data is anonymous and doesn't include any personal information. You can opt-out of telemetry by setting the " + TelemetryOptoutEnvVar + " environment variable or the " + TelemetryOptoutSetting + @" + app setting to '1', 'true' or 'yes'; |
| 44 | +"; |
| 45 | + |
| 46 | + public void Initialize(IConfiguration config, ILoggerFactory loggerFactory) |
| 47 | + { |
| 48 | + this._logger = loggerFactory.CreateLogger(LogCategories.Bindings); |
| 49 | + this.Enabled = !(Utils.GetEnvironmentVariableAsBool(TelemetryOptoutEnvVar) || Utils.GetConfigSettingAsBool(TelemetryOptoutSetting, config)); |
| 50 | + if (!this.Enabled) |
| 51 | + { |
| 52 | + this._logger.LogInformation("Telemetry disabled"); |
| 53 | + return; |
| 54 | + } |
| 55 | + this._logger.LogInformation(WelcomeMessage); |
| 56 | + // Store the session ID in a static field so that it can be reused |
| 57 | + CurrentSessionId = Guid.NewGuid().ToString(); |
| 58 | + |
| 59 | + 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)); |
| 62 | + this._initialized = true; |
| 63 | + } |
| 64 | + |
| 65 | + public bool Enabled { get; private set; } |
| 66 | + |
| 67 | + public void TrackEvent(string eventName, IDictionary<string, string> properties, |
| 68 | + IDictionary<string, double> measurements) |
| 69 | + { |
| 70 | + if (!this._initialized || !this.Enabled) |
| 71 | + { |
| 72 | + return; |
| 73 | + } |
| 74 | + this._logger.LogInformation($"Sending event {eventName}"); |
| 75 | + |
| 76 | + //continue task in existing parallel thread |
| 77 | + this._trackEventTask = this._trackEventTask.ContinueWith( |
| 78 | + x => this.TrackEventTask(eventName, properties, measurements) |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + private void InitializeTelemetry(string productVersion) |
| 83 | + { |
| 84 | + try |
| 85 | + { |
| 86 | + var config = new TelemetryConfiguration(InstrumentationKey); |
| 87 | + this._client = new TelemetryClient(config); |
| 88 | + this._client.Context.Session.Id = CurrentSessionId; |
| 89 | + this._client.Context.Device.OperatingSystem = RuntimeInformation.OSDescription; |
| 90 | + |
| 91 | + this._commonProperties = new TelemetryCommonProperties(productVersion).GetTelemetryCommonProperties(); |
| 92 | + this._commonMeasurements = new Dictionary<string, double>(); |
| 93 | + } |
| 94 | + catch (Exception e) |
| 95 | + { |
| 96 | + this._client = null; |
| 97 | + // we don't want to fail the tool if telemetry fails. |
| 98 | + Debug.Fail(e.ToString()); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private void TrackEventTask( |
| 103 | + string eventName, |
| 104 | + IDictionary<string, string> properties, |
| 105 | + IDictionary<string, double> measurements) |
| 106 | + { |
| 107 | + if (this._client is null) |
| 108 | + { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + try |
| 113 | + { |
| 114 | + Dictionary<string, string> eventProperties = this.GetEventProperties(properties); |
| 115 | + Dictionary<string, double> eventMeasurements = this.GetEventMeasures(measurements); |
| 116 | + |
| 117 | + this._client.TrackEvent($"{EventsNamespace}/{eventName}", eventProperties, eventMeasurements); |
| 118 | + this._client.Flush(); |
| 119 | + } |
| 120 | + catch (Exception e) |
| 121 | + { |
| 122 | + Debug.Fail(e.ToString()); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private Dictionary<string, double> GetEventMeasures(IDictionary<string, double> measurements) |
| 127 | + { |
| 128 | + var eventMeasurements = new Dictionary<string, double>(this._commonMeasurements); |
| 129 | + if (measurements != null) |
| 130 | + { |
| 131 | + foreach (KeyValuePair<string, double> measurement in measurements) |
| 132 | + { |
| 133 | + eventMeasurements[measurement.Key] = measurement.Value; |
| 134 | + } |
| 135 | + } |
| 136 | + return eventMeasurements; |
| 137 | + } |
| 138 | + |
| 139 | + private Dictionary<string, string> GetEventProperties(IDictionary<string, string> properties) |
| 140 | + { |
| 141 | + if (properties != null) |
| 142 | + { |
| 143 | + var eventProperties = new Dictionary<string, string>(this._commonProperties); |
| 144 | + foreach (KeyValuePair<string, string> property in properties) |
| 145 | + { |
| 146 | + eventProperties[property.Key] = property.Value; |
| 147 | + } |
| 148 | + return eventProperties; |
| 149 | + } |
| 150 | + else |
| 151 | + { |
| 152 | + return this._commonProperties; |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | + |
0 commit comments