Skip to content

Commit 62c4472

Browse files
authored
Ensure only full invocations are measured (#10177)
1 parent 13d345f commit 62c4472

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/WebJobs.Script.WebHost/Metrics/FlexConsumptionMetricsPublisher.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,13 @@ internal void OnFunctionCompleted(string functionName, string invocationId, Date
268268
{
269269
ActiveFunctionCount--;
270270
}
271+
else
272+
{
273+
// We got a completion event without a corresponding start.
274+
// This might happen during specialization for example.
275+
// Ignore the event.
276+
return;
277+
}
271278

272279
if (ActiveFunctionCount == 0)
273280
{

test/WebJobs.Script.Tests/Metrics/FlexConsumptionMetricsPublisherTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,27 @@ public void FunctionsStartStop_MinimumActivityIntervals_Scenario3()
437437
Assert.Equal(4800, publisher.FunctionExecutionTimeMS);
438438
}
439439

440+
[Fact]
441+
public void OnFunctionCompleted_NoOutstandingInvocations_IgnoresEvent()
442+
{
443+
var publisher = CreatePublisher(metricsPublishInterval: TimeSpan.FromHours(1), inStandbyMode: false);
444+
445+
Assert.Equal(1000, _options.MinimumActivityIntervalMS);
446+
Assert.Equal(0, publisher.ActiveFunctionCount);
447+
Assert.Equal(0, publisher.FunctionExecutionCount);
448+
Assert.Equal(0, publisher.FunctionExecutionTimeMS);
449+
450+
DateTime now = DateTime.UtcNow;
451+
452+
// send a function completion without a corresponding start event
453+
now += TimeSpan.FromMilliseconds(300);
454+
publisher.OnFunctionCompleted("foo", "1", now);
455+
456+
Assert.Equal(0, publisher.ActiveFunctionCount);
457+
Assert.Equal(0, publisher.FunctionExecutionCount);
458+
Assert.Equal(0, publisher.FunctionExecutionTimeMS);
459+
}
460+
440461
public void CleanupMetricsFiles()
441462
{
442463
var directory = new DirectoryInfo(_metricsFilePath);

0 commit comments

Comments
 (0)