Skip to content

Commit 096ec41

Browse files
authored
[grid] Add http logs flag to allow http trace event logs
1 parent 7b832ea commit 096ec41

File tree

8 files changed

+57
-10
lines changed

8 files changed

+57
-10
lines changed

java/client/src/org/openqa/selenium/remote/tracing/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ java_library(
2727
deps = [
2828
"//java/client/src/org/openqa/selenium:core",
2929
"//java/client/src/org/openqa/selenium/remote/http",
30+
"//java/server/src/org/openqa/selenium/grid/config",
3031
artifact("com.google.guava:guava"),
3132
artifact("io.opentelemetry:opentelemetry-api"),
3233
artifact("io.opentelemetry:opentelemetry-context"),

java/client/src/org/openqa/selenium/remote/tracing/opentelemetry/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ java_library(
1313
"//java/client/src/org/openqa/selenium:core",
1414
"//java/client/src/org/openqa/selenium/json",
1515
"//java/client/src/org/openqa/selenium/remote/tracing",
16+
"//java/server/src/org/openqa/selenium/grid/config",
1617
artifact("com.google.guava:guava"),
1718
artifact("io.opentelemetry:opentelemetry-api"),
1819
artifact("io.opentelemetry:opentelemetry-context"),

java/client/src/org/openqa/selenium/remote/tracing/opentelemetry/OpenTelemetryTracer.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
public class OpenTelemetryTracer implements org.openqa.selenium.remote.tracing.Tracer {
3232

3333
private static final Logger LOG = Logger.getLogger(OpenTelemetryTracer.class.getName());
34+
private static boolean HTTP_LOGS;
3435

3536
// We obtain the underlying tracer instance from the singleton instance
3637
// that OpenTelemetry maintains. If we blindly grabbed the tracing provider
@@ -41,6 +42,14 @@ public class OpenTelemetryTracer implements org.openqa.selenium.remote.tracing.T
4142
// adding unit tests for this.
4243
private static volatile OpenTelemetryTracer singleton;
4344

45+
public static void setHttpLogs(boolean value) {
46+
HTTP_LOGS = value;
47+
}
48+
49+
public static boolean getHttpLogs() {
50+
return HTTP_LOGS;
51+
}
52+
4453
public static OpenTelemetryTracer getInstance() {
4554
OpenTelemetryTracer localTracer = singleton;
4655
if (localTracer == null) {

java/client/src/org/openqa/selenium/remote/tracing/opentelemetry/SeleniumSpanExporter.java

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,32 @@
1818
package org.openqa.selenium.remote.tracing.opentelemetry;
1919

2020
import com.google.auto.service.AutoService;
21+
import io.opentelemetry.api.common.AttributeKey;
2122
import io.opentelemetry.api.common.Attributes;
2223
import io.opentelemetry.api.trace.StatusCode;
2324
import io.opentelemetry.sdk.autoconfigure.spi.SdkTracerProviderConfigurer;
2425
import io.opentelemetry.sdk.common.CompletableResultCode;
2526
import io.opentelemetry.sdk.trace.SdkTracerProviderBuilder;
2627
import io.opentelemetry.sdk.trace.data.EventData;
2728
import io.opentelemetry.sdk.trace.data.SpanData;
28-
import io.opentelemetry.sdk.trace.data.StatusData;
2929
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
3030
import io.opentelemetry.sdk.trace.export.SpanExporter;
3131
import org.openqa.selenium.json.Json;
3232
import org.openqa.selenium.json.JsonOutput;
33+
import org.openqa.selenium.remote.tracing.Span;
3334

3435
import java.util.Collection;
3536
import java.util.HashMap;
3637
import java.util.List;
3738
import java.util.Map;
39+
import java.util.Optional;
3840
import java.util.logging.Level;
3941
import java.util.logging.Logger;
4042

4143
@AutoService(SdkTracerProviderConfigurer.class)
4244
public class SeleniumSpanExporter implements SdkTracerProviderConfigurer {
4345
private static final Logger LOG = Logger.getLogger(SeleniumSpanExporter.class.getName());
46+
private final boolean httpLogs = OpenTelemetryTracer.getHttpLogs();
4447

4548
@Override
4649
public void configure(SdkTracerProviderBuilder tracerProvider) {
@@ -51,25 +54,20 @@ public CompletableResultCode export(Collection<SpanData> spans) {
5154
LOG.fine(String.valueOf(span));
5255

5356
String traceId = span.getTraceId();
54-
String spanId = span.getSpanId();
55-
StatusData status = span.getStatus();
5657
List<EventData> eventList = span.getEvents();
58+
59+
Level logLevel = getLogLevel(span);
60+
5761
eventList.forEach(event -> {
5862
Map<String, Object> map = new HashMap<>();
5963
map.put("eventTime", event.getEpochNanos());
6064
map.put("traceId", traceId);
61-
map.put("spanId", spanId);
62-
map.put("spanKind", span.getKind().toString());
6365
map.put("eventName", event.getName());
6466

6567
Attributes attributes = event.getAttributes();
6668
map.put("attributes", attributes.asMap());
6769
String jsonString = getJsonString(map);
68-
if (status.getStatusCode() == StatusCode.ERROR) {
69-
LOG.log(Level.WARNING, jsonString);
70-
} else {
71-
LOG.log(Level.FINE, jsonString);
72-
}
70+
LOG.log(logLevel, jsonString);
7371
});
7472
});
7573
return CompletableResultCode.ofSuccess();
@@ -96,4 +94,25 @@ private static String getJsonString(Map<String, Object> map) {
9694
}
9795
return text.toString();
9896
}
97+
98+
private Level getLogLevel(SpanData span) {
99+
Level level = Level.FINE;
100+
101+
Optional<String> kind = Optional.ofNullable(span
102+
.getAttributes()
103+
.get(AttributeKey.stringKey(org.openqa.selenium.remote.tracing.AttributeKey.SPAN_KIND.getKey())));
104+
105+
if (span.getStatus().getStatusCode() == StatusCode.ERROR) {
106+
level = Level.WARNING;
107+
} else {
108+
if (httpLogs && kind.isPresent()) {
109+
String kindValue = kind.get();
110+
if (Span.Kind.SERVER.name().equalsIgnoreCase(kindValue) ||
111+
Span.Kind.CLIENT.name().equalsIgnoreCase(kindValue)) {
112+
level = Level.INFO;
113+
}
114+
}
115+
}
116+
return level;
117+
}
99118
}

java/server/src/org/openqa/selenium/grid/config/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ java_library(
1212
"//java/server/src/org/openqa/selenium/grid:__subpackages__",
1313
"//java/server/src/org/openqa/selenium/netty/server:__pkg__",
1414
"//java/server/src/org/openqa/selenium/server/htmlrunner:__pkg__",
15+
"//java/client/src/org/openqa/selenium/remote:__pkg__",
16+
"//java/client/src/org/openqa/selenium/remote/tracing:__subpackages__",
1517
"//java/server/test/com/thoughtworks/selenium/webdriven:__pkg__",
1618
"//java/server/test/org/openqa/selenium:__subpackages__",
1719
],

java/server/src/org/openqa/selenium/grid/log/LoggingFlags.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import static org.openqa.selenium.grid.config.StandardGridRoles.ALL_ROLES;
3131
import static org.openqa.selenium.grid.log.LoggingOptions.DEFAULT_CONFIGURE_LOGGING;
32+
import static org.openqa.selenium.grid.log.LoggingOptions.DEFAULT_HTTP_LOGS;
3233
import static org.openqa.selenium.grid.log.LoggingOptions.DEFAULT_LOG_LEVEL;
3334
import static org.openqa.selenium.grid.log.LoggingOptions.DEFAULT_PLAIN_LOGS;
3435
import static org.openqa.selenium.grid.log.LoggingOptions.DEFAULT_STRUCTURED_LOGS;
@@ -59,6 +60,10 @@ public class LoggingFlags implements HasRoles {
5960
@ConfigValue(section = LOGGING_SECTION, name = "tracing", example = "true")
6061
private Boolean enableTracing = DEFAULT_TRACING_ENABLED;
6162

63+
@Parameter(description = "Enable http logging. Tracing should be enabled to log http logs.", hidden = true, names = "--http-logs", arity = 1)
64+
@ConfigValue(section = LOGGING_SECTION, name = "http-logs", example = "true")
65+
private Boolean httpLogs = DEFAULT_HTTP_LOGS;
66+
6267
@Parameter(description = "File to write out logs", names = "--log", arity = 1)
6368
@ConfigValue(section = LOGGING_SECTION, name = "log-file", example = "true")
6469
private String logFile;

java/server/src/org/openqa/selenium/grid/log/LoggingOptions.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class LoggingOptions {
4545
static final boolean DEFAULT_PLAIN_LOGS = true;
4646
static final boolean DEFAULT_STRUCTURED_LOGS = false;
4747
static final boolean DEFAULT_TRACING_ENABLED = true;
48+
public static final boolean DEFAULT_HTTP_LOGS = false;
4849
private static final Logger LOG = Logger.getLogger(LoggingOptions.class.getName());
4950
private final Config config;
5051
private Level level = Level.INFO;
@@ -57,6 +58,10 @@ public boolean isUsingStructuredLogging() {
5758
return config.getBool(LOGGING_SECTION, "structured-logs").orElse(DEFAULT_STRUCTURED_LOGS);
5859
}
5960

61+
public boolean shouldLogHttpLogs() {
62+
return config.getBool(LOGGING_SECTION, "http-logs").orElse(DEFAULT_HTTP_LOGS);
63+
}
64+
6065
public boolean isUsingPlainLogs() {
6166
return config.getBool(LOGGING_SECTION, "plain-logs").orElse(DEFAULT_PLAIN_LOGS);
6267
}
@@ -83,6 +88,8 @@ public Tracer getTracer() {
8388
return new NullTracer();
8489
}
8590

91+
OpenTelemetryTracer.setHttpLogs(shouldLogHttpLogs());
92+
8693
return OpenTelemetryTracer.getInstance();
8794
}
8895

java/server/test/org/openqa/selenium/grid/router/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ java_library(
2222
artifact("org.slf4j:slf4j-jdk14"),
2323
],
2424
deps = [
25+
"//java/server/src/org/openqa/selenium/grid/config",
2526
"//java/client/src/org/openqa/selenium:core",
2627
"//java/client/src/org/openqa/selenium/json",
2728
"//java/client/src/org/openqa/selenium/remote/http",
@@ -56,6 +57,7 @@ java_selenium_test_suite(
5657
"//java/client/test/org/openqa/selenium/testing:annotations",
5758
"//java/client/test/org/openqa/selenium/testing:test-base",
5859
"//java/server/src/org/openqa/selenium/grid",
60+
"//java/server/src/org/openqa/selenium/grid/config",
5961
"//java/server/test/org/openqa/selenium/grid/testing",
6062
artifact("com.google.guava:guava"),
6163
artifact("junit:junit"),
@@ -82,6 +84,7 @@ java_test_suite(
8284
"//java/client/test/org/openqa/selenium/testing:test-base",
8385
"//java/server/src/org/openqa/selenium/grid",
8486
"//java/server/test/org/openqa/selenium/grid/testing",
87+
"//java/server/src/org/openqa/selenium/grid/config",
8588
artifact("com.google.guava:guava"),
8689
artifact("io.opentelemetry:opentelemetry-api"),
8790
artifact("junit:junit"),

0 commit comments

Comments
 (0)