-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathTelemetryRelay.cs
83 lines (73 loc) · 2.49 KB
/
TelemetryRelay.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
namespace Microsoft.ComponentDetection.Common.Telemetry;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.ComponentDetection.Common.Telemetry.Records;
/// <summary>
/// Singleton that is responsible for relaying telemetry records to Telemetry Services.
/// </summary>
public sealed class TelemetryRelay
{
private IEnumerable<ITelemetryService> telemetryServices;
// For things not populating the telemetry services collection, let's not throw.
private TelemetryRelay() =>
this.telemetryServices = [];
/// <summary>
/// Gets a value indicating whether or not the telemetry relay has been shutdown.
/// </summary>
public static bool Active { get; private set; } = true;
/// <summary>
/// Gets the singleton.
/// </summary>
public static TelemetryRelay Instance { get; } = new TelemetryRelay();
public void Init(IEnumerable<ITelemetryService> telemetryServices) => this.telemetryServices = telemetryServices;
/// <summary>
/// Post a given telemetry record to all telemetry services.
/// </summary>
/// <param name="record">Record to post. </param>
public void PostTelemetryRecord(IDetectionTelemetryRecord record)
{
foreach (var service in this.telemetryServices)
{
try
{
service.PostRecord(record);
}
catch
{
// Telemetry should never crash the application
}
}
}
/// <summary>
/// Disables the sending of telemetry and flushes any messages out of the queue for each service.
/// </summary>
/// <returns><see cref="Task"/> representing the asynchronous operation.</returns>
public async Task ShutdownAsync()
{
Active = false;
foreach (var service in this.telemetryServices)
{
try
{
// Set a timeout for services that flush synchronously.
await AsyncExecution.ExecuteVoidWithTimeoutAsync(
() => service.Flush(),
TimeSpan.FromSeconds(20),
CancellationToken.None);
}
catch
{
Console.WriteLine("Logging output failed");
}
}
}
public void SetTelemetryMode(TelemetryMode mode)
{
foreach (var telemetryService in this.telemetryServices ?? [])
{
telemetryService.SetMode(mode);
}
}
}