Skip to content

Commit

Permalink
Fix NPE from SimpleTracer#createBaggage
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
ttddyy authored and jonatan-ivanov committed Feb 28, 2023
1 parent 46838dc commit 2bf0453
Showing 1 changed file with 5 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
*/
package io.micrometer.tracing.test.simple;

import io.micrometer.common.lang.Nullable;
import io.micrometer.tracing.Baggage;
import io.micrometer.tracing.BaggageManager;
import io.micrometer.tracing.TraceContext;

import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -63,9 +61,10 @@ public Baggage getBaggage(TraceContext traceContext, String name) {
return baggageForName(traceContext, name);
}

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

@Override
Expand Down

0 comments on commit 2bf0453

Please sign in to comment.