Skip to content

Commit

Permalink
Add option to suppress controller and view spans
Browse files Browse the repository at this point in the history
  • Loading branch information
trask committed Aug 21, 2021
1 parent 5ff7901 commit 4945738
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public static <REQUEST, RESPONSE> InstrumenterBuilder<REQUEST, RESPONSE> newBuil
private final ErrorCauseExtractor errorCauseExtractor;
@Nullable private final StartTimeExtractor<REQUEST> startTimeExtractor;
@Nullable private final EndTimeExtractor<RESPONSE> endTimeExtractor;
private final boolean disabled;
private final SpanSuppressionStrategy spanSuppressionStrategy;

Instrumenter(InstrumenterBuilder<REQUEST, RESPONSE> builder) {
Expand All @@ -88,6 +89,7 @@ public static <REQUEST, RESPONSE> InstrumenterBuilder<REQUEST, RESPONSE> newBuil
this.errorCauseExtractor = builder.errorCauseExtractor;
this.startTimeExtractor = builder.startTimeExtractor;
this.endTimeExtractor = builder.endTimeExtractor;
this.disabled = builder.disabled;
this.spanSuppressionStrategy = builder.getSpanSuppressionStrategy();
}

Expand All @@ -98,6 +100,9 @@ public static <REQUEST, RESPONSE> InstrumenterBuilder<REQUEST, RESPONSE> newBuil
* without calling those methods.
*/
public boolean shouldStart(Context parentContext, REQUEST request) {
if (disabled) {
return false;
}
SpanKind spanKind = spanKindExtractor.extract(request);
boolean suppressed = spanSuppressionStrategy.shouldSuppress(parentContext, spanKind);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public final class InstrumenterBuilder<REQUEST, RESPONSE> {
ErrorCauseExtractor errorCauseExtractor = ErrorCauseExtractor.jdk();
@Nullable StartTimeExtractor<REQUEST> startTimeExtractor = null;
@Nullable EndTimeExtractor<RESPONSE> endTimeExtractor = null;
boolean disabled = false;

private boolean enableSpanSuppressionByType = ENABLE_SPAN_SUPPRESSION_BY_TYPE;

Expand Down Expand Up @@ -136,6 +137,11 @@ public InstrumenterBuilder<REQUEST, RESPONSE> setTimeExtractors(
return this;
}

public InstrumenterBuilder<REQUEST, RESPONSE> setDisabled(boolean disabled) {
this.disabled = disabled;
return this;
}

// visible for tests
/**
* Enables suppression based on client instrumentation type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@
package io.opentelemetry.javaagent.instrumentation.springwebmvc;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.config.Config;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import org.springframework.web.servlet.ModelAndView;

public final class SpringWebMvcSingletons {
private static final String INSTRUMENTATION_NAME = "io.opentelemetry.spring-webmvc-3.1";

private static final boolean SUPPRESS_CONTROLLER_SPANS =
Config.get()
.getBoolean("otel.instrumentation.common.experimental.suppress-controller-spans", false);

private static final boolean SUPPRESS_VIEW_SPANS =
Config.get()
.getBoolean("otel.instrumentation.common.experimental.suppress-view-spans", false);

private static final Instrumenter<Object, Void> HANDLER_INSTRUMENTER;

private static final Instrumenter<ModelAndView, Void> MODEL_AND_VIEW_INSTRUMENTER;
Expand All @@ -20,6 +29,7 @@ public final class SpringWebMvcSingletons {
HANDLER_INSTRUMENTER =
Instrumenter.<Object, Void>newBuilder(
GlobalOpenTelemetry.get(), INSTRUMENTATION_NAME, new HandlerSpanNameExtractor())
.setDisabled(SUPPRESS_CONTROLLER_SPANS)
.newInstrumenter();

MODEL_AND_VIEW_INSTRUMENTER =
Expand All @@ -28,6 +38,7 @@ public final class SpringWebMvcSingletons {
INSTRUMENTATION_NAME,
new ModelAndViewSpanNameExtractor())
.addAttributesExtractor(new ModelAndViewAttributesExtractor())
.setDisabled(SUPPRESS_VIEW_SPANS)
.newInstrumenter();
}

Expand Down

0 comments on commit 4945738

Please sign in to comment.