diff --git a/span-stacktrace/src/main/java/io/opentelemetry/contrib/stacktrace/StackTraceSpanProcessor.java b/span-stacktrace/src/main/java/io/opentelemetry/contrib/stacktrace/StackTraceSpanProcessor.java index 541e97e02..62fb9ff34 100644 --- a/span-stacktrace/src/main/java/io/opentelemetry/contrib/stacktrace/StackTraceSpanProcessor.java +++ b/span-stacktrace/src/main/java/io/opentelemetry/contrib/stacktrace/StackTraceSpanProcessor.java @@ -53,7 +53,7 @@ public boolean isOnEndingRequired() { @Override public void onEnding(ReadWriteSpan span) { - if (minSpanDurationNanos < 0 || span.getLatencyNanos() < minSpanDurationNanos) { + if (span.getLatencyNanos() < minSpanDurationNanos) { return; } if (span.getAttribute(SPAN_STACKTRACE) != null) { diff --git a/span-stacktrace/src/test/java/io/opentelemetry/contrib/stacktrace/StackTraceSpanProcessorTest.java b/span-stacktrace/src/test/java/io/opentelemetry/contrib/stacktrace/StackTraceSpanProcessorTest.java index 96385b8c5..3d6d4686d 100644 --- a/span-stacktrace/src/test/java/io/opentelemetry/contrib/stacktrace/StackTraceSpanProcessorTest.java +++ b/span-stacktrace/src/test/java/io/opentelemetry/contrib/stacktrace/StackTraceSpanProcessorTest.java @@ -6,6 +6,7 @@ package io.opentelemetry.contrib.stacktrace; import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.SpanBuilder; @@ -35,6 +36,12 @@ private static long msToNs(int ms) { return Duration.ofMillis(ms).toNanos(); } + @Test + void tryInvalidMinDuration() { + assertThatCode(() -> new StackTraceSpanProcessor(-1, null)) + .isInstanceOf(IllegalArgumentException.class); + } + @Test void durationAndFiltering() { // on duration threshold @@ -45,7 +52,7 @@ void durationAndFiltering() { checkSpanWithoutStackTrace(YesPredicate.class, "2ms", msToNs(1)); // filtering out span - checkSpanWithoutStackTrace(NoPredicate.class, "1ms", 20); + checkSpanWithoutStackTrace(NoPredicate.class, "1ms", msToNs(20)); } public static class YesPredicate implements Predicate { @@ -99,7 +106,8 @@ private static void checkSpanWithStackTrace(String minDurationString, long spanD } private static void checkSpanWithoutStackTrace( - Class> predicateClass, String minDurationString, + Class> predicateClass, + String minDurationString, long spanDurationNanos) { checkSpan( predicateClass, @@ -120,21 +128,23 @@ private static void checkSpan( InMemorySpanExporter spansExporter = InMemorySpanExporter.create(); AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder(); - sdkBuilder.addPropertiesSupplier(() -> { - Map configMap = new HashMap<>(); - - configMap.put("otel.metrics.exporter", "none"); - configMap.put("otel.traces.exporter", "logging"); - configMap.put("otel.logs.exporter", "none"); - - if (minDurationString != null) { - configMap.put("otel.java.experimental.span-stacktrace.min.duration", minDurationString); - } - if (predicateClass != null) { - configMap.put("otel.java.experimental.span-stacktrace.filter", predicateClass.getName()); - } - return configMap; - }); + sdkBuilder.addPropertiesSupplier( + () -> { + Map configMap = new HashMap<>(); + + configMap.put("otel.metrics.exporter", "none"); + configMap.put("otel.traces.exporter", "logging"); + configMap.put("otel.logs.exporter", "none"); + + if (minDurationString != null) { + configMap.put("otel.java.experimental.span-stacktrace.min.duration", minDurationString); + } + if (predicateClass != null) { + configMap.put( + "otel.java.experimental.span-stacktrace.filter", predicateClass.getName()); + } + return configMap; + }); // duplicate export to our in-memory span exporter sdkBuilder.addSpanExporterCustomizer( (exporter, config) -> SpanExporter.composite(exporter, spansExporter));