-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathCommandLineTelemetryService.cs
57 lines (48 loc) · 2.01 KB
/
CommandLineTelemetryService.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
namespace Microsoft.ComponentDetection.Common.Telemetry;
using System;
using System.Collections.Concurrent;
using System.Text.Json;
using System.Text.Json.Nodes;
using Microsoft.ComponentDetection.Common.Telemetry.Records;
using Microsoft.Extensions.Logging;
/// <summary>
/// A telemetry service that writes records to a file.
/// </summary>
internal class CommandLineTelemetryService : ITelemetryService
{
private const string TelemetryRelativePath = "ScanTelemetry_{timestamp}.json";
private readonly ConcurrentQueue<JsonNode> records = new();
private readonly IFileWritingService fileWritingService;
private readonly ILogger logger;
private TelemetryMode telemetryMode = TelemetryMode.Production;
/// <summary>
/// Initializes a new instance of the <see cref="CommandLineTelemetryService"/> class.
/// </summary>
/// <param name="logger">The logger.</param>
/// <param name="fileWritingService">The file writing service.</param>
public CommandLineTelemetryService(ILogger<CommandLineTelemetryService> logger, IFileWritingService fileWritingService)
{
this.logger = logger;
this.fileWritingService = fileWritingService;
}
/// <inheritdoc/>
public void Flush() => this.fileWritingService.WriteFile(TelemetryRelativePath, JsonSerializer.Serialize(this.records));
/// <inheritdoc/>
public void PostRecord(IDetectionTelemetryRecord record)
{
if (this.telemetryMode == TelemetryMode.Disabled)
{
return;
}
var jsonRecord = JsonSerializer.SerializeToNode(record, record.GetType());
jsonRecord["Timestamp"] = DateTime.UtcNow;
jsonRecord["CorrelationId"] = TelemetryConstants.CorrelationId;
this.records.Enqueue(jsonRecord);
if (this.telemetryMode == TelemetryMode.Debug)
{
this.logger.LogInformation("Telemetry record: {Record}", jsonRecord.ToString());
}
}
/// <inheritdoc/>
public void SetMode(TelemetryMode mode) => this.telemetryMode = mode;
}