Skip to content

Commit 1e60f0a

Browse files
committed
expose ILoggerFactoryBuilder to WebHostSettings; adding app insights http tests
1 parent 66d830c commit 1e60f0a

File tree

19 files changed

+507
-190
lines changed

19 files changed

+507
-190
lines changed

src/WebJobs.Script.WebHost/App_Start/WebHostResolver.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
using System;
55
using System.Collections.Generic;
66
using System.IO;
7-
using System.Linq;
8-
using System.Reflection;
97
using System.Threading.Tasks;
108
using Microsoft.Azure.WebJobs.Script.Config;
119
using Microsoft.Azure.WebJobs.Script.Diagnostics;
@@ -108,7 +106,7 @@ private void EnsureInitialized(WebHostSettings settings)
108106
{
109107
_activeScriptHostConfig = CreateScriptHostConfiguration(settings);
110108

111-
_activeHostManager = new WebScriptHostManager(_activeScriptHostConfig, _secretManagerFactory, _eventManager, _settingsManager, settings);
109+
_activeHostManager = new WebScriptHostManager(_activeScriptHostConfig, _secretManagerFactory, _eventManager, _settingsManager, settings);
112110
_activeReceiverManager = new WebHookReceiverManager(_activeHostManager.SecretManager);
113111

114112
_standbyHostManager?.Dispose();
@@ -142,7 +140,8 @@ internal static ScriptHostConfiguration CreateScriptHostConfiguration(WebHostSet
142140
RootLogPath = settings.LogPath,
143141
FileLoggingMode = FileLoggingMode.DebugOnly,
144142
TraceWriter = settings.TraceWriter,
145-
IsSelfHost = settings.IsSelfHost
143+
IsSelfHost = settings.IsSelfHost,
144+
LoggerFactoryBuilder = settings.LoggerFactoryBuilder
146145
};
147146

148147
scriptHostConfig.HostConfig.HostId = Utility.GetDefaultHostId(_settingsManager, scriptHostConfig);

src/WebJobs.Script.WebHost/App_Start/WebHostSettings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public class WebHostSettings
3131

3232
public TraceWriter TraceWriter { get; set; }
3333

34+
public ILoggerFactoryBuilder LoggerFactoryBuilder { get; set; } = new DefaultLoggerFactoryBuilder();
35+
3436
internal static WebHostSettings CreateDefault(ScriptSettingsManager settingsManager)
3537
{
3638
WebHostSettings settings = new WebHostSettings

src/WebJobs.Script/Config/ScriptHostConfiguration.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public ScriptHostConfiguration()
2323
RootLogPath = Path.Combine(Path.GetTempPath(), "Functions");
2424
LogFilter = new LogCategoryFilter();
2525
RootExtensionsPath = ConfigurationManager.AppSettings[EnvironmentSettingNames.AzureWebJobsExtensionsPath];
26+
LoggerFactoryBuilder = new DefaultLoggerFactoryBuilder();
2627
}
2728

2829
/// <summary>
@@ -117,6 +118,12 @@ public ScriptHostConfiguration()
117118
/// </summary>
118119
public SamplingPercentageEstimatorSettings ApplicationInsightsSamplingSettings { get; set; }
119120

121+
/// <summary>
122+
/// Gets or sets the <see cref="ILoggerFactoryBuilder"/> used to register <see cref="ILoggerProvider"/>s with
123+
/// the host's <see cref="ILoggerFactory"/>.
124+
/// </summary>
125+
public ILoggerFactoryBuilder LoggerFactoryBuilder { get; set; }
126+
120127
/// <summary>
121128
/// Gets or sets a test hook for modifying the configuration after host.json has been processed.
122129
/// </summary>

src/WebJobs.Script/Host/ScriptHost.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -791,10 +791,7 @@ internal static void ConfigureLoggerFactory(ScriptHostConfiguration scriptConfig
791791
scriptConfig.HostConfig.LoggerFactory.AddProvider(new FileLoggerProvider(traceWriteFactory,
792792
(category, level) => (category == LogCategories.Function) && isFileLoggingEnabled()));
793793

794-
// Allow a way to plug in custom LoggerProviders.
795-
ILoggerFactoryBuilder builder = scriptConfig.HostConfig.GetService<ILoggerFactoryBuilder>() ??
796-
new DefaultLoggerFactoryBuilder();
797-
builder.AddLoggerProviders(scriptConfig.HostConfig.LoggerFactory, scriptConfig, settingsManager);
794+
scriptConfig.LoggerFactoryBuilder.AddLoggerProviders(scriptConfig.HostConfig.LoggerFactory, scriptConfig, settingsManager);
798795
}
799796

800797
private void TraceFileChangeRestart(string changeType, string path, bool isShutdown)

test/WebJobs.Script.Tests.Integration/ApplicationInsights/ApplicationInsightsCSharpEndToEndTests.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ public ApplicationInsightsCSharpEndToEndTests(TestFixture fixture) : base(fixtur
1212
{
1313
}
1414

15-
[Fact]
16-
public async Task ApplicationInsights_Succeeds()
17-
{
18-
await ApplicationInsights_SucceedsTest();
19-
}
20-
2115
public class TestFixture : ApplicationInsightsTestFixture
2216
{
2317
private const string ScriptRoot = @"TestScripts\CSharp";

test/WebJobs.Script.Tests.Integration/ApplicationInsights/ApplicationInsightsEndToEndTestsBase.cs

Lines changed: 217 additions & 81 deletions
Large diffs are not rendered by default.

test/WebJobs.Script.Tests.Integration/ApplicationInsights/ApplicationInsightsNodeEndToEndTests.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4-
using System.Threading.Tasks;
5-
using Xunit;
6-
74
namespace Microsoft.Azure.WebJobs.Script.Tests.ApplicationInsights
85
{
96
public class ApplicationInsightsNodeEndToEndTests : ApplicationInsightsEndToEndTestsBase<ApplicationInsightsNodeEndToEndTests.TestFixture>
@@ -12,12 +9,6 @@ public ApplicationInsightsNodeEndToEndTests(TestFixture fixture) : base(fixture)
129
{
1310
}
1411

15-
[Fact]
16-
public async Task ApplicationInsights_Succeeds()
17-
{
18-
await ApplicationInsights_SucceedsTest();
19-
}
20-
2112
public class TestFixture : ApplicationInsightsTestFixture
2213
{
2314
private const string ScriptRoot = @"TestScripts\Node";
Lines changed: 106 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,130 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Net.Http;
8+
using System.Web.Http;
9+
using Microsoft.ApplicationInsights.Channel;
10+
using Microsoft.ApplicationInsights.WindowsServer.Channel.Implementation;
11+
using Microsoft.Azure.WebJobs.Logging.ApplicationInsights;
412
using Microsoft.Azure.WebJobs.Script.Config;
13+
using Microsoft.Azure.WebJobs.Script.WebHost;
14+
using Microsoft.Extensions.Logging;
515
using Microsoft.WebJobs.Script.Tests;
616

717
namespace Microsoft.Azure.WebJobs.Script.Tests.ApplicationInsights
818
{
9-
public abstract class ApplicationInsightsTestFixture : EndToEndTestFixture
19+
public abstract class ApplicationInsightsTestFixture
1020
{
11-
static ApplicationInsightsTestFixture()
12-
{
13-
// We need to set this to something in order to trigger App Insights integration. But since
14-
// we're hitting a local HttpListener, it can be anything.
15-
ScriptSettingsManager.Instance.ApplicationInsightsInstrumentationKey = TestChannelLoggerFactoryBuilder.ApplicationInsightsKey;
16-
}
21+
private readonly ScriptSettingsManager _settingsManager;
22+
private readonly HttpConfiguration _config = new HttpConfiguration();
1723

1824
public ApplicationInsightsTestFixture(string scriptRoot, string testId)
19-
: base(scriptRoot, testId)
2025
{
26+
_settingsManager = ScriptSettingsManager.Instance;
27+
28+
HostSettings = new WebHostSettings
29+
{
30+
IsSelfHost = true,
31+
ScriptPath = Path.Combine(Environment.CurrentDirectory, scriptRoot),
32+
LogPath = Path.Combine(Path.GetTempPath(), @"Functions"),
33+
SecretsPath = Environment.CurrentDirectory, // not used
34+
LoggerFactoryBuilder = new TestLoggerFactoryBuilder(Channel),
35+
IsAuthDisabled = true
36+
};
37+
WebApiConfig.Register(_config, _settingsManager, HostSettings);
38+
39+
var resolver = _config.DependencyResolver;
40+
var hostConfig = resolver.GetService<WebHostResolver>().GetScriptHostConfiguration(HostSettings);
41+
42+
_settingsManager.ApplicationInsightsInstrumentationKey = TestChannelLoggerFactoryBuilder.ApplicationInsightsKey;
43+
44+
InitializeConfig(hostConfig);
45+
46+
var httpServer = new HttpServer(_config);
47+
HttpClient = new HttpClient(httpServer)
48+
{
49+
BaseAddress = new Uri("https://localhost/")
50+
};
51+
52+
TestHelpers.WaitForWebHost(HttpClient);
2153
}
2254

2355
public TestTelemetryChannel Channel { get; private set; } = new TestTelemetryChannel();
2456

25-
protected override void InitializeConfig(ScriptHostConfiguration config)
26-
{
27-
var builder = new TestChannelLoggerFactoryBuilder(Channel);
28-
config.HostConfig.AddService<ILoggerFactoryBuilder>(builder);
57+
public WebHostSettings HostSettings { get; private set; }
2958

30-
// turn this off as it makes validation tough
31-
config.HostConfig.Aggregator.IsEnabled = false;
59+
public HttpClient HttpClient { get; private set; }
3260

61+
protected void InitializeConfig(ScriptHostConfiguration config)
62+
{
3363
config.OnConfigurationApplied = c =>
3464
{
35-
// Overwrite the generated function whitelist to only include one function.
36-
c.Functions = new[] { "Scenarios" };
65+
// turn this off as it makes validation tough
66+
config.HostConfig.Aggregator.IsEnabled = false;
67+
68+
// Overwrite the generated function whitelist to only include two functions.
69+
c.Functions = new[] { "Scenarios", "HttpTrigger-Scenarios" };
3770
};
3871
}
72+
73+
private class TestLoggerFactoryBuilder : DefaultLoggerFactoryBuilder
74+
{
75+
private readonly TestTelemetryChannel _channel;
76+
77+
public TestLoggerFactoryBuilder(TestTelemetryChannel channel)
78+
{
79+
_channel = channel;
80+
}
81+
82+
public override void AddLoggerProviders(ILoggerFactory factory, ScriptHostConfiguration scriptConfig, ScriptSettingsManager settingsManager)
83+
{
84+
// Replace TelemetryClient
85+
var clientFactory = new TestTelemetryClientFactory(scriptConfig.LogFilter.Filter, _channel);
86+
scriptConfig.HostConfig.AddService<ITelemetryClientFactory>(clientFactory);
87+
88+
base.AddLoggerProviders(factory, scriptConfig, settingsManager);
89+
}
90+
}
91+
92+
public class TestTelemetryChannel : ITelemetryChannel
93+
{
94+
public IList<ITelemetry> Telemetries { get; private set; } = new List<ITelemetry>();
95+
96+
public bool? DeveloperMode { get; set; }
97+
98+
public string EndpointAddress { get; set; }
99+
100+
public void Dispose()
101+
{
102+
}
103+
104+
public void Flush()
105+
{
106+
}
107+
108+
public void Send(ITelemetry item)
109+
{
110+
Telemetries.Add(item);
111+
}
112+
}
113+
114+
private class TestTelemetryClientFactory : ScriptTelemetryClientFactory
115+
{
116+
private TestTelemetryChannel _channel;
117+
118+
public TestTelemetryClientFactory(Func<string, LogLevel, bool> filter, TestTelemetryChannel channel)
119+
: base(TestChannelLoggerFactoryBuilder.ApplicationInsightsKey, new SamplingPercentageEstimatorSettings(), filter)
120+
{
121+
_channel = channel;
122+
}
123+
124+
protected override ITelemetryChannel CreateTelemetryChannel()
125+
{
126+
return _channel;
127+
}
128+
}
39129
}
40130
}

test/WebJobs.Script.Tests.Integration/Controllers/ControllerScenarioTestFixture.cs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.IO;
6-
using System.Net;
76
using System.Net.Http;
87
using System.Web.Http;
98
using Autofac;
@@ -39,10 +38,10 @@ public ControllerScenarioTestFixture(bool isAuthDisabled)
3938
WebApiConfig.Register(_config, _settingsManager, HostSettings, RegisterDependencies);
4039

4140
HttpServer = new HttpServer(_config);
42-
this.HttpClient = new HttpClient(HttpServer);
43-
this.HttpClient.BaseAddress = new Uri("https://localhost/");
41+
HttpClient = new HttpClient(HttpServer);
42+
HttpClient.BaseAddress = new Uri("https://localhost/");
4443

45-
WaitForHost();
44+
TestHelpers.WaitForWebHost(HttpClient);
4645
}
4746

4847
public WebHostSettings HostSettings { get; private set; }
@@ -51,25 +50,6 @@ public ControllerScenarioTestFixture(bool isAuthDisabled)
5150

5251
public HttpServer HttpServer { get; set; }
5352

54-
private void WaitForHost()
55-
{
56-
TestHelpers.Await(() =>
57-
{
58-
return IsHostRunning();
59-
}).Wait();
60-
}
61-
62-
private bool IsHostRunning()
63-
{
64-
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, string.Empty))
65-
{
66-
using (HttpResponseMessage response = this.HttpClient.SendAsync(request).Result)
67-
{
68-
return response.StatusCode == HttpStatusCode.NoContent || response.StatusCode == HttpStatusCode.OK;
69-
}
70-
}
71-
}
72-
7353
protected virtual void RegisterDependencies(ContainerBuilder builder, WebHostSettings settings)
7454
{
7555
}

test/WebJobs.Script.Tests.Integration/SamplesEndToEndTests.cs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,8 +1186,8 @@ public TestFixture()
11861186
WebApiConfig.Register(_config, _settingsManager, HostSettings);
11871187

11881188
HttpServer = new HttpServer(_config);
1189-
this.HttpClient = new HttpClient(HttpServer);
1190-
this.HttpClient.BaseAddress = new Uri("https://localhost/");
1189+
HttpClient = new HttpClient(HttpServer);
1190+
HttpClient.BaseAddress = new Uri("https://localhost/");
11911191

11921192
string connectionString = AmbientConnectionStringProvider.Instance.GetConnectionString("Storage");
11931193
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
@@ -1211,7 +1211,7 @@ public TestFixture()
12111211
connectionString = AmbientConnectionStringProvider.Instance.GetConnectionString(ConnectionStringNames.ServiceBus);
12121212
NamespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
12131213

1214-
WaitForHost();
1214+
TestHelpers.WaitForWebHost(HttpClient);
12151215
}
12161216

12171217
public WebHostSettings HostSettings { get; private set; }
@@ -1228,25 +1228,6 @@ public TestFixture()
12281228

12291229
public HttpServer HttpServer { get; set; }
12301230

1231-
private void WaitForHost()
1232-
{
1233-
TestHelpers.Await(() =>
1234-
{
1235-
return IsHostRunning();
1236-
}).Wait();
1237-
}
1238-
1239-
private bool IsHostRunning()
1240-
{
1241-
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, string.Empty))
1242-
{
1243-
using (HttpResponseMessage response = this.HttpClient.SendAsync(request).Result)
1244-
{
1245-
return response.StatusCode == HttpStatusCode.NoContent || response.StatusCode == HttpStatusCode.OK;
1246-
}
1247-
}
1248-
}
1249-
12501231
public void Dispose()
12511232
{
12521233
HttpServer?.Dispose();

0 commit comments

Comments
 (0)