Skip to content

Commit

Permalink
increase coverage + reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
SylvainJuge committed Oct 16, 2024
1 parent 3072467 commit 60451c0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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<ReadableSpan> {
Expand Down Expand Up @@ -99,7 +106,8 @@ private static void checkSpanWithStackTrace(String minDurationString, long spanD
}

private static void checkSpanWithoutStackTrace(
Class<? extends Predicate<?>> predicateClass, String minDurationString,
Class<? extends Predicate<?>> predicateClass,
String minDurationString,
long spanDurationNanos) {
checkSpan(
predicateClass,
Expand All @@ -120,21 +128,23 @@ private static void checkSpan(
InMemorySpanExporter spansExporter = InMemorySpanExporter.create();

AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder();
sdkBuilder.addPropertiesSupplier(() -> {
Map<String, String> 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<String, String> 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));
Expand Down

0 comments on commit 60451c0

Please sign in to comment.