zipkin2.codec.SpanBytesEncoder Maven / Gradle / Ivy
/*
* Copyright The OpenZipkin Authors
* SPDX-License-Identifier: Apache-2.0
*/
package zipkin2.codec;
import java.util.List;
import zipkin2.Span;
import zipkin2.internal.JsonCodec;
import zipkin2.internal.Proto3Codec;
import zipkin2.internal.V1JsonSpanWriter;
import zipkin2.internal.V1ThriftSpanWriter;
import zipkin2.internal.V2SpanWriter;
/** Limited interface needed by those writing span reporters */
@SuppressWarnings("ImmutableEnumChecker") // because span is immutable
public enum SpanBytesEncoder implements BytesEncoder {
/** Corresponds to the Zipkin v1 json format (with tags as binary annotations) */
JSON_V1 {
@Override
public Encoding encoding() {
return Encoding.JSON;
}
@Override
public int sizeInBytes(Span input) {
return new V1JsonSpanWriter().sizeInBytes(input);
}
@Override
public byte[] encode(Span span) {
return JsonCodec.write(new V1JsonSpanWriter(), span);
}
@Override
public byte[] encodeList(List spans) {
return JsonCodec.writeList(new V1JsonSpanWriter(), spans);
}
@Override
public int encodeList(List spans, byte[] out, int pos) {
return JsonCodec.writeList(new V1JsonSpanWriter(), spans, out, pos);
}
},
/** Corresponds to the Zipkin v1 thrift format */
THRIFT {
@Override
public Encoding encoding() {
return Encoding.THRIFT;
}
@Override
public int sizeInBytes(Span input) {
return new V1ThriftSpanWriter().sizeInBytes(input);
}
@Override
public byte[] encode(Span span) {
return new V1ThriftSpanWriter().write(span);
}
@Override
public byte[] encodeList(List spans) {
return new V1ThriftSpanWriter().writeList(spans);
}
@Override
public int encodeList(List spans, byte[] out, int pos) {
return new V1ThriftSpanWriter().writeList(spans, out, pos);
}
},
/** Corresponds to the Zipkin v2 json format */
JSON_V2 {
final V2SpanWriter writer = new V2SpanWriter();
@Override
public Encoding encoding() {
return Encoding.JSON;
}
@Override
public int sizeInBytes(Span input) {
return writer.sizeInBytes(input);
}
@Override
public byte[] encode(Span span) {
return JsonCodec.write(writer, span);
}
@Override
public byte[] encodeList(List spans) {
return JsonCodec.writeList(writer, spans);
}
@Override
public int encodeList(List spans, byte[] out, int pos) {
return JsonCodec.writeList(writer, spans, out, pos);
}
},
PROTO3 {
final Proto3Codec codec = new Proto3Codec();
@Override
public Encoding encoding() {
return Encoding.PROTO3;
}
@Override
public int sizeInBytes(Span input) {
return codec.sizeInBytes(input);
}
@Override
public byte[] encode(Span span) {
return codec.write(span);
}
@Override
public byte[] encodeList(List spans) {
return codec.writeList(spans);
}
@Override
public int encodeList(List spans, byte[] out, int pos) {
return codec.writeList(spans, out, pos);
}
};
/** Allows you to encode a list of spans onto a specific offset. For example, when nesting */
public abstract int encodeList(List spans, byte[] out, int pos);
}