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

[Logs-branch] Add LoggerProvider spec changes #3707

Merged
merged 29 commits into from
Sep 30, 2022

Conversation

CodeBlanch
Copy link
Member

@CodeBlanch CodeBlanch commented Sep 29, 2022

Fixes #3637

Description

This PR brings the changes reverted on #3702 with additions from #3677 to the main-logs branch. Since it is now a dedicated branch things have been made public as if the spec was stable. Making things public is helpful to see the true shape and vet the usage 🔍

Changes

Public API Changes

OpenTelemetry.Api

namespace OpenTelemetry.Logs
{
+   public class LoggerProvider : BaseProvider
+   {
+      protected LoggerProvider() {}
+      public Logger GetLogger() {}
+      public Logger GetLogger(string? name) {}
+      public Logger GetLogger(InstrumentationScope instrumentationScope) {}
+      public virtual Logger GetLogger(LoggerOptions options) {}
+   }

+   public abstract class Logger
+   {
+      protected Logger(LoggerOptions options) {}
+      public abstract void EmitEvent(string name, in LogRecordData data, in LogRecordAttributeList attributes = default) {}
+      public abstract void EmitLog(in LogRecordData data, in LogRecordAttributeList attributes = default) {}
+   }

+   public struct LogRecordData
+   {
+      public LogRecordData() {}
+      public LogRecordData(Activity? activity) {}
+      public DateTime Timestamp { get; set; }
+      public ActivityTraceId TraceId { get; set; }
+      public ActivitySpanId SpanId { get; set; }
+      public ActivityTraceFlags TraceFlags { get; set; }
+      public LogRecordSeverity? Severity { get; set; }
+      public string? Body { get; set; } = null;
+   }

+   public enum LogRecordSeverity
+   {
+      Trace,
+      Debug,
+      Information,
+      Warning,
+      Error,
+      Fatal,
+   }

+   public struct LogRecordAttributeList : IReadOnlyList<KeyValuePair<string, object?>>
+   {
+      public int Count { get; }
+      public KeyValuePair<string, object?> this[int index] { get; set; }
+      [EditorBrowsable(EditorBrowsableState.Never)] public object? this[string key] { set; }
+      public void Add(string key, object? value) {}
+      public void Add(KeyValuePair<string, object?> attribute) {}
+      public void RecordException(Exception exception) {}
+      public Enumerator GetEnumerator() {}
+      IEnumerator<KeyValuePair<string, object?>> IEnumerable<KeyValuePair<string, object?>>.GetEnumerator() {}
+      IEnumerator IEnumerable.GetEnumerator() {}

+      public struct Enumerator : IEnumerator<KeyValuePair<string, object?>>, IEnumerator
+      {
+         public KeyValuePair<string, object?> Current { get; }
+         object IEnumerator.Current { get; }
+         public bool MoveNext() {}
+         public void Dispose() {}
+         void IEnumerator.Reset() {}
+      }
+   }

+   public sealed class LoggerOptions
+   {
+      public LoggerOptions() {}
+      public LoggerOptions(string? name) {}
+      public LoggerOptions(InstrumentationScope instrumentationScope) {}
+      public InstrumentationScope InstrumentationScope { get; }
+      public string? EventDomain { get; init; }
+      public bool IncludeTraceContext { get; init; } = true;
+   }

+   public abstract class LoggerProviderBuilder
+   {
+      protected LoggerProviderBuilder() {}
+   }
}

namespace OpenTelemetry
{
+   public sealed class InstrumentationScope
+   {
+      public InstrumentationScope() {}
+      public InstrumentationScope(string? name) {}
+      public string Name { get; }
+      public string? Version { get; init; }
+      public string? SchemaUrl { get; init; }
+      public IReadOnlyDictionary<string, object>? Attributes { get; init; }
+   }
}

OpenTelemetry

namespace OpenTelemetry.Logs
{
   public sealed class LogRecord
   {
+    public string? Body { get; set; }
+    public IReadOnlyList<KeyValuePair<string, object?>>? Attributes { get; set; }
+    public LogRecordSeverity? Severity { get; set; }
+    public InstrumentationScope? InstrumentationScope { get; }

+    [Obsolete("Use Severity instead LogLevel will be removed in a future version.")]
     public LogLevel LogLevel { get; set; }
+    [Obsolete("State cannot be accessed safely outside of an ILogger.Log call stack. It will be removed in a future version.")]
     public object? State { get; set; }
+    [Obsolete("Use Attributes instead StateValues will be removed in a future version.")]
     public IReadOnlyList<KeyValuePair<string, object?>>? StateValues { get; set; }
   }

