[tracing] Extract parent OpenTelemetry span safely
Turns out the `fields` method returns the names of fields, but it's
not mandatory that those fields are present. To cope with this, we
blindly assume that extracting the span's parent will work, and catch
the exception in the case where the parent is missing.
See:
@@ -46,10 +46,11 @@ private static SpanContext extract(Tracer tracer, HttpRequest request) {
Objects.requireNonNull(tracer, "Tracer to use must be set.");
Objects.requireNonNull(request, "Request must be set.");
- if (tracer.getHttpTextFormat().fields().stream()
- .map(field -> request.getHeader(field) != null)
- .reduce(false, Boolean::logicalAnd)) {
+ try {
return tracer.getHttpTextFormat().extract(request, (req, key) -> req.getHeader(key));
+ } catch (IllegalArgumentException ignored) {
+ // See: https://github.com/open-telemetry/opentelemetry-java/issues/767
+ // Fall through
}
return NO_OP_CONTEXT;
}
