Skip to content
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

SpanContext hides the TraceId/SpanId implementations #1594

Merged
merged 15 commits into from
Sep 3, 2020

Conversation

jkwatson
Copy link
Contributor

@jkwatson jkwatson commented Aug 26, 2020

This removes the usage of wrapper classes for the ids.
Changes the SpanContext creation methods to accept CharSequences and an offset for the ids.
Changes the IdGenerator interface to generate String ids.

Relevant issue: #1314
Extensive discussion about this took place in this draft PR: #1374

@jkwatson
Copy link
Contributor Author

I apologize for the size of this PR. Most of it is mechanical changes in types, so hopefully review will go quickly.

@codecov
Copy link

codecov bot commented Aug 26, 2020

Codecov Report

Merging #1594 into master will decrease coverage by 0.03%.
The diff coverage is 93.61%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #1594      +/-   ##
============================================
- Coverage     91.16%   91.12%   -0.04%     
- Complexity      991      995       +4     
============================================
  Files           117      117              
  Lines          3668     3664       -4     
  Branches        340      345       +5     
============================================
- Hits           3344     3339       -5     
- Misses          218      219       +1     
  Partials        106      106              
Impacted Files Coverage Δ Complexity Δ
...ntelemetry/sdk/trace/RecordEventsReadableSpan.java 81.89% <0.00%> (ø) 78.00 <0.00> (ø)
.../main/java/io/opentelemetry/sdk/trace/Sampler.java 100.00% <ø> (ø) 0.00 <0.00> (ø)
...java/io/opentelemetry/sdk/trace/data/SpanData.java 100.00% <ø> (ø) 0.00 <0.00> (ø)
...ions/trace/jaeger/sampler/JaegerRemoteSampler.java 68.62% <ø> (ø) 9.00 <0.00> (ø)
...ions/trace/jaeger/sampler/PerOperationSampler.java 0.00% <ø> (ø) 0.00 <0.00> (ø)
...ions/trace/jaeger/sampler/RateLimitingSampler.java 70.83% <ø> (ø) 7.00 <0.00> (ø)
.../main/java/io/opentelemetry/trace/SpanContext.java 87.50% <75.00%> (-12.50%) 11.00 <8.00> (+3.00) ⬇️
...java/io/opentelemetry/trace/BigendianEncoding.java 94.28% <93.33%> (-0.26%) 31.00 <13.00> (+13.00) ⬇️
.../src/main/java/io/opentelemetry/trace/TraceId.java 96.29% <95.65%> (+1.05%) 14.00 <12.00> (-9.00) ⬆️
...i/src/main/java/io/opentelemetry/trace/SpanId.java 94.11% <100.00%> (-5.89%) 12.00 <12.00> (-8.00)
... and 8 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 225acce...85aafd2. Read the comment docs.

Copy link
Contributor

@anuraaga anuraaga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@jkwatson
Copy link
Contributor Author

@anuraaga I think I got all of your comments addressed. Thanks so much for taking the time to give it a thorough review!

Copy link
Contributor

@anuraaga anuraaga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just nits, thanks!

api/src/main/java/io/opentelemetry/trace/SpanId.java Outdated Show resolved Hide resolved
for (int i = 0; i < value.length(); i++) {
char b = value.charAt(i);
// 48..57 && 97..102 are valid
if ((b < 48 || b > 57) && (b < 97 || b > 102)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure they're the same, and maybe it's just me but I find writing the same code as the comment would be more readable

Suggested change
if ((b < 48 || b > 57) && (b < 97 || b > 102)) {
if (!(b >= 48 && b <= 57) || !(b >= 97 && b <= 102)) {

Copy link
Contributor Author

@jkwatson jkwatson Aug 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heh. I agree with you. However, what you pasted isn't correct (IDEA says it's always true!). I'll figure out how to make it what you intended. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check out the update. I think it's now much easier to understand.

api/src/test/java/io/opentelemetry/trace/TraceIdTest.java Outdated Show resolved Hide resolved
@jkwatson
Copy link
Contributor Author

ok, all nits have been de-nitted!

Copy link
Contributor

@anuraaga anuraaga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks

@jkwatson
Copy link
Contributor Author

I'm going to hold off a few days to merge this, both to delay to after the 0.8.0 release, and to give others (@carlosalberto and @bogdandrutu) to chime in with any issue they might have, as this is a significant API change.

@jkwatson jkwatson added the API API related issues label Aug 29, 2020
Comment on lines 88 to 90
abstract String getTraceId();

abstract String getSpanId();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return CharSequence instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we discussed this, and decided to keep it as String for now. We can always open it up to CharSequence later, since it's a broader interface, I think.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can change return types that easily. If clients expect String they will break on CharSequence. You can widen input, not output

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, but this isn't a public method!

*/
public static SpanId getInvalid() {
return INVALID;
public static int getBase16Length() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hex? If you used hex in SpanContext method names.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes! should fix this. good catch.

Copy link
Contributor Author

@jkwatson jkwatson Sep 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return new String(result);
}

private static char[] getBuffer() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/getBuffer/getTemporaryBuffer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

*/
public void copyLowerBase16To(char[] dest, int destOffset) {
BigendianEncoding.longToBase16String(id, dest, destOffset);
public static byte[] bytesFromLowerBase16(String src, int srcOffset) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, use Hex if you decided to use that in the SC

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -59,8 +60,13 @@ public static SpanContext getInvalid() {
* @since 0.1.0
*/
public static SpanContext create(
TraceId traceId, SpanId spanId, TraceFlags traceFlags, TraceState traceState) {
return new AutoValue_SpanContext(traceId, spanId, traceFlags, traceState, /* remote=*/ false);
String traceId, String spanId, TraceFlags traceFlags, TraceState traceState) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name the params traceIdHex, spanIdHex

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

jwatson and others added 10 commits September 1, 2020 14:23
� This is the 1st commit message:

WIP on converting to String-based SpanContext

don't hand out the byte arrays publicly, but require making copies

make sure to hand out fresh invalid byte arrays.

Use strings for span and trace ids.

Switch over to CharSequence instead of String for the ids

Fix a couple of places that were casting to String

Add some simple wrappers for the generated longs to save converting until the last moment to the character-based representation.

introduce a reusable threadlocal char buffer for generating random ids.

update for changes from upstream

Change the SpanContext to store the ids as Strings internally
Change the id access methods on SpanContext to be clearly labeled as the base16 representations
Add a new create method that allows specifying offsets for traceId and spanId CharSequences

Provide an option for creating a SpanContext from longs or Strings, optionally.

fix a typo

update from upstream

� The commit message #2 will be skipped:

� don't hand out the byte arrays publicly, but require making copies
…tensions/trace/propagation/B3PropagatorInjectorSingleHeader.java

Co-authored-by: Anuraag Agrawal <[email protected]>
@carlosalberto
Copy link
Contributor

Amazing job @jkwatson sorry for the late review!

@jkwatson jkwatson merged commit 485cc52 into open-telemetry:master Sep 3, 2020
@jkwatson jkwatson deleted the string_ids branch September 3, 2020 17:03
@anuraaga
Copy link
Contributor

anuraaga commented Sep 3, 2020

Awesome!

tylerbenson added a commit to tylerbenson/opentelemetry-java that referenced this pull request Oct 30, 2020
Having abstract methods with default visibility makes the class not able to be subclassed by other packages.
Changing to protected to maintain the intended restrictions from open-telemetry#1594, but allowing it to be subclassed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API API related issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants