forked from microsoft/qsharp-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTelemetry.cs
67 lines (57 loc) · 2.65 KB
/
Telemetry.cs
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.VisualStudio.Telemetry;
namespace Microsoft.Quantum.QsLanguageExtensionVS
{
public static class Telemetry
{
public enum ExtensionEvent
{
Activate, // when the LS gets started (when a .qs file is open)
LspReady, // when the LS reports is ready.
Error
}
public const string PUBLISHER = "quantum";
public const string EXTENSION = "devkit";
public static readonly Version Version;
static Telemetry()
{
try { Version = typeof(Telemetry).Assembly.GetName().Version; }
catch { Version = new Version(0, 0); }
}
public static string PrefixEventName(string eventName) =>
$"{PUBLISHER}/{EXTENSION}/{eventName?.ToLowerInvariant()}";
public static string PrefixProperty(string propName)
{
if (propName == null) throw new ArgumentNullException(nameof(propName));
if (propName.StartsWith(PUBLISHER, StringComparison.InvariantCultureIgnoreCase)) return propName;
return $"{PUBLISHER}.{EXTENSION}.{propName?.ToLowerInvariant()}";
}
/// Does nothing unless the corresponding flag is defined upon compilation.
/// If the telemetry flag is defined upon compilation,
/// sends the telemetry event for the event with the given name and properties.
/// Ignores any properties where the key is null or "version" (case insensitive).
/// Adds the version of the extension assembly as version.
public static void SendEvent(string name, IEnumerable<KeyValuePair<string, object>> props = null)
{
#if TELEMETRY
props = props ?? Enumerable.Empty<KeyValuePair<string, object>>();
var evt = new TelemetryEvent(PrefixEventName(name));
foreach (var entry in props.Where(p => !string.IsNullOrEmpty(p.Key)))
{
evt.Properties[PrefixProperty(entry.Key)] = entry.Value;
}
evt.Properties[PrefixProperty("version")] = Telemetry.Version;
try { TelemetryService.DefaultSession.PostEvent(evt); }
catch (Exception ex)
{ Debug.Assert(false, $"error sending telemetry: \n{ex}"); }
#endif
}
public static void SendEvent(ExtensionEvent id, params (string, object)[] props) =>
SendEvent(id.ToString("g"), props.Select(entry => new KeyValuePair<string, object>(entry.Item1, entry.Item2)));
}
}