Skip to content

Commit

Permalink
[Exporter.Console, Exporter.InMemory] Nullable (#5765)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kielek authored Jul 26, 2024
1 parent 05bb05a commit 22a58aa
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#nullable enable
OpenTelemetry.Exporter.ConsoleActivityExporter
OpenTelemetry.Exporter.ConsoleActivityExporter.ConsoleActivityExporter(OpenTelemetry.Exporter.ConsoleExporterOptions options) -> void
OpenTelemetry.Exporter.ConsoleActivityExporter.ConsoleActivityExporter(OpenTelemetry.Exporter.ConsoleExporterOptions! options) -> void
OpenTelemetry.Exporter.ConsoleExporter<T>
OpenTelemetry.Exporter.ConsoleExporter<T>.ConsoleExporter(OpenTelemetry.Exporter.ConsoleExporterOptions options) -> void
OpenTelemetry.Exporter.ConsoleExporter<T>.WriteLine(string message) -> void
OpenTelemetry.Exporter.ConsoleExporter<T>.ConsoleExporter(OpenTelemetry.Exporter.ConsoleExporterOptions! options) -> void
OpenTelemetry.Exporter.ConsoleExporter<T>.WriteLine(string! message) -> void
OpenTelemetry.Exporter.ConsoleExporterOptions
OpenTelemetry.Exporter.ConsoleExporterOptions.ConsoleExporterOptions() -> void
OpenTelemetry.Exporter.ConsoleExporterOptions.Targets.get -> OpenTelemetry.Exporter.ConsoleExporterOutputTargets
Expand All @@ -11,26 +12,26 @@ OpenTelemetry.Exporter.ConsoleExporterOutputTargets
OpenTelemetry.Exporter.ConsoleExporterOutputTargets.Console = 1 -> OpenTelemetry.Exporter.ConsoleExporterOutputTargets
OpenTelemetry.Exporter.ConsoleExporterOutputTargets.Debug = 2 -> OpenTelemetry.Exporter.ConsoleExporterOutputTargets
OpenTelemetry.Exporter.ConsoleLogRecordExporter
OpenTelemetry.Exporter.ConsoleLogRecordExporter.ConsoleLogRecordExporter(OpenTelemetry.Exporter.ConsoleExporterOptions options) -> void
OpenTelemetry.Exporter.ConsoleLogRecordExporter.ConsoleLogRecordExporter(OpenTelemetry.Exporter.ConsoleExporterOptions! options) -> void
OpenTelemetry.Exporter.ConsoleMetricExporter
OpenTelemetry.Exporter.ConsoleMetricExporter.ConsoleMetricExporter(OpenTelemetry.Exporter.ConsoleExporterOptions options) -> void
OpenTelemetry.Exporter.ConsoleMetricExporter.ConsoleMetricExporter(OpenTelemetry.Exporter.ConsoleExporterOptions! options) -> void
OpenTelemetry.Logs.ConsoleExporterLoggingExtensions
OpenTelemetry.Metrics.ConsoleExporterMetricsExtensions
OpenTelemetry.Trace.ConsoleExporterHelperExtensions
override OpenTelemetry.Exporter.ConsoleActivityExporter.Export(in OpenTelemetry.Batch<System.Diagnostics.Activity> batch) -> OpenTelemetry.ExportResult
override OpenTelemetry.Exporter.ConsoleActivityExporter.Export(in OpenTelemetry.Batch<System.Diagnostics.Activity!> batch) -> OpenTelemetry.ExportResult
override OpenTelemetry.Exporter.ConsoleLogRecordExporter.Dispose(bool disposing) -> void
override OpenTelemetry.Exporter.ConsoleLogRecordExporter.Export(in OpenTelemetry.Batch<OpenTelemetry.Logs.LogRecord> batch) -> OpenTelemetry.ExportResult
override OpenTelemetry.Exporter.ConsoleMetricExporter.Export(in OpenTelemetry.Batch<OpenTelemetry.Metrics.Metric> batch) -> OpenTelemetry.ExportResult
static OpenTelemetry.Logs.ConsoleExporterLoggingExtensions.AddConsoleExporter(this OpenTelemetry.Logs.LoggerProviderBuilder loggerProviderBuilder, string name, System.Action<OpenTelemetry.Exporter.ConsoleExporterOptions> configure) -> OpenTelemetry.Logs.LoggerProviderBuilder
static OpenTelemetry.Logs.ConsoleExporterLoggingExtensions.AddConsoleExporter(this OpenTelemetry.Logs.LoggerProviderBuilder loggerProviderBuilder, System.Action<OpenTelemetry.Exporter.ConsoleExporterOptions> configure) -> OpenTelemetry.Logs.LoggerProviderBuilder
static OpenTelemetry.Logs.ConsoleExporterLoggingExtensions.AddConsoleExporter(this OpenTelemetry.Logs.LoggerProviderBuilder loggerProviderBuilder) -> OpenTelemetry.Logs.LoggerProviderBuilder
static OpenTelemetry.Logs.ConsoleExporterLoggingExtensions.AddConsoleExporter(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions loggerOptions, System.Action<OpenTelemetry.Exporter.ConsoleExporterOptions> configure) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions
static OpenTelemetry.Logs.ConsoleExporterLoggingExtensions.AddConsoleExporter(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions loggerOptions) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions
static OpenTelemetry.Metrics.ConsoleExporterMetricsExtensions.AddConsoleExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Action<OpenTelemetry.Exporter.ConsoleExporterOptions, OpenTelemetry.Metrics.MetricReaderOptions> configureExporterAndMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Metrics.ConsoleExporterMetricsExtensions.AddConsoleExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Action<OpenTelemetry.Exporter.ConsoleExporterOptions> configureExporter) -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Metrics.ConsoleExporterMetricsExtensions.AddConsoleExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action<OpenTelemetry.Exporter.ConsoleExporterOptions, OpenTelemetry.Metrics.MetricReaderOptions> configureExporterAndMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Metrics.ConsoleExporterMetricsExtensions.AddConsoleExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action<OpenTelemetry.Exporter.ConsoleExporterOptions> configureExporter) -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Metrics.ConsoleExporterMetricsExtensions.AddConsoleExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder) -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Trace.ConsoleExporterHelperExtensions.AddConsoleExporter(this OpenTelemetry.Trace.TracerProviderBuilder builder, string name, System.Action<OpenTelemetry.Exporter.ConsoleExporterOptions> configure) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.ConsoleExporterHelperExtensions.AddConsoleExporter(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<OpenTelemetry.Exporter.ConsoleExporterOptions> configure) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.ConsoleExporterHelperExtensions.AddConsoleExporter(this OpenTelemetry.Trace.TracerProviderBuilder builder) -> OpenTelemetry.Trace.TracerProviderBuilder
override OpenTelemetry.Exporter.ConsoleLogRecordExporter.Export(in OpenTelemetry.Batch<OpenTelemetry.Logs.LogRecord!> batch) -> OpenTelemetry.ExportResult
override OpenTelemetry.Exporter.ConsoleMetricExporter.Export(in OpenTelemetry.Batch<OpenTelemetry.Metrics.Metric!> batch) -> OpenTelemetry.ExportResult
static OpenTelemetry.Logs.ConsoleExporterLoggingExtensions.AddConsoleExporter(this OpenTelemetry.Logs.LoggerProviderBuilder! loggerProviderBuilder) -> OpenTelemetry.Logs.LoggerProviderBuilder!
static OpenTelemetry.Logs.ConsoleExporterLoggingExtensions.AddConsoleExporter(this OpenTelemetry.Logs.LoggerProviderBuilder! loggerProviderBuilder, string? name, System.Action<OpenTelemetry.Exporter.ConsoleExporterOptions!>? configure) -> OpenTelemetry.Logs.LoggerProviderBuilder!
static OpenTelemetry.Logs.ConsoleExporterLoggingExtensions.AddConsoleExporter(this OpenTelemetry.Logs.LoggerProviderBuilder! loggerProviderBuilder, System.Action<OpenTelemetry.Exporter.ConsoleExporterOptions!>? configure) -> OpenTelemetry.Logs.LoggerProviderBuilder!
static OpenTelemetry.Logs.ConsoleExporterLoggingExtensions.AddConsoleExporter(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions! loggerOptions) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
static OpenTelemetry.Logs.ConsoleExporterLoggingExtensions.AddConsoleExporter(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions! loggerOptions, System.Action<OpenTelemetry.Exporter.ConsoleExporterOptions!>? configure) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
static OpenTelemetry.Metrics.ConsoleExporterMetricsExtensions.AddConsoleExporter(this OpenTelemetry.Metrics.MeterProviderBuilder! builder) -> OpenTelemetry.Metrics.MeterProviderBuilder!
static OpenTelemetry.Metrics.ConsoleExporterMetricsExtensions.AddConsoleExporter(this OpenTelemetry.Metrics.MeterProviderBuilder! builder, string? name, System.Action<OpenTelemetry.Exporter.ConsoleExporterOptions!, OpenTelemetry.Metrics.MetricReaderOptions!>? configureExporterAndMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder!
static OpenTelemetry.Metrics.ConsoleExporterMetricsExtensions.AddConsoleExporter(this OpenTelemetry.Metrics.MeterProviderBuilder! builder, string? name, System.Action<OpenTelemetry.Exporter.ConsoleExporterOptions!>? configureExporter) -> OpenTelemetry.Metrics.MeterProviderBuilder!
static OpenTelemetry.Metrics.ConsoleExporterMetricsExtensions.AddConsoleExporter(this OpenTelemetry.Metrics.MeterProviderBuilder! builder, System.Action<OpenTelemetry.Exporter.ConsoleExporterOptions!, OpenTelemetry.Metrics.MetricReaderOptions!>? configureExporterAndMetricReader) -> OpenTelemetry.Metrics.MeterProviderBuilder!
static OpenTelemetry.Metrics.ConsoleExporterMetricsExtensions.AddConsoleExporter(this OpenTelemetry.Metrics.MeterProviderBuilder! builder, System.Action<OpenTelemetry.Exporter.ConsoleExporterOptions!>? configureExporter) -> OpenTelemetry.Metrics.MeterProviderBuilder!
static OpenTelemetry.Trace.ConsoleExporterHelperExtensions.AddConsoleExporter(this OpenTelemetry.Trace.TracerProviderBuilder! builder) -> OpenTelemetry.Trace.TracerProviderBuilder!
static OpenTelemetry.Trace.ConsoleExporterHelperExtensions.AddConsoleExporter(this OpenTelemetry.Trace.TracerProviderBuilder! builder, string? name, System.Action<OpenTelemetry.Exporter.ConsoleExporterOptions!>? configure) -> OpenTelemetry.Trace.TracerProviderBuilder!
static OpenTelemetry.Trace.ConsoleExporterHelperExtensions.AddConsoleExporter(this OpenTelemetry.Trace.TracerProviderBuilder! builder, System.Action<OpenTelemetry.Exporter.ConsoleExporterOptions!>? configure) -> OpenTelemetry.Trace.TracerProviderBuilder!
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public override ExportResult Export(in Batch<Activity> batch)
this.WriteLine("Resource associated with Activity:");
foreach (var resourceAttribute in resource.Attributes)
{
if (this.TagWriter.TryTransformTag(resourceAttribute, out var result))
if (this.TagWriter.TryTransformTag(resourceAttribute.Key, resourceAttribute.Value, out var result))
{
this.WriteLine($" {result.Key}: {result.Value}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static TracerProviderBuilder AddConsoleExporter(this TracerProviderBuilde
/// <param name="builder"><see cref="TracerProviderBuilder"/> builder to use.</param>
/// <param name="configure">Callback action for configuring <see cref="ConsoleExporterOptions"/>.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddConsoleExporter(this TracerProviderBuilder builder, Action<ConsoleExporterOptions> configure)
public static TracerProviderBuilder AddConsoleExporter(this TracerProviderBuilder builder, Action<ConsoleExporterOptions>? configure)
=> AddConsoleExporter(builder, name: null, configure);

/// <summary>
Expand All @@ -36,8 +36,8 @@ public static TracerProviderBuilder AddConsoleExporter(this TracerProviderBuilde
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddConsoleExporter(
this TracerProviderBuilder builder,
string name,
Action<ConsoleExporterOptions> configure)
string? name,
Action<ConsoleExporterOptions>? configure)
{
Guard.ThrowIfNull(builder);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static OpenTelemetryLoggerOptions AddConsoleExporter(this OpenTelemetryLo
/// <param name="configure">Callback action for configuring <see cref="ConsoleExporterOptions"/>.</param>
/// <returns>The instance of <see cref="OpenTelemetryLoggerOptions"/> to chain the calls.</returns>
// TODO: [Obsolete("Call LoggerProviderBuilder.AddConsoleExporter instead this method will be removed in a future version.")]
public static OpenTelemetryLoggerOptions AddConsoleExporter(this OpenTelemetryLoggerOptions loggerOptions, Action<ConsoleExporterOptions> configure)
public static OpenTelemetryLoggerOptions AddConsoleExporter(this OpenTelemetryLoggerOptions loggerOptions, Action<ConsoleExporterOptions>? configure)
{
Guard.ThrowIfNull(loggerOptions);

Expand All @@ -52,7 +52,7 @@ public static LoggerProviderBuilder AddConsoleExporter(
/// <returns>The supplied instance of <see cref="LoggerProviderBuilder"/> to chain the calls.</returns>
public static LoggerProviderBuilder AddConsoleExporter(
this LoggerProviderBuilder loggerProviderBuilder,
Action<ConsoleExporterOptions> configure)
Action<ConsoleExporterOptions>? configure)
=> AddConsoleExporter(loggerProviderBuilder, name: null, configure);

/// <summary>
Expand All @@ -64,8 +64,8 @@ public static LoggerProviderBuilder AddConsoleExporter(
/// <returns>The supplied instance of <see cref="LoggerProviderBuilder"/> to chain the calls.</returns>
public static LoggerProviderBuilder AddConsoleExporter(
this LoggerProviderBuilder loggerProviderBuilder,
string name,
Action<ConsoleExporterOptions> configure)
string? name,
Action<ConsoleExporterOptions>? configure)
{
Guard.ThrowIfNull(loggerProviderBuilder);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static MeterProviderBuilder AddConsoleExporter(this MeterProviderBuilder
/// <param name="builder"><see cref="MeterProviderBuilder"/> builder to use.</param>
/// <param name="configureExporter">Callback action for configuring <see cref="ConsoleExporterOptions"/>.</param>
/// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
public static MeterProviderBuilder AddConsoleExporter(this MeterProviderBuilder builder, Action<ConsoleExporterOptions> configureExporter)
public static MeterProviderBuilder AddConsoleExporter(this MeterProviderBuilder builder, Action<ConsoleExporterOptions>? configureExporter)
=> AddConsoleExporter(builder, name: null, configureExporter);

/// <summary>
Expand All @@ -42,8 +42,8 @@ public static MeterProviderBuilder AddConsoleExporter(this MeterProviderBuilder
/// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
public static MeterProviderBuilder AddConsoleExporter(
this MeterProviderBuilder builder,
string name,
Action<ConsoleExporterOptions> configureExporter)
string? name,
Action<ConsoleExporterOptions>? configureExporter)
{
Guard.ThrowIfNull(builder);

Expand Down Expand Up @@ -72,7 +72,7 @@ public static MeterProviderBuilder AddConsoleExporter(
/// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
public static MeterProviderBuilder AddConsoleExporter(
this MeterProviderBuilder builder,
Action<ConsoleExporterOptions, MetricReaderOptions> configureExporterAndMetricReader)
Action<ConsoleExporterOptions, MetricReaderOptions>? configureExporterAndMetricReader)
=> AddConsoleExporter(builder, name: null, configureExporterAndMetricReader);

/// <summary>
Expand All @@ -86,8 +86,8 @@ public static MeterProviderBuilder AddConsoleExporter(
/// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
public static MeterProviderBuilder AddConsoleExporter(
this MeterProviderBuilder builder,
string name,
Action<ConsoleExporterOptions, MetricReaderOptions> configureExporterAndMetricReader)
string? name,
Action<ConsoleExporterOptions, MetricReaderOptions>? configureExporterAndMetricReader)
{
Guard.ThrowIfNull(builder);

Expand Down
10 changes: 5 additions & 5 deletions src/OpenTelemetry.Exporter.Console/ConsoleLogRecordExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ConsoleLogRecordExporter : ConsoleExporter<LogRecord>
private const int RightPaddingLength = 35;
private readonly object syncObject = new();
private bool disposed;
private string disposedStackTrace;
private string? disposedStackTrace;
private bool isDisposeMessageSent;

public ConsoleLogRecordExporter(ConsoleExporterOptions options)
Expand All @@ -39,7 +39,7 @@ public override ExportResult Export(in Batch<LogRecord> batch)
this.WriteLine("The console exporter is still being invoked after it has been disposed. This could be due to the application's incorrect lifecycle management of the LoggerFactory/OpenTelemetry .NET SDK.");
this.WriteLine(Environment.StackTrace);
this.WriteLine(Environment.NewLine + "Dispose was called on the following stack trace:");
this.WriteLine(this.disposedStackTrace);
this.WriteLine(this.disposedStackTrace!);
}

return ExportResult.Failure;
Expand Down Expand Up @@ -90,7 +90,7 @@ public override ExportResult Export(in Batch<LogRecord> batch)
// See https://github.com/open-telemetry/opentelemetry-dotnet/pull/3182
// for explanation.
var valueToTransform = logRecord.Attributes[i].Key.Equals("{OriginalFormat}")
? new KeyValuePair<string, object>("OriginalFormat (a.k.a Body)", logRecord.Attributes[i].Value)
? new KeyValuePair<string, object?>("OriginalFormat (a.k.a Body)", logRecord.Attributes[i].Value)
: logRecord.Attributes[i];

if (this.TagWriter.TryTransformTag(valueToTransform, out var result))
Expand Down Expand Up @@ -125,7 +125,7 @@ void ProcessScope(LogRecordScope scope, ConsoleLogRecordExporter exporter)
exporter.WriteLine("LogRecord.ScopeValues (Key:Value):");
}

foreach (KeyValuePair<string, object> scopeItem in scope)
foreach (KeyValuePair<string, object?> scopeItem in scope)
{
if (this.TagWriter.TryTransformTag(scopeItem, out var result))
{
Expand All @@ -140,7 +140,7 @@ void ProcessScope(LogRecordScope scope, ConsoleLogRecordExporter exporter)
this.WriteLine("\nResource associated with LogRecord:");
foreach (var resourceAttribute in resource.Attributes)
{
if (this.TagWriter.TryTransformTag(resourceAttribute, out var result))
if (this.TagWriter.TryTransformTag(resourceAttribute.Key, resourceAttribute.Value, out var result))
{
this.WriteLine($"{result.Key}: {result.Value}");
}
Expand Down
4 changes: 2 additions & 2 deletions src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace OpenTelemetry.Exporter;

public class ConsoleMetricExporter : ConsoleExporter<Metric>
{
private Resource resource;
private Resource? resource;

public ConsoleMetricExporter(ConsoleExporterOptions options)
: base(options)
Expand All @@ -27,7 +27,7 @@ public override ExportResult Export(in Batch<Metric> batch)
this.WriteLine("Resource associated with Metric:");
foreach (var resourceAttribute in this.resource.Attributes)
{
if (this.TagWriter.TryTransformTag(resourceAttribute, out var result))
if (this.TagWriter.TryTransformTag(resourceAttribute.Key, resourceAttribute.Value, out var result))
{
this.WriteLine($" {result.Key}: {result.Value}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ public ConsoleTagWriter(Action<string, string> onUnsupportedTagDropped)
}

public bool TryTransformTag(KeyValuePair<string, object?> tag, out KeyValuePair<string, string> result)
{
return this.TryTransformTag(tag.Key, tag.Value, out result);
}

public bool TryTransformTag(string key, object? value, out KeyValuePair<string, string> result)
{
ConsoleTag consoleTag = default;
if (this.TryWriteTag(ref consoleTag, tag))
if (this.TryWriteTag(ref consoleTag, key, value))
{
result = new KeyValuePair<string, string>(consoleTag.Key!, consoleTag.Value!);
return true;
Expand Down
Loading

0 comments on commit 22a58aa

Please sign in to comment.