Skip to content

Commit

Permalink
Add additional HTTP attributes to event-logs (#8902)
Browse files Browse the repository at this point in the history

Co-authored-by: David Burns <[email protected]>
  • Loading branch information
pujagani and AutomatedTester authored Nov 24, 2020
1 parent b8b73c1 commit 65563b8
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
],
Expand Down
57 changes: 50 additions & 7 deletions java/client/src/org/openqa/selenium/remote/tracing/Tags.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@

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;

import java.io.PrintWriter;
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;
Expand All @@ -49,7 +51,7 @@ private Tags() {

public static final BiConsumer<Span, HttpRequest> 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<Span, HttpResponse> HTTP_RESPONSE = (span, res) -> {
Expand All @@ -71,12 +73,53 @@ private Tags() {
};

public static final BiConsumer<Map<String, EventAttributeValue>, 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<String> userAgent = Optional.ofNullable(req.getHeader(HttpHeaders.USER_AGENT));
if (userAgent.isPresent()) {
map.put(AttributeKey.HTTP_USER_AGENT.getKey(),
EventAttribute.setValue(userAgent.get()));
}

Optional<String> host = Optional.ofNullable(req.getHeader(HttpHeaders.HOST));
if (host.isPresent()) {
map.put(AttributeKey.HTTP_HOST.getKey(),
EventAttribute.setValue(host.get()));
}

Optional<String> contentLength =
Optional.ofNullable(req.getHeader(HttpHeaders.CONTENT_LENGTH));
if (contentLength.isPresent()) {
map.put(AttributeKey.HTTP_REQUEST_CONTENT_LENGTH.getKey(),
EventAttribute.setValue(contentLength.get()));
}

Optional<String> clientIpAddress =
Optional.ofNullable(req.getHeader(HttpHeaders.X_FORWARDED_FOR));
if (clientIpAddress.isPresent()) {
map.put(AttributeKey.HTTP_CLIENT_IP.getKey(),
EventAttribute.setValue(clientIpAddress.get()));
}

Optional<String> httpScheme =
Optional.ofNullable((String) req.getAttribute(AttributeKey.HTTP_SCHEME.getKey()));
if (httpScheme.isPresent()) {
map.put(AttributeKey.HTTP_SCHEME.getKey(),
EventAttribute.setValue(httpScheme.get()));
}

Optional<Integer> 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<Map<String, EventAttributeValue>, HttpResponse>
HTTP_RESPONSE_EVENT =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 65563b8

Please sign in to comment.