Skip to content

Commit 6b74453

Browse files
authored
Flex Consumption Metrics Publishing (#9506)
1 parent b21a6d9 commit 6b74453

File tree

15 files changed

+1069
-7
lines changed

15 files changed

+1069
-7
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Microsoft.Azure.WebJobs.Script.WebHost.Metrics;
6+
7+
namespace Microsoft.Azure.WebJobs.Script.WebHost.Configuration
8+
{
9+
/// <summary>
10+
/// Configuration options for <see cref="FlexConsumptionMetricsPublisher"/>.
11+
/// </summary>
12+
public class FlexConsumptionMetricsPublisherOptions
13+
{
14+
internal const int DefaultMetricsPublishIntervalMS = 5000;
15+
internal const int DefaultMinimumActivityIntervalMS = 100;
16+
17+
public FlexConsumptionMetricsPublisherOptions()
18+
{
19+
MetricsPublishIntervalMS = DefaultMetricsPublishIntervalMS;
20+
MinimumActivityIntervalMS = DefaultMinimumActivityIntervalMS;
21+
InitialPublishDelayMS = Utility.ColdStartDelayMS;
22+
23+
// Default this to 15 minutes worth of files
24+
MaxFileCount = 15 * (int)Math.Ceiling(1.0 * 60 / (MetricsPublishIntervalMS / 1000));
25+
}
26+
27+
/// <summary>
28+
/// Gets or sets the interval at which metrics are published.
29+
/// </summary>
30+
public int MetricsPublishIntervalMS { get; set; }
31+
32+
/// <summary>
33+
/// Gets or sets the initial delay before publishing metrics.
34+
/// </summary>
35+
/// <remarks>
36+
/// This has cold-start implications. We want to ensure the first publish is done after
37+
/// cold-start is complete.
38+
/// </remarks>
39+
public int InitialPublishDelayMS { get; set; }
40+
41+
/// <summary>
42+
/// Gets or sets the minimum activity metering interval.
43+
/// </summary>
44+
/// <remarks>
45+
/// If the activity duration for an interval is less than this value, we
46+
/// round up.
47+
/// </remarks>
48+
public int MinimumActivityIntervalMS { get; set; }
49+
50+
/// <summary>
51+
/// Gets or sets the file location where metrics files are published.
52+
/// </summary>
53+
public string MetricsFilePath { get; set; }
54+
55+
/// <summary>
56+
/// Gets or sets thee maximum number of files to keep in the metrics directory.
57+
/// </summary>
58+
/// <remarks>
59+
/// When over this limit, the oldest files will be deleted to make room for new files.
60+
/// </remarks>
61+
public int MaxFileCount { get; set; }
62+
}
63+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using Microsoft.Extensions.Options;
5+
6+
namespace Microsoft.Azure.WebJobs.Script.WebHost.Configuration
7+
{
8+
public class FlexConsumptionMetricsPublisherOptionsSetup : IConfigureOptions<FlexConsumptionMetricsPublisherOptions>
9+
{
10+
private IEnvironment _environment;
11+
12+
public FlexConsumptionMetricsPublisherOptionsSetup(IEnvironment environment)
13+
{
14+
_environment = environment;
15+
}
16+
17+
public void Configure(FlexConsumptionMetricsPublisherOptions options)
18+
{
19+
options.MetricsFilePath = _environment.GetEnvironmentVariable(EnvironmentSettingNames.FunctionsMetricsPublishPath);
20+
}
21+
}
22+
}

src/WebJobs.Script.WebHost/Diagnostics/MetricsEventManager.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,8 @@ internal void FunctionStarted(FunctionStartedEvent startedEvent)
418418
{
419419
_runningFunctions.Add(startedEvent);
420420
}
421+
422+
_metricsPublisher?.OnFunctionStarted(startedEvent.FunctionName, startedEvent.InvocationId.ToString());
421423
}
422424

423425
internal void FunctionCompleted(FunctionStartedEvent startedEvent)
@@ -430,6 +432,8 @@ internal void FunctionCompleted(FunctionStartedEvent startedEvent)
430432
_functionMetricsQueue.Enqueue(monitoringEvent);
431433

432434
RaiseFunctionMetricEvent(startedEvent, _activeFunctionCount, DateTime.UtcNow);
435+
436+
_metricsPublisher?.OnFunctionCompleted(startedEvent.FunctionName, startedEvent.InvocationId.ToString());
433437
}
434438

435439
internal void StopTimerAndRaiseFinishedEvent()

0 commit comments

Comments
 (0)