Add a ConsoleActivityExporter/ZipkinActivityExporter extension method with samples (#759)

* Create testrajrang

* Added overload methods to Console and Zipkin exporter

* Refactored and change variable names

* Incorporating PR feedback

* Incorporating Mikel's feedback
This commit is contained in:
Rajkumar Rangaraj 2020-07-02 10:50:48 -07:00 committed by GitHub
parent 504713f759
commit 13b1a5d2fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 184 additions and 41 deletions

View File

@ -33,12 +33,10 @@ namespace Samples
// and use a single pipeline with a custom MyProcessor, and Console exporter.
using var openTelemetry = OpenTelemetrySdk.EnableOpenTelemetry(
(builder) => builder.AddActivitySource("MyCompany.MyProduct.MyWebServer")
.SetResource(Resources.CreateServiceResource("MyServiceName"))
.AddProcessorPipeline(
(p) =>
p.AddProcessor((next) => new MyProcessor(next))
.UseConsoleActivityExporter(opt => opt.DisplayAsJson = options.DisplayAsJson)));
.SetResource(Resources.CreateServiceResource("MyServiceName"))
.UseConsoleActivityExporter(opt => opt.DisplayAsJson = options.DisplayAsJson,
(p) => p.AddProcessor((next) => new MyProcessor(next))));
// The above line is required only in Applications
// which decide to use OT.

View File

@ -48,10 +48,11 @@ namespace OpenTelemetry.Exporter.Console
/// <summary>
/// Registers a ConsoleActivity exporter to a processing pipeline.
/// </summary>
/// <param name="builder">ActivityProcessorPipelineBuilder to use.</param>
/// <param name="builder"><see cref="OpenTelemetryBuilder"/> builder 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)
/// <param name="processorConfigure">Activity processor configuration.</param>
/// <returns>The instance of <see cref="OpenTelemetryBuilder"/> to chain the calls.</returns>
public static OpenTelemetryBuilder UseConsoleActivityExporter(this OpenTelemetryBuilder builder, Action<ConsoleActivityExporterOptions> configure, Action<ActivityProcessorPipelineBuilder> processorConfigure)
{
if (builder == null)
{
@ -63,10 +64,20 @@ namespace OpenTelemetry.Exporter.Console
throw new ArgumentNullException(nameof(configure));
}
var exporterOptions = new ConsoleActivityExporterOptions();
configure(exporterOptions);
var consoleExporter = new ConsoleActivityExporter(exporterOptions);
return builder.SetExporter(consoleExporter);
if (processorConfigure == null)
{
throw new ArgumentNullException(nameof(processorConfigure));
}
return builder.AddProcessorPipeline(pipeline =>
{
var exporterOptions = new ConsoleActivityExporterOptions();
configure(exporterOptions);
var consoleExporter = new ConsoleActivityExporter(exporterOptions);
pipeline.SetExporter(consoleExporter);
processorConfigure(pipeline);
});
}
}
}

View File

@ -0,0 +1,52 @@
// <copyright file="OpenTelemetryBuilderExtensions.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 OpenTelemetry.Exporter.Zipkin;
namespace OpenTelemetry.Trace.Configuration
{
/// <summary>
/// Extension methods to simplify registering of Zipkin exporter.
/// </summary>
public static class OpenTelemetryBuilderExtensions
{
/// <summary>
/// Registers a Zipkin exporter that will receive <see cref="System.Diagnostics.Activity"/> instances.
/// </summary>
/// <param name="builder"><see cref="OpenTelemetryBuilder"/> builder to use.</param>
/// <param name="configure">Exporter configuration options.</param>
/// <param name="processorConfigure">Activity processor configuration.</param>
/// <returns>The instance of <see cref="OpenTelemetryBuilder"/> to chain the calls.</returns>
public static OpenTelemetryBuilder UseZipkinActivityExporter(this OpenTelemetryBuilder builder, Action<ZipkinTraceExporterOptions> configure = null, Action<ActivityProcessorPipelineBuilder> processorConfigure = null)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
return builder.AddProcessorPipeline(pipeline =>
{
var options = new ZipkinTraceExporterOptions();
configure?.Invoke(options);
var activityExporter = new ZipkinActivityExporter(options);
pipeline.SetExporter(activityExporter);
processorConfigure?.Invoke(pipeline);
});
}
}
}

View File

@ -83,33 +83,5 @@ namespace OpenTelemetry.Trace.Configuration
processorConfigure.Invoke(b);
});
}
/// <summary>
/// Registers a Zipkin exporter that will receive <see cref="System.Diagnostics.Activity"/> instances.
/// </summary>
/// <param name="builder"><see cref="OpenTelemetryBuilder"/> builder to use.</param>
/// <param name="configure">Exporter configuration options.</param>
/// <returns>The instance of <see cref="OpenTelemetryBuilder"/> to chain the calls.</returns>
public static OpenTelemetryBuilder UseZipkinActivityExporter(this OpenTelemetryBuilder builder, Action<ZipkinTraceExporterOptions> configure)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
return builder.AddProcessorPipeline(pipeline =>
{
var options = new ZipkinTraceExporterOptions();
configure(options);
var activityExporter = new ZipkinActivityExporter(options);
pipeline.SetExporter(activityExporter);
});
}
}
}

View File

@ -0,0 +1,69 @@
// <copyright file="TestActivityProcessor.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.Threading;
using System.Threading.Tasks;
using OpenTelemetry.Trace.Export;
namespace OpenTelemetry.Exporter.Zipkin.Tests
{
public class TestActivityProcessor : ActivityProcessor, IDisposable
{
public Action<Activity> StartAction;
public Action<Activity> EndAction;
public TestActivityProcessor()
{
}
public TestActivityProcessor(Action<Activity> onStart, Action<Activity> onEnd)
{
this.StartAction = onStart;
this.EndAction = onEnd;
}
public bool ShutdownCalled { get; private set; } = false;
public bool DisposedCalled { get; private set; } = false;
public override void OnStart(Activity span)
{
this.StartAction?.Invoke(span);
}
public override void OnEnd(Activity span)
{
this.EndAction?.Invoke(span);
}
public override Task ShutdownAsync(CancellationToken cancellationToken)
{
this.ShutdownCalled = true;
#if NET452
return Task.FromResult(0);
#else
return Task.CompletedTask;
#endif
}
public void Dispose()
{
this.DisposedCalled = true;
}
}
}

View File

@ -27,6 +27,8 @@ using System.Threading.Tasks;
using OpenTelemetry.Exporter.Zipkin.Implementation;
using OpenTelemetry.Internal.Test;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace.Configuration;
using OpenTelemetry.Trace.Export;
using Xunit;
namespace OpenTelemetry.Exporter.Zipkin.Tests
@ -127,6 +129,45 @@ namespace OpenTelemetry.Exporter.Zipkin.Tests
Responses[requestId]);
}
[Fact]
public void UseZipkinActivityExporterWithCustomActivityProcessor()
{
const string ActivitySourceName = "zipkin.test";
Guid requestId = Guid.NewGuid();
TestActivityProcessor testActivityProcessor = new TestActivityProcessor();
bool startCalled = false;
bool endCalled = false;
testActivityProcessor.StartAction =
(a) =>
{
startCalled = true;
};
testActivityProcessor.EndAction =
(a) =>
{
endCalled = true;
};
var openTelemetrySdk = OpenTelemetrySdk.EnableOpenTelemetry(b => b
.AddActivitySource(ActivitySourceName)
.UseZipkinActivityExporter(
o =>
{
o.ServiceName = "test-zipkin";
o.Endpoint = new Uri($"http://{this.testServerHost}:{this.testServerPort}/api/v2/spans?requestId={requestId}");
}, p => p.AddProcessor((next) => testActivityProcessor)));
var source = new ActivitySource(ActivitySourceName);
var activity = source.StartActivity("Test Zipkin Activity");
activity?.Stop();
Assert.True(startCalled);
Assert.True(endCalled);
}
internal static Activity CreateTestActivity(
bool setAttributes = true,
Dictionary<string, object> additionalAttributes = null,