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

IOption to Disable Activity Event sender #589

Merged
1 change: 0 additions & 1 deletion src/Activities/Internal/ActivityMetricsSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
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
{
Expand Down
8 changes: 6 additions & 2 deletions src/Activities/Internal/EventSource/ActivityEventSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,28 @@
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;
}
Expand Down Expand Up @@ -122,6 +125,7 @@ private string SanitizeString(string value, string name, string activityName)
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";
}
Expand Down
Gnol-VN marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -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
Gnol-VN marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary>
/// Path to the setting
/// </summary>
public static string MonitoringPath = "Monitoring";
Gnol-VN marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Disable ActivityEventSender so Activity metric is only sent via ActivityMetricsSender
/// </summary>
public bool ActivityEventSenderEnabled { get; set; } = true;
}
}
5 changes: 2 additions & 3 deletions src/Activities/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
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
Expand Down Expand Up @@ -42,8 +41,8 @@ public static IServiceCollection AddOmexActivitySource(this IServiceCollection s
serviceCollection.TryAddSingleton<IActivitiesEventSender, AggregatedActivitiesEventSender>();

serviceCollection
.AddOptions<MonitoringOption>()
.BindConfiguration(MonitoringOption.MonitoringPath)
.AddOptions<ActivityOption>()
.BindConfiguration(ActivityOption.MonitoringPath)
.ValidateDataAnnotations();

serviceCollection.TryAddSingleton<IActivityListenerConfigurator, DefaultActivityListenerConfigurator>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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;
Expand Down Expand Up @@ -33,10 +34,14 @@ public void LogActivityEndEvent_CreatesEvent(EventSourcesEventIds eventId, bool
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();
Expand All @@ -60,5 +65,40 @@ public void LogActivityEndEvent_CreatesEvent(EventSourcesEventIds eventId, bool
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);
}
}
}