Skip to content

Commit

Permalink
[java] skip logging of tracing spans if log level is not loggable
Browse files Browse the repository at this point in the history
  • Loading branch information
joerg1985 committed Aug 31, 2023
1 parent a6f37ca commit d5b6401
Showing 1 changed file with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class SeleniumSpanExporter {
private static final ImmutableSet<String> EXCEPTION_ATTRIBUTES =
ImmutableSet.of("exception.message", "exception.stacktrace");
private static final boolean httpLogs = OpenTelemetryTracer.getHttpLogs();
private static final AttributeKey<String> KEY_SPAN_KIND =
AttributeKey.stringKey(org.openqa.selenium.remote.tracing.AttributeKey.SPAN_KIND.getKey());

private static String getJsonString(Map<String, Object> map) {
StringBuilder text = new StringBuilder();
Expand All @@ -61,13 +63,18 @@ public static SpanProcessor getSpanProcessor() {
public CompletableResultCode export(Collection<SpanData> spans) {
spans.forEach(
span -> {
LOG.fine(String.valueOf(span));

String traceId = span.getTraceId();
List<EventData> 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<EventData> eventList = span.getEvents();
eventList.forEach(
event -> {
Map<String, Object> map = new HashMap<>();
Expand Down Expand Up @@ -111,17 +118,12 @@ public CompletableResultCode shutdown() {
private static Level getLogLevel(SpanData span) {
Level level = Level.FINE;

Optional<String> 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<String> 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)) {
Expand Down

0 comments on commit d5b6401

Please sign in to comment.