Skip to content

Commit 0b4b9d5

Browse files
committed
Adding metric log httpworker section is present in host.json.
1 parent 6b9c144 commit 0b4b9d5

File tree

12 files changed

+89
-49
lines changed

12 files changed

+89
-49
lines changed

src/WebJobs.Script/Config/HostJsonFileConfigurationSource.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public class HostJsonFileConfigurationProvider : ConfigurationProvider
5050
private static readonly string[] WellKnownHostJsonProperties = new[]
5151
{
5252
"version", "functionTimeout", "functions", "http", "watchDirectories", "queues", "serviceBus",
53-
"eventHub", "singleton", "logging", "aggregator", "healthMonitor", "extensionBundle", "managedDependencies"
53+
"eventHub", "singleton", "logging", "aggregator", "healthMonitor", "extensionBundle", "managedDependencies",
54+
"httpWorker"
5455
};
5556

5657
private readonly HostJsonFileConfigurationSource _configurationSource;

src/WebJobs.Script/Diagnostics/MetricEventNames.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ public static class MetricEventNames
5555
public const string FunctionInvokeSucceeded = "function.invoke.succeeded";
5656
public const string FunctionInvokeFailed = "function.invoke.failed";
5757

58+
// Http worker events
59+
public const string HttpWorker = "hostjsonfileconfigurationsource.httpworker";
60+
public const string DelayUntilWorkerIsInitialized = "httpworkerchannel.delayuntilworkerisinitialized";
61+
62+
// Out of proc process events
63+
public const string ProcessStart = "WorkerProcess.Start";
64+
5865
// secret managment events
5966
public const string SecretManagerDeleteSecret = "secretmanager.deletesecret.{0}";
6067
public const string SecretManagerGetFunctionSecrets = "secretmanager.getfunctionsecrets.{0}";

