Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/Microsoft.ComponentDetection.Common/DockerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -52,7 +52,7 @@ public async Task<bool> 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)
Expand All @@ -72,7 +72,7 @@ public async Task<bool> 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)
Expand All @@ -97,10 +97,10 @@ public async Task<bool> TryPullImageAsync(string image, CancellationToken cancel
var createImageProgress = new List<string>();
var progress = new Progress<JSONMessage>(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)
Expand All @@ -119,7 +119,7 @@ public async Task<ContainerDetails> 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;
Expand Down Expand Up @@ -162,11 +162,11 @@ public async Task<ContainerDetails> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<PackageReference Include="Docker.DotNet" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" />
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="System.Reactive" />
<PackageReference Include="System.Threading.Tasks.Dataflow" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// A telemetry service that writes records to a file.
/// </summary>
internal class CommandLineTelemetryService : ITelemetryService
{
private static readonly ConcurrentQueue<JObject> Records = new ConcurrentQueue<JObject>();

public const string TelemetryRelativePath = "ScanTelemetry_{timestamp}.json";

private readonly ILogger logger;
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;
}

public void Flush()
{
this.fileWritingService.WriteFile(TelemetryRelativePath, JsonConvert.SerializeObject(Records));
}
/// <inheritdoc/>
public void Flush() => this.fileWritingService.WriteFile(TelemetryRelativePath, JsonSerializer.Serialize(this.records));

/// <inheritdoc/>
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;
}
/// <inheritdoc/>
public void SetMode(TelemetryMode mode) => this.telemetryMode = mode;
}