From 2bf04533da9bbde6e0b81d3477ac08839904a7f2 Mon Sep 17 00:00:00 2001 From: Tadaya Tsuyukubo Date: Mon, 27 Feb 2023 20:22:44 -0800 Subject: [PATCH] Fix NPE from SimpleTracer#createBaggage 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 --- .../tracing/test/simple/SimpleBaggageManager.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/micrometer-tracing-tests/micrometer-tracing-test/src/main/java/io/micrometer/tracing/test/simple/SimpleBaggageManager.java b/micrometer-tracing-tests/micrometer-tracing-test/src/main/java/io/micrometer/tracing/test/simple/SimpleBaggageManager.java index 50dd7fe1..ff39416f 100644 --- a/micrometer-tracing-tests/micrometer-tracing-test/src/main/java/io/micrometer/tracing/test/simple/SimpleBaggageManager.java +++ b/micrometer-tracing-tests/micrometer-tracing-test/src/main/java/io/micrometer/tracing/test/simple/SimpleBaggageManager.java @@ -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; @@ -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