src/WebJobs.Script/Workers/Http/Configuration/HttpWorkerOptionsSetup.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Net;
55
using System.Net.Sockets;
66
using Microsoft.Azure.WebJobs.Script.Configuration;
7+
using Microsoft.Azure.WebJobs.Script.Diagnostics;
78
using Microsoft.Extensions.Configuration;
89
using Microsoft.Extensions.Logging;
910
using Microsoft.Extensions.Options;
@@ -14,12 +15,14 @@ internal class HttpWorkerOptionsSetup : IConfigureOptions<HttpWorkerOptions>
1415
{
1516
private IConfiguration _configuration;
1617
private ILogger _logger;
18+
private IMetricsLogger _metricsLogger;
1719
private ScriptJobHostOptions _scriptJobHostOptions;
1820

19-
public HttpWorkerOptionsSetup(IOptions<ScriptJobHostOptions> scriptJobHostOptions, IConfiguration configuration, ILoggerFactory loggerFactory)
21+
public HttpWorkerOptionsSetup(IOptions<ScriptJobHostOptions> scriptJobHostOptions, IConfiguration configuration, ILoggerFactory loggerFactory, IMetricsLogger metricsLogger)
2022
{
2123
_scriptJobHostOptions = scriptJobHostOptions.Value;
2224
_configuration = configuration;
25+
_metricsLogger = metricsLogger;
2326
_logger = loggerFactory.CreateLogger<HttpWorkerOptionsSetup>();
2427
}
2528

@@ -29,6 +32,7 @@ public void Configure(HttpWorkerOptions options)
2932
var httpWorkerSection = jobHostSection.GetSection(ConfigurationSectionNames.HttpWorker);
3033
if (httpWorkerSection.Exists())
3134
{
35+
_metricsLogger.LogEvent(MetricEventNames.HttpWorker);
3236
httpWorkerSection.Bind(options);
3337
HttpWorkerDescription httpWorkerDescription = options.Description;
3438

src/WebJobs.Script/Workers/Http/HttpWorkerChannel.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class HttpWorkerChannel : IHttpWorkerChannel, IDisposable
1919
private ILogger _workerChannelLogger;
2020
private IWorkerProcess _workerProcess;
2121
private IHttpWorkerService _httpWorkerService;
22+
private IMetricsLogger _metricsLogger;
2223

2324
internal HttpWorkerChannel(
2425
string workerId,
@@ -34,6 +35,7 @@ internal HttpWorkerChannel(
3435
_workerProcess = rpcWorkerProcess;
3536
_workerChannelLogger = logger;
3637
_httpWorkerService = httpWorkerService;
38+
_metricsLogger = metricsLogger;
3739
_startLatencyMetric = metricsLogger?.LatencyEvent(string.Format(MetricEventNames.WorkerInitializeLatency, "HttpWorker", attemptCount));
3840
}
3941

@@ -46,24 +48,27 @@ public Task InvokeFunction(ScriptInvocationContext context)
4648

4749
internal async Task DelayUntilWokerInitialized(CancellationToken cancellationToken)
4850
{
49-
_workerChannelLogger.LogDebug("Initializing HttpWorker.");
50-
try
51+
using (_metricsLogger.LatencyEvent(MetricEventNames.DelayUntilWorkerIsInitialized))
5152
{
52-
bool isWorkerReady = await _httpWorkerService.IsWorkerReady(cancellationToken);
53-
if (!isWorkerReady)
53+
_workerChannelLogger.LogDebug("Initializing HttpWorker.");
54+
try
5455
{
55-
PublishWorkerErrorEvent(new TimeoutException("Initializing HttpWorker timed out."));
56+
bool isWorkerReady = await _httpWorkerService.IsWorkerReady(cancellationToken);
57+
if (!isWorkerReady)
58+
{
59+
PublishWorkerErrorEvent(new TimeoutException("Initializing HttpWorker timed out."));
60+
}
61+
else
62+
{
63+
_workerChannelLogger.LogDebug("HttpWorker is Initialized.");
64+
}
5665
}
57-
else
66+
catch (Exception ex)
5867
{
59-
_workerChannelLogger.LogDebug("HttpWorker is Initialized.");
68+
// HttpFunctionInvocationDispatcher will handdle the worker error events
69+
PublishWorkerErrorEvent(ex);
6070
}
6171
}
62-
catch (Exception ex)
63-
{
64-
// HttpFunctionInvocationDispatcher will handdle the worker error events
65-
PublishWorkerErrorEvent(ex);
66-
}
6772
}
6873

6974
public async Task StartWorkerProcessAsync(CancellationToken cancellationToken)

src/WebJobs.Script/Workers/Http/HttpWorkerProcess.cs

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

44
using System;
55
using System.Diagnostics;
6+
using Microsoft.Azure.WebJobs.Script.Diagnostics;
67
using Microsoft.Azure.WebJobs.Script.Eventing;
78
using Microsoft.Extensions.Logging;
89
using Mono.Unix;
@@ -28,8 +29,9 @@ internal HttpWorkerProcess(string workerId,
2829
IProcessRegistry processRegistry,
2930
ILogger workerProcessLogger,
3031
IWorkerConsoleLogSource consoleLogSource,
31-
IEnvironment environment)
32-
: base(eventManager, processRegistry, workerProcessLogger, consoleLogSource)
32+
IEnvironment environment,
33+
IMetricsLogger metricsLogger)
34+
: base(eventManager, processRegistry, workerProcessLogger, consoleLogSource, metricsLogger)
3335
{
3436
_processFactory = processFactory;
3537
_eventManager = eventManager;

src/WebJobs.Script/Workers/Http/HttpWorkerProcessFactory.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System;
5+
using Microsoft.Azure.WebJobs.Script.Diagnostics;
56
using Microsoft.Azure.WebJobs.Script.Eventing;
67
using Microsoft.Azure.WebJobs.Script.Workers.Rpc;
78
using Microsoft.Extensions.Logging;
@@ -16,26 +17,29 @@ internal class HttpWorkerProcessFactory : IHttpWorkerProcessFactory
1617
private readonly IScriptEventManager _eventManager = null;
1718
private readonly IWorkerConsoleLogSource _consoleLogSource;
1819
private readonly IEnvironment _environment;
20+
private readonly IMetricsLogger _metricsLogger;
1921

2022
public HttpWorkerProcessFactory(IScriptEventManager eventManager,
2123
ILoggerFactory loggerFactory,
2224
IWorkerProcessFactory defaultWorkerProcessFactory,
2325
IProcessRegistry processRegistry,
2426
IWorkerConsoleLogSource consoleLogSource,
25-
IEnvironment environment)
27+
IEnvironment environment,
28+
IMetricsLogger metricsLogger)
2629
{
2730
_loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
2831
_eventManager = eventManager ?? throw new ArgumentNullException(nameof(eventManager));
2932
_consoleLogSource = consoleLogSource ?? throw new ArgumentNullException(nameof(consoleLogSource));
3033
_workerProcessFactory = defaultWorkerProcessFactory ?? throw new ArgumentNullException(nameof(defaultWorkerProcessFactory));
3134
_processRegistry = processRegistry ?? throw new ArgumentNullException(nameof(processRegistry));
35+
_metricsLogger = metricsLogger ?? throw new ArgumentNullException(nameof(metricsLogger));
3236
_environment = environment;
3337
}
3438

3539
public IWorkerProcess Create(string workerId, string scriptRootPath, HttpWorkerOptions httpWorkerOptions)
3640
{
3741
ILogger workerProcessLogger = _loggerFactory.CreateLogger($"Worker.HttpWorkerProcess.{workerId}");
38-
return new HttpWorkerProcess(workerId, scriptRootPath, httpWorkerOptions, _eventManager, _workerProcessFactory, _processRegistry, workerProcessLogger, _consoleLogSource, _environment);
42+
return new HttpWorkerProcess(workerId, scriptRootPath, httpWorkerOptions, _eventManager, _workerProcessFactory, _processRegistry, workerProcessLogger, _consoleLogSource, _environment, _metricsLogger);
3943
}
4044
}
4145
}

