Skip to content

Commit

Permalink
brave: allows AsyncZipkinSpanHandler to be build with custom formats
Browse files Browse the repository at this point in the history
This is needed for stackdriver and any future OTLP work, as well as
developing a standard proto encoder.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
  • Loading branch information
Adrian Cole committed Jan 9, 2024
1 parent af70e4c commit 96ff702
Showing 1 changed file with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.io.Flushable;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import zipkin2.reporter.BytesEncoder;
import zipkin2.reporter.Encoding;
import zipkin2.reporter.Reporter;
import zipkin2.reporter.ReporterMetrics;
import zipkin2.reporter.Sender;
Expand Down Expand Up @@ -69,15 +71,18 @@ public Builder toBuilder() {
/** @since 2.14 */
public static final class Builder extends ZipkinSpanHandler.Builder {
final AsyncReporter.Builder delegate;
final Encoding encoding;

Builder(AsyncZipkinSpanHandler handler) {
this.delegate = ((AsyncReporter<MutableSpan>) handler.spanReporter).toBuilder();
this.encoding = handler.encoding;
this.alwaysReportSpans = handler.alwaysReportSpans;
this.errorTag = handler.errorTag;
}

Builder(Sender sender) {
this.delegate = AsyncReporter.newBuilder(sender);
this.encoding = sender.encoding();
}

/**
Expand Down Expand Up @@ -151,18 +156,42 @@ public Builder queuedMaxBytes(int queuedMaxBytes) {
return (Builder) super.alwaysReportSpans(alwaysReportSpans);
}

/**
* Builds an async span handler that encodes zipkin spans according to the sender's encoding.
*/
// AsyncZipkinSpanHandler not SpanHandler, so that Flushable and Closeable are accessible
public AsyncZipkinSpanHandler build() {
return new AsyncZipkinSpanHandler(this);
switch (encoding) {
case JSON:
return build(new JsonV2Encoder(errorTag));
default:
throw new UnsupportedOperationException(encoding.name());
}
}

/**
* Builds an async span handler that encodes zipkin spans according to the encoder.
*
* <p>Note: The input encoder must use the same error tag implementation as configured by
* {@link #errorTag(Tag)}.
*
* @since 3.1
*/
// AsyncZipkinSpanHandler not SpanHandler, so that Flushable and Closeable are accessible
public AsyncZipkinSpanHandler build(BytesEncoder<MutableSpan> encoder) {
if (encoder == null) throw new NullPointerException("encoder == null");
return new AsyncZipkinSpanHandler(delegate.build(encoder), this);
}
}

final Reporter<MutableSpan> spanReporter;
final Encoding encoding;
final Tag<Throwable> errorTag; // for toBuilder()
final boolean alwaysReportSpans;

AsyncZipkinSpanHandler(Builder builder) {
this.spanReporter = builder.delegate.build(new JsonV2Encoder(builder.errorTag));
AsyncZipkinSpanHandler(AsyncReporter<MutableSpan> spanReporter, Builder builder) {
this.spanReporter = spanReporter;
this.encoding = builder.encoding;
this.errorTag = builder.errorTag;
this.alwaysReportSpans = builder.alwaysReportSpans;
}
Expand Down

0 comments on commit 96ff702

Please sign in to comment.