diff --git a/java/src/org/openqa/selenium/remote/tracing/opentelemetry/SeleniumSpanExporter.java b/java/src/org/openqa/selenium/remote/tracing/opentelemetry/SeleniumSpanExporter.java index cf3ae9547d93f..07e039355537a 100644 --- a/java/src/org/openqa/selenium/remote/tracing/opentelemetry/SeleniumSpanExporter.java +++ b/java/src/org/openqa/selenium/remote/tracing/opentelemetry/SeleniumSpanExporter.java @@ -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; @@ -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; @@ -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 EXCEPTION_ATTRIBUTES = + ImmutableSet.of("exception.message", "exception.stacktrace"); private final boolean httpLogs = OpenTelemetryTracer.getHttpLogs(); + private static String getJsonString(Map 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 spans) { @@ -67,6 +83,14 @@ public CompletableResultCode export(Collection 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); }); @@ -87,15 +111,6 @@ public CompletableResultCode shutdown() { })); } - private static String getJsonString(Map 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;