Skip to content

Commit

Permalink
Option to disable legacy telemetry (#679)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gnol-VN authored May 27, 2024
1 parent 88b74b3 commit 6eccc2e
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Activities/Option/ActivityOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal class ActivityOption
/// <summary>
/// Disable ActivityEventSender so Activity metric is only sent via ActivityMetricsSender
/// </summary>
public bool ActivityEventSenderEnabled { get; set; } = true;
public bool ActivityEventSenderEnabled { get; set; } = false;

/// <summary>
/// Sets each activities parent name as a dimension value.
Expand Down
5 changes: 4 additions & 1 deletion src/Logging/Internal/OmexLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@ public OmexLogger(
IExternalScopeProvider externalScopeProvider,
IEnumerable<ILogScrubbingRule> textScrubbers,
string categoryName,
OmexLoggingOptions omexLoggingOptions,
ILogEventReplayer? replayer = null)
{
m_logsEventSender = logsEventSource;
m_externalScopeProvider = externalScopeProvider;
m_textScrubbers = textScrubbers.ToArray(); // Convert to an array for improved iteration performance on each call.
m_categoryName = categoryName;
m_omexLoggingOptions = omexLoggingOptions;
m_replayer = replayer;
}

public IDisposable? BeginScope<TState>(TState state) where TState : notnull => m_externalScopeProvider.Push(state);

public bool IsEnabled(LogLevel logLevel) => m_logsEventSender.IsEnabled(logLevel);
public bool IsEnabled(LogLevel logLevel) => m_omexLoggingOptions.OmexLoggerEnabled && m_logsEventSender.IsEnabled(logLevel);

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
Expand Down Expand Up @@ -67,6 +69,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
private readonly ILogEventSender m_logsEventSender;
private readonly ILogScrubbingRule[] m_textScrubbers;
private readonly string m_categoryName;
private readonly OmexLoggingOptions m_omexLoggingOptions;
private readonly ILogEventReplayer? m_replayer;
}
}
17 changes: 17 additions & 0 deletions src/Logging/Internal/OmexLoggerOptionsSetup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using Microsoft.Extensions.Logging.Configuration;
using Microsoft.Extensions.Options;

namespace Microsoft.Omex.Extensions.Logging;

internal class OmexLoggerOptionsSetup : ConfigureFromConfigurationOptions<OmexLoggingOptions>
{
[Obsolete("OmexLoggerOptionsSetup is deprecated and pending for removal on 1 July 2024", DiagnosticId = "OMEX188")]
public OmexLoggerOptionsSetup(ILoggerProviderConfiguration<OmexLoggerProvider> providerConfiguration)
: base(providerConfiguration.Configuration)
{
}
}
6 changes: 5 additions & 1 deletion src/Logging/Internal/OmexLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Omex.Extensions.Logging.Replayable;
using Microsoft.Omex.Extensions.Logging.Scrubbing;

Expand All @@ -17,16 +18,18 @@ public OmexLoggerProvider(
ILogEventSender logsEventSender,
IExternalScopeProvider defaultExternalScopeProvider,
IEnumerable<ILogScrubbingRule> textScrubbers,
IOptions<OmexLoggingOptions> options,
ILogEventReplayer? replayer = null)
{
m_logsEventSender = logsEventSender;
m_defaultExternalScopeProvider = defaultExternalScopeProvider;
m_textScrubbers = textScrubbers;
m_options = options;
m_replayer = replayer;
}

public ILogger CreateLogger(string categoryName) =>
new OmexLogger(m_logsEventSender, m_externalScopeProvider ?? m_defaultExternalScopeProvider, m_textScrubbers, categoryName, m_replayer);
new OmexLogger(m_logsEventSender, m_externalScopeProvider ?? m_defaultExternalScopeProvider, m_textScrubbers, categoryName, m_options.Value, m_replayer);

public void Dispose() { }

Expand All @@ -37,6 +40,7 @@ public void Dispose() { }
private readonly ILogEventSender m_logsEventSender;
private readonly IExternalScopeProvider m_defaultExternalScopeProvider;
private readonly IEnumerable<ILogScrubbingRule> m_textScrubbers;
private readonly IOptions<OmexLoggingOptions> m_options;
private readonly ILogEventReplayer? m_replayer;
}
}
5 changes: 5 additions & 0 deletions src/Logging/OmexLoggingOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,10 @@ internal class OmexLoggingOptions
/// Maximum number of events that activity can store for replay
/// </summary>
public uint MaxReplayedEventsPerActivity { get; set; } = 1000;

