Skip to content

Commit 0a13b40

Browse files
authored
Fix NPE in APMTracer through RestController (elastic#128314) (elastic#128349)
Our APMTracer doesn't like nulls - this is a sensible thing, as APM in general does not allow nulls (it only allows a precise set of types). This PR changes the attribute to a sentinel "" in place of null values. It also makes a small change to APMTracer to give a better error message in case of null values in attributes.
1 parent 44a7553 commit 0a13b40

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

docs/changelog/128314.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 128314
2+
summary: Fix NPE in APMTracer through `RestController`
3+
area: Infra/REST API
4+
type: bug
5+
issues: []

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
@@ -308,6 +308,8 @@ private void setSpanAttributes(@Nullable Map<String, Object> spanAttributes, Spa
308308
spanBuilder.setAttribute(key, (Double) value);
309309
} else if (value instanceof Boolean) {
310310
spanBuilder.setAttribute(key, (Boolean) value);
311+
} else if (value == null) {
312+
throw new IllegalArgumentException("span attributes cannot have a null value");
311313
} else {
312314
throw new IllegalArgumentException(
313315
"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
@@ -60,6 +60,7 @@
6060
import java.util.List;
6161
import java.util.Locale;
6262
import java.util.Map;
63+
import java.util.Objects;
6364
import java.util.Set;
6465
import java.util.SortedMap;
6566
import java.util.TreeMap;
@@ -594,10 +595,10 @@ private void startTrace(ThreadContext threadContext, RestChannel channel, String
594595
final Map<String, Object> attributes = Maps.newMapWithExpectedSize(req.getHeaders().size() + 3);
595596
req.getHeaders().forEach((key, values) -> {
596597
final String lowerKey = key.toLowerCase(Locale.ROOT).replace('-', '_');
597-
attributes.put("http.request.headers." + lowerKey, values.size() == 1 ? values.get(0) : String.join("; ", values));
598+
attributes.put("http.request.headers." + lowerKey, values == null ? "" : String.join("; ", values));
598599
});
599-
attributes.put("http.method", method);
600-
attributes.put("http.url", req.uri());
600+
attributes.put("http.method", Objects.requireNonNullElse(method, "<unknown>"));
601+
attributes.put("http.url", Objects.requireNonNullElse(req.uri(), "<unknown>"));
601602
switch (req.getHttpRequest().protocolVersion()) {
602603
case HTTP_1_0 -> attributes.put("http.flavour", "1.0");
603604
case HTTP_1_1 -> attributes.put("http.flavour", "1.1");

0 commit comments

Comments
 (0)