Skip to content

Commit

Permalink
GitHub Forward Integration - 4d42192 21462368
Browse files Browse the repository at this point in the history
  • Loading branch information
omexac committed Jul 17, 2023
2 parents 769f1ae + 4d42192 commit 6770784
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 43 deletions.
4 changes: 2 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
<PackageVersion Include="Microsoft.CodeAnalysis.Csharp" Version="4.6.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="7.0.8" />
<PackageVersion Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="7.0.9" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.ObjectPool" Version="7.0.8" />
<PackageVersion Include="Microsoft.Extensions.ObjectPool" Version="7.0.9" />
<PackageVersion Include="Microsoft.Extensions.Options" Version="7.0.1" />
<PackageVersion Include="Microsoft.Extensions.Options.DataAnnotations" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="7.0.0" />
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "7.0.305",
"version": "7.0.306",
"rollForward": "latestMajor"
}
}
5 changes: 0 additions & 5 deletions src/Abstractions/Option/MonitoringOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,5 @@ public class MonitoringOption
/// Path to the setting
/// </summary>
public static string MonitoringPath = "Monitoring";

/// <summary>
/// Setting to determine whether using Histogram ar Counter for metrics
/// </summary>
public bool UseHistogramForActivityMonitoring { get; set; }
}
}
30 changes: 8 additions & 22 deletions src/Activities/Internal/ActivityMetricsSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,25 @@ namespace Microsoft.Omex.Extensions.Activities
internal sealed class ActivityMetricsSender : IActivitiesEventSender, IDisposable
{
private readonly Meter m_meter;
private readonly Counter<double> m_activityCounter;
private readonly Counter<double> m_healthCheckActivityCounter;
private readonly Histogram<double> m_activityHistogram;
private readonly Histogram<double> m_healthCheckActivityHistogram;
private readonly Histogram<long> m_activityHistogram;
private readonly Histogram<long> m_healthCheckActivityHistogram;
private readonly IExecutionContext m_context;
private readonly IHostEnvironment m_hostEnvironment;
private readonly IOptions<MonitoringOption> m_monitoringOption;
private readonly ArrayPool<KeyValuePair<string, object?>> m_arrayPool;

public ActivityMetricsSender(IExecutionContext executionContext, IHostEnvironment hostEnvironment, IOptions<MonitoringOption> monitoringOption)
public ActivityMetricsSender(IExecutionContext executionContext, IHostEnvironment hostEnvironment)
{
m_context = executionContext;
m_hostEnvironment = hostEnvironment;
m_monitoringOption = monitoringOption;
m_meter = new Meter("Microsoft.Omex.Activities", "1.0.0");
m_activityCounter = m_meter.CreateCounter<double>("Activities");
m_healthCheckActivityCounter = m_meter.CreateCounter<double>("HealthCheckActivities");
m_activityHistogram = m_meter.CreateHistogram<double>("Activities");
m_healthCheckActivityHistogram = m_meter.CreateHistogram<double>("HealthCheckActivities");
m_activityHistogram = m_meter.CreateHistogram<long>("Activities");
m_healthCheckActivityHistogram = m_meter.CreateHistogram<long>("HealthCheckActivities");
m_arrayPool = ArrayPool<KeyValuePair<string, object?>>.Create();
}

public void SendActivityMetric(Activity activity)
{
double durationMs = activity.Duration.TotalMilliseconds;
long durationMs = Convert.ToInt64(activity.Duration.TotalMilliseconds);

int tagsCount = s_customTags.Length + activity.TagObjects.Count() + activity.Baggage.Count();

Expand All @@ -67,17 +61,9 @@ public void SendActivityMetric(Activity activity)

ReadOnlySpan<KeyValuePair<string, object?>> tagsSpan = MemoryExtensions.AsSpan(tags, 0, tagsCount);

Histogram<double> histogram = activity.IsHealthCheck() ? m_healthCheckActivityHistogram : m_activityHistogram;
Counter<double> counter = activity.IsHealthCheck() ? m_healthCheckActivityCounter : m_activityCounter;
Histogram<long> histogram = activity.IsHealthCheck() ? m_healthCheckActivityHistogram : m_activityHistogram;

if (m_monitoringOption.Value.UseHistogramForActivityMonitoring)
{
histogram.Record(durationMs, tagsSpan);
}
else
{
counter.Add(durationMs, tagsSpan);
}
histogram.Record(durationMs, tagsSpan);

m_arrayPool.Return(tags, clearArray: true);
}
Expand Down
18 changes: 5 additions & 13 deletions tests/Activities.UnitTests/Internal/ActivityMetricsSenderTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Metrics;
Expand All @@ -9,7 +10,6 @@
using Microsoft.Extensions.Options;
using Microsoft.Omex.Extensions.Abstractions.Activities;
using Microsoft.Omex.Extensions.Abstractions.ExecutionContext;
using Microsoft.Omex.Extensions.Abstractions.Option;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

Expand All @@ -19,9 +19,7 @@ namespace Microsoft.Omex.Extensions.Activities.UnitTests.Internal
public class ActivityMetricsSenderTests
{
[TestMethod]
[DataRow(true)]
[DataRow(false)]
public void SendActivityMetric_ProduceMetrics(bool useHistogramForActivityMonitoring)
public void SendActivityMetric_ProduceMetrics()
{
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
Activity.ForceDefaultIdFormat = true;
Expand All @@ -43,13 +41,7 @@ public void SendActivityMetric_ProduceMetrics(bool useHistogramForActivityMonito
environmentMock.Setup(e => e.EnvironmentName).Returns("TestEnv");
IHostEnvironment environment = environmentMock.Object;

Mock<IOptions<MonitoringOption>> mockMonitorOption = new();
mockMonitorOption.Setup(options => options.Value).Returns(new MonitoringOption()
{
UseHistogramForActivityMonitoring = useHistogramForActivityMonitoring
});
ActivityMetricsSender sender = new(context, environment, mockMonitorOption.Object);

ActivityMetricsSender sender = new(context, environment);
Listener listener = new();

Activity testActivity = new(nameof(testActivity));
Expand Down Expand Up @@ -83,7 +75,7 @@ private void VerifyTagsExist(Listener listener, Activity activity, bool isHealth
: listener.Results.First(m => !m.Tags.ContainsKey(s_healthCheckTag) && environment.EnvironmentName.Equals(m.Tags[s_environmentTagName]));

Assert.AreEqual(instrumentationName, result.Instrument.Name);
Assert.AreEqual(activity.Duration.TotalMilliseconds, result.Measurement);
Assert.AreEqual(Convert.ToInt64(activity.Duration.TotalMilliseconds), result.Measurement);
AssertTag(result, "Name", activity.OperationName);
AssertTag(result, s_environmentTagName, environment.EnvironmentName);
AssertTag(result, "RegionName", context.RegionName);
Expand Down Expand Up @@ -135,7 +127,7 @@ public Listener()

Results = new List<MeasurementResult>();

listener.SetMeasurementEventCallback<double>((instrument, measurement, tags, state) =>
listener.SetMeasurementEventCallback<long>((instrument, measurement, tags, state) =>
{
Results.Add(new MeasurementResult(instrument, measurement, tags.ToArray().ToDictionary(p => p.Key, p => p.Value), state));
});
Expand Down

0 comments on commit 6770784

Please sign in to comment.