This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 344
Simple layer on top of ByteBuffer for BINARY format. #276
Merged
carlosalberto
merged 6 commits into
opentracing:v0.32.0
from
carlosalberto:binary_format_proposal_simple
Oct 19, 2018
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a9577eb
Implement a simple layer on top of ByteBuffer for BINARY format.
carlosalberto 3dd5a61
Fix the license year header section.
carlosalberto eae9d20
Improve the BINARY format of MockTracer.
carlosalberto 7f83b59
Refactor the binary adapters code.
carlosalberto fd46b6b
Rename injectBuffer/extractBuffer to injectionBuffer/extractionBuffer.
carlosalberto 31e458b
Remove setInjectionBufferLength() for Binary.
carlosalberto 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
69 changes: 69 additions & 0 deletions
69
opentracing-api/src/main/java/io/opentracing/propagation/Binary.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,69 @@ | ||
/* | ||
* Copyright 2016-2018 The OpenTracing 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 io.opentracing.propagation; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
/** | ||
* Binary is an interface defining the required operations for a binary carrier for | ||
* Tracer.inject() and Tracer.extract(). Binary can be defined either as inbound (extraction) | ||
* or outbound (injection). | ||
* | ||
* When Binary is defined as inbound, extractBuffer() will be called to retrieve the ByteBuffer | ||
* containing the data used for SpanContext extraction. | ||
* | ||
* When Binary is defined as outbound, setInjectBufferLength() will be called in order to hint | ||
* the required buffer length to inject the SpanContext, and injectBuffer() will be called | ||
* afterwards to retrieve the actual ByteBuffer used for the SpanContext injection. | ||
* | ||
* @see Format.Builtin#BINARY | ||
* @see io.opentracing.Tracer#inject(SpanContext, Format, Object) | ||
* @see io.opentracing.Tracer#extract(Format, Object) | ||
*/ | ||
public interface Binary { | ||
/** | ||
* Hints the buffer length required for SpanContext injection. | ||
* The user may use this to allocate the ByteBuffer | ||
* or resize an existing one. This method is always called | ||
* before injectBuffer(). | ||
* | ||
* It is an error to call this method when Binary is used | ||
* for SpanContext extraction. It is also an error to call | ||
* this method more than one time. | ||
* | ||
* @param length The buffer length required for SpanContext injection. | ||
* It needs to be larger than zero. | ||
*/ | ||
void setInjectBufferLength(int length); | ||
|
||
/** | ||
* Gets the buffer containing the data used for SpanContext injection. | ||
* | ||
* It is an error to call this method when Binary is used | ||
* for SpanContext extraction. | ||
* | ||
* @return The buffer used for SpanContext injection. | ||
*/ | ||
ByteBuffer injectBuffer(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe |
||
|
||
/** | ||
* Gets the buffer containing the data used for SpanContext extraction. | ||
* | ||
* It is an error to call this method when Binary is used | ||
* for SpanContext injection. | ||
* | ||
* @return The buffer used for SpanContext extraction. | ||
*/ | ||
ByteBuffer extractBuffer(); | ||
} |
102 changes: 102 additions & 0 deletions
102
opentracing-api/src/main/java/io/opentracing/propagation/BinaryAdapters.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,102 @@ | ||
/* | ||
* Copyright 2016-2018 The OpenTracing 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 io.opentracing.propagation; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
public final class BinaryAdapters { | ||
|
||
private BinaryAdapters() {} | ||
|
||
/** | ||
* Creates an inbound Binary instance used for extraction with the | ||
* specified ByteBuffer as input. | ||
* | ||
* @param buffer The ByteBuffer used as input. | ||
* | ||
* @return The new Binary carrier used for extraction. | ||
*/ | ||
public static Binary extractionCarrier(ByteBuffer buffer) { | ||
if (buffer == null) { | ||
throw new NullPointerException(); | ||
} | ||
|
||
return new BinaryExtractAdapter(buffer); | ||
} | ||
|
||
/** | ||
* Creates an outbound Binary instance used for injection, | ||
* allocating a new ByteBuffer instance when | ||
* setInjectBufferLength() is called. The ByteBuffer can | ||
* be later retrieved using injectBuffer(). | ||
* | ||
* @return The new Binary carrier used for injection. | ||
*/ | ||
public static Binary injectionCarrier() { | ||
return new BinaryInjectAdapter(); | ||
} | ||
|
||
static class BinaryExtractAdapter implements Binary { | ||
ByteBuffer buffer; | ||
|
||
public BinaryExtractAdapter(ByteBuffer buffer) { | ||
this.buffer = buffer; | ||
} | ||
|
||
@Override | ||
public void setInjectBufferLength(int length) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public ByteBuffer injectBuffer() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public ByteBuffer extractBuffer() { | ||
return buffer; | ||
} | ||
} | ||
|
||
static class BinaryInjectAdapter implements Binary { | ||
ByteBuffer buffer; | ||
|
||
@Override | ||
public void setInjectBufferLength(int length) { | ||
if (length < 1) { | ||
throw new IllegalArgumentException("length needs to be larger than 0"); | ||
} | ||
if (buffer != null) { | ||
throw new IllegalStateException("injectBuffer() length has already been set."); | ||
} | ||
|
||
buffer = ByteBuffer.allocate(length); | ||
} | ||
|
||
@Override | ||
public ByteBuffer injectBuffer() { | ||
if (buffer == null) { | ||
throw new IllegalStateException("setInjectBufferLength() needs to be called before injectBuffer()"); | ||
} | ||
|
||
return buffer; | ||
} | ||
|
||
@Override | ||
public ByteBuffer extractBuffer() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} | ||
} |
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
69 changes: 69 additions & 0 deletions
69
opentracing-api/src/test/java/io/opentracing/propagation/BinaryAdaptersTest.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,69 @@ | ||
/* | ||
* Copyright 2016-2018 The OpenTracing 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 io.opentracing.propagation; | ||
|
||
import java.nio.ByteBuffer; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertArrayEquals; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
public class BinaryAdaptersTest { | ||
|
||
@Test | ||
public void testExtractBinary() { | ||
ByteBuffer buff = ByteBuffer.wrap(new byte[0]); | ||
Binary binary = BinaryAdapters.extractionCarrier(buff); | ||
assertEquals(buff, binary.extractBuffer()); | ||
} | ||
|
||
@Test(expected = NullPointerException.class) | ||
public void testExtractBinaryNull() { | ||
BinaryAdapters.extractionCarrier(null); | ||
} | ||
|
||
@Test(expected = UnsupportedOperationException.class) | ||
public void testExtractBinaryInjectBuffer() { | ||
Binary binary = BinaryAdapters.extractionCarrier(ByteBuffer.allocate(1)); | ||
binary.injectBuffer(); | ||
} | ||
|
||
@Test | ||
public void testInjectBinary() { | ||
Binary binary = BinaryAdapters.injectionCarrier(); | ||
binary.setInjectBufferLength(1); | ||
assertNotNull(binary.injectBuffer()); | ||
assertEquals(0, binary.injectBuffer().position()); | ||
assertEquals(1, binary.injectBuffer().capacity()); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testInjectBinaryInvalidLength() { | ||
Binary binary = BinaryAdapters.injectionCarrier(); | ||
binary.setInjectBufferLength(0); | ||
} | ||
|
||
@Test(expected = IllegalStateException.class) | ||
public void testInjectBinaryNoLength() { | ||
Binary binary = BinaryAdapters.injectionCarrier(); | ||
binary.injectBuffer(); | ||
} | ||
|
||
@Test(expected = UnsupportedOperationException.class) | ||
public void testInjectBinaryExtractBuffer() { | ||
Binary binary = BinaryAdapters.injectionCarrier(); | ||
binary.extractBuffer(); | ||
} | ||
} |
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.
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.
There is no setInjectBufferLenght() method defined in the interface, so it seems to me the documentation does not match the implementation here.
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.
Oh, definitely. Will update, thanks for the observation.