/** * This sets the client IP:port to the {@linkplain HttpServletRequest#getRemoteAddr() remote * address} if the {@link HttpServerAdapter#parseClientIpAndPort default parsing} fails. */ @Override public boolean parseClientIpAndPort(HttpServletRequest req, Span span) { if (parseClientIpFromXForwardedFor(req, span)) return true; return span.remoteIpAndPort(req.getRemoteAddr(), req.getRemotePort()); }
/** * Returns the first value in the "X-Forwarded-For" header, or null if not present. * * @since 5.2 */ public boolean parseClientIpFromXForwardedFor(Req req, Span span) { String forwardedFor = requestHeader(req, "X-Forwarded-For"); if (forwardedFor == null) return false; int indexOfComma = forwardedFor.indexOf(','); if (indexOfComma != -1) forwardedFor = forwardedFor.substring(0, indexOfComma); return span.remoteIpAndPort(forwardedFor, 0); } }
static void parseTargetAddress(HttpRequestWrapper httpRequest, Span span) { if (span.isNoop()) return; HttpHost target = httpRequest.getTarget(); if (target == null) return; InetAddress address = target.getAddress(); if (address != null) { if (span.remoteIpAndPort(address.getHostAddress(), target.getPort())) return; } span.remoteIpAndPort(target.getHostName(), target.getPort()); }
static void parseTargetAddress(HttpRequestWrapper httpRequest, Span span) { if (span.isNoop()) return; HttpHost target = httpRequest.getTarget(); if (target == null) return; InetAddress address = target.getAddress(); if (address != null) { if (span.remoteIpAndPort(address.getHostAddress(), target.getPort())) return; } span.remoteIpAndPort(target.getHostName(), target.getPort()); } }
/** * This sets the client IP:port to the {@linkplain Channel#remoteAddress() remote address} if * {@link HttpServerAdapter#parseClientIpAndPort} fails. */ void parseChannelAddress(ChannelHandlerContext ctx, HttpRequest request, Span span) { if (adapter.parseClientIpFromXForwardedFor(request, span)) return; InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress(); if (remoteAddress.getAddress() == null) return; span.remoteIpAndPort(Platform.get().getHostString(remoteAddress), remoteAddress.getPort()); }
span.remoteServiceName(defaultRemoteServiceName); span.remoteIpAndPort(url.getHost(), url.getPort()); } catch (Exception e) {
static void parseRemoteAddress(RpcContext rpcContext, Span span) { InetSocketAddress remoteAddress = rpcContext.getRemoteAddress(); if (remoteAddress == null) return; span.remoteIpAndPort(Platform.get().getHostString(remoteAddress), remoteAddress.getPort()); }
static void parseRouteAddress(Chain chain, Span span) { if (span.isNoop()) return; Connection connection = chain.connection(); if (connection == null) return; InetSocketAddress socketAddress = connection.route().socketAddress(); span.remoteIpAndPort(socketAddress.getHostString(), socketAddress.getPort()); }
/** * MySQL exposes the host connecting to, but not the port. This attempts to get the port from the * JDBC URL. Ex. 5555 from {@code jdbc:mysql://localhost:5555/database}, or 3306 if absent. */ static void parseServerIpAndPort(Connection connection, Span span) { try { URI url = URI.create(connection.getMetaData().getURL().substring(5)); // strip "jdbc:" String remoteServiceName = connection.getProperties().getProperty("zipkinServiceName"); if (remoteServiceName == null || "".equals(remoteServiceName)) { String databaseName = connection.getCatalog(); if (databaseName != null && !databaseName.isEmpty()) { remoteServiceName = "mysql-" + databaseName; } else { remoteServiceName = "mysql"; } } span.remoteServiceName(remoteServiceName); String host = connection.getHost(); if (host != null) { span.remoteIpAndPort(host, url.getPort() == -1 ? 3306 : url.getPort()); } } catch (Exception e) { // remote address is optional } }
/** * MySQL exposes the host connecting to, but not the port. This attempts to get the port from the * JDBC URL. Ex. 5555 from {@code jdbc:mysql://localhost:5555/database}, or 3306 if absent. */ static void parseServerIpAndPort(MysqlConnection connection, Span span) { try { URI url = URI.create(connection.getURL().substring(5)); // strip "jdbc:" String remoteServiceName = connection.getProperties().getProperty("zipkinServiceName"); if (remoteServiceName == null || "".equals(remoteServiceName)) { String databaseName = getDatabaseName(connection); if (databaseName != null && !databaseName.isEmpty()) { remoteServiceName = "mysql-" + databaseName; } else { remoteServiceName = "mysql"; } } span.remoteServiceName(remoteServiceName); String host = getHost(connection); if (host != null) { span.remoteIpAndPort(host, url.getPort() == -1 ? 3306 : url.getPort()); } } catch (Exception e) { // remote address is optional } }
/** * MySQL exposes the host connecting to, but not the port. This attempts to get the port from the * JDBC URL. Ex. 5555 from {@code jdbc:mysql://localhost:5555/database}, or 3306 if absent. */ static void parseServerIpAndPort(MysqlConnection connection, Span span) { try { URI url = URI.create(connection.getURL().substring(5)); // strip "jdbc:" String remoteServiceName = connection.getProperties().getProperty("zipkinServiceName"); if (remoteServiceName == null || "".equals(remoteServiceName)) { String databaseName = getDatabaseName(connection); if (databaseName != null && !databaseName.isEmpty()) { remoteServiceName = "mysql-" + databaseName; } else { remoteServiceName = "mysql"; } } span.remoteServiceName(remoteServiceName); String host = getHost(connection); if (host != null) { span.remoteIpAndPort(host, url.getPort() == -1 ? 3306 : url.getPort()); } } catch (Exception e) { // remote address is optional } }
span.remoteIpAndPort(address.getHostAddress(), port);
/** * This sets the client IP:port to the {@linkplain HttpServerRequest#remoteAddress() remote * address} if the {@link HttpServerAdapter#parseClientIpAndPort default parsing} fails. */ @Override public boolean parseClientIpAndPort(HttpServerRequest req, Span span) { if (parseClientIpFromXForwardedFor(req, span)) return true; SocketAddress addr = req.remoteAddress(); return span.remoteIpAndPort(addr.host(), addr.port()); }
/** * @deprecated Use {@link #remoteServiceName(String)} {@link #remoteIpAndPort(String, int)}. * Will be removed in Brave v6. */ @Deprecated public Span remoteEndpoint(Endpoint endpoint) { if (endpoint == null) return this; if (endpoint.serviceName() != null) remoteServiceName(endpoint.serviceName()); String ip = endpoint.ipv6() != null ? endpoint.ipv6() : endpoint.ipv4(); remoteIpAndPort(ip, endpoint.portAsInt()); return this; }
void trySetRemoteIpAndPort() { if (remoteIpV4 != null) delegate.remoteIpAndPort(remoteIpV4, remotePort); if (remoteIpV6 != null) delegate.remoteIpAndPort(remoteIpV6, remotePort); } }
static void parseTargetAddress(HttpRequestWrapper httpRequest, Span span) { if (span.isNoop()) return; HttpHost target = httpRequest.getTarget(); if (target == null) return; InetAddress address = target.getAddress(); if (address != null) { if (span.remoteIpAndPort(address.getHostAddress(), target.getPort())) return; } span.remoteIpAndPort(target.getHostName(), target.getPort()); }
static void parseTargetAddress(HttpRequestWrapper httpRequest, Span span) { if (span.isNoop()) return; HttpHost target = httpRequest.getTarget(); if (target == null) return; InetAddress address = target.getAddress(); if (address != null) { if (span.remoteIpAndPort(address.getHostAddress(), target.getPort())) return; } span.remoteIpAndPort(target.getHostName(), target.getPort()); } }
static void parseRouteAddress(Chain chain, Span span) { if (span.isNoop()) return; Connection connection = chain.connection(); if (connection == null) return; InetSocketAddress socketAddress = connection.route().socketAddress(); span.remoteIpAndPort(socketAddress.getHostString(), socketAddress.getPort()); }
/** * MySQL exposes the host connecting to, but not the port. This attempts to get the port from the * JDBC URL. Ex. 5555 from {@code jdbc:mysql://localhost:5555/database}, or 3306 if absent. */ static void parseServerIpAndPort(MysqlConnection connection, Span span) { try { URI url = URI.create(connection.getURL().substring(5)); // strip "jdbc:" String remoteServiceName = connection.getProperties().getProperty("zipkinServiceName"); if (remoteServiceName == null || "".equals(remoteServiceName)) { String databaseName = getDatabaseName(connection); if (databaseName != null && !databaseName.isEmpty()) { remoteServiceName = "mysql-" + databaseName; } else { remoteServiceName = "mysql"; } } span.remoteServiceName(remoteServiceName); String host = getHost(connection); if (host != null) { span.remoteIpAndPort(host, url.getPort() == -1 ? 3306 : url.getPort()); } } catch (Exception e) { // remote address is optional } }
/** * @deprecated Use {@link #remoteServiceName(String)} {@link #remoteIpAndPort(String, int)}. * Will be removed in Brave v6. */ @Deprecated public Span remoteEndpoint(Endpoint endpoint) { if (endpoint == null) return this; if (endpoint.serviceName() != null) remoteServiceName(endpoint.serviceName()); String ip = endpoint.ipv6() != null ? endpoint.ipv6() : endpoint.ipv4(); remoteIpAndPort(ip, endpoint.portAsInt()); return this; }