   public class OpenTelemetryLoggerOptions
   {
+      public bool IncludeState { get; set; } = true;
+      public bool IncludeTraceState { get; set; }

+      [Obsolete("Use LoggerProviderBuilder instead of OpenTelemetryLoggerOptions to configure a LoggerProvider this method will be removed in a future version.")]
       public OpenTelemetryLoggerOptions AddProcessor(BaseProcessor<LogRecord> processor) {}

+      [Obsolete("Use LoggerProviderBuilder instead of OpenTelemetryLoggerOptions to configure a LoggerProvider this method will be removed in a future version.")]
       public OpenTelemetryLoggerOptions SetResourceBuilder(ResourceBuilder resourceBuilder) {}

+      [Obsolete("Use LoggerProviderBuilder instead of OpenTelemetryLoggerOptions to configure a LoggerProvider this method will be removed in a future version.")]
       public OpenTelemetryLoggerOptions ConfigureResource(Action<ResourceBuilder> configure) {}
   }

+   public static class LoggerProviderBuilderExtensions
+   {
+      public static LoggerProviderBuilder SetResourceBuilder(this LoggerProviderBuilder loggerProviderBuilder, ResourceBuilder resourceBuilder) {}
+      public static LoggerProviderBuilder ConfigureResource(this LoggerProviderBuilder loggerProviderBuilder, Action<ResourceBuilder> configure) {}
+      public static LoggerProviderBuilder AddProcessor(this LoggerProviderBuilder loggerProviderBuilder, BaseProcessor<LogRecord> processor) {}
+      public static LoggerProviderBuilder AddProcessor<T>(this LoggerProviderBuilder loggerProviderBuilder) where T : BaseProcessor<LogRecord> {}
+      public static LoggerProviderBuilder AddExporter(this LoggerProviderBuilder loggerProviderBuilder, ExportProcessorType exportProcessorType, BaseExporter<LogRecord> exporter) {}
+      public static LoggerProviderBuilder AddExporter(this LoggerProviderBuilder loggerProviderBuilder, ExportProcessorType exportProcessorType, BaseExporter<LogRecord> exporter, Action<ExportLogRecordProcessorOptions> configure) {}
+      public static LoggerProviderBuilder AddExporter(this LoggerProviderBuilder loggerProviderBuilder, ExportProcessorType exportProcessorType, BaseExporter<LogRecord> exporter, string? name, Action<ExportLogRecordProcessorOptions>? configure) {}
+      public static LoggerProviderBuilder AddExporter<T>(this LoggerProviderBuilder loggerProviderBuilder, ExportProcessorType exportProcessorType) where T : BaseExporter<LogRecord> {}
+      public static LoggerProviderBuilder AddExporter<T>(this LoggerProviderBuilder loggerProviderBuilder, ExportProcessorType exportProcessorType, Action<ExportLogRecordProcessorOptions> configure) where T : BaseExporter<LogRecord> {}
+      public static LoggerProviderBuilder AddExporter<T>(this LoggerProviderBuilder loggerProviderBuilder, ExportProcessorType exportProcessorType, string? name, Action<ExportLogRecordProcessorOptions>? configure) where T : BaseExporter<LogRecord> {}
+      public static LoggerProviderBuilder ConfigureServices(this LoggerProviderBuilder loggerProviderBuilder, Action<IServiceCollection> configure) {}
+      public static LoggerProviderBuilder ConfigureBuilder(this LoggerProviderBuilder loggerProviderBuilder, Action<IServiceProvider, LoggerProviderBuilder> configure) {}
+      public static LoggerProvider Build(this LoggerProviderBuilder loggerProviderBuilder) {}
+      public static LoggerProviderBuilder AddInstrumentation<T>(this LoggerProviderBuilder loggerProviderBuilder) where T : class {}
+      public static LoggerProviderBuilder AddInstrumentation<T>(this LoggerProviderBuilder loggerProviderBuilder, Func<IServiceProvider, LoggerProvider, T> instrumentationFactory) where T : class {}
+   }

+   public static class LoggerProviderExtensions
+   {
+      public static LoggerProvider AddProcessor(this LoggerProvider provider, BaseProcessor<LogRecord> processor) {}
+      public static bool ForceFlush(this LoggerProvider provider, int timeoutMilliseconds = Timeout.Infinite) {}
+      public static bool Shutdown(this LoggerProvider provider, int timeoutMilliseconds = Timeout.Infinite) {}
+   }

+   public static class LoggerProviderBuilderServiceCollectionExtensions
+   {
+      public static IServiceCollection ConfigureOpenTelemetryLogging(this IServiceCollection services) {}
+      public static IServiceCollection ConfigureOpenTelemetryLogging(this IServiceCollection services, Action<LoggerProviderBuilder>? configure) {}
+   }