/// <summary>
/// Enable or disable OmexLogger
/// </summary>
public bool OmexLoggerEnabled { get; set; } = false;
}
}
7 changes: 6 additions & 1 deletion src/Logging/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Configuration;
using Microsoft.Extensions.Options;
using Microsoft.Omex.Extensions.Abstractions.Activities.Processing;
using Microsoft.Omex.Extensions.Abstractions.ExecutionContext;
using Microsoft.Omex.Extensions.Logging.Replayable;
Expand Down Expand Up @@ -47,7 +48,8 @@ public static ILoggingBuilder AddOmexLogging(this ILoggingBuilder builder)
[Obsolete("OmexLogger and OmexLogEventSource are obsolete and pending for removal by 1 July 2024. Please consider using a different Logger.", DiagnosticId = "OMEX188")]
public static IServiceCollection AddOmexLogging(this IServiceCollection serviceCollection)
{
serviceCollection.AddLogging();
serviceCollection.AddLogging(builder =>
builder.AddConfiguration());

serviceCollection.TryAddTransient<IServiceContext, EmptyServiceContext>();
serviceCollection.TryAddTransient<IExecutionContext, BaseExecutionContext>();
Expand All @@ -60,6 +62,9 @@ public static IServiceCollection AddOmexLogging(this IServiceCollection serviceC
serviceCollection.TryAddEnumerable(ServiceDescriptor.Transient<IActivityStopObserver, ReplayableActivityStopObserver>());
serviceCollection.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, OmexLoggerProvider>());

serviceCollection.TryAddEnumerable(ServiceDescriptor.Singleton
<IConfigureOptions<OmexLoggingOptions>, OmexLoggerOptionsSetup>());

return serviceCollection;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public void LogActivityEndEvent_CreatesEvent(EventSourcesEventIds eventId, bool
contextMock.Setup(c => c.ServiceName).Returns("TestService");

Mock<IOptions<ActivityOption>> mockOptions = new();
mockOptions.Setup(m => m.Value).Returns(new ActivityOption());
ActivityOption activityOption = new() { ActivityEventSenderEnabled = true };
mockOptions.Setup(m => m.Value).Returns(activityOption);

ActivityEventSender logEventSource = new(
ActivityEventSource.Instance,
Expand Down
34 changes: 33 additions & 1 deletion tests/Logging.UnitTests/OmexLoggerProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Diagnostics;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Omex.Extensions.Logging.Scrubbing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
Expand All @@ -22,7 +23,11 @@ public void CreateLogger_PropagatesCategory()
Mock<ILogEventSender> mockEventSource = new();
IExternalScopeProvider mockExternalScopeProvider = new Mock<IExternalScopeProvider>().Object;

ILoggerProvider loggerProvider = new OmexLoggerProvider(mockEventSource.Object, mockExternalScopeProvider, Array.Empty<ILogScrubbingRule>());
Mock<IOptions<OmexLoggingOptions>> mockOmexLoggingOption = new();
OmexLoggingOptions omexLoggingOptions = new OmexLoggingOptions() { OmexLoggerEnabled = true };
mockOmexLoggingOption.Setup(m => m.Value).Returns(omexLoggingOptions);

ILoggerProvider loggerProvider = new OmexLoggerProvider(mockEventSource.Object, mockExternalScopeProvider, Array.Empty<ILogScrubbingRule>(), mockOmexLoggingOption.Object);
ILogger logger = loggerProvider.CreateLogger(testCategory);

Assert.IsInstanceOfType(logger, typeof(OmexLogger));
Expand All @@ -33,5 +38,32 @@ public void CreateLogger_PropagatesCategory()

mockEventSource.Verify(e => e.LogMessage(It.IsAny<Activity>(), testCategory, LogLevel.Error, It.IsAny<EventId>(), It.IsAny<int>(), testMessage, It.IsAny<Exception>()), Times.Once);
}

[TestMethod]
[DataTestMethod]
[DataRow(true)]
[DataRow(false)]
public void CreateLogger_Control_OmexLoggerEnabledBoolean_LogProducedAccordingly(bool omexLoggerEnabled)
{
const string testCategory = "SomeCategoryName";
const string testMessage = "TestMessage";
Mock<ILogEventSender> mockEventSource = new();
IExternalScopeProvider mockExternalScopeProvider = new Mock<IExternalScopeProvider>().Object;

Mock<IOptions<OmexLoggingOptions>> mockOmexLoggingOption = new();
OmexLoggingOptions omexLoggingOptions = new OmexLoggingOptions() { OmexLoggerEnabled = omexLoggerEnabled };
mockOmexLoggingOption.Setup(m => m.Value).Returns(omexLoggingOptions);

ILoggerProvider loggerProvider = new OmexLoggerProvider(mockEventSource.Object, mockExternalScopeProvider, Array.Empty<ILogScrubbingRule>(), mockOmexLoggingOption.Object);
ILogger logger = loggerProvider.CreateLogger(testCategory);

Assert.IsInstanceOfType(logger, typeof(OmexLogger));

mockEventSource.Setup(e => e.IsEnabled(It.IsAny<LogLevel>())).Returns(true);

logger.LogError(testMessage);

mockEventSource.Verify(e => e.LogMessage(It.IsAny<Activity>(), testCategory, LogLevel.Error, It.IsAny<EventId>(), It.IsAny<int>(), testMessage, It.IsAny<Exception>()), omexLoggerEnabled ? Times.Once : Times.Never);
}
}
}
3 changes: 2 additions & 1 deletion tests/Logging.UnitTests/OmexLoggerUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ private static (ILogger, Mock<IExternalScopeProvider>) LogMessage(
[CallerMemberName] string suffix = "")
{
Mock<IExternalScopeProvider> scopeProviderMock = new();
ILogger logger = new OmexLogger(eventSourceMock.Object, scopeProviderMock.Object, textScrubbers, GetLogCategory(suffix), logEventReplayer);
OmexLoggingOptions omexLoggingOptions = new OmexLoggingOptions() { OmexLoggerEnabled = true };
ILogger logger = new OmexLogger(eventSourceMock.Object, scopeProviderMock.Object, textScrubbers, GetLogCategory(suffix), omexLoggingOptions, logEventReplayer);

logger.LogError(CreateEventId(eventId, suffix), s_expectedPropagatedException, GetLogMessage(suffix));

Expand Down

0 comments on commit 6eccc2e

Please sign in to comment.