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

Fix NPE from SimpleTracer#createBaggage #176

Merged
merged 1 commit into from
Feb 28, 2023

Conversation

ttddyy
Copy link
Contributor

@ttddyy ttddyy commented Feb 28, 2023

I was writing a unittest with SimpleTracer.

Following this baggage API documentation:

@Test
void foo() {
  Tracer tracer = new SimpleTracer();
  Span span = tracer.nextSpan().name("parent").start();

// Assuming that there's a span in scope...
  try (Tracer.SpanInScope ws = tracer.withSpan(span)) {

    // Not passing a TraceContext explicitly will bind the baggage to the
    // current TraceContext
    Baggage baggageForSpanInScopeOne = tracer.createBaggage("from_span_in_scope 1", "value 1");
    ...
  }
}

This gets NPE while creating a baggage.

It is because in SimpleBaggageManager

private SimpleBaggageInScope baggageForName(TraceContext traceContext, String name) {
return this.baggagesByContext.get(traceContext).stream().filter(bag -> name.equalsIgnoreCase(bag.name()))
.findFirst().orElse(null);
}

The this.baggagesByContext.get(traceContext) returns null. Then trying to stream() causes NPE.

This PR changes to handle the case when baggagesByContext map does not have a corresponding traceContext.
The callers of this method already handle null return value.

Copy link
Member

@shakuzen shakuzen left a comment

Choose a reason for hiding this comment

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

Looks good to me. Thank you. Could you rebase and target the 1.0.x branch?

@shakuzen shakuzen added this to the 1.0.3 milestone Feb 28, 2023
@ttddyy ttddyy changed the base branch from main to 1.0.x February 28, 2023 06:36
@ttddyy ttddyy changed the base branch from 1.0.x to main February 28, 2023 06:36
@ttddyy ttddyy changed the base branch from main to 1.0.x February 28, 2023 17:25
@ttddyy ttddyy changed the base branch from 1.0.x to main February 28, 2023 17:26
@ttddyy ttddyy changed the base branch from main to 1.0.x February 28, 2023 20:10
@ttddyy ttddyy changed the base branch from 1.0.x to main February 28, 2023 20:10
When creating a baggage with `SimpleTracer#createBaggage`, it fails
with NPE since the internal map `baggagesByContext` does not have a
corresponding value and returns `null`, but the code expects non-null
collection to be returned.

This commit updates the method to handle when no corresponding
baggage-in-scopes exist.

Signed-off-by: Tadaya Tsuyukubo <[email protected]>
@jonatan-ivanov jonatan-ivanov merged commit b586358 into micrometer-metrics:1.0.x Feb 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants