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

ILogger integration - part 3 #1328

Merged
merged 11 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/logs/getting-started/LoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

using System;
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Logs;

internal static class LoggerExtensions
{
Expand All @@ -29,4 +31,14 @@ public static void LogEx(this ILogger logger, object obj)
{
LogExAction(logger, obj, null);
}

public static OpenTelemetryLoggerOptions AddMyExporter(this OpenTelemetryLoggerOptions options)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}

return options.AddProcessor(new BatchExportProcessor<LogRecord>(new MyExporter()));
}
}
63 changes: 63 additions & 0 deletions docs/logs/getting-started/MyExporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// <copyright file="MyExporter.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;
using OpenTelemetry;
using OpenTelemetry.Logs;

internal class MyExporter : BaseExporter<LogRecord>
{
private readonly string name;

public MyExporter(string name = "MyExporter")
{
this.name = name;
}

public override ExportResult Export(in Batch<LogRecord> batch)
{
// SuppressInstrumentationScope should be used to prevent exporter
// code from generating telemetry and causing live-loop.
using var scope = SuppressInstrumentationScope.Begin();

var sb = new StringBuilder();
foreach (var record in batch)
{
if (sb.Length > 0)
{
sb.Append(", ");
}

sb.Append($"{record}");
}

Console.WriteLine($"{this.name}.Export([{sb.ToString()}])");
return ExportResult.Success;
}

protected override bool OnShutdown(int timeoutMilliseconds)
{
Console.WriteLine($"{this.name}.OnShutdown(timeoutMilliseconds={timeoutMilliseconds})");
return true;
}

protected override void Dispose(bool disposing)
{
Console.WriteLine($"{this.name}.Dispose({disposing})");
}
}
37 changes: 3 additions & 34 deletions docs/logs/getting-started/MyProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

using System;
using System.Collections.Generic;
using OpenTelemetry;
using OpenTelemetry.Logs;

internal class MyProcessor : LogProcessor
internal class MyProcessor : BaseProcessor<LogRecord>
{
private readonly string name;

Expand All @@ -29,39 +30,7 @@ public MyProcessor(string name = "MyProcessor")

public override void OnEnd(LogRecord record)
{
var state = record.State;

if (state is IReadOnlyCollection<KeyValuePair<string, object>> dict)
{
var isUnstructuredLog = dict.Count == 1;

if (isUnstructuredLog)
{
foreach (var entry in dict)
{
Console.WriteLine($"{record.Timestamp:yyyy-MM-ddTHH:mm:ss.fffffffZ} {record.CategoryName}({record.LogLevel}, Id={record.EventId}): {entry.Value}");
}
}
else
{
Console.WriteLine($"{record.Timestamp:yyyy-MM-ddTHH:mm:ss.fffffffZ} {record.CategoryName}({record.LogLevel}, Id={record.EventId}):");
foreach (var entry in dict)
{
if (string.Equals(entry.Key, "{OriginalFormat}", StringComparison.Ordinal))
{
Console.WriteLine($" $format: {entry.Value}");
continue;
}

Console.WriteLine($" {entry.Key}: {entry.Value}");
}
}

if (record.Exception != null)
{
Console.WriteLine($" $exception: {record.Exception}");
}
}
Console.WriteLine($"{this.name}.OnEnd({record})");
}

protected override bool OnForceFlush(int timeoutMilliseconds)
Expand Down
5 changes: 4 additions & 1 deletion docs/logs/getting-started/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public static void Main()
using var loggerFactory = LoggerFactory.Create(builder =>
#endif
{
builder.AddOpenTelemetry(options => options.AddProcessor(new MyProcessor()));
builder.AddOpenTelemetry(options => options
.AddProcessor(new MyProcessor("A"))
.AddProcessor(new MyProcessor("B"))
.AddMyExporter());
});

#if NETCOREAPP2_1
Expand Down
2 changes: 1 addition & 1 deletion docs/trace/extending-the-sdk/MyExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
using OpenTelemetry;
using OpenTelemetry.Trace;

internal class MyExporter : ActivityExporter
internal class MyExporter : BaseExporter<Activity>
{
private readonly string name;

Expand Down
4 changes: 3 additions & 1 deletion docs/trace/extending-the-sdk/MyExporterHelperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// </copyright>

using System;
using System.Diagnostics;
using OpenTelemetry;
using OpenTelemetry.Trace;

internal static class MyExporterHelperExtensions
Expand All @@ -26,6 +28,6 @@ public static TracerProviderBuilder AddMyExporter(this TracerProviderBuilder bui
throw new ArgumentNullException(nameof(builder));
}

return builder.AddProcessor(new BatchExportActivityProcessor(new MyExporter()));
return builder.AddProcessor(new BatchExportProcessor<Activity>(new MyExporter()));
}
}
4 changes: 2 additions & 2 deletions docs/trace/extending-the-sdk/MyProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

