Skip to content

Commit

Permalink
Merge branch 'main' into tracer-addexporter-namedoptions
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch authored Sep 14, 2022
2 parents 924486d + d99307d commit a949def
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static class InMemoryExporterHelperExtensions
/// Adds InMemory exporter to the TracerProvider.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> builder to use.</param>
/// <param name="exportedItems">Collection which will be populated with the exported Activity.</param>
/// <param name="exportedItems">Collection which will be populated with the exported <see cref="Activity"/>.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddInMemoryExporter(this TracerProviderBuilder builder, ICollection<Activity> exportedItems)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ namespace OpenTelemetry.Logs
{
public static class InMemoryExporterLoggingExtensions
{
/// <summary>
/// Adds InMemory exporter to the OpenTelemetryLoggerOptions.
/// </summary>
/// <param name="loggerOptions"><see cref="OpenTelemetryLoggerOptions"/> options to use.</param>
/// <param name="exportedItems">Collection which will be populated with the exported <see cref="LogRecord"/>.</param>
/// <returns>The instance of <see cref="OpenTelemetryLoggerOptions"/> to chain the calls.</returns>
public static OpenTelemetryLoggerOptions AddInMemoryExporter(this OpenTelemetryLoggerOptions loggerOptions, ICollection<LogRecord> exportedItems)
{
Guard.ThrowIfNull(loggerOptions);
Expand Down
12 changes: 12 additions & 0 deletions src/OpenTelemetry/Internal/OpenTelemetrySdkEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,18 @@ public void ProcessorForceFlushInvoked(string processorType, bool result)
this.WriteEvent(43, processorType, result);
}

[Event(44, Message = "OpenTelemetryLoggerProvider event: '{0}'", Level = EventLevel.Verbose)]
public void OpenTelemetryLoggerProviderEvent(string message)
{
this.WriteEvent(44, message);
}

[Event(45, Message = "ForceFlush invoked for OpenTelemetryLoggerProvider with timeoutMilliseconds = '{0}'.", Level = EventLevel.Verbose)]
public void OpenTelemetryLoggerProviderForceFlushInvoked(int timeoutMilliseconds)
{
this.WriteEvent(45, timeoutMilliseconds);
}

#if DEBUG
public class OpenTelemetryEventListener : EventListener
{
Expand Down
27 changes: 27 additions & 0 deletions src/OpenTelemetry/Logs/OpenTelemetryLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System;
using System.Collections;
using System.Diagnostics;
using System.Text;
using System.Threading;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -72,6 +73,8 @@ public OpenTelemetryLoggerProvider()

internal OpenTelemetryLoggerProvider(OpenTelemetryLoggerOptions options, IServiceProvider? serviceProvider, bool ownsServiceProvider)
{
OpenTelemetrySdkEventSource.Log.OpenTelemetryLoggerProviderEvent("Building OpenTelemetryLoggerProvider.");

Guard.ThrowIfNull(options);

this.IncludeScopes = options.IncludeScopes;
Expand Down Expand Up @@ -102,11 +105,14 @@ internal OpenTelemetryLoggerProvider(OpenTelemetryLoggerOptions options, IServic
configurationActions[i](serviceProvider, this);
}

OpenTelemetrySdkEventSource.Log.OpenTelemetryLoggerProviderEvent($"Number of actions configured = {configurationActions.Count}.");
options.ConfigurationActions = null;
}

this.Resource = this.ResourceBuilder.Build();
this.ResourceBuilder = null;

OpenTelemetrySdkEventSource.Log.OpenTelemetryLoggerProviderEvent("OpenTelemetryLoggerProvider built successfully.");
}

internal IExternalScopeProvider? ScopeProvider { get; private set; }
Expand Down Expand Up @@ -173,6 +179,7 @@ public ILogger CreateLogger(string categoryName)
/// </remarks>
public bool ForceFlush(int timeoutMilliseconds = Timeout.Infinite)
{
OpenTelemetrySdkEventSource.Log.OpenTelemetryLoggerProviderForceFlushInvoked(timeoutMilliseconds);
return this.Processor?.ForceFlush(timeoutMilliseconds) ?? true;
}

Expand All @@ -188,25 +195,43 @@ public bool ForceFlush(int timeoutMilliseconds = Timeout.Infinite)
/// <returns>The supplied <see cref="OpenTelemetryLoggerOptions"/> for chaining.</returns>
public OpenTelemetryLoggerProvider AddProcessor(BaseProcessor<LogRecord> processor)
{
OpenTelemetrySdkEventSource.Log.OpenTelemetryLoggerProviderEvent("Started adding processor.");

Guard.ThrowIfNull(processor);

processor.SetParentProvider(this);

StringBuilder processorAdded = new StringBuilder();

if (this.threadStaticPool != null && this.ContainsBatchProcessor(processor))
{
OpenTelemetrySdkEventSource.Log.OpenTelemetryLoggerProviderEvent("Using shared thread pool.");

this.threadStaticPool = null;
}

if (this.Processor == null)
{
processorAdded.Append("Setting processor to ");
processorAdded.Append(processor);

this.Processor = processor;
}
else if (this.Processor is CompositeProcessor<LogRecord> compositeProcessor)
{
processorAdded.Append("Adding processor ");
processorAdded.Append(processor);
processorAdded.Append(" to composite processor");

compositeProcessor.AddProcessor(processor);
}
else
{
processorAdded.Append("Creating new composite processor with processor ");
processorAdded.Append(this.Processor);
processorAdded.Append(" and adding new processor ");
processorAdded.Append(processor);

var newCompositeProcessor = new CompositeProcessor<LogRecord>(new[]
{
this.Processor,
Expand All @@ -216,6 +241,8 @@ public OpenTelemetryLoggerProvider AddProcessor(BaseProcessor<LogRecord> process
this.Processor = newCompositeProcessor;
}

OpenTelemetrySdkEventSource.Log.OpenTelemetryLoggerProviderEvent($"Completed adding processor = \"{processorAdded}\".");

return this;
}

Expand Down

0 comments on commit a949def

Please sign in to comment.