Skip to content

Commit

Permalink
[grid] Printing readable exceptions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
diemol committed May 10, 2022
1 parent 780ae58 commit ac145e5
Showing 1 changed file with 28 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
package org.openqa.selenium.remote.tracing.opentelemetry;

import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableSet;

import org.openqa.selenium.json.Json;
import org.openqa.selenium.json.JsonOutput;
import org.openqa.selenium.remote.tracing.Span;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.StatusCode;
Expand All @@ -29,9 +35,6 @@
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.json.JsonOutput;
import org.openqa.selenium.remote.tracing.Span;

import java.util.Collection;
import java.util.HashMap;
Expand All @@ -43,11 +46,24 @@

@AutoService(SdkTracerProviderConfigurer.class)
public class SeleniumSpanExporter implements SdkTracerProviderConfigurer {

private static final Logger LOG = Logger.getLogger(SeleniumSpanExporter.class.getName());
private static final ImmutableSet<String> EXCEPTION_ATTRIBUTES =
ImmutableSet.of("exception.message", "exception.stacktrace");
private final boolean httpLogs = OpenTelemetryTracer.getHttpLogs();

private static String getJsonString(Map<String, Object> map) {
StringBuilder text = new StringBuilder();
try (JsonOutput json = new Json().newOutput(text).setPrettyPrint(false)) {
json.write(map);
text.append('\n');
}
return text.toString();
}

@Override
public void configure(SdkTracerProviderBuilder tracerProvider, ConfigProperties configProperties) {
public void configure(SdkTracerProviderBuilder tracerProvider,
ConfigProperties configProperties) {
tracerProvider.addSpanProcessor(SimpleSpanProcessor.create(new SpanExporter() {
@Override
public CompletableResultCode export(Collection<SpanData> spans) {
Expand All @@ -67,6 +83,14 @@ public CompletableResultCode export(Collection<SpanData> spans) {

Attributes attributes = event.getAttributes();
map.put("attributes", attributes.asMap());

EXCEPTION_ATTRIBUTES.forEach(exceptionAttribute -> {
attributes.asMap().keySet()
.stream()
.filter(key -> exceptionAttribute.equalsIgnoreCase(key.getKey()))
.findFirst()
.ifPresent(key -> LOG.log(logLevel, attributes.asMap().get(key).toString()));
});
String jsonString = getJsonString(map);
LOG.log(logLevel, jsonString);
});
Expand All @@ -87,15 +111,6 @@ public CompletableResultCode shutdown() {
}));
}

private static String getJsonString(Map<String, Object> map) {
StringBuilder text = new StringBuilder();
try (JsonOutput json = new Json().newOutput(text).setPrettyPrint(false)) {
json.write(map);
text.append('\n');
}
return text.toString();
}

private Level getLogLevel(SpanData span) {
Level level = Level.FINE;

Expand Down

0 comments on commit ac145e5

Please sign in to comment.