Skip to content

Commit

Permalink
Use single instance of WebhostMetricsLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
yojagad committed Oct 2, 2019
1 parent 023e969 commit e9d2df3
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private static ExpectedDependencyBuilder CreateExpectedDependencies()
expected.ExpectNone<IEventGenerator>();

expected.Expect<ILoggerFactory, ScriptLoggerFactory>();
expected.Expect<IMetricsLogger, WebHostMetricsLogger>();
expected.ExpectInstance<IMetricsLogger, WebHostMetricsLogger>();

expected.Expect<IWebJobsExceptionHandler, WebScriptHostExceptionHandler>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,6 @@ public static void AddWebJobsScriptHost(this IServiceCollection services, IConfi
services.AddTransient<VirtualFileSystem>();
services.AddTransient<VirtualFileSystemMiddleware>();

// Logging and diagnostics
services.AddSingleton<IMetricsLogger, WebHostMetricsLogger>();

// Secret management
services.TryAddSingleton<ISecretManagerProvider, DefaultSecretManagerProvider>();

Expand Down
8 changes: 7 additions & 1 deletion src/WebJobs.Script.WebHost/WebScriptHostBuilderExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,13 @@ public static IHostBuilder AddWebScriptHost(this IHostBuilder builder, IServiceP
}

// Logging and diagnostics
services.AddSingleton<IMetricsLogger, WebHostMetricsLogger>();
// Get WebHostMetricsLogger registered at webhost
IMetricsLogger metricsLogger = rootServiceProvider.GetService<IMetricsLogger>();
if (metricsLogger != null)
{
services.AddSingleton(metricsLogger);
}

services.AddSingleton<IEventCollectorProvider, FunctionInstanceLogCollectorProvider>();

// Hosted services
Expand Down
3 changes: 3 additions & 0 deletions src/WebJobs.Script/ScriptHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ public static IHostBuilder AddScriptHostCore(this IHostBuilder builder, ScriptAp
services.AddSingleton<ITypeLocator, ScriptTypeLocator>();
services.AddSingleton<ScriptSettingsManager>();
services.AddTransient<IExtensionsManager, ExtensionsManager>();

// Fallback option if webhost is not present.
services.TryAddSingleton<IMetricsLogger, MetricsLogger>();

services.TryAddSingleton<IScriptJobHostEnvironment, ConsoleScriptJobHostEnvironment>();
services.AddTransient<IExtensionBundleContentProvider, ExtensionBundleContentProvider>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,6 @@ private RunDependencies CreateDependencies(Action<IServiceCollection> configureS
compilationServiceFactory.Setup(f => f.CreateService(DotNetScriptTypes.CSharp, It.IsAny<IFunctionMetadataResolver>()))
.Returns(compilationService.Object);

var metricsLogger = new MetricsLogger();

var hostBuilder = new HostBuilder()
.ConfigureDefaultTestWebScriptHost(o =>
{
Expand All @@ -461,7 +459,7 @@ private RunDependencies CreateDependencies(Action<IServiceCollection> configureS
CompilationServiceFactory = compilationServiceFactory,
LoggerProvider = loggerProvider,
LoggerFactory = loggerFactory,
MetricsLogger = metricsLogger
MetricsLogger = new TestMetricsLogger(),
};
}

Expand Down Expand Up @@ -495,7 +493,7 @@ private class RunDependencies

public ILoggerFactory LoggerFactory { get; set; }

public MetricsLogger MetricsLogger { get; set; }
public IMetricsLogger MetricsLogger { get; set; }
}
}
}

0 comments on commit e9d2df3

Please sign in to comment.