Skip to content

Commit

Permalink
Multiple processors on tracer: configuration (#286)
Browse files Browse the repository at this point in the history
* test

* Multiple processor chains

* Update src/OpenTelemetry/Trace/Export/Internal/NoopSpanProcessor.cs

Co-Authored-By: Sergey Kanzhelev <S.Kanzhelev@live.com>

* clean up and make tests pass

* more clean up

* Tests for multiple processors

* readme and rebase
  • Loading branch information
Liudmila Molkova authored Oct 22, 2019
1 parent 66a65cb commit c6c87ab
Show file tree
Hide file tree
Showing 35 changed files with 1,005 additions and 244 deletions.
40 changes: 34 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,8 @@ var jaegerOptions = new JaegerExporterOptions()
AgentHost = <jaeger server>
};

using (var tracerFactory = TracerFactory.Create(
builder => builder.SetExporter(new JaegerTraceExporter(jaegerOptions))))
using (var tracerFactory = TracerFactory.Create(builder => builder
.AddProcessorPipeline(c => c.SetExporter(new JaegerTraceExporter(jaegerOptions)))))
{
var tracer = tracerFactory.GetTracer("jaeger-test");
var span = tracer
Expand Down Expand Up @@ -435,11 +435,38 @@ There is also a constructor for specifying path to the service account credentia
4. Instantiate a new instance of `StackdriverExporter` with your Google Cloud's ProjectId
5. See [sample][stackdriver-sample] for example use.

### Advanced configuration

You may want to filter on enrich spans and send them to multiple destinations (e.g. for debugging or telemetry self-diagnostics purposes).
You may configure multiple processing pipelines for each destination like shown in below example.

In this example

1. First pipeline sends all sampled in spans to Zipkin
2. Second pipeline sends spans to ApplicationInsights, but filters them first with custom built `FilteringSpanProcessor`
3. Third pipeline adds custom `DebuggingSpanProcessor` that simply logs all calls to debug output

```csharp
using (var tracerFactory = TracerFactory.Create(builder => builder
.UseZipkin(o =>
{
o.ServiceName = "test-zipkin";
o.Endpoint = new Uri(zipkinUri);
})
.UseApplicationInsights(
o => o.InstrumentationKey = "your-instrumentation-key",
p => p.AddProcessor(nextProcessor => new FilteringSpanProcessor(nextProcessor)))
.AddProcessorPipeline(pipelineBuilder => pipelineBuilder.AddProcessor(_ => new DebuggingSpanProcessor()))))
{
// ...
}
```

#### Traces

```csharp
using (var tracerFactory = TracerFactory.Create(
builder => builder.SetExporter(new StackdriverTraceExporter("YOUR-GOOGLE-PROJECT-ID"))))
using (var tracerFactory = TracerFactory.Create(builder => builder
.AddProcessorPipeline(c => c.SetExporter(new StackdriverTraceExporter("YOUR-GOOGLE-PROJECT-ID")))))
{
var tracer = tracerFactory.GetTracer("stackdriver-test");
var span = tracer
Expand Down Expand Up @@ -513,12 +540,13 @@ class MyExporter : SpanExporter
}
```

Users may configure the exporter similarly to other exporters. You cay also provide additional methods to simplify configuration similarly to `UseZipkin` extension method.
Users may configure the exporter similarly to other exporters.
You should also provide additional methods to simplify configuration similarly to `UseZipkin` extension method.

```csharp
var exporter = new MyExporter();
using (var tracerFactory = TracerFactory.Create(
builder => builder.SetExporter(new MyExporter())))
builder => builder.AddProcessorPipeline(b => b.SetExporter(new MyExporter())))
{
// ...
}
Expand Down
6 changes: 2 additions & 4 deletions benchmarks/OpenTelemetrySdkBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ public class OpenTelemetrySdkBenchmarks
public OpenTelemetrySdkBenchmarks()
{
this.alwaysSampleTracer = TracerFactory
.Create(b => b.SetProcessor(_ => new NoopProcessor()).SetSampler(
Samplers.AlwaysSample))
.Create(b => b.SetSampler(Samplers.AlwaysSample))
.GetTracer(null);
this.neverSampleTracer = TracerFactory
.Create(b => b.SetProcessor(_ => new NoopProcessor()).SetSampler(
Samplers.NeverSample))
.Create(b => b.SetSampler(Samplers.NeverSample))
.GetTracer(null);
this.noopTracer = TracerFactoryBase.Default.GetTracer(null);
}
Expand Down
3 changes: 2 additions & 1 deletion samples/Exporters/TestJaeger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ internal static object Run(string host, int port)
};

// Create a tracer.
using (var tracerFactory = TracerFactory.Create(builder => builder.SetExporter(new JaegerTraceExporter(jaegerOptions))))
using (var tracerFactory = TracerFactory.Create(builder => builder
.AddProcessorPipeline(c => c.SetExporter(new JaegerTraceExporter(jaegerOptions)))))
{
var tracer = tracerFactory.GetTracer("jaeger-test");

Expand Down
2 changes: 1 addition & 1 deletion samples/Exporters/TestLightstep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal static object Run(string accessToken)
});

// Create a tracer.
using (var tracerFactory = TracerFactory.Create(builder => builder.SetExporter(exporter)))
using (var tracerFactory = TracerFactory.Create(builder => builder.AddProcessorPipeline(c => c.SetExporter(exporter))))
{
var tracer = tracerFactory.GetTracer("lightstep-test");
using (tracer.WithSpan(tracer.StartSpan("Main")))
Expand Down
2 changes: 1 addition & 1 deletion samples/Exporters/TestStackdriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ internal static object Run(string projectId)
Stats.ViewManager);
metricExporter.Start();

using (var tracerFactory = TracerFactory.Create(builder => builder.SetExporter(spanExporter)))
using (var tracerFactory = TracerFactory.Create(builder => builder.AddProcessorPipeline(c => c.SetExporter(spanExporter))))
{
var tracer = tracerFactory.GetTracer("stackdriver-test");

Expand Down
2 changes: 1 addition & 1 deletion samples/Exporters/TestZipkin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System;
using System.Collections.Generic;
using System.Threading;
using OpenTelemetry.Exporter.Zipkin;
using OpenTelemetry.Trace;
using OpenTelemetry.Trace.Configuration;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System;
using Microsoft.ApplicationInsights.Extensibility;
using OpenTelemetry.Exporter.ApplicationInsights;
using OpenTelemetry.Trace.Export;

namespace OpenTelemetry.Trace.Configuration
{
Expand All @@ -36,7 +37,36 @@ public static TracerBuilder UseApplicationInsights(this TracerBuilder builder, A

var configuration = new TelemetryConfiguration();
configure(configuration);
return builder.SetExporter(new ApplicationInsightsTraceExporter(configuration));
return builder.AddProcessorPipeline(b => b
.SetExporter(new ApplicationInsightsTraceExporter(configuration))
.SetExportingProcessor(e => new BatchingSpanProcessor(e)));
}

public static TracerBuilder UseApplicationInsights(this TracerBuilder builder, Action<TelemetryConfiguration> applicationInsightsConfigure, Action<
SpanProcessorPipelineBuilder> processorConfigure)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

if (applicationInsightsConfigure == null)
{
throw new ArgumentNullException(nameof(applicationInsightsConfigure));
}

if (processorConfigure == null)
{
throw new ArgumentNullException(nameof(processorConfigure));
}

var options = new TelemetryConfiguration();
applicationInsightsConfigure(options);
return builder.AddProcessorPipeline(b =>
{
b.SetExporter(new ApplicationInsightsTraceExporter(options));
processorConfigure.Invoke(b);
});
}
}
}
32 changes: 31 additions & 1 deletion src/OpenTelemetry.Exporter.Zipkin/TracerBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System;
using OpenTelemetry.Exporter.Zipkin;
using OpenTelemetry.Trace.Export;

namespace OpenTelemetry.Trace.Configuration
{
Expand All @@ -35,7 +36,36 @@ public static TracerBuilder UseZipkin(this TracerBuilder builder, Action<ZipkinT

var options = new ZipkinTraceExporterOptions();
configure(options);
return builder.SetExporter(new ZipkinTraceExporter(options));
return builder.AddProcessorPipeline(b => b
.SetExporter(new ZipkinTraceExporter(options))
.SetExportingProcessor(e => new BatchingSpanProcessor(e)));
}

public static TracerBuilder UseZipkin(this TracerBuilder builder, Action<ZipkinTraceExporterOptions> zipkinConfigure, Action<
SpanProcessorPipelineBuilder> processorConfigure)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

if (zipkinConfigure == null)
{
throw new ArgumentNullException(nameof(zipkinConfigure));
}

if (processorConfigure == null)
{
throw new ArgumentNullException(nameof(processorConfigure));
}

var options = new ZipkinTraceExporterOptions();
zipkinConfigure(options);
return builder.AddProcessorPipeline(b =>
{
b.SetExporter(new ZipkinTraceExporter(options));
processorConfigure.Invoke(b);
});
}
}
}
77 changes: 0 additions & 77 deletions src/OpenTelemetry/Implementation/OpenTelemetryEventSource.cs

This file was deleted.

11 changes: 6 additions & 5 deletions src/OpenTelemetry/Internal/OpenTelemetrySdkEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System;
using System.Diagnostics.Tracing;
using System.Globalization;
Expand All @@ -30,11 +31,11 @@ internal class OpenTelemetrySdkEventSource : EventSource
public static OpenTelemetrySdkEventSource Log = new OpenTelemetrySdkEventSource();

[NonEvent]
public void SpanProcessorException(Exception ex)
public void SpanProcessorException(string evnt, Exception ex)
{
if (this.IsEnabled(EventLevel.Warning, EventKeywords.All))
{
this.SpanProcessorException(ToInvariantString(ex));
this.SpanProcessorException(evnt, ToInvariantString(ex));
}
}

Expand All @@ -56,10 +57,10 @@ public void ExporterErrorResult(SpanExporter.ExportResult exportResult)
this.WriteEvent(3, exportResult.ToString());
}

[Event(4, Message = "Unknown error in SpanProcessor: '{0}'.", Level = EventLevel.Warning)]
public void SpanProcessorException(string ex)
[Event(4, Message = "Unknown error in SpanProcessor event '{0}': '{1}'.", Level = EventLevel.Warning)]
public void SpanProcessorException(string evnt, string ex)
{
this.WriteEvent(4, ex);
this.WriteEvent(4, evnt, ex);
}

[Event(5, Message = "Calling '{0}' on ended span.", Level = EventLevel.Warning)]
Expand Down
4 changes: 4 additions & 0 deletions src/OpenTelemetry/OpenTelemetry.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@
<PackageReference Include="System.Collections.Immutable" Version="1.4.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="Implementation\" />
</ItemGroup>

</Project>
Loading

0 comments on commit c6c87ab

Please sign in to comment.