IOption to Disable Activity Event sender (#589)

---------

Co-authored-by: Long Do Thanh <dothanhl@microsoft.com>
Co-authored-by: Neil <49037171+neilr81@users.noreply.github.com>
This commit is contained in:
Gnol Long 2023-08-10 10:34:48 +01:00 коммит произвёл GitHub
Родитель 179fb66537
Коммит d734f8d64a
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 56 добавлений и 9 удалений

Просмотреть файл

@ -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
{

Просмотреть файл

@ -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<ActivityEventSender> logger)
public ActivityEventSender(ActivityEventSource eventSource, IExecutionContext executionContext, ILogger<ActivityEventSender> logger, IOptions<ActivityOption> 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<ActivityEventSender> m_logger;
private readonly IOptions<ActivityOption> m_options;
private static readonly string s_logCategory = typeof(ActivityEventSource).FullName ?? nameof(ActivityEventSource);
private const string NullPlaceholder = "null";
}

Просмотреть файл

@ -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
{
/// <summary>
/// Monitoring option
/// </summary>
public class MonitoringOption
public class ActivityOption
{
/// <summary>
/// Path to the setting
/// </summary>
public static string MonitoringPath = "Monitoring";
/// <summary>
/// Disable ActivityEventSender so Activity metric is only sent via ActivityMetricsSender
/// </summary>
public bool ActivityEventSenderEnabled { get; set; } = true;
}
}

Просмотреть файл

@ -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<IActivitiesEventSender, AggregatedActivitiesEventSender>();
serviceCollection
.AddOptions<MonitoringOption>()
.BindConfiguration(MonitoringOption.MonitoringPath)
.AddOptions<ActivityOption>()
.BindConfiguration(ActivityOption.MonitoringPath)
.ValidateDataAnnotations();
serviceCollection.TryAddSingleton<IActivityListenerConfigurator, DefaultActivityListenerConfigurator>();

Просмотреть файл

@ -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<IExecutionContext> contextMock = new();
contextMock.Setup(c => c.ServiceName).Returns("TestService");
Mock<IOptions<ActivityOption>> mockOptions = new();
mockOptions.Setup(m => m.Value).Returns(new ActivityOption());
ActivityEventSender logEventSource = new(
ActivityEventSource.Instance,
contextMock.Object,
new NullLogger<ActivityEventSender>());
new NullLogger<ActivityEventSender>(),
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<IExecutionContext> contextMock = new();
contextMock.Setup(c => c.ServiceName).Returns("TestService");
Mock<IOptions<ActivityOption>> mockOptions = new();
ActivityOption activityOption = new() { ActivityEventSenderEnabled = false };
mockOptions.Setup(m => m.Value).Returns(activityOption);
ActivityEventSender logEventSource = new(
ActivityEventSource.Instance,
contextMock.Object,
new NullLogger<ActivityEventSender>(),
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);
}
}
}