src/WebJobs.Script/Workers/ProcessManagement/WorkerProcess.cs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Linq;
88
using System.Threading.Tasks;
99
using Microsoft.Azure.WebJobs.Logging;
10+
using Microsoft.Azure.WebJobs.Script.Diagnostics;
1011
using Microsoft.Azure.WebJobs.Script.Eventing;
1112
using Microsoft.Extensions.Logging;
1213

@@ -18,17 +19,19 @@ internal abstract class WorkerProcess : IWorkerProcess, IDisposable
1819
private readonly ILogger _workerProcessLogger;
1920
private readonly IWorkerConsoleLogSource _consoleLogSource;
2021
private readonly IScriptEventManager _eventManager;
22+
private readonly IMetricsLogger _metricsLogger;
2123

2224
private Process _process;
2325
private bool _disposing;
2426
private Queue<string> _processStdErrDataQueue = new Queue<string>(3);
2527

26-
internal WorkerProcess(IScriptEventManager eventManager, IProcessRegistry processRegistry, ILogger workerProcessLogger, IWorkerConsoleLogSource consoleLogSource)
28+
internal WorkerProcess(IScriptEventManager eventManager, IProcessRegistry processRegistry, ILogger workerProcessLogger, IWorkerConsoleLogSource consoleLogSource, IMetricsLogger metricsLogger)
2729
{
2830
_processRegistry = processRegistry;
2931
_workerProcessLogger = workerProcessLogger;
3032
_consoleLogSource = consoleLogSource;
3133
_eventManager = eventManager;
34+
_metricsLogger = metricsLogger;
3235
}
3336

3437
public int Id => _process.Id;
@@ -39,29 +42,32 @@ internal WorkerProcess(IScriptEventManager eventManager, IProcessRegistry proces
3942

4043
public Task StartProcessAsync()
4144
{
42-
_process = CreateWorkerProcess();
43-
try
45+
using (_metricsLogger.LatencyEvent(MetricEventNames.ProcessStart))
4446
{
45-
_process.ErrorDataReceived += (sender, e) => OnErrorDataReceived(sender, e);
46-
_process.OutputDataReceived += (sender, e) => OnOutputDataReceived(sender, e);
47-
_process.Exited += (sender, e) => OnProcessExited(sender, e);
48-
_process.EnableRaisingEvents = true;
47+
_process = CreateWorkerProcess();
48+
try
49+
{
50+
_process.ErrorDataReceived += (sender, e) => OnErrorDataReceived(sender, e);
51+
_process.OutputDataReceived += (sender, e) => OnOutputDataReceived(sender, e);
52+
_process.Exited += (sender, e) => OnProcessExited(sender, e);
53+
_process.EnableRaisingEvents = true;
4954

50-
_workerProcessLogger?.LogInformation($"Starting worker process:{_process.StartInfo.FileName} {_process.StartInfo.Arguments}");
51-
_process.Start();
52-
_workerProcessLogger?.LogInformation($"{_process.StartInfo.FileName} process with Id={_process.Id} started");
55+
_workerProcessLogger?.LogInformation($"Starting worker process:{_process.StartInfo.FileName} {_process.StartInfo.Arguments}");
56+
_process.Start();
57+
_workerProcessLogger?.LogInformation($"{_process.StartInfo.FileName} process with Id={_process.Id} started");
5358

54-
_process.BeginErrorReadLine();
55-
_process.BeginOutputReadLine();
59+
_process.BeginErrorReadLine();
60+
_process.BeginOutputReadLine();
5661

57-
// Register process only after it starts
58-
_processRegistry?.Register(_process);
59-
return Task.CompletedTask;
60-
}
61-
catch (Exception ex)
62-
{
63-
_workerProcessLogger.LogError(ex, "Failed to start Worker Channel");
64-
return Task.FromException(ex);
62+
// Register process only after it starts
63+
_processRegistry?.Register(_process);
64+
return Task.CompletedTask;
65+
}
66+
catch (Exception ex)
67+
{
68+
_workerProcessLogger.LogError(ex, "Failed to start Worker Channel");
69+
return Task.FromException(ex);
70+
}
6571
}
6672
}
6773

src/WebJobs.Script/Workers/Rpc/RpcWorkerProcess.cs

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

44
using System;
55
using System.Diagnostics;
6+
using Microsoft.Azure.WebJobs.Script.Diagnostics;
67
using Microsoft.Azure.WebJobs.Script.Eventing;
78
using Microsoft.Extensions.Logging;
89

@@ -28,8 +29,9 @@ internal RpcWorkerProcess(string runtime,
2829
IWorkerProcessFactory processFactory,
2930
IProcessRegistry processRegistry,
3031
ILogger workerProcessLogger,
31-
IWorkerConsoleLogSource consoleLogSource)
32-
: base(eventManager, processRegistry, workerProcessLogger, consoleLogSource)
32+
IWorkerConsoleLogSource consoleLogSource,
33+
IMetricsLogger metricsLogger)
34+
: base(eventManager, processRegistry, workerProcessLogger, consoleLogSource, metricsLogger)
3335
{
3436
_runtime = runtime;
3537
_processFactory = processFactory;

src/WebJobs.Script/Workers/Rpc/RpcWorkerProcessFactory.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using Microsoft.Azure.WebJobs.Script.Abstractions;
5+
using Microsoft.Azure.WebJobs.Script.Diagnostics;
56
using Microsoft.Azure.WebJobs.Script.Eventing;
67
using Microsoft.Extensions.Logging;
78

@@ -15,26 +16,29 @@ internal class RpcWorkerProcessFactory : IRpcWorkerProcessFactory
1516
private readonly IScriptEventManager _eventManager = null;
1617
private readonly IRpcServer _rpcServer = null;
1718
private readonly IWorkerConsoleLogSource _consoleLogSource;
19+
private readonly IMetricsLogger _metricsLogger;
1820

1921
public RpcWorkerProcessFactory(IRpcServer rpcServer,
2022
IScriptEventManager eventManager,
2123
ILoggerFactory loggerFactory,
2224
IWorkerProcessFactory defaultWorkerProcessFactory,
2325
IProcessRegistry processRegistry,
24-
IWorkerConsoleLogSource consoleLogSource)
26+
IWorkerConsoleLogSource consoleLogSource,
27+
IMetricsLogger metricsLogger)
2528
{
2629
_loggerFactory = loggerFactory;
2730
_eventManager = eventManager;
2831
_rpcServer = rpcServer;
2932
_consoleLogSource = consoleLogSource;
3033
_workerProcessFactory = defaultWorkerProcessFactory;
3134
_processRegistry = processRegistry;
35+
_metricsLogger = metricsLogger;
3236
}
3337

3438
public IWorkerProcess Create(string workerId, string runtime, string scriptRootPath, RpcWorkerConfig workerConfig)
3539
{
3640
ILogger workerProcessLogger = _loggerFactory.CreateLogger($"Worker.rpcWorkerProcess.{runtime}.{workerId}");
37-
return new RpcWorkerProcess(runtime, workerId, scriptRootPath, _rpcServer.Uri, workerConfig.Arguments, _eventManager, _workerProcessFactory, _processRegistry, workerProcessLogger, _consoleLogSource);
41+
return new RpcWorkerProcess(runtime, workerId, scriptRootPath, _rpcServer.Uri, workerConfig.Arguments, _eventManager, _workerProcessFactory, _processRegistry, workerProcessLogger, _consoleLogSource, _metricsLogger);
3842
}
3943
}
4044
}

0 commit comments

Comments
 (0)