Skip to content

Commit 7507230

Browse files
committed
FunctionInstanceMonitor cleanup (optimization/refactoring)
1 parent 4af158f commit 7507230

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

src/WebJobs.Script/Description/FunctionInvokerBase.cs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,12 @@ public void Dispose()
216216
// Per function instance
217217
public class FunctionInstanceMonitor
218218
{
219-
// From ctor
220219
private readonly FunctionMetadata _metadata;
221220
private readonly IMetricsLogger _metrics;
222221
private readonly Guid _invocationId;
223222

224-
private readonly Stopwatch _invocationStopWatch = new Stopwatch();
225-
226-
private FunctionStartedEvent startedEvent;
227-
private object invokeLatencyEvent;
223+
private FunctionStartedEvent _startedEvent;
224+
private object _invokeLatencyEvent;
228225

229226
public FunctionInstanceMonitor(
230227
FunctionMetadata metadata,
@@ -238,36 +235,37 @@ public FunctionInstanceMonitor(
238235

239236
public void Start()
240237
{
241-
startedEvent = new FunctionStartedEvent(_invocationId, _metadata);
242-
_metrics.BeginEvent(startedEvent);
243-
invokeLatencyEvent = FunctionInvokerBase.LogInvocationMetrics(_metrics, _metadata);
244-
_invocationStopWatch.Start();
238+
_startedEvent = new FunctionStartedEvent(_invocationId, _metadata);
239+
_metrics.BeginEvent(_startedEvent);
240+
_invokeLatencyEvent = FunctionInvokerBase.LogInvocationMetrics(_metrics, _metadata);
245241
}
246242

247243
// Called on success and failure
248244
public void End(bool success)
249245
{
250-
startedEvent.Success = success;
246+
_startedEvent.Success = success;
251247
string eventName = success ? MetricEventNames.FunctionInvokeSucceeded : MetricEventNames.FunctionInvokeFailed;
252-
dynamic data = new JObject();
253-
data.Language = startedEvent.FunctionMetadata.Language;
254-
data.FunctionName = _metadata != null ? _metadata.Name : string.Empty;
255-
data.Success = success;
248+
249+
var data = new JObject
250+
{
251+
["Language"] = _startedEvent.FunctionMetadata.Language,
252+
["FunctionName"] = _metadata != null ? _metadata.Name : string.Empty,
253+
["Success"] = success
254+
};
255+
256256
string jsonData = data.ToString();
257-
startedEvent.Data = jsonData;
258-
_metrics.LogEvent(eventName, startedEvent.FunctionName, jsonData);
259257

260-
_metrics.EndEvent(startedEvent);
258+
_startedEvent.Data = jsonData;
259+
_metrics.LogEvent(eventName, _startedEvent.FunctionName, jsonData);
261260

262-
if (invokeLatencyEvent != null)
261+
_metrics.EndEvent(_startedEvent);
262+
263+
if (_invokeLatencyEvent is MetricEvent metricEvent)
263264
{
264-
MetricEvent metricEvent = invokeLatencyEvent as MetricEvent;
265-
if (metricEvent != null)
266-
{
267-
metricEvent.Data = jsonData;
268-
}
269-
_metrics.EndEvent(invokeLatencyEvent);
265+
metricEvent.Data = jsonData;
270266
}
267+
268+
_metrics.EndEvent(_invokeLatencyEvent);
271269
}
272270
}
273271
}

0 commit comments

Comments
 (0)