Skip to content

Commit

Permalink
Fix NPE in ApacheHttpClientApiAdapter#getHostName.
Browse files Browse the repository at this point in the history
  • Loading branch information
NicklasWallgren committed Dec 28, 2023
1 parent f1c5748 commit ef78427
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -36,12 +37,12 @@ public abstract class AbstractApacheHttpClientAdvice {
TextHeaderGetter<REQUEST>> Object startSpan(final Tracer tracer,
final ApacheHttpClientApiAdapter<REQUEST, WRAPPER, HTTPHOST, RESPONSE> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package co.elastic.apm.agent.httpclient.common;


import javax.annotation.Nullable;
import java.net.URI;
import java.net.URISyntaxException;

Expand All @@ -27,7 +28,8 @@ public interface ApacheHttpClientApiAdapter<REQUEST, WRAPPER extends REQUEST, HT

URI getUri(WRAPPER request) throws URISyntaxException;

CharSequence getHostName(HTTPHOST httpHost);
@Nullable
CharSequence getHostName(@Nullable HTTPHOST httpHost, WRAPPER request);

int getResponseCode(RESPONSE response);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public URI getUri(HttpRequestWrapper request) {
}

@Override
public CharSequence getHostName(HttpHost httpHost) {
public CharSequence getHostName(HttpHost httpHost, HttpRequestWrapper request) {
return httpHost.getHostName();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static class ApacheHttpClient5Advice extends AbstractApacheHttpClientAdvi

@Nullable
@Advice.OnMethodEnter(suppress = Throwable.class, inline = false)
public static Object onBeforeExecute(@Advice.Argument(0) HttpHost httpHost,
public static Object onBeforeExecute(@Advice.Argument(0) @Nullable HttpHost httpHost,
@Advice.Argument(1) ClassicHttpRequest request) throws URISyntaxException {
return startSpan(tracer, adapter, request, httpHost, RequestHeaderAccessor.INSTANCE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,25 @@
package co.elastic.apm.agent.httpclient.v5.helper;

import co.elastic.apm.agent.httpclient.common.ApacheHttpClientApiAdapter;
import co.elastic.apm.agent.sdk.logging.Logger;
import co.elastic.apm.agent.sdk.logging.LoggerFactory;
import org.apache.hc.client5.http.CircularRedirectException;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.routing.RoutingSupport;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpRequest;

import javax.annotation.Nullable;
import java.net.URI;
import java.net.URISyntaxException;

public class ApacheHttpClient5ApiAdapter implements ApacheHttpClientApiAdapter<HttpRequest, ClassicHttpRequest, HttpHost, CloseableHttpResponse> {
private static final ApacheHttpClient5ApiAdapter INSTANCE = new ApacheHttpClient5ApiAdapter();

private static final Logger logger = LoggerFactory.getLogger(ApacheHttpClient5ApiAdapter.class);

private ApacheHttpClient5ApiAdapter() {
}

Expand All @@ -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
Expand Down

0 comments on commit ef78427

Please sign in to comment.