netty 4.0: improve request uri parsing
This commit is contained in:
parent
d4943eb2b6
commit
a3219639a0
|
@ -16,8 +16,12 @@ import io.opentracing.propagation.Format;
|
||||||
import io.opentracing.tag.Tags;
|
import io.opentracing.tag.Tags;
|
||||||
import io.opentracing.util.GlobalTracer;
|
import io.opentracing.util.GlobalTracer;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
public class HttpClientRequestTracingHandler extends ChannelOutboundHandlerAdapter {
|
public class HttpClientRequestTracingHandler extends ChannelOutboundHandlerAdapter {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -37,10 +41,6 @@ public class HttpClientRequestTracingHandler extends ChannelOutboundHandlerAdapt
|
||||||
final HttpRequest request = (HttpRequest) msg;
|
final HttpRequest request = (HttpRequest) msg;
|
||||||
final InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress();
|
final InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress();
|
||||||
|
|
||||||
String url = request.getUri();
|
|
||||||
if (request.headers().contains(HOST)) {
|
|
||||||
url = "http://" + request.headers().get(HOST) + url;
|
|
||||||
}
|
|
||||||
final Span span =
|
final Span span =
|
||||||
GlobalTracer.get()
|
GlobalTracer.get()
|
||||||
.buildSpan("netty.client.request")
|
.buildSpan("netty.client.request")
|
||||||
|
@ -48,7 +48,7 @@ public class HttpClientRequestTracingHandler extends ChannelOutboundHandlerAdapt
|
||||||
.withTag(Tags.PEER_HOSTNAME.getKey(), remoteAddress.getHostName())
|
.withTag(Tags.PEER_HOSTNAME.getKey(), remoteAddress.getHostName())
|
||||||
.withTag(Tags.PEER_PORT.getKey(), remoteAddress.getPort())
|
.withTag(Tags.PEER_PORT.getKey(), remoteAddress.getPort())
|
||||||
.withTag(Tags.HTTP_METHOD.getKey(), request.getMethod().name())
|
.withTag(Tags.HTTP_METHOD.getKey(), request.getMethod().name())
|
||||||
.withTag(Tags.HTTP_URL.getKey(), url)
|
.withTag(Tags.HTTP_URL.getKey(), formatUrl(request))
|
||||||
.withTag(Tags.COMPONENT.getKey(), "netty-client")
|
.withTag(Tags.COMPONENT.getKey(), "netty-client")
|
||||||
.withTag(DDTags.SPAN_TYPE, DDSpanTypes.HTTP_CLIENT)
|
.withTag(DDTags.SPAN_TYPE, DDSpanTypes.HTTP_CLIENT)
|
||||||
.start();
|
.start();
|
||||||
|
@ -56,8 +56,8 @@ public class HttpClientRequestTracingHandler extends ChannelOutboundHandlerAdapt
|
||||||
// AWS calls are often signed, so we can't add headers without breaking the signature.
|
// AWS calls are often signed, so we can't add headers without breaking the signature.
|
||||||
if (!request.headers().contains("amz-sdk-invocation-id")) {
|
if (!request.headers().contains("amz-sdk-invocation-id")) {
|
||||||
GlobalTracer.get()
|
GlobalTracer.get()
|
||||||
.inject(
|
.inject(
|
||||||
span.context(), Format.Builtin.HTTP_HEADERS, new NettyResponseInjectAdapter(request));
|
span.context(), Format.Builtin.HTTP_HEADERS, new NettyResponseInjectAdapter(request));
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.channel().attr(AttributeKeys.CLIENT_ATTRIBUTE_KEY).set(span);
|
ctx.channel().attr(AttributeKeys.CLIENT_ATTRIBUTE_KEY).set(span);
|
||||||
|
@ -75,4 +75,18 @@ public class HttpClientRequestTracingHandler extends ChannelOutboundHandlerAdapt
|
||||||
scope.close();
|
scope.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String formatUrl(final HttpRequest request) {
|
||||||
|
try {
|
||||||
|
URI uri = new URI(request.getUri());
|
||||||
|
if ((uri.getHost() == null || uri.getHost().equals("")) && request.headers().contains(HOST)) {
|
||||||
|
uri = new URI("http://" + request.headers().get(HOST) + request.getUri());
|
||||||
|
}
|
||||||
|
return new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(), null, null)
|
||||||
|
.toString();
|
||||||
|
} catch (final URISyntaxException e) {
|
||||||
|
log.debug("Cannot parse netty uri: {}", request.getUri());
|
||||||
|
return request.getUri();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue