From d5b64014726a182f5c996630bc8f03f45ca948fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Sautter?= Date: Thu, 31 Aug 2023 17:18:19 +0200 Subject: [PATCH] [java] skip logging of tracing spans if log level is not loggable --- .../opentelemetry/SeleniumSpanExporter.java | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) 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 b9c93dda5185c..9045e0a8355f8 100644 --- a/java/src/org/openqa/selenium/remote/tracing/opentelemetry/SeleniumSpanExporter.java +++ b/java/src/org/openqa/selenium/remote/tracing/opentelemetry/SeleniumSpanExporter.java @@ -44,6 +44,8 @@ public class SeleniumSpanExporter { private static final ImmutableSet EXCEPTION_ATTRIBUTES = ImmutableSet.of("exception.message", "exception.stacktrace"); private static final boolean httpLogs = OpenTelemetryTracer.getHttpLogs(); + private static final AttributeKey KEY_SPAN_KIND = + AttributeKey.stringKey(org.openqa.selenium.remote.tracing.AttributeKey.SPAN_KIND.getKey()); private static String getJsonString(Map map) { StringBuilder text = new StringBuilder(); @@ -61,13 +63,18 @@ public static SpanProcessor getSpanProcessor() { public CompletableResultCode export(Collection spans) { spans.forEach( span -> { - LOG.fine(String.valueOf(span)); - - String traceId = span.getTraceId(); - List eventList = span.getEvents(); + if (LOG.isLoggable(Level.FINE)) { + LOG.fine(String.valueOf(span)); + } Level logLevel = getLogLevel(span); + if (!LOG.isLoggable(logLevel)) { + return; + } + + String traceId = span.getTraceId(); + List eventList = span.getEvents(); eventList.forEach( event -> { Map map = new HashMap<>(); @@ -111,17 +118,12 @@ public CompletableResultCode shutdown() { private static Level getLogLevel(SpanData span) { Level level = Level.FINE; - Optional kind = - Optional.ofNullable( - span.getAttributes() - .get( - AttributeKey.stringKey( - org.openqa.selenium.remote.tracing.AttributeKey.SPAN_KIND.getKey()))); - if (span.getStatus().getStatusCode() == StatusCode.ERROR) { level = Level.WARNING; - } else { - if (httpLogs && kind.isPresent()) { + } else if (httpLogs) { + Optional kind = Optional.ofNullable(span.getAttributes().get(KEY_SPAN_KIND)); + + if (kind.isPresent()) { String kindValue = kind.get(); if (Span.Kind.SERVER.name().equalsIgnoreCase(kindValue) || Span.Kind.CLIENT.name().equalsIgnoreCase(kindValue)) {