From 65563b8e7e3ab753cb6d400acee528f622872b64 Mon Sep 17 00:00:00 2001 From: Puja Jagani Date: Tue, 24 Nov 2020 17:28:22 +0530 Subject: [PATCH] Add additional HTTP attributes to event-logs (#8902) Co-authored-by: David Burns --- .../selenium/remote/tracing/AttributeKey.java | 7 +++ .../selenium/remote/tracing/BUILD.bazel | 1 + .../openqa/selenium/remote/tracing/Tags.java | 57 ++++++++++++++++--- .../grid/web/ReverseProxyHandler.java | 4 ++ .../openqa/selenium/netty/server/BUILD.bazel | 1 + .../netty/server/RequestConverter.java | 6 ++ 6 files changed, 69 insertions(+), 7 deletions(-) diff --git a/java/client/src/org/openqa/selenium/remote/tracing/AttributeKey.java b/java/client/src/org/openqa/selenium/remote/tracing/AttributeKey.java index 8db58e1050dbe..d7873fff5fc52 100644 --- a/java/client/src/org/openqa/selenium/remote/tracing/AttributeKey.java +++ b/java/client/src/org/openqa/selenium/remote/tracing/AttributeKey.java @@ -34,6 +34,13 @@ public enum AttributeKey { HTTP_TARGET_HOST(SemanticAttributes.HTTP_TARGET.getKey()), HTTP_CLIENT_CLASS("http.client_class"), HTTP_HANDLER_CLASS("http.handler_class"), + HTTP_USER_AGENT(SemanticAttributes.HTTP_USER_AGENT.getKey()), + HTTP_HOST(SemanticAttributes.HTTP_HOST.getKey()), + HTTP_TARGET(SemanticAttributes.HTTP_TARGET.getKey()), + HTTP_REQUEST_CONTENT_LENGTH(SemanticAttributes.HTTP_REQUEST_CONTENT_LENGTH.getKey()), + HTTP_CLIENT_IP(SemanticAttributes.HTTP_CLIENT_IP.getKey()), + HTTP_SCHEME(SemanticAttributes.HTTP_SCHEME.getKey()), + HTTP_FLAVOR(SemanticAttributes.HTTP_FLAVOR.getKey()), LOGGER_CLASS("logger"), diff --git a/java/client/src/org/openqa/selenium/remote/tracing/BUILD.bazel b/java/client/src/org/openqa/selenium/remote/tracing/BUILD.bazel index 5950c02beb49a..c068e6f7966d8 100644 --- a/java/client/src/org/openqa/selenium/remote/tracing/BUILD.bazel +++ b/java/client/src/org/openqa/selenium/remote/tracing/BUILD.bazel @@ -27,6 +27,7 @@ java_library( deps = [ "//java/client/src/org/openqa/selenium:core", "//java/client/src/org/openqa/selenium/remote/http", + artifact("com.google.guava:guava"), artifact("io.opentelemetry:opentelemetry-api"), artifact("io.opentelemetry:opentelemetry-context"), ], diff --git a/java/client/src/org/openqa/selenium/remote/tracing/Tags.java b/java/client/src/org/openqa/selenium/remote/tracing/Tags.java index 63b852afb1af9..87c02ac7a835d 100644 --- a/java/client/src/org/openqa/selenium/remote/tracing/Tags.java +++ b/java/client/src/org/openqa/selenium/remote/tracing/Tags.java @@ -17,6 +17,7 @@ package org.openqa.selenium.remote.tracing; +import com.google.common.net.HttpHeaders; import org.openqa.selenium.remote.http.HttpRequest; import org.openqa.selenium.remote.http.HttpResponse; @@ -24,6 +25,7 @@ import java.io.StringWriter; import java.util.AbstractMap.SimpleEntry; import java.util.Map; +import java.util.Optional; import java.util.function.BiConsumer; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -49,7 +51,7 @@ private Tags() { public static final BiConsumer HTTP_REQUEST = (span, req) -> { span.setAttribute(AttributeKey.HTTP_METHOD.getKey(), req.getMethod().toString()); - span.setAttribute(AttributeKey.HTTP_URL.getKey(), req.getUri()); + span.setAttribute(AttributeKey.HTTP_TARGET.getKey(), req.getUri()); }; public static final BiConsumer HTTP_RESPONSE = (span, res) -> { @@ -71,12 +73,53 @@ private Tags() { }; public static final BiConsumer, HttpRequest> - HTTP_REQUEST_EVENT = - (map, req) -> { - map.put(AttributeKey.HTTP_METHOD.getKey(), - EventAttribute.setValue(req.getMethod().toString())); - map.put(AttributeKey.HTTP_URL.getKey(), EventAttribute.setValue(req.getUri())); - }; + HTTP_REQUEST_EVENT = + (map, req) -> { + map.put(AttributeKey.HTTP_METHOD.getKey(), + EventAttribute.setValue(req.getMethod().toString())); + map.put(AttributeKey.HTTP_TARGET.getKey(), EventAttribute.setValue(req.getUri())); + + Optional userAgent = Optional.ofNullable(req.getHeader(HttpHeaders.USER_AGENT)); + if (userAgent.isPresent()) { + map.put(AttributeKey.HTTP_USER_AGENT.getKey(), + EventAttribute.setValue(userAgent.get())); + } + + Optional host = Optional.ofNullable(req.getHeader(HttpHeaders.HOST)); + if (host.isPresent()) { + map.put(AttributeKey.HTTP_HOST.getKey(), + EventAttribute.setValue(host.get())); + } + + Optional contentLength = + Optional.ofNullable(req.getHeader(HttpHeaders.CONTENT_LENGTH)); + if (contentLength.isPresent()) { + map.put(AttributeKey.HTTP_REQUEST_CONTENT_LENGTH.getKey(), + EventAttribute.setValue(contentLength.get())); + } + + Optional clientIpAddress = + Optional.ofNullable(req.getHeader(HttpHeaders.X_FORWARDED_FOR)); + if (clientIpAddress.isPresent()) { + map.put(AttributeKey.HTTP_CLIENT_IP.getKey(), + EventAttribute.setValue(clientIpAddress.get())); + } + + Optional httpScheme = + Optional.ofNullable((String) req.getAttribute(AttributeKey.HTTP_SCHEME.getKey())); + if (httpScheme.isPresent()) { + map.put(AttributeKey.HTTP_SCHEME.getKey(), + EventAttribute.setValue(httpScheme.get())); + } + + Optional httpVersion = + Optional.ofNullable((Integer) req.getAttribute(AttributeKey.HTTP_FLAVOR.getKey())); + if (httpVersion.isPresent()) { + map.put(AttributeKey.HTTP_FLAVOR.getKey(), + EventAttribute.setValue(httpVersion.get())); + } + + }; public static final BiConsumer, HttpResponse> HTTP_RESPONSE_EVENT = diff --git a/java/server/src/org/openqa/selenium/grid/web/ReverseProxyHandler.java b/java/server/src/org/openqa/selenium/grid/web/ReverseProxyHandler.java index 984004eb8471c..6ef2a3d580f85 100644 --- a/java/server/src/org/openqa/selenium/grid/web/ReverseProxyHandler.java +++ b/java/server/src/org/openqa/selenium/grid/web/ReverseProxyHandler.java @@ -74,6 +74,10 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException { HttpRequest toUpstream = new HttpRequest(req.getMethod(), req.getUri()); + for(String attributeName: req.getAttributeNames()) { + toUpstream.setAttribute(attributeName, req.getAttribute(attributeName)); + } + for (String name : req.getQueryParameterNames()) { for (String value : req.getQueryParameters(name)) { toUpstream.addQueryParameter(name, value); diff --git a/java/server/src/org/openqa/selenium/netty/server/BUILD.bazel b/java/server/src/org/openqa/selenium/netty/server/BUILD.bazel index 3909df9942750..fe6fc59b1957a 100644 --- a/java/server/src/org/openqa/selenium/netty/server/BUILD.bazel +++ b/java/server/src/org/openqa/selenium/netty/server/BUILD.bazel @@ -18,6 +18,7 @@ java_library( deps = [ "//java/client/src/org/openqa/selenium:core", "//java/client/src/org/openqa/selenium/remote/http", + "//java/client/src/org/openqa/selenium/remote:remote", "//java/server/src/org/openqa/selenium/grid/server", "//java/server/src/org/openqa/selenium/grid/web", artifact("com.google.guava:guava"), diff --git a/java/server/src/org/openqa/selenium/netty/server/RequestConverter.java b/java/server/src/org/openqa/selenium/netty/server/RequestConverter.java index 1a1820969026f..001ed9463004b 100644 --- a/java/server/src/org/openqa/selenium/netty/server/RequestConverter.java +++ b/java/server/src/org/openqa/selenium/netty/server/RequestConverter.java @@ -34,6 +34,7 @@ import org.openqa.selenium.remote.http.HttpMethod; import org.openqa.selenium.remote.http.HttpRequest; import org.openqa.selenium.remote.http.HttpResponse; +import org.openqa.selenium.remote.tracing.AttributeKey; import java.io.IOException; import java.io.InputStream; @@ -82,6 +83,11 @@ protected void channelRead0( return; } + req.setAttribute(AttributeKey.HTTP_SCHEME.getKey(), + nettyRequest.protocolVersion().protocolName()); + req.setAttribute(AttributeKey.HTTP_FLAVOR.getKey(), + nettyRequest.protocolVersion().majorVersion()); + out = new PipedOutputStream(); InputStream in = new PipedInputStream(out);