3
3
4
4
using System ;
5
5
using System . Diagnostics . Metrics ;
6
+ using System . Linq ;
6
7
using Microsoft . Azure . WebJobs . Script . Diagnostics . OpenTelemetry ;
7
8
using Microsoft . Azure . WebJobs . Script . Metrics ;
8
9
using Microsoft . Extensions . DependencyInjection ;
9
10
using Microsoft . Extensions . Diagnostics . Metrics . Testing ;
11
+ using Microsoft . Extensions . Logging ;
10
12
using Microsoft . WebJobs . Script . Tests ;
11
13
using Xunit ;
12
14
@@ -16,14 +18,23 @@ namespace Microsoft.Azure.WebJobs.Script.Tests.Metrics
16
18
public class HostMetricsTests
17
19
{
18
20
private readonly IServiceProvider _serviceProvider ;
21
+ private TestLogger < HostMetrics > _logger ;
19
22
20
23
public HostMetricsTests ( )
21
24
{
22
25
var serviceCollection = new ServiceCollection ( ) ;
23
26
serviceCollection . AddMetrics ( ) ;
24
27
serviceCollection . AddFakeLogging ( ) ;
25
28
serviceCollection . AddSingleton < IEnvironment > ( new TestEnvironment ( ) ) ;
26
- serviceCollection . AddSingleton < IHostMetrics , HostMetrics > ( ) ;
29
+
30
+ _logger = new TestLogger < HostMetrics > ( ) ;
31
+
32
+ // Register HostMetrics with Scoped lifetime and provide the logger
33
+ serviceCollection . AddScoped < IHostMetrics > ( provider =>
34
+ {
35
+ return new HostMetrics ( provider . GetRequiredService < IMeterFactory > ( ) , provider . GetRequiredService < IEnvironment > ( ) , _logger ) ;
36
+ } ) ;
37
+
27
38
_serviceProvider = serviceCollection . BuildServiceProvider ( ) ;
28
39
}
29
40
@@ -85,5 +96,39 @@ public void FunctionGroupTag_Set_AfterEnvironmentVariableIsUpdated()
85
96
Assert . True ( measurements [ 1 ] . Tags . TryGetValue ( OpenTelemetryConstants . AzureFunctionsGroup , out funcGroup ) ) ;
86
97
Assert . Equal ( "function:test" , funcGroup ) ;
87
98
}
99
+
100
+ [ Theory ]
101
+ [ InlineData ( true ) ]
102
+ [ InlineData ( false ) ]
103
+ public void FunctionGroupTag_IsNullOrEmpty_UnableToResolveDebugLog_OnlyOnFlex ( bool isFlexSku )
104
+ {
105
+ // Arrange
106
+ var metrics = _serviceProvider . GetRequiredService < IHostMetrics > ( ) ;
107
+ var meterFactory = _serviceProvider . GetRequiredService < IMeterFactory > ( ) ;
108
+ var environment = _serviceProvider . GetRequiredService < IEnvironment > ( ) ;
109
+ var collector = new MetricCollector < long > ( meterFactory , HostMetrics . MeterName , HostMetrics . StartedInvocationCount ) ;
110
+
111
+ if ( isFlexSku )
112
+ {
113
+ environment . SetEnvironmentVariable ( EnvironmentSettingNames . AzureWebsiteSku , ScriptConstants . FlexConsumptionSku ) ;
114
+ }
115
+
116
+ // Act
117
+ metrics . IncrementStartedInvocationCount ( ) ;
118
+
119
+ // Assert
120
+ var logs = _logger . GetLogMessages ( ) ;
121
+
122
+ if ( isFlexSku )
123
+ {
124
+ var log = logs . Single ( ) ;
125
+ Assert . Equal ( LogLevel . Debug , log . Level ) ;
126
+ Assert . Equal ( $ "Unable to resolve FunctionGroupTag, { EnvironmentSettingNames . FunctionsTargetGroup } is null or empty.", log . FormattedMessage ) ;
127
+ }
128
+ else
129
+ {
130
+ Assert . Empty ( logs ) ;
131
+ }
132
+ }
88
133
}
89
134
}
0 commit comments