Skip to content

Commit ac145e5

Browse files
committed
[grid] Printing readable exceptions
When triaging Grid issues, it is common to see logs with exceptions generated in the SeleniumSpanExporter. However, they are json strings printed in a single line, which makes them really hard to read. With this change, if the event has information about an exception, it will be printed in a human-readable way.
1 parent 780ae58 commit ac145e5

1 file changed

Lines changed: 28 additions & 13 deletions

File tree

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

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
package org.openqa.selenium.remote.tracing.opentelemetry;
1919

2020
import com.google.auto.service.AutoService;
21+
import com.google.common.collect.ImmutableSet;
22+
23+
import org.openqa.selenium.json.Json;
24+
import org.openqa.selenium.json.JsonOutput;
25+
import org.openqa.selenium.remote.tracing.Span;
26+
2127
import io.opentelemetry.api.common.AttributeKey;
2228
import io.opentelemetry.api.common.Attributes;
2329
import io.opentelemetry.api.trace.StatusCode;
@@ -29,9 +35,6 @@
2935
import io.opentelemetry.sdk.trace.data.SpanData;
3036
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
3137
import io.opentelemetry.sdk.trace.export.SpanExporter;
32-
import org.openqa.selenium.json.Json;
33-
import org.openqa.selenium.json.JsonOutput;
34-
import org.openqa.selenium.remote.tracing.Span;
3538

3639
import java.util.Collection;
3740
import java.util.HashMap;
@@ -43,11 +46,24 @@
4346

4447
@AutoService(SdkTracerProviderConfigurer.class)
4548
public class SeleniumSpanExporter implements SdkTracerProviderConfigurer {
49+
4650
private static final Logger LOG = Logger.getLogger(SeleniumSpanExporter.class.getName());
51+
private static final ImmutableSet<String> EXCEPTION_ATTRIBUTES =
52+
ImmutableSet.of("exception.message", "exception.stacktrace");
4753
private final boolean httpLogs = OpenTelemetryTracer.getHttpLogs();
4854

55+
private static String getJsonString(Map<String, Object> map) {
56+
StringBuilder text = new StringBuilder();
57+
try (JsonOutput json = new Json().newOutput(text).setPrettyPrint(false)) {
58+
json.write(map);
59+
text.append('\n');
60+
}
61+
return text.toString();
62+
}
63+
4964
@Override
50-
public void configure(SdkTracerProviderBuilder tracerProvider, ConfigProperties configProperties) {
65+
public void configure(SdkTracerProviderBuilder tracerProvider,
66+
ConfigProperties configProperties) {
5167
tracerProvider.addSpanProcessor(SimpleSpanProcessor.create(new SpanExporter() {
5268
@Override
5369
public CompletableResultCode export(Collection<SpanData> spans) {
@@ -67,6 +83,14 @@ public CompletableResultCode export(Collection<SpanData> spans) {
6783

6884
Attributes attributes = event.getAttributes();
6985
map.put("attributes", attributes.asMap());
86+
87+
EXCEPTION_ATTRIBUTES.forEach(exceptionAttribute -> {
88+
attributes.asMap().keySet()
89+
.stream()
90+
.filter(key -> exceptionAttribute.equalsIgnoreCase(key.getKey()))
91+
.findFirst()
92+
.ifPresent(key -> LOG.log(logLevel, attributes.asMap().get(key).toString()));
93+
});
7094
String jsonString = getJsonString(map);
7195
LOG.log(logLevel, jsonString);
7296
});
@@ -87,15 +111,6 @@ public CompletableResultCode shutdown() {
87111
}));
88112
}
89113

90-
private static String getJsonString(Map<String, Object> map) {
91-
StringBuilder text = new StringBuilder();
92-
try (JsonOutput json = new Json().newOutput(text).setPrettyPrint(false)) {
93-
json.write(map);
94-
text.append('\n');
95-
}
96-
return text.toString();
97-
}
98-
99114
private Level getLogLevel(SpanData span) {
100115
Level level = Level.FINE;
101116

0 commit comments

Comments
 (0)