Add custom processor example and consoleexporter extension method. (#754)
This commit is contained in:
parent
0aa8bfef96
commit
5404ca0354
|
|
@ -16,8 +16,11 @@
|
|||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using OpenTelemetry.Exporter.Console;
|
||||
using OpenTelemetry.Trace.Configuration;
|
||||
using OpenTelemetry.Trace.Export;
|
||||
|
||||
namespace Samples
|
||||
{
|
||||
|
|
@ -26,10 +29,13 @@ namespace Samples
|
|||
internal static object Run(ConsoleActivityOptions options)
|
||||
{
|
||||
// Enable OpenTelemetry for the source "MyCompany.MyProduct.MyWebServer"
|
||||
// and use Console exporter
|
||||
// and use a single pipeline with a custom MyProcessor, and Console exporter.
|
||||
using var openTelemetry = OpenTelemetrySdk.EnableOpenTelemetry(
|
||||
(builder) => builder.AddActivitySource("MyCompany.MyProduct.MyWebServer")
|
||||
.UseConsoleActivityExporter(opt => opt.DisplayAsJson = options.DisplayAsJson));
|
||||
.AddProcessorPipeline(
|
||||
(p) =>
|
||||
p.AddProcessor((next) => new MyProcessor(next))
|
||||
.UseConsoleActivityExporter(opt => opt.DisplayAsJson = options.DisplayAsJson)));
|
||||
|
||||
// The above line is required only in Applications
|
||||
// which decide to use OT.
|
||||
|
|
@ -91,5 +97,42 @@ namespace Samples
|
|||
|
||||
return null;
|
||||
}
|
||||
|
||||
internal class MyProcessor : ActivityProcessor
|
||||
{
|
||||
private ActivityProcessor next;
|
||||
|
||||
public MyProcessor(ActivityProcessor next)
|
||||
{
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
public override void OnEnd(Activity activity)
|
||||
{
|
||||
this.next.OnEnd(activity);
|
||||
}
|
||||
|
||||
public override void OnStart(Activity activity)
|
||||
{
|
||||
if (activity.IsAllDataRequested)
|
||||
{
|
||||
if (activity.Kind == ActivityKind.Server)
|
||||
{
|
||||
activity.AddTag("customServerTag", "Custom Tag Value for server");
|
||||
}
|
||||
else if (activity.Kind == ActivityKind.Client)
|
||||
{
|
||||
activity.AddTag("customClientTag", "Custom Tag Value for Client");
|
||||
}
|
||||
}
|
||||
|
||||
this.next.OnStart(activity);
|
||||
}
|
||||
|
||||
public override Task ShutdownAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return this.next.ShutdownAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ namespace OpenTelemetry.Exporter.Console
|
|||
public static class OpenTelemetryBuilderExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Registers a ConsoleActivity exporter.
|
||||
/// Adds new processing pipeline and registers a ConsoleActivity exporter to it.
|
||||
/// </summary>
|
||||
/// <param name="builder">Open Telemetry builder to use.</param>
|
||||
/// <param name="configure">Exporter configuration options.</param>
|
||||
|
|
@ -44,5 +44,29 @@ namespace OpenTelemetry.Exporter.Console
|
|||
var consoleExporter = new ConsoleActivityExporter(exporterOptions);
|
||||
return builder.AddProcessorPipeline(pipeline => pipeline.SetExporter(consoleExporter));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers a ConsoleActivity exporter to a processing pipeline.
|
||||
/// </summary>
|
||||
/// <param name="builder">ActivityProcessorPipelineBuilder to use.</param>
|
||||
/// <param name="configure">Exporter configuration options.</param>
|
||||
/// <returns>The instance of <see cref="ActivityProcessorPipelineBuilder"/> to chain the calls.</returns>
|
||||
public static ActivityProcessorPipelineBuilder UseConsoleActivityExporter(this ActivityProcessorPipelineBuilder builder, Action<ConsoleActivityExporterOptions> configure)
|
||||
{
|
||||
if (builder == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(builder));
|
||||
}
|
||||
|
||||
if (configure == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(configure));
|
||||
}
|
||||
|
||||
var exporterOptions = new ConsoleActivityExporterOptions();
|
||||
configure(exporterOptions);
|
||||
var consoleExporter = new ConsoleActivityExporter(exporterOptions);
|
||||
return builder.SetExporter(consoleExporter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue