forked from microsoft/iqsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTelemetry.cs
More file actions
185 lines (143 loc) · 7.61 KB
/
Copy pathTelemetry.cs
File metadata and controls
185 lines (143 loc) · 7.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#if TELEMETRY
using System;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using Microsoft.Applications.Events;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Jupyter.Core;
using Microsoft.Quantum.QsCompiler.CompilationBuilder;
using Microsoft.Quantum.Simulation.Simulators;
using static Microsoft.Jupyter.Core.BaseEngine;
namespace Microsoft.Quantum.IQSharp
{
public class Telemetry
{
public Telemetry(ILogger logger)
{
if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}
this.Logger = logger;
}
public ILogger Logger { get; }
public void InitServices(IServiceProvider services, IConfiguration config)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
InitLogManager(config);
var console = services.GetService<Microsoft.Extensions.Logging.ILogger<Telemetry>>();
Microsoft.Extensions.Logging.LoggerExtensions.LogInformation(console, "Starting Telemetry.");
Microsoft.Extensions.Logging.LoggerExtensions.LogDebug(console, $"DeviceId: {GetDeviceId()}.");
var snippets = services.GetService<ISnippets>();
var workspace = services.GetService<IWorkspace>();
var references = services.GetService<IReferences>();
var executionEngine = services.GetService<IExecutionEngine>();
snippets.SnippetCompiled += (_, info) => this.Logger.LogEvent(info.AsTelemetryEvent());
workspace.Reloaded += (_, info) => this.Logger.LogEvent(info.AsTelemetryEvent());
references.PackageLoaded += (_, info) => this.Logger.LogEvent(info.AsTelemetryEvent());
if (executionEngine is BaseEngine engine)
{
engine.MagicExecuted += (_, info) => this.Logger.LogEvent(info.AsTelemetryEvent());
engine.HelpExecuted += (_, info) => this.Logger.LogEvent(info.AsTelemetryEvent());
}
}
private readonly static string TOKEN = "55aee962ee9445f3a86af864fc0fa766-48882422-3439-40de-8030-228042bd9089-7794";
public static Telemetry _instance;
private static IConfiguration _config;
public static void Start(KernelApplication app, IConfiguration config)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (!string.IsNullOrWhiteSpace(config?.GetValue<string>("IQSHARP_TELEMETRY_OPT_OUT")))
{
Console.WriteLine("--> IQ# Telemetry opted-out. No telemetry data will be generated.");
return;
}
_config = config;
app.KernelStarted += OnKenerlStart;
app.KernelStopped += OnKernelStop;
}
public static void OnKenerlStart(ServiceProvider services)
{
LogManager.Start(new LogConfiguration());
_instance = new Telemetry(LogManager.GetLogger(TOKEN, out EVTStatus value));
_instance.InitServices(services, _config);
_instance.Logger.LogEvent("SessionStart".AsTelemetryEvent());
}
public static void OnKernelStop()
{
_instance.Logger.LogEvent("SessionEnd".AsTelemetryEvent());
LogManager.Teardown();
}
public static void InitLogManager(IConfiguration config)
{
LogManager.SetSharedContext("AppInfo.Id", "iq#");
LogManager.SetSharedContext("AppInfo.Version", Jupyter.Constants.IQSharpKernelProperties.KernelVersion);
LogManager.SetSharedContext("CompilerVersion".WithTelemetryNamespace(), typeof(CompilationUnitManager).Assembly.GetName().Version.ToString());
LogManager.SetSharedContext("SimulationVersion".WithTelemetryNamespace(), typeof(QuantumSimulator).Assembly.GetName().Version.ToString());
LogManager.SetSharedContext("DeviceId".WithTelemetryNamespace(), GetDeviceId(), PiiKind.GenericData);
LogManager.SetSharedContext("UserAgent".WithTelemetryNamespace(), config?.GetValue<string>("IQSHARP_USER_AGENT"));
LogManager.SetSharedContext("HostingEnvironment".WithTelemetryNamespace(), config?.GetValue<string>("IQSHARP_HOSTING_ENV"));
}
/// <summary>
/// Return an Id for this device, namely, the first non-empty MAC address it can find across all network interfaces (if any).
/// </summary>
public static string GetDeviceId() =>
NetworkInterface.GetAllNetworkInterfaces()?
.Select(n => n?.GetPhysicalAddress()?.ToString())
.Where(address => address != null && !string.IsNullOrWhiteSpace(address) && !address.StartsWith("000000"))
.FirstOrDefault();
}
public static class TelemetryExtensions
{
public static string WithTelemetryNamespace(this string name) =>
$"Quantum.IQSharp.{name}";
public static EventProperties AsTelemetryEvent(this string name) =>
new EventProperties() { Name = name.WithTelemetryNamespace() };
public static EventProperties AsTelemetryEvent(this ReloadedEventArgs info)
{
var evt = new EventProperties() { Name = "WorkspaceReload".WithTelemetryNamespace() };
evt.SetProperty("Workspace".WithTelemetryNamespace(), Path.GetFileName(info.Workspace), PiiKind.GenericData);
evt.SetProperty("Status".WithTelemetryNamespace(), info.Status);
evt.SetProperty("FileCount".WithTelemetryNamespace(), info.FileCount);
evt.SetProperty("Errors".WithTelemetryNamespace(), string.Join(",", info.Errors?.OrderBy(e => e) ?? Enumerable.Empty<string>()));
evt.SetProperty("Duration".WithTelemetryNamespace(), info.Duration.ToString());
return evt;
}
public static EventProperties AsTelemetryEvent(this SnippetCompiledEventArgs info)
{
var evt = new EventProperties() { Name = "Compile".WithTelemetryNamespace() };
evt.SetProperty("Status".WithTelemetryNamespace(), info.Status);
evt.SetProperty("Errors".WithTelemetryNamespace(), string.Join(",", info.Errors?.OrderBy(e => e) ?? Enumerable.Empty<string>()));
evt.SetProperty("Duration".WithTelemetryNamespace(), info.Duration.ToString());
return evt;
}
public static EventProperties AsTelemetryEvent(this PackageLoadedEventArgs info)
{
var evt = new EventProperties() { Name = "PackageLoad".WithTelemetryNamespace() };
evt.SetProperty("PackageId".WithTelemetryNamespace(), info.PackageId);
evt.SetProperty("PackageVersion".WithTelemetryNamespace(), info.PackageVersion);
evt.SetProperty("Duration".WithTelemetryNamespace(), info.Duration.ToString());
return evt;
}
public static EventProperties AsTelemetryEvent(this ExecutedEventArgs info)
{
var evt = new EventProperties() { Name = "Action".WithTelemetryNamespace() };
evt.SetProperty("Command".WithTelemetryNamespace(), info.Symbol?.Name);
evt.SetProperty("Kind".WithTelemetryNamespace(), info.Symbol?.Kind.ToString());
evt.SetProperty("Status".WithTelemetryNamespace(), info.Result.Status.ToString());
evt.SetProperty("Duration".WithTelemetryNamespace(), info.Duration.ToString());
return evt;
}
}
}
#endif