   public static class OpenTelemetryLoggingExtensions
   {
        // The return type has change on these. See the section on backwards compatibility
-       public static ILoggingBuilder AddOpenTelemetry(this ILoggingBuilder builder) {}
-       public static ILoggingBuilder AddOpenTelemetry(this ILoggingBuilder builder, Action<OpenTelemetryLoggerOptions> configureOptions) {}
+       public static OpenTelemetryLoggingBuilder AddOpenTelemetry(this ILoggingBuilder builder) {}
+       public static OpenTelemetryLoggingBuilder AddOpenTelemetry(this ILoggingBuilder builder, Action<OpenTelemetryLoggerOptions> configureOptions) {}

+      public static ILoggingBuilder AddOpenTelemetry(this ILoggingBuilder builder, LoggerProvider loggerProvider) {}
+      public static ILoggingBuilder AddOpenTelemetry(this ILoggingBuilder builder, LoggerProvider loggerProvider, Action<OpenTelemetryLoggerOptions> configureOptions) {}
+      public static ILoggingBuilder AddOpenTelemetry(this ILoggingBuilder builder, LoggerProvider loggerProvider, Action<OpenTelemetryLoggerOptions>? configureOptions, bool disposeProvider) {}
   }

    // See the section on backwards compatibility
+   public sealed class OpenTelemetryLoggingBuilder : LoggerProviderBuilder, ILoggingBuilder
+   {
+      public IServiceCollection Services { get; } // Needed for ILoggingBuilder
+   }

    // This class is similar to MetricReaderOptions and is used by AddExporter extensions
+   public class ExportLogRecordProcessorOptions
+   {
+      public ExportProcessorType ExportProcessorType { get; set; }
+      public BatchExportLogRecordProcessorOptions BatchExportProcessorOptions { get; set; }
+   }

    // This class is similar to BatchExportActivityProcessorOptions and exists so separate environment variables may be used to configure batch log processor settings
+   public class BatchExportLogRecordProcessorOptions : BatchExportProcessorOptions<LogRecord>
+   {
+   }
}

namespace OpenTelemetry
{
   public static class Sdk
   {
+      public static LoggerProviderBuilder CreateLoggerProviderBuilder() {}
   }
}

Also...

Backwards compatibility

The stable API in 1.3.1 uses OpenTelemetryLoggerOptions as the builder for logging. There isn't much to it, only AddProcessor and SetResourceBuilder are available. Extensions are coded to use that API when building up the provider. 1.4 is adding ConfigureResource.

This PR is essentially splitting things up so that LoggingProviderBuilder is responsible for building the spec LoggerProvider and OpenTelemetryLoggerOptions is responsible for configuring the OpenTelemetryLoggerProvider which is the ILogger integration.

This was tricky and I spent a lot of time trying many different things. This is the best I could come up with! Open to suggestions 🤔

  • AddProcessor, SetResourceBuilder, & ConfigureResource still exist on OpenTelemetryLoggerOptions. Code and extensions that rely on them will continue to function. How this works is anything set on the options will be applied to the provider when it is ready.
  • LoggingProviderBuilder has all the great stuff now. This is the API we want to use instead. To encourage migration AddProcessor, SetResourceBuilder, & ConfigureResource are now marked as Obsolete as are the extensions using OpenTelemetryLoggerOptions. New extensions have been added targeting LoggingProviderBuilder instead.

