Skip to content

Commit

Permalink
Refactor diagnostics for simplicity
Browse files Browse the repository at this point in the history
  • Loading branch information
stevejgordon committed Feb 13, 2024
1 parent 592036b commit d3c675c
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 59 deletions.
2 changes: 1 addition & 1 deletion examples/Example.Elastic.OpenTelemetry.Worker/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

var builder = Host.CreateApplicationBuilder(args);

builder.EnableElasticOpenTelemetry("CustomActivitySource");
builder.AddElasticOpenTelemetry("CustomActivitySource");

builder.Services.AddHostedService<Worker>();

Expand Down
2 changes: 1 addition & 1 deletion src/Elastic.OpenTelemetry/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Reflection;
using Microsoft.Extensions.Logging;

using static Elastic.OpenTelemetry.Diagnostics.ElasticOpenTelemetryDiagnosticSource;
using static Elastic.OpenTelemetry.Diagnostics.ElasticOpenTelemetryDiagnostics;

namespace Elastic.OpenTelemetry;

Expand Down
29 changes: 15 additions & 14 deletions src/Elastic.OpenTelemetry/AgentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

using static Elastic.OpenTelemetry.Diagnostics.ElasticOpenTelemetryDiagnosticSource;
using static Elastic.OpenTelemetry.Diagnostics.ElasticOpenTelemetryDiagnostics;

namespace Elastic.OpenTelemetry;

Expand Down Expand Up @@ -43,6 +43,7 @@ public class AgentBuilder
private Action<ResourceBuilder>? _resourceBuilderAction;
private Action<OtlpExporterOptions>? _otlpExporterConfiguration;
private string? _otlpExporterName;
private readonly IDisposable? _diagnosticSourceSubscription;

/// <summary>
/// TODO
Expand All @@ -55,8 +56,7 @@ public AgentBuilder()
_ = new LoggingEventListener(LogFileWriter.Instance);

// Enables logging of Elastic OpenTelemetry diagnostic source events
var listener = new LoggingDiagnosticSourceListener(LogFileWriter.Instance);
DiagnosticListener.AllListeners.Subscribe(listener);
_diagnosticSourceSubscription = EnableFileLogging();
}

Log(AgentBuilderInitializedEvent, () => new DiagnosticEvent<StackTrace?>(new StackTrace(true)));
Expand Down Expand Up @@ -193,7 +193,7 @@ public IAgent Build()

Log(AgentBuilderBuiltTracerProviderEvent);

var agent = tracerProvider is not null ? new Agent(tracerProvider) : new Agent();
var agent = tracerProvider is not null ? new Agent(_diagnosticSourceSubscription, tracerProvider) : new Agent(_diagnosticSourceSubscription);

Log(AgentBuilderBuiltAgentEvent);

Expand All @@ -212,7 +212,9 @@ public IServiceCollection Register(IServiceCollection serviceCollection)

_ = serviceCollection
.AddHostedService<ElasticOtelDistroService>()
.AddSingleton<IAgent, Agent>()
// This is purely to register an instance of the agent such that should the service provider be disposed, the agent
// will also be disposed which in turn avoids further diagnostics subscriptions and file logging.
.AddSingleton<IAgent>(new Agent(_diagnosticSourceSubscription))
.AddSingleton<LoggerResolver>()
.AddOpenTelemetry()
.WithTracing(TracerProviderBuilderAction);
Expand All @@ -233,11 +235,6 @@ public IServiceCollection Register(IServiceCollection serviceCollection)
.AddGrpcClientInstrumentation()
.AddEntityFrameworkCoreInstrumentation(); // TODO - Should we add this by default?
// TODO - Update these to capture the builder type also
//Log.AddedInstrumentation("HttpClient");
//Log.AddedInstrumentation("GrpcClient");
//Log.AddedInstrumentation("EntityFrameworkCore");
tracerProviderBuilder.AddElasticProcessors();
if (_resourceBuilderAction is not null)
Expand All @@ -251,7 +248,6 @@ public IServiceCollection Register(IServiceCollection serviceCollection)
tracerProviderBuilder.ConfigureResource(DefaultResourceBuilderConfiguration);
}
// TODO - Can/should we use reflection to determine and log what is configured by the user action?
_tracerProviderBuilderAction?.Invoke(tracerProviderBuilder);
tracerProviderBuilder.AddElasticOtlpExporter(_otlpExporterConfiguration, _otlpExporterName);
Expand All @@ -267,30 +263,35 @@ public void ConfigureOtlpExporter(Action<OtlpExporterOptions> configure, string?
_otlpExporterName = name;
}

private class Agent(TracerProvider? tracerProvider, MeterProvider? meterProvider) : IAgent
private class Agent(IDisposable? diagnosticSubscription, TracerProvider? tracerProvider, MeterProvider? meterProvider) : IAgent
{
private readonly IDisposable? _diagnosticSubscription = diagnosticSubscription;
private readonly TracerProvider? _tracerProvider = tracerProvider;
private readonly MeterProvider? _meterProvider = meterProvider;

public Agent() : this(null, null)
public Agent(IDisposable? diagnosticSubscription)
: this(diagnosticSubscription,null, null)
{
}

internal Agent(TracerProvider tracerProvider) : this(tracerProvider, null)
internal Agent(IDisposable? diagnosticSubscription, TracerProvider tracerProvider)
: this(diagnosticSubscription, tracerProvider, null)
{
}

public void Dispose()
{
_tracerProvider?.Dispose();
_meterProvider?.Dispose();
_diagnosticSubscription?.Dispose();
LogFileWriter.Instance.Dispose();
}

public async ValueTask DisposeAsync()
{
_tracerProvider?.Dispose();
_meterProvider?.Dispose();
_diagnosticSubscription?.Dispose();
await LogFileWriter.Instance.DisposeAsync().ConfigureAwait(false);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ public static class ServiceCollectionExtensions
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
public static IHostApplicationBuilder EnableElasticOpenTelemetry(this IHostApplicationBuilder builder) =>
EnableElasticOpenTelemetry(builder, []);
public static IHostApplicationBuilder AddElasticOpenTelemetry(this IHostApplicationBuilder builder) =>
AddElasticOpenTelemetry(builder, []);

/// <summary>
/// TODO
/// </summary>
/// <param name="builder"></param>
/// <param name="activitySourceNames"></param>
/// <returns></returns>
public static IHostApplicationBuilder EnableElasticOpenTelemetry(this IHostApplicationBuilder builder, params string[] activitySourceNames)
public static IHostApplicationBuilder AddElasticOpenTelemetry(this IHostApplicationBuilder builder, params string[] activitySourceNames)
{
builder.Services.AddElasticOpenTelemetry(activitySourceNames);
return builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,39 @@ public void OnNext(KeyValuePair<string, object?> data)

switch (data.Key)
{
case ElasticOpenTelemetryDiagnosticSource.AgentBuilderInitializedEvent:
case ElasticOpenTelemetryDiagnostics.AgentBuilderInitializedEvent:
AgentBuilderInitialized(data);
break;

case ElasticOpenTelemetryDiagnosticSource.AgentBuilderBuiltTracerProviderEvent:
case ElasticOpenTelemetryDiagnostics.AgentBuilderBuiltTracerProviderEvent:
AgentBuilderBuiltTracerProvider(data);
break;

case ElasticOpenTelemetryDiagnosticSource.AgentBuilderBuiltAgentEvent:
case ElasticOpenTelemetryDiagnostics.AgentBuilderBuiltAgentEvent:
AgentBuilderBuiltAgent(data);
break;

case ElasticOpenTelemetryDiagnosticSource.AgentBuildCalledMultipleTimesEvent:
case ElasticOpenTelemetryDiagnostics.AgentBuildCalledMultipleTimesEvent:
AgentBuilderBuildCalledMultipleTimes(data);
break;

case ElasticOpenTelemetryDiagnosticSource.AgentSetAgentCalledMultipleTimesEvent:
case ElasticOpenTelemetryDiagnostics.AgentSetAgentCalledMultipleTimesEvent:
AgentBuilderSetAgentCalledMultipleTimes(data);
break;

case ElasticOpenTelemetryDiagnosticSource.AgentBuilderRegisteredDistroServicesEvent:
case ElasticOpenTelemetryDiagnostics.AgentBuilderRegisteredDistroServicesEvent:
AgentBuilderRegisteredDistroServices(data);
break;

case ElasticOpenTelemetryDiagnosticSource.TransactionIdAddedEvent:
case ElasticOpenTelemetryDiagnostics.TransactionIdAddedEvent:
TransactionIdAdded(data);
break;

case ElasticOpenTelemetryDiagnosticSource.ProcessorAddedEvent:
case ElasticOpenTelemetryDiagnostics.ProcessorAddedEvent:
ProcessorAdded(data);
break;

case ElasticOpenTelemetryDiagnosticSource.SourceAddedEvent:
case ElasticOpenTelemetryDiagnostics.SourceAddedEvent:
SourceAdded(data);
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@

namespace Elastic.OpenTelemetry.Diagnostics;

internal static class ElasticOpenTelemetryDiagnosticSource
internal static class ElasticOpenTelemetryDiagnostics
{
private static readonly DiagnosticListener Listener = new(DiagnosticSourceName);

public const string DiagnosticSourceName = "Elastic.OpenTelemetry";

internal static readonly DiagnosticSource DiagnosticSource = new DiagnosticListener(DiagnosticSourceName);
internal static readonly DiagnosticSource DiagnosticSource = Listener;

public static IDisposable EnableFileLogging() =>
Listener.Subscribe(new ElasticDiagnosticLoggingObserver(LogFileWriter.Instance));

public static void Log(string name)
{
Expand Down
1 change: 0 additions & 1 deletion src/Elastic.OpenTelemetry/Diagnostics/LogFileWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Channels;
using System.Xml.Serialization;

namespace Elastic.OpenTelemetry.Diagnostics;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using System.Diagnostics;
using Elastic.OpenTelemetry.Diagnostics;

using static Elastic.OpenTelemetry.Diagnostics.ElasticOpenTelemetryDiagnosticSource;
using static Elastic.OpenTelemetry.Diagnostics.ElasticOpenTelemetryDiagnostics;

namespace Elastic.OpenTelemetry.Extensions;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Microsoft.Extensions.Logging;
using OpenTelemetry;

using static Elastic.OpenTelemetry.Diagnostics.ElasticOpenTelemetryDiagnosticSource;
using static Elastic.OpenTelemetry.Diagnostics.ElasticOpenTelemetryDiagnostics;

namespace Elastic.OpenTelemetry.Processors;

Expand Down

0 comments on commit d3c675c

Please sign in to comment.