From d734f8d64a971a8ad658351a3d03bc3c0f2cdb40 Mon Sep 17 00:00:00 2001 From: Gnol Long Date: Thu, 10 Aug 2023 10:34:48 +0100 Subject: [PATCH] IOption to Disable Activity Event sender (#589) --------- Co-authored-by: Long Do Thanh Co-authored-by: Neil <49037171+neilr81@users.noreply.github.com> --- .../Internal/ActivityMetricsSender.cs | 1 - .../EventSource/ActivityEventSender.cs | 8 +++- .../Option/ActivityOption.cs} | 9 +++- src/Activities/ServiceCollectionExtensions.cs | 5 +-- .../Internal/ActivitiesEventSourceTests.cs | 42 ++++++++++++++++++- 5 files changed, 56 insertions(+), 9 deletions(-) rename src/{Abstractions/Option/MonitoringOption.cs => Activities/Option/ActivityOption.cs} (51%) diff --git a/src/Activities/Internal/ActivityMetricsSender.cs b/src/Activities/Internal/ActivityMetricsSender.cs index 5b9da789..e5558cc6 100644 --- a/src/Activities/Internal/ActivityMetricsSender.cs +++ b/src/Activities/Internal/ActivityMetricsSender.cs @@ -11,7 +11,6 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options; using Microsoft.Omex.Extensions.Abstractions.Activities; using Microsoft.Omex.Extensions.Abstractions.ExecutionContext; -using Microsoft.Omex.Extensions.Abstractions.Option; namespace Microsoft.Omex.Extensions.Activities { diff --git a/src/Activities/Internal/EventSource/ActivityEventSender.cs b/src/Activities/Internal/EventSource/ActivityEventSender.cs index 51504ba5..ad7806bc 100644 --- a/src/Activities/Internal/EventSource/ActivityEventSender.cs +++ b/src/Activities/Internal/EventSource/ActivityEventSender.cs @@ -6,25 +6,28 @@ using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using Microsoft.Omex.Extensions.Abstractions; using Microsoft.Omex.Extensions.Abstractions.Activities; using Microsoft.Omex.Extensions.Abstractions.Activities.Processing; using Microsoft.Omex.Extensions.Abstractions.ExecutionContext; +using Microsoft.Omex.Extensions.Activities; namespace Microsoft.Omex.Extensions.Activities { internal sealed class ActivityEventSender : IActivitiesEventSender { - public ActivityEventSender(ActivityEventSource eventSource, IExecutionContext executionContext, ILogger logger) + public ActivityEventSender(ActivityEventSource eventSource, IExecutionContext executionContext, ILogger logger, IOptions options) { m_eventSource = eventSource; m_serviceName = executionContext.ServiceName; m_logger = logger; + m_options = options; } public void SendActivityMetric(Activity activity) { - if (!m_eventSource.IsEnabled()) + if (!m_options.Value.ActivityEventSenderEnabled || !m_eventSource.IsEnabled()) { return; } @@ -122,6 +125,7 @@ namespace Microsoft.Omex.Extensions.Activities private readonly ActivityEventSource m_eventSource; private readonly string m_serviceName; private readonly ILogger m_logger; + private readonly IOptions m_options; private static readonly string s_logCategory = typeof(ActivityEventSource).FullName ?? nameof(ActivityEventSource); private const string NullPlaceholder = "null"; } diff --git a/src/Abstractions/Option/MonitoringOption.cs b/src/Activities/Option/ActivityOption.cs similarity index 51% rename from src/Abstractions/Option/MonitoringOption.cs rename to src/Activities/Option/ActivityOption.cs index 90893f66..49f5679e 100644 --- a/src/Abstractions/Option/MonitoringOption.cs +++ b/src/Activities/Option/ActivityOption.cs @@ -1,16 +1,21 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. -namespace Microsoft.Omex.Extensions.Abstractions.Option +namespace Microsoft.Omex.Extensions.Activities { /// /// Monitoring option /// - public class MonitoringOption + public class ActivityOption { /// /// Path to the setting /// public static string MonitoringPath = "Monitoring"; + + /// + /// Disable ActivityEventSender so Activity metric is only sent via ActivityMetricsSender + /// + public bool ActivityEventSenderEnabled { get; set; } = true; } } diff --git a/src/Activities/ServiceCollectionExtensions.cs b/src/Activities/ServiceCollectionExtensions.cs index a4a5233e..5ecb5b7c 100644 --- a/src/Activities/ServiceCollectionExtensions.cs +++ b/src/Activities/ServiceCollectionExtensions.cs @@ -6,7 +6,6 @@ using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.ObjectPool; using Microsoft.Omex.Extensions.Abstractions.Activities.Processing; using Microsoft.Omex.Extensions.Abstractions.ExecutionContext; -using Microsoft.Omex.Extensions.Abstractions.Option; using Microsoft.Omex.Extensions.Activities; namespace Microsoft.Extensions.DependencyInjection @@ -42,8 +41,8 @@ namespace Microsoft.Extensions.DependencyInjection serviceCollection.TryAddSingleton(); serviceCollection - .AddOptions() - .BindConfiguration(MonitoringOption.MonitoringPath) + .AddOptions() + .BindConfiguration(ActivityOption.MonitoringPath) .ValidateDataAnnotations(); serviceCollection.TryAddSingleton(); diff --git a/tests/Activities.UnitTests/Internal/ActivitiesEventSourceTests.cs b/tests/Activities.UnitTests/Internal/ActivitiesEventSourceTests.cs index 26c1f600..4ae39223 100644 --- a/tests/Activities.UnitTests/Internal/ActivitiesEventSourceTests.cs +++ b/tests/Activities.UnitTests/Internal/ActivitiesEventSourceTests.cs @@ -6,6 +6,7 @@ using System.Diagnostics; using System.Diagnostics.Tracing; using System.Linq; using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.Extensions.Options; using Microsoft.Omex.Extensions.Abstractions.Activities; using Microsoft.Omex.Extensions.Abstractions.EventSources; using Microsoft.Omex.Extensions.Abstractions.ExecutionContext; @@ -33,10 +34,14 @@ namespace Microsoft.Omex.Extensions.Activities.UnitTests Mock contextMock = new(); contextMock.Setup(c => c.ServiceName).Returns("TestService"); + Mock> mockOptions = new(); + mockOptions.Setup(m => m.Value).Returns(new ActivityOption()); + ActivityEventSender logEventSource = new( ActivityEventSource.Instance, contextMock.Object, - new NullLogger()); + new NullLogger(), + mockOptions.Object); Guid correlationId = Guid.NewGuid(); using Activity activity = new Activity(name).Start(); @@ -60,5 +65,40 @@ namespace Microsoft.Omex.Extensions.Activities.UnitTests eventInfo.AssertPayload("activityId", activity.Id); eventInfo.AssertPayload("correlationId", correlationId.ToString()); } + + [DataTestMethod] + [DataRow(EventSourcesEventIds.LogActivityTestContext, true)] + [DataRow(EventSourcesEventIds.LogActivity, false)] + public void LogActivityEndEvent_DisableByOption_CreatesNoEvent(EventSourcesEventIds eventId, bool isHealthCheck) + { + using TestEventListener listener = new(); + listener.EnableEvents(ActivityEventSource.Instance, EventLevel.Informational); + + const string name = "TestName"; + + Mock contextMock = new(); + contextMock.Setup(c => c.ServiceName).Returns("TestService"); + + Mock> mockOptions = new(); + ActivityOption activityOption = new() { ActivityEventSenderEnabled = false }; + mockOptions.Setup(m => m.Value).Returns(activityOption); + + ActivityEventSender logEventSource = new( + ActivityEventSource.Instance, + contextMock.Object, + new NullLogger(), + mockOptions.Object); + + Guid correlationId = Guid.NewGuid(); + using Activity activity = new Activity(name).Start(); + if (isHealthCheck) + { + activity.MarkAsHealthCheck(); + } + + logEventSource.SendActivityMetric(activity); + + Assert.AreEqual(0, listener.EventsInformation.Count); + } } }