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

Add separate meterprovider for standard metrics #36369

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using Azure.Core;
using Azure.Monitor.OpenTelemetry.Exporter.Internals;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OpenTelemetry.Metrics;
Expand Down Expand Up @@ -47,8 +46,6 @@ public static MeterProviderBuilder AddAzureMonitorMetricExporter(
builder.ConfigureServices(services => services.Configure(finalOptionsName, configure));
}

builder.AddMeter(StandardMetricConstants.StandardMetricMeterName);

return builder.AddReader(sp =>
{
var exporterOptions = sp.GetRequiredService<IOptionsMonitor<AzureMonitorExporterOptions>>().Get(finalOptionsName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static TracerProviderBuilder AddAzureMonitorTraceExporter(

return new CompositeProcessor<Activity>(new BaseProcessor<Activity>[]
{
new StandardMetricsExtractionProcessor(),
new StandardMetricsExtractionProcessor(new AzureMonitorMetricExporter(exporterOptions)),
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
new BatchActivityExportProcessor(new AzureMonitorTraceExporter(exporterOptions))
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
using System.Diagnostics.Metrics;
using Azure.Monitor.OpenTelemetry.Exporter.Models;
using OpenTelemetry;
using OpenTelemetry.Metrics;

namespace Azure.Monitor.OpenTelemetry.Exporter.Internals
{
internal class StandardMetricsExtractionProcessor : BaseProcessor<Activity>
{
private bool _disposed;
private AzureMonitorResource? _resource;
internal readonly MeterProvider? _meterProvider;
private readonly Meter _meter;
private readonly Histogram<double> _requestDuration;
private readonly Histogram<double> _dependencyDuration;
Expand All @@ -26,8 +28,13 @@ internal class StandardMetricsExtractionProcessor : BaseProcessor<Activity>

internal AzureMonitorResource? StandardMetricResource => _resource ??= ParentProvider?.GetResource().UpdateRoleNameAndInstance();

internal StandardMetricsExtractionProcessor()
internal StandardMetricsExtractionProcessor(AzureMonitorMetricExporter metricExporter)
{
_meterProvider = Sdk.CreateMeterProviderBuilder()
.AddMeter(StandardMetricConstants.StandardMetricMeterName)
.AddReader(new PeriodicExportingMetricReader(metricExporter)
{ TemporalityPreference = MetricReaderTemporalityPreference.Delta })
.Build();
_meter = new Meter(StandardMetricConstants.StandardMetricMeterName);
_requestDuration = _meter.CreateHistogram<double>(StandardMetricConstants.RequestDurationInstrumentName);
_dependencyDuration = _meter.CreateHistogram<double>(StandardMetricConstants.DependencyDurationInstrumentName);
Expand Down Expand Up @@ -111,6 +118,7 @@ protected override void Dispose(bool disposing)
{
try
{
_meterProvider?.Dispose();
_meter?.Dispose();
}
catch (Exception)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
using System.Linq;
using System;
using System.Threading;
using OpenTelemetry.Resources;
using System.Collections.Generic;

namespace Azure.Monitor.OpenTelemetry.Exporter.Tests
{
Expand All @@ -25,21 +27,19 @@ public void ValidateRequestDurationMetric()
var traceTelemetryItems = new ConcurrentBag<TelemetryItem>();
var metricTelemetryItems = new ConcurrentBag<TelemetryItem>();

var standardMetricCustomProcessor = new StandardMetricsExtractionProcessor();
var standardMetricCustomProcessor = new StandardMetricsExtractionProcessor(new AzureMonitorMetricExporter(new MockTransmitter(metricTelemetryItems)));

var traceServiceName = new KeyValuePair<string, object>("service.name", "trace.service");
var resourceAttributes = new KeyValuePair<string, object>[] { traceServiceName };

using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.SetSampler(new AlwaysOnSampler())
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddAttributes(resourceAttributes))
.AddSource(nameof(StandardMetricTests.ValidateRequestDurationMetric))
.AddProcessor(standardMetricCustomProcessor)
.AddProcessor(new BatchActivityExportProcessor(new AzureMonitorTraceExporter(new MockTransmitter(traceTelemetryItems))))
.Build();

using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddMeter(StandardMetricConstants.StandardMetricMeterName)
.AddReader(new PeriodicExportingMetricReader(new AzureMonitorMetricExporter(new MockTransmitter(metricTelemetryItems)))
{ TemporalityPreference = MetricReaderTemporalityPreference.Delta })
.Build();

using (var activity = activitySource.StartActivity("Test", ActivityKind.Server))
{
activity?.SetTag(SemanticConventions.AttributeHttpStatusCode, 200);
Expand All @@ -49,7 +49,7 @@ public void ValidateRequestDurationMetric()

WaitForActivityExport(traceTelemetryItems);

meterProvider?.ForceFlush();
standardMetricCustomProcessor._meterProvider?.ForceFlush();

Assert.Single(metricTelemetryItems);

Expand All @@ -63,7 +63,8 @@ public void ValidateRequestDurationMetric()
Assert.True(metricData.Properties.TryGetValue(StandardMetricConstants.IsAutoCollectedKey, out var isAutoCollectedFlag));
Assert.Equal("True", isAutoCollectedFlag);
Assert.True(metricData.Properties.TryGetValue(StandardMetricConstants.CloudRoleInstanceKey, out _));
Assert.True(metricData.Properties.TryGetValue(StandardMetricConstants.CloudRoleNameKey, out _));
Assert.True(metricData.Properties.TryGetValue(StandardMetricConstants.CloudRoleNameKey, out var cloudRoleName));
Assert.Equal("trace.service", cloudRoleName);
Assert.True(metricData.Properties.TryGetValue(StandardMetricConstants.MetricIdKey, out var metricId));
Assert.Equal(StandardMetricConstants.RequestDurationMetricIdValue, metricId);
}
Expand All @@ -75,21 +76,19 @@ public void ValidateDependencyDurationMetric()
var traceTelemetryItems = new ConcurrentBag<TelemetryItem>();
var metricTelemetryItems = new ConcurrentBag<TelemetryItem>();

var standardMetricCustomProcessor = new StandardMetricsExtractionProcessor();
var standardMetricCustomProcessor = new StandardMetricsExtractionProcessor(new AzureMonitorMetricExporter(new MockTransmitter(metricTelemetryItems)));

var traceServiceName = new KeyValuePair<string, object>("service.name", "trace.service");
var resourceAttributes = new KeyValuePair<string, object>[] { traceServiceName };

using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.SetSampler(new AlwaysOnSampler())
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddAttributes(resourceAttributes))
.AddSource(nameof(StandardMetricTests.ValidateDependencyDurationMetric))
.AddProcessor(standardMetricCustomProcessor)
.AddProcessor(new BatchActivityExportProcessor(new AzureMonitorTraceExporter(new MockTransmitter(traceTelemetryItems))))
.Build();

using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddMeter(StandardMetricConstants.StandardMetricMeterName)
.AddReader(new PeriodicExportingMetricReader(new AzureMonitorMetricExporter(new MockTransmitter(metricTelemetryItems)))
{ TemporalityPreference = MetricReaderTemporalityPreference.Delta })
.Build();

using (var activity = activitySource.StartActivity("Test", ActivityKind.Client))
{
activity?.SetTag(SemanticConventions.AttributeHttpStatusCode, 200);
Expand All @@ -101,7 +100,7 @@ public void ValidateDependencyDurationMetric()

WaitForActivityExport(traceTelemetryItems);

meterProvider?.ForceFlush();
standardMetricCustomProcessor._meterProvider?.ForceFlush();

Assert.Single(metricTelemetryItems);

Expand All @@ -115,7 +114,8 @@ public void ValidateDependencyDurationMetric()
Assert.True(metricData.Properties.TryGetValue(StandardMetricConstants.IsAutoCollectedKey, out var isAutoCollectedFlag));
Assert.Equal("True", isAutoCollectedFlag);
Assert.True(metricData.Properties.TryGetValue(StandardMetricConstants.CloudRoleInstanceKey, out _));
Assert.True(metricData.Properties.TryGetValue(StandardMetricConstants.CloudRoleNameKey, out _));
Assert.True(metricData.Properties.TryGetValue(StandardMetricConstants.CloudRoleNameKey, out var cloudRoleName));
Assert.Equal("trace.service", cloudRoleName);
Assert.True(metricData.Properties.TryGetValue(StandardMetricConstants.MetricIdKey, out var metricId));
Assert.Equal(StandardMetricConstants.DependencyDurationMetricIdValue, metricId);
Assert.True(metricData.Properties.TryGetValue(StandardMetricConstants.DependencyTypeKey, out var dependencyType));
Expand Down