Skip to content

Commit dd24afa

Browse files
committed
Emitting metric events for binding type/direction usage
1 parent 7c7c96d commit dd24afa

File tree

4 files changed

+98
-4
lines changed

4 files changed

+98
-4
lines changed

src/WebJobs.Script/Description/FunctionInvokerBase.cs

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

44
using System;
55
using System.Collections.Generic;
6+
using System.Collections.ObjectModel;
67
using System.Diagnostics;
78
using System.IO;
89
using System.Linq;
@@ -116,9 +117,7 @@ public async Task Invoke(object[] parameters)
116117
FunctionStartedEvent startedEvent = new FunctionStartedEvent(functionExecutionContext.InvocationId, Metadata);
117118
_metrics.BeginEvent(startedEvent);
118119

119-
var triggerType = Metadata.Bindings.First(p => p.IsTrigger).Type;
120-
string eventName = string.Format(MetricEventNames.FunctionInvokeByTriggerFormat, triggerType);
121-
_metrics.LogEvent(eventName);
120+
LogInvocationMetrics(_metrics, Metadata.Bindings);
122121

123122
try
124123
{
@@ -167,6 +166,20 @@ public async Task Invoke(object[] parameters)
167166
}
168167
}
169168

169+
internal static void LogInvocationMetrics(IMetricsLogger metrics, Collection<BindingMetadata> bindings)
170+
{
171+
metrics.LogEvent(MetricEventNames.FunctionInvoke);
172+
173+
// log events for each of the binding types used
174+
foreach (var binding in bindings)
175+
{
176+
string eventName = binding.IsTrigger ?
177+
string.Format(MetricEventNames.FunctionBindingTypeFormat, binding.Type) :
178+
string.Format(MetricEventNames.FunctionBindingTypeDirectionFormat, binding.Type, binding.Direction);
179+
metrics.LogEvent(eventName);
180+
}
181+
}
182+
170183
private void LogFunctionFailed(FunctionStartedEvent startedEvent, string resultString, string invocationId)
171184
{
172185
if (startedEvent != null)

src/WebJobs.Script/Diagnostics/MetricEventNames.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ public static class MetricEventNames
99
public const string HostStartupLatency = "host.startup.latency";
1010

1111
// function level events
12-
public const string FunctionInvokeByTriggerFormat = "function.invoke.{0}";
12+
public const string FunctionInvoke = "function.invoke";
13+
public const string FunctionBindingTypeFormat = "function.binding.{0}";
14+
public const string FunctionBindingTypeDirectionFormat = "function.binding.{0}.{1}";
1315
public const string FunctionCompileLatencyByLanguageFormat = "function.compile.{0}.latency";
1416
}
1517
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 System.Collections.ObjectModel;
6+
using Microsoft.Azure.WebJobs.Script.Description;
7+
using Microsoft.Azure.WebJobs.Script.Diagnostics;
8+
using Xunit;
9+
10+
namespace Microsoft.Azure.WebJobs.Script.Tests
11+
{
12+
public class FunctionInvokerBaseTests
13+
{
14+
[Fact]
15+
public void LogInvocationMetrics_EmitsExpectedEvents()
16+
{
17+
var metrics = new TestMetricsLogger();
18+
Collection<BindingMetadata> bindings = new Collection<BindingMetadata>
19+
{
20+
new BindingMetadata { Type = "httpTrigger" },
21+
new BindingMetadata { Type = "blob", Direction = BindingDirection.In },
22+
new BindingMetadata { Type = "blob", Direction = BindingDirection.Out },
23+
new BindingMetadata { Type = "table", Direction = BindingDirection.In },
24+
new BindingMetadata { Type = "table", Direction = BindingDirection.In }
25+
};
26+
27+
FunctionInvokerBase.LogInvocationMetrics(metrics, bindings);
28+
29+
Assert.Equal(6, metrics.LoggedEvents.Count);
30+
Assert.Equal("function.invoke", metrics.LoggedEvents[0]);
31+
Assert.Equal("function.binding.httpTrigger", metrics.LoggedEvents[1]);
32+
Assert.Equal("function.binding.blob.In", metrics.LoggedEvents[2]);
33+
Assert.Equal("function.binding.blob.Out", metrics.LoggedEvents[3]);
34+
Assert.Equal("function.binding.table.In", metrics.LoggedEvents[4]);
35+
Assert.Equal("function.binding.table.In", metrics.LoggedEvents[5]);
36+
}
37+
38+
private class TestMetricsLogger : IMetricsLogger
39+
{
40+
public TestMetricsLogger()
41+
{
42+
LoggedEvents = new Collection<string>();
43+
}
44+
45+
public Collection<string> LoggedEvents { get; }
46+
47+
public void BeginEvent(MetricEvent metricEvent)
48+
{
49+
throw new NotImplementedException();
50+
}
51+
52+
public object BeginEvent(string eventName)
53+
{
54+
throw new NotImplementedException();
55+
}
56+
57+
public void EndEvent(object eventHandle)
58+
{
59+
throw new NotImplementedException();
60+
}
61+
62+
public void EndEvent(MetricEvent metricEvent)
63+
{
64+
throw new NotImplementedException();
65+
}
66+
67+
public void LogEvent(string eventName)
68+
{
69+
LoggedEvents.Add(eventName);
70+
}
71+
72+
public void LogEvent(MetricEvent metricEvent)
73+
{
74+
throw new NotImplementedException();
75+
}
76+
}
77+
}
78+
}

test/WebJobs.Script.Tests/WebJobs.Script.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@
429429
<Compile Include="FunctionEntryPointResolverTests.cs" />
430430
<Compile Include="FunctionGeneratorTests.cs" />
431431
<Compile Include="Description\DotNet\PackageAssemblyResolverTests.cs" />
432+
<Compile Include="FunctionInvokerBaseTests.cs" />
432433
<Compile Include="HttpRouteFactoryTests.cs" />
433434
<Compile Include="HttpTriggerAttributeBindingProviderTests.cs" />
434435
<Compile Include="MetricsEventManagerTests.cs" />

0 commit comments

Comments
 (0)