66using System . Diagnostics ;
77using System . Linq ;
88using System . Threading ;
9+ using OpenTelemetry . Resources ;
910using Azure . Monitor . OpenTelemetry . Exporter . Internals ;
1011using Azure . Monitor . OpenTelemetry . Exporter . Models ;
1112using Azure . Monitor . OpenTelemetry . Exporter . Tests . CommonTestFramework ;
@@ -25,21 +26,19 @@ public void ValidateRequestDurationMetric()
2526 var traceTelemetryItems = new List < TelemetryItem > ( ) ;
2627 var metricTelemetryItems = new List < TelemetryItem > ( ) ;
2728
28- var standardMetricCustomProcessor = new StandardMetricsExtractionProcessor ( ) ;
29+ var standardMetricCustomProcessor = new StandardMetricsExtractionProcessor ( new AzureMonitorMetricExporter ( new MockTransmitter ( metricTelemetryItems ) ) ) ;
30+
31+ var traceServiceName = new KeyValuePair < string , object > ( "service.name" , "trace.service" ) ;
32+ var resourceAttributes = new KeyValuePair < string , object > [ ] { traceServiceName } ;
2933
3034 using var tracerProvider = Sdk . CreateTracerProviderBuilder ( )
3135 . SetSampler ( new AlwaysOnSampler ( ) )
36+ . SetResourceBuilder ( ResourceBuilder . CreateDefault ( ) . AddAttributes ( resourceAttributes ) )
3237 . AddSource ( nameof ( StandardMetricTests . ValidateRequestDurationMetric ) )
3338 . AddProcessor ( standardMetricCustomProcessor )
3439 . AddProcessor ( new BatchActivityExportProcessor ( new AzureMonitorTraceExporter ( new MockTransmitter ( traceTelemetryItems ) ) ) )
3540 . Build ( ) ;
3641
37- using var meterProvider = Sdk . CreateMeterProviderBuilder ( )
38- . AddMeter ( StandardMetricConstants . StandardMetricMeterName )
39- . AddReader ( new PeriodicExportingMetricReader ( new AzureMonitorMetricExporter ( new MockTransmitter ( metricTelemetryItems ) ) )
40- { TemporalityPreference = MetricReaderTemporalityPreference . Delta } )
41- . Build ( ) ;
42-
4342 using ( var activity = activitySource . StartActivity ( "Test" , ActivityKind . Server ) )
4443 {
4544 activity ? . SetTag ( SemanticConventions . AttributeHttpStatusCode , 200 ) ;
@@ -49,7 +48,7 @@ public void ValidateRequestDurationMetric()
4948
5049 WaitForActivityExport ( traceTelemetryItems ) ;
5150
52- meterProvider ? . ForceFlush ( ) ;
51+ standardMetricCustomProcessor . _meterProvider ? . ForceFlush ( ) ;
5352
5453 // Standard Metrics + Resource Metrics.
5554 Assert . Single ( metricTelemetryItems ) ;
@@ -64,7 +63,8 @@ public void ValidateRequestDurationMetric()
6463 Assert . True ( metricData . Properties . TryGetValue ( StandardMetricConstants . IsAutoCollectedKey , out var isAutoCollectedFlag ) ) ;
6564 Assert . Equal ( "True" , isAutoCollectedFlag ) ;
6665 Assert . True ( metricData . Properties . TryGetValue ( StandardMetricConstants . CloudRoleInstanceKey , out _ ) ) ;
67- Assert . True ( metricData . Properties . TryGetValue ( StandardMetricConstants . CloudRoleNameKey , out _ ) ) ;
66+ Assert . True ( metricData . Properties . TryGetValue ( StandardMetricConstants . CloudRoleNameKey , out var cloudRoleName ) ) ;
67+ Assert . Equal ( "trace.service" , cloudRoleName ) ;
6868 Assert . True ( metricData . Properties . TryGetValue ( StandardMetricConstants . MetricIdKey , out var metricId ) ) ;
6969 Assert . Equal ( StandardMetricConstants . RequestDurationMetricIdValue , metricId ) ;
7070 }
@@ -76,21 +76,19 @@ public void ValidateDependencyDurationMetric()
7676 var traceTelemetryItems = new List < TelemetryItem > ( ) ;
7777 var metricTelemetryItems = new List < TelemetryItem > ( ) ;
7878
79- var standardMetricCustomProcessor = new StandardMetricsExtractionProcessor ( ) ;
79+ var standardMetricCustomProcessor = new StandardMetricsExtractionProcessor ( new AzureMonitorMetricExporter ( new MockTransmitter ( metricTelemetryItems ) ) ) ;
80+
81+ var traceServiceName = new KeyValuePair < string , object > ( "service.name" , "trace.service" ) ;
82+ var resourceAttributes = new KeyValuePair < string , object > [ ] { traceServiceName } ;
8083
8184 using var tracerProvider = Sdk . CreateTracerProviderBuilder ( )
8285 . SetSampler ( new AlwaysOnSampler ( ) )
86+ . SetResourceBuilder ( ResourceBuilder . CreateDefault ( ) . AddAttributes ( resourceAttributes ) )
8387 . AddSource ( nameof ( StandardMetricTests . ValidateDependencyDurationMetric ) )
8488 . AddProcessor ( standardMetricCustomProcessor )
8589 . AddProcessor ( new BatchActivityExportProcessor ( new AzureMonitorTraceExporter ( new MockTransmitter ( traceTelemetryItems ) ) ) )
8690 . Build ( ) ;
8791
88- using var meterProvider = Sdk . CreateMeterProviderBuilder ( )
89- . AddMeter ( StandardMetricConstants . StandardMetricMeterName )
90- . AddReader ( new PeriodicExportingMetricReader ( new AzureMonitorMetricExporter ( new MockTransmitter ( metricTelemetryItems ) ) )
91- { TemporalityPreference = MetricReaderTemporalityPreference . Delta } )
92- . Build ( ) ;
93-
9492 using ( var activity = activitySource . StartActivity ( "Test" , ActivityKind . Client ) )
9593 {
9694 activity ? . SetTag ( SemanticConventions . AttributeHttpStatusCode , 200 ) ;
@@ -102,7 +100,7 @@ public void ValidateDependencyDurationMetric()
102100
103101 WaitForActivityExport ( traceTelemetryItems ) ;
104102
105- meterProvider ? . ForceFlush ( ) ;
103+ standardMetricCustomProcessor . _meterProvider ? . ForceFlush ( ) ;
106104
107105 // Standard Metrics + Resource Metrics.
108106 Assert . Single ( metricTelemetryItems ) ;
@@ -117,7 +115,8 @@ public void ValidateDependencyDurationMetric()
117115 Assert . True ( metricData . Properties . TryGetValue ( StandardMetricConstants . IsAutoCollectedKey , out var isAutoCollectedFlag ) ) ;
118116 Assert . Equal ( "True" , isAutoCollectedFlag ) ;
119117 Assert . True ( metricData . Properties . TryGetValue ( StandardMetricConstants . CloudRoleInstanceKey , out _ ) ) ;
120- Assert . True ( metricData . Properties . TryGetValue ( StandardMetricConstants . CloudRoleNameKey , out _ ) ) ;
118+ Assert . True ( metricData . Properties . TryGetValue ( StandardMetricConstants . CloudRoleNameKey , out var cloudRoleName ) ) ;
119+ Assert . Equal ( "trace.service" , cloudRoleName ) ;
121120 Assert . True ( metricData . Properties . TryGetValue ( StandardMetricConstants . MetricIdKey , out var metricId ) ) ;
122121 Assert . Equal ( StandardMetricConstants . DependencyDurationMetricIdValue , metricId ) ;
123122 Assert . True ( metricData . Properties . TryGetValue ( StandardMetricConstants . DependencyTypeKey , out var dependencyType ) ) ;
0 commit comments