From 7517b7653a784dc5c1394c3a88dec44aca35d9ba Mon Sep 17 00:00:00 2001 From: Jamie Magee Date: Thu, 4 May 2023 14:24:09 -0700 Subject: [PATCH 1/2] refactor: migrate to `System.Text.Json` in `Microsoft.ComponentDetection.Common` --- .../DockerService.cs | 16 +++--- .../Telemetry/CommandLineTelemetryService.cs | 54 ++++++++++--------- 2 files changed, 37 insertions(+), 33 deletions(-) diff --git a/src/Microsoft.ComponentDetection.Common/DockerService.cs b/src/Microsoft.ComponentDetection.Common/DockerService.cs index 906384648..66033f183 100644 --- a/src/Microsoft.ComponentDetection.Common/DockerService.cs +++ b/src/Microsoft.ComponentDetection.Common/DockerService.cs @@ -4,6 +4,7 @@ namespace Microsoft.ComponentDetection.Common; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text.Json; using System.Threading; using System.Threading.Tasks; using Docker.DotNet; @@ -12,7 +13,6 @@ namespace Microsoft.ComponentDetection.Common; using Microsoft.ComponentDetection.Contracts; using Microsoft.ComponentDetection.Contracts.BcdeModels; using Microsoft.Extensions.Logging; -using Newtonsoft.Json; public class DockerService : IDockerService { @@ -52,7 +52,7 @@ public async Task CanRunLinuxContainersAsync(CancellationToken cancellatio try { var systemInfoResponse = await Client.System.GetSystemInfoAsync(cancellationToken); - record.SystemInfo = JsonConvert.SerializeObject(systemInfoResponse); + record.SystemInfo = JsonSerializer.Serialize(systemInfoResponse); return string.Equals(systemInfoResponse.OSType, "linux", StringComparison.OrdinalIgnoreCase); } catch (Exception e) @@ -72,7 +72,7 @@ public async Task ImageExistsLocallyAsync(string image, CancellationToken try { var imageInspectResponse = await Client.Images.InspectImageAsync(image, cancellationToken); - record.ImageInspectResponse = JsonConvert.SerializeObject(imageInspectResponse); + record.ImageInspectResponse = JsonSerializer.Serialize(imageInspectResponse); return true; } catch (Exception e) @@ -97,10 +97,10 @@ public async Task TryPullImageAsync(string image, CancellationToken cancel var createImageProgress = new List(); var progress = new Progress(message => { - createImageProgress.Add(JsonConvert.SerializeObject(message)); + createImageProgress.Add(JsonSerializer.Serialize(message)); }); await Client.Images.CreateImageAsync(parameters, null, progress, cancellationToken); - record.CreateImageProgress = JsonConvert.SerializeObject(createImageProgress); + record.CreateImageProgress = JsonSerializer.Serialize(createImageProgress); return true; } catch (Exception e) @@ -119,7 +119,7 @@ public async Task InspectImageAsync(string image, Cancellation try { var imageInspectResponse = await Client.Images.InspectImageAsync(image, cancellationToken); - record.ImageInspectResponse = JsonConvert.SerializeObject(imageInspectResponse); + record.ImageInspectResponse = JsonSerializer.Serialize(imageInspectResponse); var baseImageRef = string.Empty; var baseImageDigest = string.Empty; @@ -162,11 +162,11 @@ public async Task InspectImageAsync(string image, Cancellation using var record = new DockerServiceTelemetryRecord { Image = image, - Command = JsonConvert.SerializeObject(command), + Command = JsonSerializer.Serialize(command), }; await this.TryPullImageAsync(image, cancellationToken); var container = await CreateContainerAsync(image, command, cancellationToken); - record.Container = JsonConvert.SerializeObject(container); + record.Container = JsonSerializer.Serialize(container); var stream = await AttachContainerAsync(container.ID, cancellationToken); await StartContainerAsync(container.ID, cancellationToken); var (stdout, stderr) = await stream.ReadOutputToEndAsync(cancellationToken); diff --git a/src/Microsoft.ComponentDetection.Common/Telemetry/CommandLineTelemetryService.cs b/src/Microsoft.ComponentDetection.Common/Telemetry/CommandLineTelemetryService.cs index 450a04f73..0d537c524 100644 --- a/src/Microsoft.ComponentDetection.Common/Telemetry/CommandLineTelemetryService.cs +++ b/src/Microsoft.ComponentDetection.Common/Telemetry/CommandLineTelemetryService.cs @@ -2,52 +2,56 @@ 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; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; +/// +/// A telemetry service that writes records to a file. +/// internal class CommandLineTelemetryService : ITelemetryService { - private static readonly ConcurrentQueue Records = new ConcurrentQueue(); - - public const string TelemetryRelativePath = "ScanTelemetry_{timestamp}.json"; - - private readonly ILogger logger; + private const string TelemetryRelativePath = "ScanTelemetry_{timestamp}.json"; + private readonly ConcurrentQueue records = new(); private readonly IFileWritingService fileWritingService; - + private readonly ILogger logger; private TelemetryMode telemetryMode = TelemetryMode.Production; + /// + /// Initializes a new instance of the class. + /// + /// The logger. + /// The file writing service. public CommandLineTelemetryService(ILogger logger, IFileWritingService fileWritingService) { this.logger = logger; this.fileWritingService = fileWritingService; } - public void Flush() - { - this.fileWritingService.WriteFile(TelemetryRelativePath, JsonConvert.SerializeObject(Records)); - } + /// + public void Flush() => this.fileWritingService.WriteFile(TelemetryRelativePath, JsonSerializer.Serialize(this.records)); + /// public void PostRecord(IDetectionTelemetryRecord record) { - if (this.telemetryMode != TelemetryMode.Disabled) + if (this.telemetryMode == TelemetryMode.Disabled) { - var jsonRecord = JObject.FromObject(record); - jsonRecord.Add("Timestamp", DateTime.UtcNow); - jsonRecord.Add("CorrelationId", TelemetryConstants.CorrelationId); + return; + } + + var jsonRecord = JsonSerializer.SerializeToNode(record, record.GetType()); + jsonRecord["Timestamp"] = DateTime.UtcNow; + jsonRecord["CorrelationId"] = TelemetryConstants.CorrelationId; - Records.Enqueue(jsonRecord); + this.records.Enqueue(jsonRecord); - if (this.telemetryMode == TelemetryMode.Debug) - { - this.logger.LogInformation("Telemetry record: {Record}", jsonRecord.ToString()); - } + if (this.telemetryMode == TelemetryMode.Debug) + { + this.logger.LogInformation("Telemetry record: {Record}", jsonRecord.ToString()); } } - public void SetMode(TelemetryMode mode) - { - this.telemetryMode = mode; - } + /// + public void SetMode(TelemetryMode mode) => this.telemetryMode = mode; } From bf75c7c18644a45a48ccb2bc79b0222517ca4777 Mon Sep 17 00:00:00 2001 From: Jamie Magee Date: Thu, 4 May 2023 14:31:41 -0700 Subject: [PATCH 2/2] remove reference to `Newtonsoft.Json` --- .../Microsoft.ComponentDetection.Common.csproj | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Microsoft.ComponentDetection.Common/Microsoft.ComponentDetection.Common.csproj b/src/Microsoft.ComponentDetection.Common/Microsoft.ComponentDetection.Common.csproj index 9387d8839..2ede352f3 100644 --- a/src/Microsoft.ComponentDetection.Common/Microsoft.ComponentDetection.Common.csproj +++ b/src/Microsoft.ComponentDetection.Common/Microsoft.ComponentDetection.Common.csproj @@ -4,7 +4,6 @@ -