1818package org .openqa .selenium .remote .tracing .opentelemetry ;
1919
2020import 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+
2127import io .opentelemetry .api .common .AttributeKey ;
2228import io .opentelemetry .api .common .Attributes ;
2329import io .opentelemetry .api .trace .StatusCode ;
2935import io .opentelemetry .sdk .trace .data .SpanData ;
3036import io .opentelemetry .sdk .trace .export .SimpleSpanProcessor ;
3137import 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
3639import java .util .Collection ;
3740import java .util .HashMap ;
4346
4447@ AutoService (SdkTracerProviderConfigurer .class )
4548public 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