Extend ConsoleExporter to add support for Logs (#1438)

* Added support for Logs; Removed disaplayAsJson option from ConsoleExporter; Added net451 as a traget framework for OpenTelemetry.Exporter.Console

* Print LogRecord exception on console

Co-authored-by: Cijo Thomas <cithomas@microsoft.com>
This commit is contained in:
Utkarsh Umesan Pillai 2020-11-02 23:02:13 -08:00 committed by GitHub
parent 097925980d
commit d82ab98320
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 42 additions and 121 deletions

View File

@ -116,22 +116,16 @@ namespace Examples.Console
[Verb("console", HelpText = "Specify the options required to test console exporter")]
internal class ConsoleOptions
{
[Option('p', "displayasjson", HelpText = "Specify if the output should be displayed as json or not (default: false)", Default = false)]
public bool DisplayAsJson { get; set; }
}
[Verb("otelshim", HelpText = "Specify the options required to test OpenTelemetry Shim with console exporter")]
internal class OpenTelemetryShimOptions
{
[Option('p', "displayasjson", HelpText = "Specify if the output should be displayed as json or not (default: false)", Default = false)]
public bool DisplayAsJson { get; set; }
}
[Verb("opentracing", HelpText = "Specify the options required to test OpenTracing Shim with console exporter")]
internal class OpenTracingShimOptions
{
[Option('p', "displayasjson", HelpText = "Specify if the output should be displayed as json or not (default: false)", Default = false)]
public bool DisplayAsJson { get; set; }
}
[Verb("otlp", HelpText = "Specify the options required to test OpenTelemetry Protocol (OTLP)")]

View File

@ -32,7 +32,7 @@ namespace Examples.Console
.AddSource("MyCompany.MyProduct.MyWebServer")
.SetResource(Resources.CreateServiceResource("MyServiceName"))
.AddProcessor(new MyProcessor()) // This must be added before ConsoleExporter
.AddConsoleExporter(opt => opt.DisplayAsJson = options.DisplayAsJson)
.AddConsoleExporter()
.Build();
// The above line is required only in applications

View File

@ -31,7 +31,7 @@ namespace Examples.Console
.AddHttpClientInstrumentation()
.SetResource(Resources.CreateServiceResource("http-service-example"))
.AddSource("http-client-test")
.AddConsoleExporter(opt => opt.DisplayAsJson = false)
.AddConsoleExporter()
.Build();
var source = new ActivitySource("http-client-test");

View File

@ -29,7 +29,7 @@ namespace Examples.Console
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource("MyCompany.MyProduct.MyWebServer")
.SetResource(Resources.CreateServiceResource("MyServiceName"))
.AddConsoleExporter(opt => opt.DisplayAsJson = options.DisplayAsJson)
.AddConsoleExporter()
.Build();
// The above line is required only in applications

View File

@ -32,7 +32,7 @@ namespace Examples.Console
using var openTelemetry = Sdk.CreateTracerProviderBuilder()
.AddSource("MyCompany.MyProduct.MyWebServer")
.SetResource(Resources.CreateServiceResource("MyServiceName"))
.AddConsoleExporter(opt => opt.DisplayAsJson = options.DisplayAsJson)
.AddConsoleExporter()
.Build();
// The above line is required only in applications

View File

@ -1,36 +0,0 @@
// <copyright file="ActivitySpanIdConverter.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System;
using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace OpenTelemetry.Exporter
{
internal class ActivitySpanIdConverter : JsonConverter<ActivitySpanId>
{
public override ActivitySpanId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
public override void Write(Utf8JsonWriter writer, ActivitySpanId value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
}
}

View File

@ -1,36 +0,0 @@
// <copyright file="ActivityTraceIdConverter.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System;
using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace OpenTelemetry.Exporter
{
internal class ActivityTraceIdConverter : JsonConverter<ActivityTraceId>
{
public override ActivityTraceId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
public override void Write(Utf8JsonWriter writer, ActivityTraceId value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
}
}

View File

@ -17,42 +17,28 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
#if NET461 || NETSTANDARD2_0
using OpenTelemetry.Logs;
#endif
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
namespace OpenTelemetry.Exporter
{
public class ConsoleExporter : BaseExporter<Activity>
public class ConsoleExporter<T> : BaseExporter<T>
where T : class
{
private readonly JsonSerializerOptions serializerOptions;
private readonly bool displayAsJson;
public ConsoleExporter(ConsoleExporterOptions options)
{
this.serializerOptions = new JsonSerializerOptions()
{
WriteIndented = true,
};
this.displayAsJson = options?.DisplayAsJson ?? false;
this.serializerOptions.Converters.Add(new JsonStringEnumConverter());
this.serializerOptions.Converters.Add(new ActivitySpanIdConverter());
this.serializerOptions.Converters.Add(new ActivityTraceIdConverter());
}
public override ExportResult Export(in Batch<Activity> batch)
public override ExportResult Export(in Batch<T> batch)
{
foreach (var activity in batch)
if (typeof(T) == typeof(Activity))
{
if (this.displayAsJson)
{
Console.WriteLine(JsonSerializer.Serialize(activity, this.serializerOptions));
}
else
foreach (var item in batch)
{
var activity = item as Activity;
Console.WriteLine($"Activity.Id: {activity.Id}");
if (!string.IsNullOrEmpty(activity.ParentId))
{
@ -123,6 +109,30 @@ namespace OpenTelemetry.Exporter
Console.WriteLine();
}
}
#if NET461 || NETSTANDARD2_0
else if (typeof(T) == typeof(LogRecord))
{
var rightPaddingLength = 30;
foreach (var item in batch)
{
var logRecord = item as LogRecord;
Console.WriteLine($"{"LogRecord.TraceId:".PadRight(rightPaddingLength)}{logRecord.TraceId}");
Console.WriteLine($"{"LogRecord.SpanId:".PadRight(rightPaddingLength)}{logRecord.SpanId}");
Console.WriteLine($"{"LogRecord.Timestamp:".PadRight(rightPaddingLength)}{logRecord.Timestamp:yyyy-MM-ddTHH:mm:ss.fffffffZ}");
Console.WriteLine($"{"LogRecord.EventId:".PadRight(rightPaddingLength)}{logRecord.EventId}");
Console.WriteLine($"{"LogRecord.CategoryName:".PadRight(rightPaddingLength)}{logRecord.CategoryName}");
Console.WriteLine($"{"LogRecord.LogLevel:".PadRight(rightPaddingLength)}{logRecord.LogLevel}");
Console.WriteLine($"{"LogRecord.TraceFlags:".PadRight(rightPaddingLength)}{logRecord.TraceFlags}");
Console.WriteLine($"{"LogRecord.State:".PadRight(rightPaddingLength)}{logRecord.State}");
if (logRecord.Exception is { })
{
Console.WriteLine($"{"LogRecord.Exception:".PadRight(rightPaddingLength)}{logRecord.Exception?.Message}");
}
Console.WriteLine();
}
}
#endif
return ExportResult.Success;
}

View File

@ -38,7 +38,7 @@ namespace OpenTelemetry.Trace
var options = new ConsoleExporterOptions();
configure?.Invoke(options);
return builder.AddProcessor(new SimpleExportProcessor<Activity>(new ConsoleExporter(options)));
return builder.AddProcessor(new SimpleExportProcessor<Activity>(new ConsoleExporter<Activity>(options)));
}
}
}

View File

@ -18,6 +18,5 @@ namespace OpenTelemetry.Exporter
{
public class ConsoleExporterOptions
{
public bool DisplayAsJson { get; set; }
}
}

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net452;net461;netstandard2.0</TargetFrameworks>
<Description>Console exporter for OpenTelemetry .NET</Description>
<PackageTags>$(PackageTags);Console;distributed-tracing</PackageTags>
</PropertyGroup>
@ -14,8 +14,4 @@
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry\OpenTelemetry.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Text.Json" Version="$(SystemTextJsonPkgVer)" />
</ItemGroup>
</Project>

View File

@ -15,15 +15,9 @@ environment.
dotnet add package OpenTelemetry.Exporter.Console
```
## Configuration
You can configure the `ConsoleExporter` by following the directions below:
* `DisplayAsJson`: Boolean to show data as JSON.
See the
[`TestConsoleExporter.cs`](../../examples/Console/TestConsoleExporter.cs)
for an example of how to use the exporter.
[`TestConsoleExporter.cs`](../../examples/Console/TestConsoleExporter.cs) for an
example of how to use the exporter.
## References