Skip to content

Commit 95ccfd9

Browse files
committed
Protect against NPE on attributes; better error message in case of nulls
1 parent 445c3ea commit 95ccfd9

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

modules/apm/src/main/java/org/elasticsearch/telemetry/apm/internal/tracing/APMTracer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,8 @@ private void setSpanAttributes(@Nullable Map<String, Object> spanAttributes, Spa
304304
spanBuilder.setAttribute(key, (Double) value);
305305
} else if (value instanceof Boolean) {
306306
spanBuilder.setAttribute(key, (Boolean) value);
307+
} else if (value == null) {
308+
throw new IllegalArgumentException("span attributes cannot have a null value");
307309
} else {
308310
throw new IllegalArgumentException(
309311
"span attributes do not support value type of [" + value.getClass().getCanonicalName() + "]"

server/src/main/java/org/elasticsearch/rest/RestController.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import java.util.List;
6262
import java.util.Locale;
6363
import java.util.Map;
64+
import java.util.Objects;
6465
import java.util.Set;
6566
import java.util.SortedMap;
6667
import java.util.TreeMap;
@@ -560,10 +561,10 @@ private void startTrace(ThreadContext threadContext, RestChannel channel, String
560561
final Map<String, Object> attributes = Maps.newMapWithExpectedSize(req.getHeaders().size() + 3);
561562
req.getHeaders().forEach((key, values) -> {
562563
final String lowerKey = key.toLowerCase(Locale.ROOT).replace('-', '_');
563-
attributes.put("http.request.headers." + lowerKey, values.size() == 1 ? values.get(0) : String.join("; ", values));
564+
attributes.put("http.request.headers." + lowerKey, values == null ? "" : String.join("; ", values));
564565
});
565-
attributes.put("http.method", method);
566-
attributes.put("http.url", req.uri());
566+
attributes.put("http.method", Objects.requireNonNullElse(method, "<unknown>"));
567+
attributes.put("http.url", Objects.requireNonNullElse(req.uri(), "<unknown>"));
567568
switch (req.getHttpRequest().protocolVersion()) {
568569
case HTTP_1_0 -> attributes.put("http.flavour", "1.0");
569570
case HTTP_1_1 -> attributes.put("http.flavour", "1.1");

0 commit comments

Comments
 (0)