Skip to content

Commit

Permalink
Make behavior opt-in
Browse files Browse the repository at this point in the history
  • Loading branch information
trask committed Jul 12, 2024
1 parent 8d74151 commit 493ae80
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public static <REQUEST, RESPONSE> InstrumenterBuilder<REQUEST, RESPONSE> builder
private final ContextCustomizer<? super REQUEST>[] contextCustomizers;
private final OperationListener[] operationListeners;
private final ErrorCauseExtractor errorCauseExtractor;
private final boolean propagateOperationListenersToOnEnd;
private final boolean enabled;
private final SpanSuppressor spanSuppressor;

Expand All @@ -93,6 +94,7 @@ public static <REQUEST, RESPONSE> InstrumenterBuilder<REQUEST, RESPONSE> builder
this.contextCustomizers = builder.contextCustomizers.toArray(new ContextCustomizer[0]);
this.operationListeners = builder.buildOperationListeners().toArray(new OperationListener[0]);
this.errorCauseExtractor = builder.errorCauseExtractor;
this.propagateOperationListenersToOnEnd = builder.propagateOperationListenersToOnEnd;
this.enabled = builder.enabled;
this.spanSuppressor = builder.buildSpanSuppressor();
}
Expand Down Expand Up @@ -202,10 +204,12 @@ private Context doStart(Context parentContext, REQUEST request, @Nullable Instan
context = operationListeners[i].onStart(context, attributes, startNanos);
}
}
// when start and end are not called on the same instrumenter we need to use the operation
// listeners that were used during start and end to correctly handle metrics like
// http.server.active_requests that is recorded both in start and end
context = context.with(START_OPERATION_LISTENERS, operationListeners);
if (propagateOperationListenersToOnEnd) {
// when start and end are not called on the same instrumenter we need to use the operation
// listeners that were used during start and end to correctly handle metrics like
// http.server.active_requests that is recorded both in start and end
context = context.with(START_OPERATION_LISTENERS, operationListeners);
}

