Skip to content

Commit 5ef8f6c

Browse files
authored
Do not log 'unable to resolve FunctionGroupTag' outside of flex (#10791)
1 parent 95b60e9 commit 5ef8f6c

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

src/WebJobs.Script/Metrics/HostMetrics.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ private KeyValuePair<string, object> FunctionGroupTag
7777
{
7878
_cachedFunctionGroupTag = functionGroupTag;
7979
}
80-
else
80+
else if (_environment.IsFlexConsumptionSku())
8181
{
82+
// FunctionGroup is only used in Flex Consumption and would not be set in other SKUs.
8283
_logger.LogDebug("Unable to resolve {tagName}, {funcGroup} is null or empty.", nameof(FunctionGroupTag), EnvironmentSettingNames.FunctionsTargetGroup);
8384
}
8485

test/WebJobs.Script.Tests/Metrics/HostMetricsTests.cs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33

44
using System;
55
using System.Diagnostics.Metrics;
6+
using System.Linq;
67
using Microsoft.Azure.WebJobs.Script.Diagnostics.OpenTelemetry;
78
using Microsoft.Azure.WebJobs.Script.Metrics;
89
using Microsoft.Extensions.DependencyInjection;
910
using Microsoft.Extensions.Diagnostics.Metrics.Testing;
11+
using Microsoft.Extensions.Logging;
1012
using Microsoft.WebJobs.Script.Tests;
1113
using Xunit;
1214

@@ -16,14 +18,23 @@ namespace Microsoft.Azure.WebJobs.Script.Tests.Metrics
1618
public class HostMetricsTests
1719
{
1820
private readonly IServiceProvider _serviceProvider;
21+
private TestLogger<HostMetrics> _logger;
1922

2023
public HostMetricsTests()
2124
{
2225
var serviceCollection = new ServiceCollection();
2326
serviceCollection.AddMetrics();
2427
serviceCollection.AddFakeLogging();
2528
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+
2738
_serviceProvider = serviceCollection.BuildServiceProvider();
2839
}
2940

@@ -85,5 +96,39 @@ public void FunctionGroupTag_Set_AfterEnvironmentVariableIsUpdated()
8596
Assert.True(measurements[1].Tags.TryGetValue(OpenTelemetryConstants.AzureFunctionsGroup, out funcGroup));
8697
Assert.Equal("function:test", funcGroup);
8798
}
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+
}
88133
}
89134
}

0 commit comments

Comments
 (0)