Skip to content

Commit

Permalink
(more) correctly disposing TelemetryConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
jdvor committed Apr 24, 2019
1 parent f2c7eba commit b305d5f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 50 deletions.
64 changes: 23 additions & 41 deletions sample/SandboxConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,65 +28,47 @@ public static class Program

public static void Main()
{
//TestTc();
//return;

var recordEvery = TimeSpan.FromSeconds(2);
var reportEvery = TimeSpan.FromSeconds(60);

Init();

TotalTime = Stopwatch.StartNew();
var cts = new CancellationTokenSource();
backgroundTasks = new[]
using (var cts = new CancellationTokenSource())
{
Task.Run(() => Record(recordEvery, cts.Token)),
Task.Run(() => Report(reportEvery, cts.Token)),
};
backgroundTasks = new[]
{
Task.Run(() => Record(recordEvery, cts.Token)),
Task.Run(() => Report(reportEvery, cts.Token)),
};

PrintHelp(recordEvery, reportEvery);
PrintHelp(recordEvery, reportEvery);

ConsoleKey consoleKey;
do
{
consoleKey = Console.ReadKey().Key;
switch (consoleKey)
ConsoleKey consoleKey;
do
{
case ConsoleKey.P:
PrintMetricsToConsole();
break;
consoleKey = Console.ReadKey().Key;
switch (consoleKey)
{
case ConsoleKey.P:
PrintMetricsToConsole();
break;

case ConsoleKey.R:
ReportOnDemand();
break;
case ConsoleKey.R:
ReportOnDemand();
break;
}
}
}
while (consoleKey != ConsoleKey.Escape);
while (consoleKey != ConsoleKey.Escape);

cts.Cancel();
Task.WaitAll(backgroundTasks, 5000);
cts.Cancel();
Task.WaitAll(backgroundTasks, 5000);
}

Console.WriteLine();
Console.WriteLine($"In {TotalTime.Elapsed} the metrics have been recorded {RecordCount} times and TelemetryClient flushed {FlushCount} times.");
}

private static void TestTc()
{
var rnd = new Random();
var tc = new TelemetryClient(new TelemetryConfiguration("84c12b81-6b0e-4581-b44d-11070f1e8490"));
for (int i = 0; i < 300; i++)
{
tc.GetMetric("tc_counter_one").TrackValue(1);
tc.GetMetric("tc_timer_one").TrackValue(rnd.Next(0, 201));
tc.GetMetric("tc_counter_two", "apples").TrackValue(rnd.Next(1, 4));
tc.GetMetric("tc_counter_two", "pears").TrackValue(1);
Console.WriteLine(i);
Thread.Sleep(2000);
}

tc.Flush();
}

private static void Init()
{
var cfg = new ConfigurationBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,27 @@ namespace App.Metrics.Reporting.ApplicationInsights
using System.Threading;
using System.Threading.Tasks;

public sealed class ApplicationInsightsReporter : IReportMetrics
public sealed class ApplicationInsightsReporter : IReportMetrics, IDisposable
{
private const string UnitKey = "unit";
private static readonly ILog Logger = LogProvider.For<ApplicationInsightsReporter>();

/// <summary>
/// Suprisingly <see cref="TelemetryConfiguration"/> implements <see cref="IDisposable"/> unlike <see cref="TelemetryClient"/>.
/// https://github.com/Microsoft/ApplicationInsights-dotnet/blob/develop/src/Microsoft.ApplicationInsights/Extensibility/TelemetryConfiguration.cs#L340
/// </summary>
private readonly TelemetryConfiguration clientCfg;
private readonly TelemetryClient client;
private bool disposed;

/// <inheritdoc />
public IFilterMetrics Filter { get; set; }

/// <inheritdoc />
public TimeSpan FlushInterval { get; set; }

/// <inheritdoc />
public IMetricsOutputFormatter Formatter { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="ApplicationInsightsReporter"/> class.
Expand All @@ -36,23 +52,27 @@ public ApplicationInsightsReporter(ApplicationInsightsReporterOptions options)
throw new ArgumentNullException(nameof(options));
}

client = new TelemetryClient(new TelemetryConfiguration(options.InstrumentationKey));
clientCfg = new TelemetryConfiguration(options.InstrumentationKey);
client = new TelemetryClient(clientCfg);
FlushInterval = options.FlushInterval > TimeSpan.Zero
? options.FlushInterval
: AppMetricsConstants.Reporting.DefaultFlushInterval;
Filter = options.Filter;

Logger.Info($"Using Metrics Reporter {this}. FlushInterval: {FlushInterval}");
Logger.Info($"Using metrics reporter {nameof(ApplicationInsightsReporter)}. FlushInterval: {FlushInterval}");
}

/// <inheritdoc />
public IFilterMetrics Filter { get; set; }
public void Dispose()
{
if (disposed)
{
return;
}

/// <inheritdoc />
public TimeSpan FlushInterval { get; set; }
clientCfg.Dispose();

/// <inheritdoc />
public IMetricsOutputFormatter Formatter { get; set; }
disposed = true;
}

/// <inheritdoc />
public Task<bool> FlushAsync(MetricsDataValueSource metricsData, CancellationToken cancellationToken = default)
Expand Down

0 comments on commit b305d5f

Please sign in to comment.