Skip to content

Commit aa93070

Browse files
committed
re-enabling FunctionResultAggregator
1 parent fc0c087 commit aa93070

File tree

4 files changed

+86
-4
lines changed

4 files changed

+86
-4
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Microsoft.Azure.WebJobs.Host.Executors;
6+
using Microsoft.Azure.WebJobs.Host.Loggers;
7+
using Microsoft.Azure.WebJobs.Script.Diagnostics;
8+
using Microsoft.Extensions.Configuration;
9+
using Microsoft.Extensions.Logging;
10+
11+
namespace Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics
12+
{
13+
internal class FunctionInstanceLogCollectorProvider : IEventCollectorProvider
14+
{
15+
private readonly IFunctionMetadataManager _metadataManager;
16+
private readonly IProxyMetadataManager _proxyMetadataManager;
17+
private readonly IMetricsLogger _metrics;
18+
private readonly IHostIdProvider _hostIdProvider;
19+
private readonly IConfiguration _configuration;
20+
private readonly ILoggerFactory _loggerFactory;
21+
22+
public FunctionInstanceLogCollectorProvider(IFunctionMetadataManager metadataManager, IProxyMetadataManager proxyMetadataManager,
23+
IMetricsLogger metrics, IHostIdProvider hostIdProvider, IConfiguration configuration, ILoggerFactory loggerFactory)
24+
{
25+
_metadataManager = metadataManager ?? throw new ArgumentNullException(nameof(metadataManager));
26+
_proxyMetadataManager = proxyMetadataManager ?? throw new ArgumentNullException(nameof(proxyMetadataManager));
27+
_metrics = metrics ?? throw new ArgumentNullException(nameof(metrics));
28+
_hostIdProvider = hostIdProvider ?? throw new ArgumentNullException(nameof(hostIdProvider));
29+
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
30+
_loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
31+
}
32+
33+
public IAsyncCollector<FunctionInstanceLogEntry> Create()
34+
{
35+
return new FunctionInstanceLogger(_metadataManager, _proxyMetadataManager, _metrics, _hostIdProvider, _configuration, _loggerFactory);
36+
}
37+
}
38+
}

src/WebJobs.Script.WebHost/WebScriptHostBuilderExtension.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using Microsoft.Azure.WebJobs.Host.Config;
66
using Microsoft.Azure.WebJobs.Host.Executors;
7+
using Microsoft.Azure.WebJobs.Host.Loggers;
78
using Microsoft.Azure.WebJobs.Host.Timers;
89
using Microsoft.Azure.WebJobs.Script.Diagnostics;
910
using Microsoft.Azure.WebJobs.Script.WebHost.Configuration;
@@ -72,7 +73,7 @@ public static IHostBuilder AddWebScriptHost(this IHostBuilder builder, IServiceP
7273

7374
// Logging and diagnostics
7475
services.TryAddSingleton<IMetricsLogger, WebHostMetricsLogger>();
75-
services.AddSingleton<IAsyncCollector<Host.Loggers.FunctionInstanceLogEntry>, FunctionInstanceLogger>();
76+
services.AddSingleton<IEventCollectorProvider, FunctionInstanceLogCollectorProvider>();
7677

7778
// Hosted services
7879
services.TryAddEnumerable(ServiceDescriptor.Singleton<IHostedService, HttpInitializationService>());

test/WebJobs.Script.Tests.Integration/WebHostEndToEnd/CSharpEndToEndTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Net.Http.Headers;
1111
using System.Text;
1212
using System.Threading.Tasks;
13+
using Microsoft.Azure.WebJobs.Logging;
1314
using Microsoft.Azure.WebJobs.Script.Diagnostics;
1415
using Microsoft.Azure.WebJobs.Script.Rpc;
1516
using Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics;
@@ -111,6 +112,14 @@ await TestHelpers.Await(() =>
111112
// Make sure we get a metric logged from both ILogger and TraceWriter
112113
var key = MetricsEventManager.GetAggregateKey(MetricEventNames.FunctionUserLog, "Scenarios");
113114
Assert.Equal(2, Fixture.MetricsLogger.LoggedEvents.Where(p => p == key).Count());
115+
116+
// Make sure we've gotten a log from the aggregator
117+
IEnumerable<LogMessage> getAggregatorLogs() => Fixture.Host.GetLogMessages().Where(p => p.Category == LogCategories.Aggregator);
118+
119+
await TestHelpers.Await(() => getAggregatorLogs().Any());
120+
121+
var aggregatorLogs = getAggregatorLogs();
122+
Assert.Equal(1, aggregatorLogs.Count());
114123
}
115124

116125
[Fact]
@@ -405,6 +414,11 @@ public override void ConfigureJobHost(IWebJobsBuilder webJobsBuilder)
405414
"Scenarios"
406415
};
407416
});
417+
418+
webJobsBuilder.Services.Configure<FunctionResultAggregatorOptions>(o =>
419+
{
420+
o.BatchSize = 1;
421+
});
408422
}
409423
}
410424

