Skip to content

Commit

Permalink
Merge branch 'main' into nuget_packages_update_21986898
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Cully authored Aug 14, 2023
2 parents 1d259b7 + d734f8d commit da183fe
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 9 deletions.
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
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
{
/// <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;
}
}
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);
}
}
}

0 comments on commit da183fe

Please sign in to comment.