Skip to content

Commit 1d0d118

Browse files
committed
Add EventSource for ApplicationInsightsMetricExporter
1 parent 7d7d97a commit 1d0d118

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.Diagnostics.Metrics;
6+
using System.Diagnostics.Tracing;
7+
8+
namespace Microsoft.Azure.WebJobs.Script.Diagnostics
9+
{
10+
public sealed partial class ApplicationInsightsMetricExporter
11+
{
12+
[EventSource(Name = "Azure-Functions-Application-Insights-Metric-Exporter")]
13+
private sealed class Events : EventSource
14+
{
15+
public static readonly Events Log = new();
16+
17+
private Events()
18+
{
19+
}
20+
21+
[Event(1, Message ="Failed to collect observable instruments: {0}", Level = EventLevel.Error)]
22+
public void FailedToCollectInstruments(Exception error)
23+
{
24+
WriteEvent(1, $"{error.GetType().FullName}: {error.Message}");
25+
}
26+
27+
[Event(2, Message = "Begin collecting observable instruments.")]
28+
public void BeginCollectObservables()
29+
{
30+
WriteEvent(2);
31+
}
32+
33+
[Event(3, Message = "End collecting observable instruments.")]
34+
public void EndCollectObservables()
35+
{
36+
WriteEvent(2);
37+
}
38+
39+
[Event(4, Message = "Meter listening started.")]
40+
public void MeterListeningStarted()
41+
{
42+
WriteEvent(4);
43+
}
44+
45+
[Event(5, Message = "Meter listening stopped.")]
46+
public void MeterListeningStopped()
47+
{
48+
WriteEvent(5);
49+
}
50+
51+
[Event(6, Message = "Subscribed to instrument {0} on meter {1}.")]
52+
public void SubscribedToInstrument(Instrument instrument)
53+
{
54+
WriteEvent(6, instrument.Name, instrument.Meter.Name);
55+
}
56+
}
57+
}
58+
}

src/WebJobs.Script/Diagnostics/ApplicationInsightsMetricExporter.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace Microsoft.Azure.WebJobs.Script.Diagnostics
1818
/// <summary>
1919
/// A meter listener which exports metrics to Application Insights.
2020
/// </summary>
21-
public sealed class ApplicationInsightsMetricExporter : ITelemetryModule, IAsyncDisposable
21+
public sealed partial class ApplicationInsightsMetricExporter : ITelemetryModule, IAsyncDisposable
2222
{
2323
private readonly MeterListener _listener;
2424
private readonly ApplicationInsightsMetricExporterOptions _options;
@@ -42,6 +42,7 @@ public ApplicationInsightsMetricExporter(IOptions<ApplicationInsightsMetricExpor
4242
{
4343
if (_options.ShouldListenTo(instrument))
4444
{
45+
Events.Log.SubscribedToInstrument(instrument);
4546
listener.EnableMeasurementEvents(instrument, this);
4647
}
4748
},
@@ -65,6 +66,8 @@ public void Initialize(TelemetryConfiguration configuration)
6566
{
6667
ArgumentNullException.ThrowIfNull(configuration);
6768
ObjectDisposedException.ThrowIf(_shutdown.IsCancellationRequested, this);
69+
70+
Events.Log.MeterListeningStarted();
6871
_client = new TelemetryClient(configuration);
6972
_listener.Start();
7073
_exportTask = CollectAsync(_shutdown.Token);
@@ -73,6 +76,7 @@ public void Initialize(TelemetryConfiguration configuration)
7376
/// <inheritdoc />
7477
public async ValueTask DisposeAsync()
7578
{
79+
Events.Log.MeterListeningStopped();
7680
_listener.Dispose();
7781

7882
await _shutdown.CancelNoThrowAsync();
@@ -114,12 +118,14 @@ private async Task CollectAsync(CancellationToken cancellation)
114118
{
115119
try
116120
{
121+
Events.Log.BeginCollectObservables();
117122
_listener.RecordObservableInstruments();
123+
Events.Log.EndCollectObservables();
118124
await Task.Delay(_options.CollectInterval, cancellation);
119125
}
120126
catch (Exception ex) when (!ex.IsFatal())
121127
{
122-
// swallow exceptions
128+
Events.Log.FailedToCollectInstruments(ex);
123129
}
124130
}
125131
}

0 commit comments

Comments
 (0)