forked from Azure/azure-sdk-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTelemetryService.cs
More file actions
60 lines (50 loc) · 2.45 KB
/
TelemetryService.cs
File metadata and controls
60 lines (50 loc) · 2.45 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Text.Json;
namespace Azure.Sdk.Tools.Cli.Services;
public static class TelemetryService
{
private const int MaxInstrumentationUploadTime = 5;
public static void InstrumentationBefore(ILogger logger, string toolName, object? args, CancellationToken ct)
{
using var timeoutCts = new CancellationTokenSource(TimeSpan.FromSeconds(MaxInstrumentationUploadTime));
using var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(ct, timeoutCts.Token);
Task.Run(() => _instrumentationBefore(logger, toolName, args), linkedCts.Token);
}
private static void _instrumentationBefore(ILogger logger, string toolName, object? args)
{
// TODO: replace with app insights
logger.LogDebug("[tool req] {toolName} [args] {args}", toolName, args);
}
public static void InstrumentationAfter(ILogger logger, string toolName, object? result, CancellationToken ct)
{
using var timeoutCts = new CancellationTokenSource(TimeSpan.FromSeconds(MaxInstrumentationUploadTime));
using var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(ct, timeoutCts.Token);
Task.Run(() => _instrumentationAfter(logger, toolName, result), linkedCts.Token);
}
private static void _instrumentationAfter(ILogger logger, string toolName, object? result)
{
var serialized = "SERIALIZER ERROR";
try
{
serialized = JsonSerializer.Serialize(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error serializing tool response for instrumentation");
}
// TODO: replace with app insights
logger.LogDebug("[tool resp] {toolName} [result] {result}", toolName, serialized);
}
public static void InstrumentationError(ILogger logger, string toolName, Exception ex, CancellationToken ct)
{
using var timeoutCts = new CancellationTokenSource(TimeSpan.FromSeconds(MaxInstrumentationUploadTime));
using var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(ct, timeoutCts.Token);
// TODO: replace with app insights
Task.Run(() => _instrumentationError(logger, toolName, ex), linkedCts.Token);
}
private static void _instrumentationError(ILogger logger, string toolName, Exception ex)
{
logger.LogError(ex, "[tool error] {toolName} [error] {error}", toolName, ex.Message);
}
}