using System;
using System.Diagnostics;
using OpenTelemetry.Trace;
using OpenTelemetry;

internal class MyProcessor : ActivityProcessor
internal class MyProcessor : BaseProcessor<Activity>
{
private readonly string name;

Expand Down
2 changes: 1 addition & 1 deletion docs/trace/extending-the-sdk/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void Main()
.AddSource("OTel.Demo")
.AddProcessor(new MyProcessor("ProcessorA"))
.AddProcessor(new MyProcessor("ProcessorB"))
.AddProcessor(new SimpleExportActivityProcessor(new MyExporter("ExporterX")))
.AddProcessor(new SimpleExportProcessor<Activity>(new MyExporter("ExporterX")))
.AddMyExporter()
.Build();

Expand Down
16 changes: 8 additions & 8 deletions docs/trace/extending-the-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ OpenTelemetry .NET SDK has provided the following built-in exporters:
Custom exporters can be implemented to send telemetry data to places which are
not covered by the built-in exporters:

* Exporters should derive from `OpenTelemetry.Trace.ActivityExporter` (which
* Exporters should derive from `OpenTelemetry.BaseExporter<Activity>` (which
belongs to the [OpenTelemetry](../../../src/OpenTelemetry/README.md) package)
and implement the `Export` method.
* Exporters can optionally implement the `OnShutdown` method.
Expand All @@ -34,7 +34,7 @@ not covered by the built-in exporters:
done via `OpenTelemetry.SuppressInstrumentationScope`.

```csharp
class MyExporter : ActivityExporter
class MyExporter : BaseExporter<Activity>
{
public override ExportResult Export(in Batch<Activity> batch)
{
Expand Down Expand Up @@ -65,14 +65,14 @@ TBD

OpenTelemetry .NET SDK has provided the following built-in processors:

* [BatchExportActivityProcessor](../../../src/OpenTelemetry/Trace/BatchExportActivityProcessor.cs)
* [CompositeActivityProcessor](../../../src/OpenTelemetry/Trace/CompositeActivityProcessor.cs)
* [ReentrantExportActivityProcessor](../../../src/OpenTelemetry/Trace/ReentrantExportActivityProcessor.cs)
* [SimpleExportActivityProcessor](../../../src/OpenTelemetry/Trace/SimpleExportActivityProcessor.cs)
* [BatchExportProcessor&lt;T&gt;](../../../src/OpenTelemetry/BatchExportProcessor.cs)
* [CompositeProcessor&lt;T&gt;](../../../src/OpenTelemetry/CompositeProcessor.cs)
* [ReentrantExportProcessor&lt;T&gt;](../../../src/OpenTelemetry/ReentrantExportProcessor.cs)
* [SimpleExportProcessor&lt;T&gt;](../../../src/OpenTelemetry/SimpleExportProcessor.cs)

Custom processors can be implemented to cover more scenarios:

* Processors should inherit from `OpenTelemetry.Trace.ActivityProcessor` (which
* Processors should inherit from `OpenTelemetry.BaseProcessor<Activity>` (which
belongs to the [OpenTelemetry](../../../src/OpenTelemetry/README.md) package),
and implement the `OnStart` and `OnEnd` methods.
* Processors can optionally implement the `OnForceFlush` and `OnShutdown`
Expand All @@ -83,7 +83,7 @@ Custom processors can be implemented to cover more scenarios:
time, since they will be called on critical code path.

```csharp
class MyProcessor : ActivityProcessor
class MyProcessor : BaseProcessor<Activity>
{
public override void OnStart(Activity activity)
{
Expand Down
2 changes: 1 addition & 1 deletion examples/Console/TestConsoleExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ internal static object Run(ConsoleOptions options)
return null;
}

internal class MyProcessor : ActivityProcessor
internal class MyProcessor : BaseProcessor<Activity>
{
public override void OnStart(Activity activity)
{
Expand Down
3 changes: 2 additions & 1 deletion src/OpenTelemetry.Exporter.Console/ConsoleExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

namespace OpenTelemetry.Exporter.Console
{
public class ConsoleExporter : ActivityExporter
public class ConsoleExporter : BaseExporter<Activity>
{
private readonly JsonSerializerOptions serializerOptions;
private readonly bool displayAsJson;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// </copyright>

using System;
using System.Diagnostics;
using OpenTelemetry.Exporter.Console;

namespace OpenTelemetry.Trace
Expand All @@ -37,7 +38,7 @@ public static TracerProviderBuilder AddConsoleExporter(this TracerProviderBuilde

var options = new ConsoleExporterOptions();
configure?.Invoke(options);
return builder.AddProcessor(new SimpleExportActivityProcessor(new ConsoleExporter(options)));
return builder.AddProcessor(new SimpleExportProcessor<Activity>(new ConsoleExporter(options)));
}
}
}
3 changes: 2 additions & 1 deletion src/OpenTelemetry.Exporter.Jaeger/JaegerExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using OpenTelemetry;
using OpenTelemetry.Exporter.Jaeger.Implementation;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
Expand All @@ -28,7 +29,7 @@

namespace OpenTelemetry.Exporter.Jaeger
{
public class JaegerExporter : ActivityExporter
public class JaegerExporter : BaseExporter<Activity>
{
private readonly int maxPayloadSizeInBytes;
private readonly TProtocolFactory protocolFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// </copyright>

using System;
using System.Diagnostics;
using OpenTelemetry.Exporter.Jaeger;

namespace OpenTelemetry.Trace
Expand Down Expand Up @@ -43,7 +44,7 @@ public static TracerProviderBuilder AddJaegerExporter(this TracerProviderBuilder
var jaegerExporter = new JaegerExporter(exporterOptions);

// TODO: Pick Simple vs Batching based on JaegerExporterOptions
return builder.AddProcessor(new BatchExportActivityProcessor(jaegerExporter));
return builder.AddProcessor(new BatchExportProcessor<Activity>(jaegerExporter));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using Grpc.Core;
using OpenTelemetry;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
using OpenTelemetry.Trace;
using OtlpCollector = Opentelemetry.Proto.Collector.Trace.V1;
Expand All @@ -28,7 +29,7 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol
/// Exporter consuming <see cref="Activity"/> and exporting the data using
/// the OpenTelemetry protocol (OTLP).
/// </summary>
public class OtlpExporter : ActivityExporter
public class OtlpExporter : BaseExporter<Activity>
{
private readonly Channel channel;
private readonly OtlpCollector.TraceService.TraceServiceClient traceClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// </copyright>

using System;
using System.Diagnostics;
using OpenTelemetry.Exporter.OpenTelemetryProtocol;

namespace OpenTelemetry.Trace
Expand Down Expand Up @@ -43,7 +44,7 @@ public static TracerProviderBuilder AddOtlpExporter(this TracerProviderBuilder b
var otlpExporter = new OtlpExporter(exporterOptions);

// TODO: Pick Simple vs Batching based on OtlpExporterOptions
return builder.AddProcessor(new BatchExportActivityProcessor(otlpExporter));
return builder.AddProcessor(new BatchExportProcessor<Activity>(otlpExporter));
}
}
}
3 changes: 2 additions & 1 deletion src/OpenTelemetry.Exporter.ZPages/ZPagesExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System;
using System.Diagnostics;
using System.Timers;
using OpenTelemetry;
using OpenTelemetry.Exporter.ZPages.Implementation;
using OpenTelemetry.Trace;
using Timer = System.Timers.Timer;
Expand All @@ -26,7 +27,7 @@ namespace OpenTelemetry.Exporter.ZPages
/// <summary>
/// Implements ZPages exporter.
/// </summary>
public class ZPagesExporter : ActivityExporter
public class ZPagesExporter : BaseExporter<Activity>
{
internal readonly ZPagesExporterOptions Options;
private readonly Timer minuteTimer;
Expand Down
2 changes: 1 addition & 1 deletion src/OpenTelemetry.Exporter.ZPages/ZPagesProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace OpenTelemetry.Exporter.ZPages
/// <summary>
/// Implements the zpages span processor that exports spans in OnEnd call without batching.
/// </summary>
public class ZPagesProcessor : ActivityProcessor
public class ZPagesProcessor : BaseProcessor<Activity>
{
private readonly ZPagesExporter exporter;

Expand Down
3 changes: 2 additions & 1 deletion src/OpenTelemetry.Exporter.Zipkin/ZipkinExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#endif
using System.Threading;
using System.Threading.Tasks;
using OpenTelemetry;
using OpenTelemetry.Exporter.Zipkin.Implementation;
using OpenTelemetry.Trace;

Expand All @@ -36,7 +37,7 @@ namespace OpenTelemetry.Exporter.Zipkin
/// <summary>
/// Zipkin exporter.
/// </summary>
public class ZipkinExporter : ActivityExporter
public class ZipkinExporter : BaseExporter<Activity>
{
private readonly ZipkinExporterOptions options;
#if !NET452
Expand Down
Loading