Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ConfigRefresher for SelfDiagnostics #2262

Merged
merged 20 commits into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

// SelfDiagnosticsEventListener should be moved from SelfDiagnosticsInternals to SelfDiagnostics.
// Pending on https://github.com/microsoft/ApplicationInsights-dotnet/pull/2262
// Here still using this old namespace to show less changes in git diff view.
using Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing.SelfDiagnosticsInternals;

[TestClass]
class SelfDiagnosticsEventListenerTest
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
namespace Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing.SelfDiagnostics
xiang17 marked this conversation as resolved.
Show resolved Hide resolved
{
using System;
using System.Diagnostics;
using System.Diagnostics.Tracing;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

/// <summary>
/// SelfDiagnosticsConfigRefresher class checks a location for a configuration file
/// and open a MemoryMappedFile of a configured size at the configured file path.
/// The class provides a stream object with proper write position if the configuration
/// file is present and valid. Otherwise, the stream object would be unavailable,
/// nothing will be logged to any file.
/// </summary>
internal class SelfDiagnosticsConfigRefresher : IDisposable
{
private const int ConfigurationUpdatePeriodMilliSeconds = 10000;

private readonly CancellationTokenSource cancellationTokenSource;
private readonly Task worker;
private readonly SelfDiagnosticsConfigParser configParser;
private readonly MemoryMappedFileHandler memoryMappedFileHandler;

xiang17 marked this conversation as resolved.
Show resolved Hide resolved
private bool disposedValue;

// Once the configuration file is valid, an eventListener object will be created.
private SelfDiagnosticsEventListener eventListener;

private EventLevel logEventLevel = (EventLevel)(-1);

public SelfDiagnosticsConfigRefresher()
{
this.configParser = new SelfDiagnosticsConfigParser();
this.memoryMappedFileHandler = new MemoryMappedFileHandler();
this.UpdateMemoryMappedFileFromConfiguration();
this.cancellationTokenSource = new CancellationTokenSource();
this.worker = Task.Run(() => this.Worker(this.cancellationTokenSource.Token), this.cancellationTokenSource.Token);
}

/// <inheritdoc/>
public void Dispose()
rajkumar-rangaraj marked this conversation as resolved.
Show resolved Hide resolved
{
this.Dispose(true);
GC.SuppressFinalize(this);
}

private async Task Worker(CancellationToken cancellationToken)
{
await Task.Delay(ConfigurationUpdatePeriodMilliSeconds, cancellationToken).ConfigureAwait(false);
while (!cancellationToken.IsCancellationRequested)
{
this.UpdateMemoryMappedFileFromConfiguration();
await Task.Delay(ConfigurationUpdatePeriodMilliSeconds, cancellationToken).ConfigureAwait(false);
}
}

private void UpdateMemoryMappedFileFromConfiguration()
{
if (this.configParser.TryGetConfiguration(out string newLogDirectory, out int fileSizeInKB, out EventLevel newEventLevel))
{
int newFileSize = fileSizeInKB * 1024;
if (!newLogDirectory.Equals(this.memoryMappedFileHandler.LogDirectory, StringComparison.Ordinal) || this.memoryMappedFileHandler.LogFileSize != newFileSize)
{
this.memoryMappedFileHandler.CloseLogFile();
this.memoryMappedFileHandler.CreateLogFile(newLogDirectory, newFileSize);
}

if (!newEventLevel.Equals(this.logEventLevel))
{
if (this.eventListener != null)
{
this.eventListener.Dispose();
}

this.eventListener = new SelfDiagnosticsEventListener(newEventLevel, this.memoryMappedFileHandler);
this.logEventLevel = newEventLevel;
}
}
else
{
this.memoryMappedFileHandler.CloseLogFile();
}
}

private void Dispose(bool disposing)
{
if (!this.disposedValue)
{
if (disposing)
{
this.cancellationTokenSource.Cancel(false);
try
{
this.worker.Wait();
}
catch (AggregateException)
{
}
finally
{
this.cancellationTokenSource.Dispose();
}

// Ensure worker thread properly finishes.
// Or it might have created another MemoryMappedFile in that thread
// after the Dispose() below is called.
this.memoryMappedFileHandler.Dispose();
}

this.disposedValue = true;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing.SelfDiagnosticsInternals
namespace Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing.SelfDiagnostics
{
using System;
using System.Collections.Generic;
Expand All @@ -8,11 +8,6 @@
using System.Text;
using System.Threading;

// SelfDiagnosticsEventListener should be moved from SelfDiagnosticsInternals to SelfDiagnostics.
// Pending on https://github.com/microsoft/ApplicationInsights-dotnet/pull/2262
// Here still using this old namespace to show less changes in git diff view.
using Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing.SelfDiagnostics;

/// <summary>
/// SelfDiagnosticsEventListener class enables the events from OpenTelemetry event sources
/// and write the events to a local file in a circular way.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing
namespace Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing.SelfDiagnostics
{
using System;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.ApplicationInsights.Extensibility.Implementation.Endpoints;
using Microsoft.ApplicationInsights.Extensibility.Implementation.Sampling;
using Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing;
using Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing.SelfDiagnostics;
using Microsoft.ApplicationInsights.Metrics;
using Microsoft.ApplicationInsights.Metrics.Extensibility;

Expand Down