From ef78427b34be488478500fc744a588a88d439465 Mon Sep 17 00:00:00 2001 From: Nicklas Wallgren Date: Thu, 28 Dec 2023 08:51:58 +0100 Subject: [PATCH] Fix NPE in ApacheHttpClientApiAdapter#getHostName. --- .../AbstractApacheHttpClientAdvice.java | 5 +++-- .../common/ApacheHttpClientApiAdapter.java | 4 +++- .../helper/ApacheHttpClient4ApiAdapter.java | 2 +- .../v5/ApacheHttpClient5Instrumentation.java | 2 +- .../helper/ApacheHttpClient5ApiAdapter.java | 22 +++++++++++++++++-- 5 files changed, 28 insertions(+), 7 deletions(-) diff --git a/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient-common/src/main/java/co/elastic/apm/agent/httpclient/common/AbstractApacheHttpClientAdvice.java b/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient-common/src/main/java/co/elastic/apm/agent/httpclient/common/AbstractApacheHttpClientAdvice.java index fce3555a6d9..aa6c60f04d6 100644 --- a/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient-common/src/main/java/co/elastic/apm/agent/httpclient/common/AbstractApacheHttpClientAdvice.java +++ b/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient-common/src/main/java/co/elastic/apm/agent/httpclient/common/AbstractApacheHttpClientAdvice.java @@ -27,6 +27,7 @@ import co.elastic.apm.agent.tracer.dispatch.TextHeaderGetter; import co.elastic.apm.agent.tracer.dispatch.TextHeaderSetter; +import javax.annotation.Nullable; import java.net.URISyntaxException; public abstract class AbstractApacheHttpClientAdvice { @@ -36,12 +37,12 @@ public abstract class AbstractApacheHttpClientAdvice { TextHeaderGetter> Object startSpan(final Tracer tracer, final ApacheHttpClientApiAdapter adapter, final WRAPPER request, - final HTTPHOST httpHost, + @Nullable final HTTPHOST httpHost, final HeaderAccessor headerAccessor) throws URISyntaxException { ElasticContext elasticContext = tracer.currentContext(); Span span = null; if (elasticContext.getSpan() != null) { - span = HttpClientHelper.startHttpClientSpan(elasticContext, adapter.getMethod(request), adapter.getUri(request), adapter.getHostName(httpHost)); + span = HttpClientHelper.startHttpClientSpan(elasticContext, adapter.getMethod(request), adapter.getUri(request), adapter.getHostName(httpHost, request)); if (span != null) { span.activate(); } diff --git a/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient-common/src/main/java/co/elastic/apm/agent/httpclient/common/ApacheHttpClientApiAdapter.java b/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient-common/src/main/java/co/elastic/apm/agent/httpclient/common/ApacheHttpClientApiAdapter.java index 4363cc3e175..a3834035cd9 100644 --- a/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient-common/src/main/java/co/elastic/apm/agent/httpclient/common/ApacheHttpClientApiAdapter.java +++ b/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient-common/src/main/java/co/elastic/apm/agent/httpclient/common/ApacheHttpClientApiAdapter.java @@ -19,6 +19,7 @@ package co.elastic.apm.agent.httpclient.common; +import javax.annotation.Nullable; import java.net.URI; import java.net.URISyntaxException; @@ -27,7 +28,8 @@ public interface ApacheHttpClientApiAdapter { private static final ApacheHttpClient5ApiAdapter INSTANCE = new ApacheHttpClient5ApiAdapter(); + private static final Logger logger = LoggerFactory.getLogger(ApacheHttpClient5ApiAdapter.class); + private ApacheHttpClient5ApiAdapter() { } @@ -49,8 +56,19 @@ public URI getUri(ClassicHttpRequest request) throws URISyntaxException { } @Override - public CharSequence getHostName(HttpHost httpHost) { - return httpHost.getHostName(); + public CharSequence getHostName(@Nullable HttpHost httpHost, ClassicHttpRequest request) { + if (httpHost != null) { + return httpHost.getHostName(); + } + + try { + return RoutingSupport.determineHost(request) + .getHostName(); + } catch (HttpException e) { + logger.error("Exception while determining HttpHost", e); + + return null; + } } @Override