if (localRoot) {
context = LocalRootSpan.store(context, span);
Expand Down Expand Up @@ -236,11 +240,14 @@ private void doEnd(
}
span.setAllAttributes(attributes);

OperationListener[] startOperationListeners = context.get(START_OPERATION_LISTENERS);
if (startOperationListeners != null && startOperationListeners.length != 0) {
OperationListener[] operationListeners = context.get(START_OPERATION_LISTENERS);
if (operationListeners == null) {
operationListeners = this.operationListeners;
}
if (operationListeners.length != 0) {
long endNanos = getNanos(endTime);
for (int i = startOperationListeners.length - 1; i >= 0; i--) {
startOperationListeners[i].onEnd(context, attributes, endNanos);
for (int i = operationListeners.length - 1; i >= 0; i--) {
operationListeners[i].onEnd(context, attributes, endNanos);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public final class InstrumenterBuilder<REQUEST, RESPONSE> {
SpanStatusExtractor<? super REQUEST, ? super RESPONSE> spanStatusExtractor =
SpanStatusExtractor.getDefault();
ErrorCauseExtractor errorCauseExtractor = ErrorCauseExtractor.getDefault();
boolean propagateOperationListenersToOnEnd = false;
boolean enabled = true;

InstrumenterBuilder(
Expand Down Expand Up @@ -370,6 +371,10 @@ private Set<SpanKey> getSpanKeysFromAttributesExtractors() {
.collect(Collectors.toSet());
}

private void propagateOperationListenersToOnEnd() {
propagateOperationListenersToOnEnd = true;
}

private interface InstrumenterConstructor<RQ, RS> {
Instrumenter<RQ, RS> create(InstrumenterBuilder<RQ, RS> builder);

Expand Down Expand Up @@ -406,6 +411,12 @@ public <RQ, RS> Instrumenter<RQ, RS> buildDownstreamInstrumenter(
SpanKindExtractor<RQ> spanKindExtractor) {
return builder.buildDownstreamInstrumenter(setter, spanKindExtractor);
}

@Override
public <RQ, RS> void propagateOperationListenersToOnEnd(
InstrumenterBuilder<RQ, RS> builder) {
builder.propagateOperationListenersToOnEnd();
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ <REQUEST, RESPONSE> Instrumenter<REQUEST, RESPONSE> buildDownstreamInstrumenter(
InstrumenterBuilder<REQUEST, RESPONSE> builder,
TextMapSetter<REQUEST> setter,
SpanKindExtractor<REQUEST> spanKindExtractor);

<REQUEST, RESPONSE> void propagateOperationListenersToOnEnd(
InstrumenterBuilder<REQUEST,RESPONSE> builder);
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,11 @@ public static <REQUEST, RESPONSE> Instrumenter<REQUEST, RESPONSE> buildDownstrea
builder, setter, spanKindExtractor);
}

public static <REQUEST, RESPONSE> void propagateOperationListenersToOnEnd(
InstrumenterBuilder<REQUEST, RESPONSE> builder) {
// instrumenterBuilderAccess is guaranteed to be non-null here
instrumenterBuilderAccess.propagateOperationListenersToOnEnd(builder);
}

private InstrumenterUtil() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public final class Jetty11Singletons {
ServletInstrumenterBuilder.<HttpServletRequest, HttpServletResponse>create()
.addContextCustomizer(
(context, request, attributes) -> new AppServerBridge.Builder().init(context))
.propagateOperationListenersToOnEnd()
.build(INSTRUMENTATION_NAME, Servlet5Accessor.INSTANCE);

private static final JettyHelper<HttpServletRequest, HttpServletResponse> HELPER =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public final class Jetty8Singletons {
ServletInstrumenterBuilder.<HttpServletRequest, HttpServletResponse>create()
.addContextCustomizer(
(context, request, attributes) -> new AppServerBridge.Builder().init(context))
.propagateOperationListenersToOnEnd()
.build(INSTRUMENTATION_NAME, Servlet3Accessor.INSTANCE);

private static final JettyHelper<HttpServletRequest, HttpServletResponse> HELPER =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public final class LibertySingletons {
.addContextCustomizer(
(context, request, attributes) ->
new AppServerBridge.Builder().recordException().init(context))
.propagateOperationListenersToOnEnd()
.build(INSTRUMENTATION_NAME, Servlet3Accessor.INSTANCE);

private static final LibertyHelper<HttpServletRequest, HttpServletResponse> HELPER =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder;
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor;
import io.opentelemetry.instrumentation.api.internal.InstrumenterUtil;
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerAttributesExtractor;
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerAttributesGetter;
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerMetrics;
Expand All @@ -31,6 +32,8 @@ private ServletInstrumenterBuilder() {}
private final List<ContextCustomizer<? super ServletRequestContext<REQUEST>>> contextCustomizers =
new ArrayList<>();

private boolean propagateOperationListenersToOnEnd;

public static <REQUEST, RESPONSE> ServletInstrumenterBuilder<REQUEST, RESPONSE> create() {
return new ServletInstrumenterBuilder<>();
}
Expand All @@ -42,6 +45,12 @@ public ServletInstrumenterBuilder<REQUEST, RESPONSE> addContextCustomizer(
return this;
}

@CanIgnoreReturnValue
public ServletInstrumenterBuilder<REQUEST, RESPONSE> propagateOperationListenersToOnEnd() {
propagateOperationListenersToOnEnd = true;
return this;
}

public Instrumenter<ServletRequestContext<REQUEST>, ServletResponseContext<RESPONSE>> build(
String instrumentationName,
ServletAccessor<REQUEST, RESPONSE> accessor,
Expand Down Expand Up @@ -85,6 +94,9 @@ public Instrumenter<ServletRequestContext<REQUEST>, ServletResponseContext<RESPO
.addAttributesExtractor(HttpExperimentalAttributesExtractor.create(httpAttributesGetter))
.addOperationMetrics(HttpServerExperimentalMetrics.get());
}
if (propagateOperationListenersToOnEnd) {
InstrumenterUtil.propagateOperationListenersToOnEnd(builder);
}
return builder.buildServerInstrumenter(new ServletRequestGetter<>(accessor));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.opentelemetry.instrumentation.api.incubator.semconv.http.HttpServerExperimentalMetrics;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder;
import io.opentelemetry.instrumentation.api.internal.InstrumenterUtil;
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerAttributesExtractor;
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerMetrics;
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRoute;
Expand Down Expand Up @@ -61,6 +62,7 @@ public static <REQUEST, RESPONSE> Instrumenter<Request, Response> create(
.addAttributesExtractor(HttpExperimentalAttributesExtractor.create(httpAttributesGetter))
.addOperationMetrics(HttpServerExperimentalMetrics.get());
}
InstrumenterUtil.propagateOperationListenersToOnEnd(builder);
return builder.buildServerInstrumenter(TomcatRequestGetter.INSTANCE);
}
}

0 comments on commit 493ae80

Please sign in to comment.