test/WebJobs.Script.Tests/Configuration/AggregatorConfigurationTests.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Reflection;
8+
using Microsoft.Azure.WebJobs.Host.Loggers;
69
using Microsoft.Azure.WebJobs.Logging;
710
using Microsoft.Azure.WebJobs.Script.Configuration;
11+
using Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics;
812
using Microsoft.Extensions.Configuration;
913
using Microsoft.Extensions.DependencyInjection;
1014
using Microsoft.Extensions.Hosting;
@@ -16,17 +20,42 @@ namespace Microsoft.Azure.WebJobs.Script.Tests.Configuration
1620
{
1721
public class AggregatorConfigurationTests
1822
{
23+
private static readonly string AggregatorPath = ConfigurationPath.Combine(ConfigurationSectionNames.JobHost, ConfigurationSectionNames.Aggregator);
24+
25+
[Fact]
26+
public void Aggregator_Registered_ByDefault()
27+
{
28+
IHost host = new HostBuilder()
29+
.ConfigureDefaultTestWebScriptHost()
30+
.Build();
31+
32+
// Make sure there are two providers registered, and that one has a type with
33+
// "FunctionResultAggregatorProvider" (since it is internal to WebJobs)
34+
var eventCollectorProviders = host.Services.GetServices<IEventCollectorProvider>();
35+
Assert.Equal(2, eventCollectorProviders.Count());
36+
Assert.Single(eventCollectorProviders.OfType<FunctionInstanceLogCollectorProvider>());
37+
Assert.Single(eventCollectorProviders.Where(p => p.GetType().Name.Contains("FunctionResultAggregatorProvider")));
38+
39+
// Also make sure that when requesting the collectors, we end up with a composite that
40+
// includes both of the collectors above. This prevents any overriding of the IEventCollectorFactory
41+
var eventLogger = host.Services.GetServices<IAsyncCollector<FunctionInstanceLogEntry>>().Single();
42+
var field = eventLogger.GetType().GetField("_collectors", BindingFlags.NonPublic | BindingFlags.Instance);
43+
var collectors = (IEnumerable<IAsyncCollector<FunctionInstanceLogEntry>>)field.GetValue(eventLogger);
44+
Assert.Equal(2, collectors.Count());
45+
Assert.Single(collectors.OfType<FunctionInstanceLogger>());
46+
Assert.Single(collectors.Where(p => p.GetType().Name.Contains("FunctionResultAggregator")));
47+
}
48+
1949
[Fact]
2050
public void Configuration_BindsTo_AggregatorOptions()
2151
{
22-
string aggregatorPath = ConfigurationPath.Combine(ConfigurationSectionNames.JobHost, ConfigurationSectionNames.Aggregator);
2352
IHost host = new HostBuilder()
2453
.ConfigureAppConfiguration(c =>
2554
{
2655
c.AddInMemoryCollection(new Dictionary<string, string>
2756
{
28-
{ ConfigurationPath.Combine(aggregatorPath, "BatchSize"), "33" },
29-
{ ConfigurationPath.Combine(aggregatorPath, "FlushTimeout"), "00:00:33" }
57+
{ ConfigurationPath.Combine(AggregatorPath, "BatchSize"), "33" },
58+
{ ConfigurationPath.Combine(AggregatorPath, "FlushTimeout"), "00:00:33" }
3059
});
3160
})
3261
.ConfigureDefaultTestWebScriptHost()

0 commit comments

Comments
 (0)