Skip to content

Commit 7d7d97a

Browse files
committed
Address PR comments, fix build
1 parent 514ad74 commit 7d7d97a

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

src/WebJobs.Script/Diagnostics/ApplicationInsightsMetricExporter.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public sealed class ApplicationInsightsMetricExporter : ITelemetryModule, IAsync
3030
/// <summary>
3131
/// Initializes a new instance of the <see cref="ApplicationInsightsMetricExporter"/> class.
3232
/// </summary>
33-
/// <param name="lifetime">The application lifetime.</param>
3433
/// <param name="options">The options.</param>
3534
public ApplicationInsightsMetricExporter(IOptions<ApplicationInsightsMetricExporterOptions> options)
3635
{
@@ -65,9 +64,10 @@ public ApplicationInsightsMetricExporter(IOptions<ApplicationInsightsMetricExpor
6564
public void Initialize(TelemetryConfiguration configuration)
6665
{
6766
ArgumentNullException.ThrowIfNull(configuration);
67+
ObjectDisposedException.ThrowIf(_shutdown.IsCancellationRequested, this);
6868
_client = new TelemetryClient(configuration);
69-
_exportTask = CollectAsync(_shutdown.Token);
7069
_listener.Start();
70+
_exportTask = CollectAsync(_shutdown.Token);
7171
}
7272

7373
/// <inheritdoc />
@@ -77,7 +77,12 @@ public async ValueTask DisposeAsync()
7777

7878
await _shutdown.CancelNoThrowAsync();
7979
await _exportTask.ConfigureAwait(false);
80-
await _client.FlushAsync(default).ConfigureAwait(false);
80+
81+
if (_client is { } client)
82+
{
83+
await client.FlushAsync(default).ConfigureAwait(false);
84+
}
85+
8186
_shutdown.Dispose();
8287
}
8388

src/WebJobs.Script/Diagnostics/ApplicationInsightsMetricExporterOptions.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System;
77
using System.Collections.Generic;
88
using System.Diagnostics.Metrics;
9+
using Microsoft.Azure.WebJobs.Script.Metrics;
910

1011
namespace Microsoft.Azure.WebJobs.Script.Diagnostics
1112
{
@@ -14,6 +15,13 @@ namespace Microsoft.Azure.WebJobs.Script.Diagnostics
1415
/// </summary>
1516
public class ApplicationInsightsMetricExporterOptions
1617
{
18+
// AppInsights has some metrics which are already emitted a different way.
19+
// We ignore them here to ensure we don't duplicate the metric.
20+
private static readonly HashSet<string> IgnoredInstruments =
21+
[
22+
HostMetrics.FaasInvokeDuration,
23+
];
24+
1725
/// <summary>
1826
/// Gets the set of meter names to listen to.
1927
/// </summary>
@@ -40,7 +48,7 @@ public bool ShouldListenTo(Instrument instrument)
4048

4149
// TODO: consider allowing wildcards or regex
4250
// For now, just exact match on meter name
43-
return Meters.Contains(instrument.Meter.Name);
51+
return Meters.Contains(instrument.Meter.Name) && !IgnoredInstruments.Contains(instrument.Name);
4452
}
4553
}
4654
}

test/WebJobs.Script.Tests/Diagnostics/ApplicationInsightsMetricExporterTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,10 @@ public void MeterListener_TracksConfiguredInstruments(string test)
133133
});
134134
}
135135

136-
private static ApplicationInsightsMetricExporter CreateExporter(
137-
params ReadOnlySpan<string> meters)
136+
private static ApplicationInsightsMetricExporter CreateExporter(params string[] meters)
138137
=> new(CreateOptions(meters));
139138

140-
private static OptionsWrapper<ApplicationInsightsMetricExporterOptions> CreateOptions(
141-
params ReadOnlySpan<string> meters)
139+
private static OptionsWrapper<ApplicationInsightsMetricExporterOptions> CreateOptions(params string[] meters)
142140
{
143141
ApplicationInsightsMetricExporterOptions options = new();
144142
foreach (string meter in meters)

0 commit comments

Comments
 (0)