Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Egress Extensibility] Fix Logging Verbosity Propagation #4136

Merged
Show file tree
Hide file tree
Changes from 5 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
25 changes: 12 additions & 13 deletions src/Extensions/AzureBlobStorage/AzureBlobEgressProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,17 @@ internal partial class AzureBlobEgressProvider : EgressProvider<AzureBlobEgressP
{
private int DefaultBlobStorageBufferSize = 4 * 1024 * 1024;

public AzureBlobEgressProvider(ILogger logger) : base(logger)
{
}

public override async Task<string> EgressAsync(
ILogger logger,
AzureBlobEgressProviderOptions options,
Func<Stream, CancellationToken, Task> action,
EgressArtifactSettings artifactSettings,
CancellationToken token)
{
try
{
Logger = logger;

AddConfiguredMetadataAsync(options, artifactSettings);

var containerClient = await GetBlobContainerClientAsync(options, token);
Expand All @@ -60,7 +59,7 @@ public override async Task<string> EgressAsync(
//3. After 4Gi of data has been staged, the data will be committed. This can be forced earlier by flushing
//the stream.
// Since we want the data to be readily available, we automatically flush (and therefore commit) every time we fill up the buffer.
_logger.EgressProviderInvokeStreamAction(Constants.AzureBlobStorageProviderName);
Logger.EgressProviderInvokeStreamAction(Constants.AzureBlobStorageProviderName);
await action(flushStream, token);

await flushStream.FlushAsync(token);
Expand All @@ -72,7 +71,7 @@ public override async Task<string> EgressAsync(
await SetBlobClientMetadata(blobClient, artifactSettings, token);

string blobUriString = GetBlobUri(blobClient);
_logger.EgressProviderSavedStream(Constants.AzureBlobStorageProviderName, blobUriString);
Logger.EgressProviderSavedStream(Constants.AzureBlobStorageProviderName, blobUriString);

if (CheckQueueEgressOptions(options))
{
Expand Down Expand Up @@ -107,7 +106,7 @@ public async Task SetBlobClientMetadata(BlobBaseClient blobClient, EgressArtifac
}
else
{
_logger.DuplicateKeyInMetadata(metadataPair.Key);
Logger.DuplicateKeyInMetadata(metadataPair.Key);
}
}

Expand All @@ -118,7 +117,7 @@ public async Task SetBlobClientMetadata(BlobBaseClient blobClient, EgressArtifac
}
catch (Exception ex) when (ex is InvalidOperationException || ex is RequestFailedException)
{
_logger.InvalidMetadata(ex);
Logger.InvalidMetadata(ex);
await blobClient.SetMetadataAsync(artifactSettings.Metadata, cancellationToken: token);
}
}
Expand All @@ -127,7 +126,7 @@ public void AddConfiguredMetadataAsync(AzureBlobEgressProviderOptions options, E
{
if (artifactSettings.EnvBlock.Count == 0)
{
_logger.EnvironmentBlockNotSupported();
Logger.EnvironmentBlockNotSupported();
return;
}

Expand All @@ -139,7 +138,7 @@ public void AddConfiguredMetadataAsync(AzureBlobEgressProviderOptions options, E
}
else
{
_logger.EnvironmentVariableNotFound(metadataPair.Value);
Logger.EnvironmentVariableNotFound(metadataPair.Value);
}
}
}
Expand All @@ -151,7 +150,7 @@ private bool CheckQueueEgressOptions(AzureBlobEgressProviderOptions options)

if (queueNameSet ^ queueAccountUriSet)
{
_logger.QueueOptionsPartiallySet();
Logger.QueueOptionsPartiallySet();
}

return queueNameSet && queueAccountUriSet;
Expand Down Expand Up @@ -190,11 +189,11 @@ private async Task EgressMessageToQueue(string blobName, AzureBlobEgressProvider
}
catch (RequestFailedException ex) when (ex.Status == ((int)HttpStatusCode.NotFound))
{
_logger.QueueDoesNotExist(options.QueueName);
Logger.QueueDoesNotExist(options.QueueName);
}
catch (Exception ex)
{
_logger.WritingMessageToQueueFailed(options.QueueName, ex);
Logger.WritingMessageToQueueFailed(options.QueueName, ex);
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/Extensions/AzureBlobStorage/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ internal sealed class Program
{
static async Task<int> Main(string[] args)
{
ILogger logger = Utilities.CreateLogger();
AzureBlobEgressProvider provider = new();

AzureBlobEgressProvider provider = new(logger);

Action<ExtensionEgressPayload, AzureBlobEgressProviderOptions> configureOptions = (configPayload, options) =>
Action<ExtensionEgressPayload, AzureBlobEgressProviderOptions, ILogger> configureOptions = (configPayload, options, logger) =>
{
// If account key was not provided but the name was provided,
// lookup the account key property value from EgressOptions.Properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.CommandLine;
Expand All @@ -19,7 +20,7 @@ internal sealed class EgressHelper
private static Stream StdInStream;
private static CancellationTokenSource CancelSource = new CancellationTokenSource();

internal static Command CreateEgressCommand<TOptions>(EgressProvider<TOptions> provider, Action<ExtensionEgressPayload, TOptions> configureOptions = null) where TOptions : class, new()
internal static Command CreateEgressCommand<TOptions>(EgressProvider<TOptions> provider, Action<ExtensionEgressPayload, TOptions, ILogger> configureOptions = null) where TOptions : class, new()
{
Command egressCmd = new Command("Egress", "The class of extension being invoked; Egress is for egressing an artifact.");

Expand All @@ -28,14 +29,20 @@ internal sealed class EgressHelper
return egressCmd;
}

private static async Task<int> Egress<TOptions>(EgressProvider<TOptions> provider, CancellationToken token, Action<ExtensionEgressPayload, TOptions> configureOptions = null) where TOptions : class, new()
private static async Task<int> Egress<TOptions>(EgressProvider<TOptions> provider, CancellationToken token, Action<ExtensionEgressPayload, TOptions, ILogger> configureOptions = null) where TOptions : class, new()
{
EgressArtifactResult result = new();
try
{
string jsonConfig = Console.ReadLine();
ExtensionEgressPayload configPayload = JsonSerializer.Deserialize<ExtensionEgressPayload>(jsonConfig);
TOptions options = BuildOptions(configPayload, configureOptions);

ILogger logger = LoggerFactory.Create(builder =>
kkeirstead marked this conversation as resolved.
Show resolved Hide resolved
{
builder.AddConsole().SetMinimumLevel(configPayload.MinimumLogLevel);
}).CreateLogger<EgressHelper>();

TOptions options = BuildOptions(configPayload, logger, configureOptions);

var context = new ValidationContext(options);

Expand All @@ -51,7 +58,8 @@ internal sealed class EgressHelper

Console.CancelKeyPress += Console_CancelKeyPress;

result.ArtifactPath = await provider.EgressAsync(options,
result.ArtifactPath = await provider.EgressAsync(logger,
options,
GetStream,
configPayload.Settings,
token);
Expand All @@ -71,11 +79,11 @@ internal sealed class EgressHelper
return result.Succeeded ? 0 : 1;
}

private static TOptions BuildOptions<TOptions>(ExtensionEgressPayload configPayload, Action<ExtensionEgressPayload, TOptions> configureOptions = null) where TOptions : new()
jander-msft marked this conversation as resolved.
Show resolved Hide resolved
private static TOptions BuildOptions<TOptions>(ExtensionEgressPayload configPayload, ILogger logger, Action<ExtensionEgressPayload, TOptions, ILogger> configureOptions = null) where TOptions : new()
{
TOptions options = GetOptions<TOptions>(configPayload);

configureOptions?.Invoke(configPayload, options);
configureOptions?.Invoke(configPayload, options, logger);

return options;
}
Expand Down Expand Up @@ -114,5 +122,6 @@ internal sealed class ExtensionEgressPayload
public Dictionary<string, string> Properties { get; set; }
public Dictionary<string, string> Configuration { get; set; }
public string ProviderName { get; set; }
public LogLevel MinimumLogLevel { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ namespace Microsoft.Diagnostics.Monitoring.Extension.Common
{
internal abstract class EgressProvider<TOptions> where TOptions : class
{
protected readonly ILogger _logger;
internal ILogger Logger { get; set; }
kkeirstead marked this conversation as resolved.
Show resolved Hide resolved

public EgressProvider(ILogger logger)
{
_logger = logger;
}
public EgressProvider() { }

public abstract Task<string> EgressAsync(
ILogger logger,
TOptions options,
Func<Stream, CancellationToken, Task> action,
EgressArtifactSettings artifactSettings,
Expand Down

This file was deleted.

5 changes: 1 addition & 4 deletions src/Extensions/S3Storage/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Diagnostics.Monitoring.Extension.Common;
using Microsoft.Extensions.Logging;
using System.CommandLine;
using System.Threading.Tasks;

Expand All @@ -12,9 +11,7 @@ internal sealed class Program
{
static async Task<int> Main(string[] args)
{
ILogger logger = Utilities.CreateLogger();

S3StorageEgressProvider provider = new(logger);
S3StorageEgressProvider provider = new();

// Expected command line format is: dotnet-monitor-egress-s3storage.exe Egress
RootCommand rootCommand = new RootCommand("Egresses an artifact to S3 storage.");
Expand Down
10 changes: 4 additions & 6 deletions src/Extensions/S3Storage/S3StorageEgressProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ internal class StorageFactory

internal StorageFactory ClientFactory = new();

public S3StorageEgressProvider(ILogger logger) : base(logger)
{
}

public override async Task<string> EgressAsync(
ILogger logger,
S3StorageEgressProviderOptions options,
Func<Stream, CancellationToken, Task> action,
EgressArtifactSettings artifactSettings,
CancellationToken token)
{
Logger = logger;
IS3Storage client = null;
string uploadId = null;
bool uploadDone = false;
Expand All @@ -44,7 +42,7 @@ public override async Task<string> EgressAsync(
client = await ClientFactory.CreateAsync(options, artifactSettings, token);
uploadId = await client.InitMultiPartUploadAsync(artifactSettings.Metadata, token);
await using var stream = new MultiPartUploadStream(client, options.BucketName, artifactSettings.Name, uploadId, options.CopyBufferSize);
_logger.EgressProviderInvokeStreamAction(Constants.S3StorageProviderName);
Logger.EgressProviderInvokeStreamAction(Constants.S3StorageProviderName);
await action(stream, token);
await stream.FinalizeAsync(token); // force to push the last part

Expand Down Expand Up @@ -80,7 +78,7 @@ private string GetResourceId(IS3Storage client, S3StorageEgressProviderOptions o

DateTime expires = DateTime.UtcNow.Add(options.PreSignedUrlExpiry!.Value);
string resourceId = client.GetTemporaryResourceUrl(expires);
_logger.EgressProviderSavedStream(Constants.S3StorageProviderName, resourceId);
Logger.EgressProviderSavedStream(Constants.S3StorageProviderName, resourceId);
return resourceId;
}

Expand Down
Loading