-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Decreases switch statements and deprecates BytesMessageEncoder (#250)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
- Loading branch information
1 parent
b5fbad8
commit bb3f5b7
Showing
19 changed files
with
306 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
brave/src/main/java/zipkin2/reporter/brave/MutableSpanBytesEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright 2016-2024 The OpenZipkin 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. | ||
*/ | ||
package zipkin2.reporter.brave; | ||
|
||
import brave.Tag; | ||
import brave.Tags; | ||
import brave.handler.MutableSpan; | ||
import zipkin2.reporter.BytesEncoder; | ||
import zipkin2.reporter.Encoding; | ||
|
||
/** Includes built-in formats used in Zipkin. */ | ||
public enum MutableSpanBytesEncoder implements BytesEncoder<MutableSpan> { | ||
/** Corresponds to the Zipkin v2 json format */ | ||
JSON_V2 { | ||
@Override public Encoding encoding() { | ||
return Encoding.JSON; | ||
} | ||
|
||
@Override public int sizeInBytes(MutableSpan input) { | ||
return JsonV2Encoder.INSTANCE.sizeInBytes(input); | ||
} | ||
|
||
@Override public byte[] encode(MutableSpan input) { | ||
return JsonV2Encoder.INSTANCE.encode(input); | ||
} | ||
}; | ||
|
||
/** | ||
* Returns the default {@linkplain MutableSpan} encoder for given encoding. | ||
* | ||
* @throws UnsupportedOperationException if the encoding is not yet supported. | ||
* @since 3.3 | ||
*/ | ||
public static BytesEncoder<MutableSpan> forEncoding(Encoding encoding) { | ||
if (encoding == null) throw new NullPointerException("encoding == null"); | ||
switch (encoding) { | ||
case JSON: | ||
return JSON_V2; | ||
case PROTO3: | ||
throw new UnsupportedOperationException("PROTO3 is not yet a built-in encoder"); | ||
case THRIFT: | ||
throw new UnsupportedOperationException("THRIFT is not yet a built-in encoder"); | ||
default: // BUG: as encoding is an enum! | ||
throw new UnsupportedOperationException("BUG: " + encoding.name()); | ||
} | ||
} | ||
|
||
/** | ||
* Like {@linkplain #forEncoding(Encoding)}, except you can override the default throwable parser, | ||
* which is {@linkplain brave.Tags#ERROR}. | ||
* | ||
* @since 3.3 | ||
*/ | ||
public static BytesEncoder<MutableSpan> create(Encoding encoding, Tag<Throwable> errorTag) { | ||
if (encoding == null) throw new NullPointerException("encoding == null"); | ||
if (errorTag == null) throw new NullPointerException("errorTag == null"); | ||
if (errorTag == Tags.ERROR) return forEncoding(encoding); | ||
switch (encoding) { | ||
case JSON: | ||
return new JsonV2Encoder(errorTag); | ||
case PROTO3: | ||
throw new UnsupportedOperationException("PROTO3 is not yet a built-in encoder"); | ||
case THRIFT: | ||
throw new UnsupportedOperationException("THRIFT is not yet a built-in encoder"); | ||
default: // BUG: as encoding is an enum! | ||
throw new UnsupportedOperationException("BUG: " + encoding.name()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
brave/src/test/java/zipkin2/reporter/brave/MutableSpanBytesEncoderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2016-2024 The OpenZipkin 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. | ||
*/ | ||
package zipkin2.reporter.brave; | ||
|
||
import brave.Tag; | ||
import brave.Tags; | ||
import brave.handler.MutableSpan; | ||
import brave.propagation.TraceContext; | ||
import java.nio.charset.StandardCharsets; | ||
import org.junit.jupiter.api.Test; | ||
import zipkin2.reporter.BytesEncoder; | ||
import zipkin2.reporter.Encoding; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
class MutableSpanBytesEncoderTest { | ||
@Test void forEncoding() { | ||
assertThat(MutableSpanBytesEncoder.forEncoding(Encoding.JSON)) | ||
.isSameAs(MutableSpanBytesEncoder.JSON_V2); | ||
assertThatThrownBy(() -> MutableSpanBytesEncoder.forEncoding(Encoding.PROTO3)) | ||
.isInstanceOf(UnsupportedOperationException.class) | ||
.hasMessage("PROTO3 is not yet a built-in encoder"); | ||
assertThatThrownBy(() -> MutableSpanBytesEncoder.forEncoding(Encoding.THRIFT)) | ||
.isInstanceOf(UnsupportedOperationException.class) | ||
.hasMessage("THRIFT is not yet a built-in encoder"); | ||
} | ||
|
||
Tag<Throwable> iceCream = new Tag<>("exception") { | ||
@Override protected String parseValue(Throwable throwable, TraceContext traceContext) { | ||
return "ice cream"; | ||
} | ||
}; | ||
|
||
@Test void create_json() { | ||
// doesn't allocate on defaults | ||
assertThat(MutableSpanBytesEncoder.create(Encoding.JSON, Tags.ERROR)) | ||
.isSameAs(MutableSpanBytesEncoder.JSON_V2); | ||
|
||
MutableSpan span = new MutableSpan(); | ||
span.traceId("1"); | ||
span.id("2"); | ||
span.error(new OutOfMemoryError("out of memory")); | ||
|
||
// Default makes a tag named error | ||
assertThat(new String(MutableSpanBytesEncoder.JSON_V2.encode(span), StandardCharsets.UTF_8)) | ||
.isEqualTo("{\"traceId\":\"0000000000000001\",\"id\":\"0000000000000002\",\"tags\":{\"error\":\"out of memory\"}}"); | ||
|
||
|
||
// but, using create, you can override with something else. | ||
BytesEncoder<MutableSpan> iceCreamEncoder = | ||
MutableSpanBytesEncoder.create(Encoding.JSON, iceCream); | ||
assertThat(new String(iceCreamEncoder.encode(span), StandardCharsets.UTF_8)) | ||
.isEqualTo("{\"traceId\":\"0000000000000001\",\"id\":\"0000000000000002\",\"tags\":{\"exception\":\"ice cream\"}}"); | ||
} | ||
|
||
@Test void create_unsupported() { | ||
assertThatThrownBy(() -> MutableSpanBytesEncoder.create(Encoding.PROTO3, iceCream)) | ||
.isInstanceOf(UnsupportedOperationException.class) | ||
.hasMessage("PROTO3 is not yet a built-in encoder"); | ||
assertThatThrownBy(() -> MutableSpanBytesEncoder.create(Encoding.THRIFT, iceCream)) | ||
.isInstanceOf(UnsupportedOperationException.class) | ||
.hasMessage("THRIFT is not yet a built-in encoder"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.