-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Decreases switch statements and deprecates BytesMessageEncoder #250
Merged
+306
−108
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool thing is that I have a pending change to support this. However, anyone using
forEncoding
could update into it without prior knowledge.