Here is some working 1.3.1/1.4 code:

builder.Logging
    .ClearProviders()
    .AddOpenTelemetry(options =>
    {
        options.ParseStateValues = true;
        options
            .SetResourceBuilder(resourceBuilder)
            .AddConsoleExporter();
    })
    .AddEventLog();

That will continue to work but now generates warnings due to the obsoletions. Here is the new pattern:

builder.Logging
    .ClearProviders()
    .AddEventLog() // Order must be changed
    .AddOpenTelemetry(options => options.ParseStateValues = true)
    .SetResourceBuilder(resourceBuilder)
    .AddConsoleExporter();

AddOpenTelemetry returns OpenTelemetryLoggingBuilder which implements LoggerProviderBuilder to configure the builder.

This pattern works equally well:

// Configure the spec LoggerProvider in the service collection
builder.Services.ConfigureOpenTelemetryLogging(builder =>
   builder
      .SetResourceBuilder(resourceBuilder)
      .AddConsoleExporter());

// Configure the ILogger integration to use the LoggerProvider in the service collection
builder.Logging
    .ClearProviders()
    .AddOpenTelemetry(options => options.ParseStateValues = true);

As does...

// Configure LoggerProvider detached
var provider = Sdk.CreateLoggerProviderBuilder()
   .SetResourceBuilder(resourceBuilder)
   .AddConsoleExporter()
   .Build();

// Configure the ILogger integration to use the detached LoggerProvider
builder.Logging
    .ClearProviders()
    .AddOpenTelemetry(provider, options => options.ParseStateValues = true, disposeProvider: true);

The first pattern configures everything off of ILoggingBuilder in one spot. The second two configure LoggerProvider independently and then tell ILoggerBuilder (ILogger integration) to use it. We don't have to provide a "one-spot" option, but if ILogger integration is our most common scenario it is kind of nice.

TODOs

  • Appropriate CHANGELOG.md updated for non-trivial changes
  • Changes in public API reviewed

@CodeBlanch CodeBlanch requested a review from a team September 29, 2022 18:36
@codecov
Copy link

codecov bot commented Sep 29, 2022

Codecov Report

Merging #3707 (7fea768) into main-logs (7fd3783) will increase coverage by 0.13%.
The diff coverage is 85.02%.

Additional details and impacted files

Impacted file tree graph

@@              Coverage Diff              @@
##           main-logs    #3707      +/-   ##
=============================================
+ Coverage      87.14%   87.27%   +0.13%     
=============================================
  Files            274      296      +22     
  Lines          10008    10590     +582     
=============================================
+ Hits            8721     9242     +521     
- Misses          1287     1348      +61     
Impacted Files Coverage Δ
...Telemetry.Api/Internal/Shims/NullableAttributes.cs 0.00% <ø> (ø)
src/OpenTelemetry.Api/Logs/NoopLogger.cs 0.00% <0.00%> (ø)
src/OpenTelemetry/Logs/LoggerProviderExtensions.cs 0.00% <0.00%> (ø)
...nTelemetry/Internal/OpenTelemetrySdkEventSource.cs 76.51% <33.33%> (-3.33%) ⬇️
...nTelemetry/Logs/ExportLogRecordProcessorOptions.cs 40.00% <40.00%> (ø)
src/OpenTelemetry/Logs/LoggerSdk.cs 40.00% <40.00%> (ø)
src/OpenTelemetry/Logs/LogRecord.cs 61.01% <50.00%> (-3.34%) ⬇️
src/OpenTelemetry/ProviderExtensions.cs 76.92% <50.00%> (-13.99%) ⬇️
...metry/Logs/BatchExportLogRecordProcessorOptions.cs 60.00% <60.00%> (ø)
...lemetry/Logs/ILogger/OpenTelemetryLoggerOptions.cs 80.00% <60.00%> (ø)
... and 40 more

Copy link
Member

@alanwest alanwest left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BLRP! Excuse me...

@CodeBlanch CodeBlanch merged commit f73e453 into open-telemetry:main-logs Sep 30, 2022
@CodeBlanch CodeBlanch deleted the log-api-additions branch September 30, 2022 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants