Skip to content

Commit

Permalink
Update OpenTelemetry.Exporter.Console to use file scoped namespaces (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jarosal authored Jul 21, 2023
1 parent 1d3bb4b commit 358a1cd
Show file tree
Hide file tree
Showing 9 changed files with 595 additions and 604 deletions.
167 changes: 83 additions & 84 deletions src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,130 +18,129 @@
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

namespace OpenTelemetry.Exporter
namespace OpenTelemetry.Exporter;

public class ConsoleActivityExporter : ConsoleExporter<Activity>
{
public class ConsoleActivityExporter : ConsoleExporter<Activity>
public ConsoleActivityExporter(ConsoleExporterOptions options)
: base(options)
{
public ConsoleActivityExporter(ConsoleExporterOptions options)
: base(options)
{
}
}

public override ExportResult Export(in Batch<Activity> batch)
public override ExportResult Export(in Batch<Activity> batch)
{
foreach (var activity in batch)
{
foreach (var activity in batch)
this.WriteLine($"Activity.TraceId: {activity.TraceId}");
this.WriteLine($"Activity.SpanId: {activity.SpanId}");
this.WriteLine($"Activity.TraceFlags: {activity.ActivityTraceFlags}");
if (!string.IsNullOrEmpty(activity.TraceStateString))
{
this.WriteLine($"Activity.TraceId: {activity.TraceId}");
this.WriteLine($"Activity.SpanId: {activity.SpanId}");
this.WriteLine($"Activity.TraceFlags: {activity.ActivityTraceFlags}");
if (!string.IsNullOrEmpty(activity.TraceStateString))
{
this.WriteLine($"Activity.TraceState: {activity.TraceStateString}");
}
this.WriteLine($"Activity.TraceState: {activity.TraceStateString}");
}

if (activity.ParentSpanId != default)
{
this.WriteLine($"Activity.ParentSpanId: {activity.ParentSpanId}");
}
if (activity.ParentSpanId != default)
{
this.WriteLine($"Activity.ParentSpanId: {activity.ParentSpanId}");
}

this.WriteLine($"Activity.ActivitySourceName: {activity.Source.Name}");
this.WriteLine($"Activity.DisplayName: {activity.DisplayName}");
this.WriteLine($"Activity.Kind: {activity.Kind}");
this.WriteLine($"Activity.StartTime: {activity.StartTimeUtc:yyyy-MM-ddTHH:mm:ss.fffffffZ}");
this.WriteLine($"Activity.Duration: {activity.Duration}");
var statusCode = string.Empty;
var statusDesc = string.Empty;
this.WriteLine($"Activity.ActivitySourceName: {activity.Source.Name}");
this.WriteLine($"Activity.DisplayName: {activity.DisplayName}");
this.WriteLine($"Activity.Kind: {activity.Kind}");
this.WriteLine($"Activity.StartTime: {activity.StartTimeUtc:yyyy-MM-ddTHH:mm:ss.fffffffZ}");
this.WriteLine($"Activity.Duration: {activity.Duration}");
var statusCode = string.Empty;
var statusDesc = string.Empty;

if (activity.TagObjects.Any())
if (activity.TagObjects.Any())
{
this.WriteLine("Activity.Tags:");
foreach (ref readonly var tag in activity.EnumerateTagObjects())
{
this.WriteLine("Activity.Tags:");
foreach (ref readonly var tag in activity.EnumerateTagObjects())
if (tag.Key == SpanAttributeConstants.StatusCodeKey)
{
if (tag.Key == SpanAttributeConstants.StatusCodeKey)
{
statusCode = tag.Value as string;
continue;
}
statusCode = tag.Value as string;
continue;
}

if (tag.Key == SpanAttributeConstants.StatusDescriptionKey)
{
statusDesc = tag.Value as string;
continue;
}
if (tag.Key == SpanAttributeConstants.StatusDescriptionKey)
{
statusDesc = tag.Value as string;
continue;
}

if (ConsoleTagTransformer.Instance.TryTransformTag(tag, out var result))
{
this.WriteLine($" {result}");
}
if (ConsoleTagTransformer.Instance.TryTransformTag(tag, out var result))
{
this.WriteLine($" {result}");
}
}
}

if (activity.Status != ActivityStatusCode.Unset)
if (activity.Status != ActivityStatusCode.Unset)
{
this.WriteLine($"StatusCode: {activity.Status}");
if (!string.IsNullOrEmpty(activity.StatusDescription))
{
this.WriteLine($"StatusCode: {activity.Status}");
if (!string.IsNullOrEmpty(activity.StatusDescription))
{
this.WriteLine($"Activity.StatusDescription: {activity.StatusDescription}");
}
this.WriteLine($"Activity.StatusDescription: {activity.StatusDescription}");
}
else if (!string.IsNullOrEmpty(statusCode))
}
else if (!string.IsNullOrEmpty(statusCode))
{
this.WriteLine($" StatusCode: {statusCode}");
if (!string.IsNullOrEmpty(statusDesc))
{
this.WriteLine($" StatusCode: {statusCode}");
if (!string.IsNullOrEmpty(statusDesc))
{
this.WriteLine($" Activity.StatusDescription: {statusDesc}");
}
this.WriteLine($" Activity.StatusDescription: {statusDesc}");
}
}

if (activity.Events.Any())
if (activity.Events.Any())
{
this.WriteLine("Activity.Events:");
foreach (ref readonly var activityEvent in activity.EnumerateEvents())
{
this.WriteLine("Activity.Events:");
foreach (ref readonly var activityEvent in activity.EnumerateEvents())
this.WriteLine($" {activityEvent.Name} [{activityEvent.Timestamp}]");
foreach (ref readonly var attribute in activityEvent.EnumerateTagObjects())
{
this.WriteLine($" {activityEvent.Name} [{activityEvent.Timestamp}]");
foreach (ref readonly var attribute in activityEvent.EnumerateTagObjects())
if (ConsoleTagTransformer.Instance.TryTransformTag(attribute, out var result))
{
if (ConsoleTagTransformer.Instance.TryTransformTag(attribute, out var result))
{
this.WriteLine($" {result}");
}
this.WriteLine($" {result}");
}
}
}
}

if (activity.Links.Any())
if (activity.Links.Any())
{
this.WriteLine("Activity.Links:");
foreach (ref readonly var activityLink in activity.EnumerateLinks())
{
this.WriteLine("Activity.Links:");
foreach (ref readonly var activityLink in activity.EnumerateLinks())
this.WriteLine($" {activityLink.Context.TraceId} {activityLink.Context.SpanId}");
foreach (ref readonly var attribute in activityLink.EnumerateTagObjects())
{
this.WriteLine($" {activityLink.Context.TraceId} {activityLink.Context.SpanId}");
foreach (ref readonly var attribute in activityLink.EnumerateTagObjects())
if (ConsoleTagTransformer.Instance.TryTransformTag(attribute, out var result))
{
if (ConsoleTagTransformer.Instance.TryTransformTag(attribute, out var result))
{
this.WriteLine($" {result}");
}
this.WriteLine($" {result}");
}
}
}
}

var resource = this.ParentProvider.GetResource();
if (resource != Resource.Empty)
var resource = this.ParentProvider.GetResource();
if (resource != Resource.Empty)
{
this.WriteLine("Resource associated with Activity:");
foreach (var resourceAttribute in resource.Attributes)
{
this.WriteLine("Resource associated with Activity:");
foreach (var resourceAttribute in resource.Attributes)
if (ConsoleTagTransformer.Instance.TryTransformTag(resourceAttribute, out var result))
{
if (ConsoleTagTransformer.Instance.TryTransformTag(resourceAttribute, out var result))
{
this.WriteLine($" {result}");
}
this.WriteLine($" {result}");
}
}

this.WriteLine(string.Empty);
}

return ExportResult.Success;
this.WriteLine(string.Empty);
}

return ExportResult.Success;
}
}
39 changes: 19 additions & 20 deletions src/OpenTelemetry.Exporter.Console/ConsoleExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,32 @@
// limitations under the License.
// </copyright>

namespace OpenTelemetry.Exporter
namespace OpenTelemetry.Exporter;

public abstract class ConsoleExporter<T> : BaseExporter<T>
where T : class
{
public abstract class ConsoleExporter<T> : BaseExporter<T>
where T : class
private readonly ConsoleExporterOptions options;

protected ConsoleExporter(ConsoleExporterOptions options)
{
private readonly ConsoleExporterOptions options;
this.options = options ?? new ConsoleExporterOptions();
ConsoleTagTransformer.LogUnsupportedAttributeType = (string tagValueType, string tagKey) =>
{
this.WriteLine($"Unsupported attribute type {tagValueType} for {tagKey}.");
};
}

protected ConsoleExporter(ConsoleExporterOptions options)
protected void WriteLine(string message)
{
if (this.options.Targets.HasFlag(ConsoleExporterOutputTargets.Console))
{
this.options = options ?? new ConsoleExporterOptions();
ConsoleTagTransformer.LogUnsupportedAttributeType = (string tagValueType, string tagKey) =>
{
this.WriteLine($"Unsupported attribute type {tagValueType} for {tagKey}.");
};
Console.WriteLine(message);
}

protected void WriteLine(string message)
if (this.options.Targets.HasFlag(ConsoleExporterOutputTargets.Debug))
{
if (this.options.Targets.HasFlag(ConsoleExporterOutputTargets.Console))
{
Console.WriteLine(message);
}

if (this.options.Targets.HasFlag(ConsoleExporterOutputTargets.Debug))
{
System.Diagnostics.Trace.WriteLine(message);
}
System.Diagnostics.Trace.WriteLine(message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,54 +19,53 @@
using OpenTelemetry.Exporter;
using OpenTelemetry.Internal;

namespace OpenTelemetry.Trace
namespace OpenTelemetry.Trace;

public static class ConsoleExporterHelperExtensions
{
public static class ConsoleExporterHelperExtensions
{
/// <summary>
/// Adds Console exporter to the TracerProvider.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> builder to use.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddConsoleExporter(this TracerProviderBuilder builder)
=> AddConsoleExporter(builder, name: null, configure: null);
/// <summary>
/// Adds Console exporter to the TracerProvider.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> builder to use.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddConsoleExporter(this TracerProviderBuilder builder)
=> AddConsoleExporter(builder, name: null, configure: null);

/// <summary>
/// Adds Console exporter to the TracerProvider.
/// </summary>
/// <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)
=> AddConsoleExporter(builder, name: null, configure);
/// <summary>
/// Adds Console exporter to the TracerProvider.
/// </summary>
/// <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)
=> AddConsoleExporter(builder, name: null, configure);

/// <summary>
/// Adds Console exporter to the TracerProvider.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> builder to use.</param>
/// <param name="name">Name which is used when retrieving options.</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,
string name,
Action<ConsoleExporterOptions> configure)
{
Guard.ThrowIfNull(builder);
/// <summary>
/// Adds Console exporter to the TracerProvider.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> builder to use.</param>
/// <param name="name">Name which is used when retrieving options.</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,
string name,
Action<ConsoleExporterOptions> configure)
{
Guard.ThrowIfNull(builder);

name ??= Options.DefaultName;
name ??= Options.DefaultName;

if (configure != null)
{
builder.ConfigureServices(services => services.Configure(name, configure));
}
if (configure != null)
{
builder.ConfigureServices(services => services.Configure(name, configure));
}

return builder.AddProcessor(sp =>
{
var options = sp.GetRequiredService<IOptionsMonitor<ConsoleExporterOptions>>().Get(name);
return builder.AddProcessor(sp =>
{
var options = sp.GetRequiredService<IOptionsMonitor<ConsoleExporterOptions>>().Get(name);

return new SimpleActivityExportProcessor(new ConsoleActivityExporter(options));
});
}
return new SimpleActivityExportProcessor(new ConsoleActivityExporter(options));
});
}
}
Loading

0 comments on commit 358a1cd

Please sign in to comment.