Skip to content

Commit

Permalink
Migrate OtlpExporter to BatchExportActivityProcessor (#1104)
Browse files Browse the repository at this point in the history
* Changing OtlpExporter

* updating changelog with last PR number

* solving null reference while converting activitylinks

Co-authored-by: Mikel Blanchard <[email protected]>
  • Loading branch information
eddynaka and CodeBlanch authored Aug 20, 2020
1 parent 4690783 commit a969c8f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 27 deletions.
6 changes: 4 additions & 2 deletions src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

* Allow configurable gRPC channel options
([#1033](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1033))

* Renamed extension method from `UseOtlpExporter` to `AddOtlpExporter`.
* Renamed extension method from `UseOtlpExporter` to `AddOtlpExporter`
([#1066](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1066))
* Changed `OtlpExporter` to use `BatchExportActivityProcessor` by default
([#1104](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1104))

## 0.4.0-beta.2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,15 @@ private static OtlpTrace.Span.Types.Link ToOtlpLink(ActivityLink activityLink)
SpanId = ByteString.CopyFrom(spanIdBytes.ToArray()),
};

foreach (var attribute in from tag in activityLink.Tags
let attribute = ToOtlpAttributes(tag)
where attribute != null && attribute.Any()
select attribute)
if (activityLink.Tags != null)
{
otlpLink.Attributes.AddRange(attribute);
foreach (var attribute in from tag in activityLink.Tags
let attribute = ToOtlpAttributes(tag)
where attribute != null && attribute.Any()
select attribute)
{
otlpLink.Attributes.AddRange(attribute);
}
}

return otlpLink;
Expand Down
32 changes: 13 additions & 19 deletions src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@

using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

using Grpc.Core;

using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
using OpenTelemetry.Trace;

using OtlpCollector = Opentelemetry.Proto.Collector.Trace.V1;

namespace OpenTelemetry.Exporter.OpenTelemetryProtocol
Expand All @@ -32,7 +27,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 : ActivityExporterSync
{
private readonly Channel channel;
private readonly OtlpCollector.TraceService.TraceServiceClient traceClient;
Expand All @@ -50,31 +45,30 @@ public OtlpExporter(OtlpExporterOptions options)
}

/// <inheritdoc/>
public override async Task<ExportResult> ExportAsync(
IEnumerable<Activity> activityBatch,
CancellationToken cancellationToken)
public override ExportResultSync Export(in Batch<Activity> activityBatch)
{
var exporterRequest = new OtlpCollector.ExportTraceServiceRequest();
exporterRequest.ResourceSpans.AddRange(activityBatch.ToOtlpResourceSpans());

var activities = new List<Activity>();
foreach (var activity in activityBatch)
{
activities.Add(activity);
}

exporterRequest.ResourceSpans.AddRange(activities.ToOtlpResourceSpans());

try
{
await this.traceClient.ExportAsync(exporterRequest, headers: this.headers, cancellationToken: cancellationToken);
this.traceClient.Export(exporterRequest, headers: this.headers);
}
catch (RpcException ex)
{
OpenTelemetryProtocolExporterEventSource.Log.FailedToReachCollector(ex);

return ExportResult.FailedRetryable;
return ExportResultSync.Failure;
}

return ExportResult.Success;
}

/// <inheritdoc/>
public override async Task ShutdownAsync(CancellationToken cancellationToken)
{
await this.channel.ShutdownAsync().ConfigureAwait(false);
return ExportResultSync.Success;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,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 SimpleActivityProcessor(otlpExporter));
return builder.AddProcessor(new BatchExportActivityProcessor(otlpExporter));
}
}
}

0 comments on commit a969c8f

Please sign in to comment.