Add request param to EndTimeExtractor (#3947)

This commit is contained in:
Mateusz Rzeszutek 2021-08-27 14:34:47 +02:00 committed by GitHub
parent ab9c688e7a
commit 585cc55921
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 12 additions and 10 deletions

View File

@ -14,8 +14,8 @@ import org.checkerframework.checker.nullness.qual.Nullable;
* must be comparable. * must be comparable.
*/ */
@FunctionalInterface @FunctionalInterface
public interface EndTimeExtractor<RESPONSE> { public interface EndTimeExtractor<REQUEST, RESPONSE> {
/** Returns the timestamp marking the end of the response processing. */ /** Returns the timestamp marking the end of the response processing. */
Instant extract(@Nullable RESPONSE response); Instant extract(REQUEST request, @Nullable RESPONSE response);
} }

View File

@ -72,7 +72,7 @@ public class Instrumenter<REQUEST, RESPONSE> {
private final List<? extends RequestListener> requestListeners; private final List<? extends RequestListener> requestListeners;
private final ErrorCauseExtractor errorCauseExtractor; private final ErrorCauseExtractor errorCauseExtractor;
@Nullable private final StartTimeExtractor<REQUEST> startTimeExtractor; @Nullable private final StartTimeExtractor<REQUEST> startTimeExtractor;
@Nullable private final EndTimeExtractor<RESPONSE> endTimeExtractor; @Nullable private final EndTimeExtractor<REQUEST, RESPONSE> endTimeExtractor;
private final boolean disabled; private final boolean disabled;
private final SpanSuppressionStrategy spanSuppressionStrategy; private final SpanSuppressionStrategy spanSuppressionStrategy;
@ -187,7 +187,7 @@ public class Instrumenter<REQUEST, RESPONSE> {
} }
if (endTimeExtractor != null) { if (endTimeExtractor != null) {
span.end(endTimeExtractor.extract(response)); span.end(endTimeExtractor.extract(request, response));
} else { } else {
span.end(); span.end();
} }

View File

@ -54,7 +54,7 @@ public final class InstrumenterBuilder<REQUEST, RESPONSE> {
SpanStatusExtractor.getDefault(); SpanStatusExtractor.getDefault();
ErrorCauseExtractor errorCauseExtractor = ErrorCauseExtractor.jdk(); ErrorCauseExtractor errorCauseExtractor = ErrorCauseExtractor.jdk();
@Nullable StartTimeExtractor<REQUEST> startTimeExtractor = null; @Nullable StartTimeExtractor<REQUEST> startTimeExtractor = null;
@Nullable EndTimeExtractor<RESPONSE> endTimeExtractor = null; @Nullable EndTimeExtractor<REQUEST, RESPONSE> endTimeExtractor = null;
boolean disabled = false; boolean disabled = false;
private boolean enableSpanSuppressionByType = ENABLE_SPAN_SUPPRESSION_BY_TYPE; private boolean enableSpanSuppressionByType = ENABLE_SPAN_SUPPRESSION_BY_TYPE;
@ -130,7 +130,8 @@ public final class InstrumenterBuilder<REQUEST, RESPONSE> {
* determining start and end timestamps to the OpenTelemetry SDK. * determining start and end timestamps to the OpenTelemetry SDK.
*/ */
public InstrumenterBuilder<REQUEST, RESPONSE> setTimeExtractors( public InstrumenterBuilder<REQUEST, RESPONSE> setTimeExtractors(
StartTimeExtractor<REQUEST> startTimeExtractor, EndTimeExtractor<RESPONSE> endTimeExtractor) { StartTimeExtractor<REQUEST> startTimeExtractor,
EndTimeExtractor<REQUEST, RESPONSE> endTimeExtractor) {
this.startTimeExtractor = requireNonNull(startTimeExtractor); this.startTimeExtractor = requireNonNull(startTimeExtractor);
this.endTimeExtractor = requireNonNull(endTimeExtractor); this.endTimeExtractor = requireNonNull(endTimeExtractor);
return this; return this;

View File

@ -487,7 +487,7 @@ class InstrumenterTest {
Instrumenter<Instant, Instant> instrumenter = Instrumenter<Instant, Instant> instrumenter =
Instrumenter.<Instant, Instant>newBuilder( Instrumenter.<Instant, Instant>newBuilder(
otelTesting.getOpenTelemetry(), "test", request -> "test span") otelTesting.getOpenTelemetry(), "test", request -> "test span")
.setTimeExtractors(request -> request, response -> response) .setTimeExtractors(request -> request, (request, response) -> response)
.newInstrumenter(); .newInstrumenter();
Instant startTime = Instant.ofEpochSecond(100); Instant startTime = Instant.ofEpochSecond(100);
@ -912,12 +912,12 @@ class InstrumenterTest {
} }
private static Instrumenter<Map<String, String>, Map<String, String>> getInstrumenterWithType( private static Instrumenter<Map<String, String>, Map<String, String>> getInstrumenterWithType(
boolean enableInstrumenation, AttributesExtractor... attributeExtractors) { boolean enableInstrumentation, AttributesExtractor... attributeExtractors) {
InstrumenterBuilder<Map<String, String>, Map<String, String>> builder = InstrumenterBuilder<Map<String, String>, Map<String, String>> builder =
Instrumenter.<Map<String, String>, Map<String, String>>newBuilder( Instrumenter.<Map<String, String>, Map<String, String>>newBuilder(
otelTesting.getOpenTelemetry(), "test", unused -> "span") otelTesting.getOpenTelemetry(), "test", unused -> "span")
.addAttributesExtractors(attributeExtractors) .addAttributesExtractors(attributeExtractors)
.enableInstrumentationTypeSuppression(enableInstrumenation); .enableInstrumentationTypeSuppression(enableInstrumentation);
return builder.newClientInstrumenter(Map::put); return builder.newClientInstrumenter(Map::put);
} }

View File

@ -36,7 +36,8 @@ public final class JmsSingletons {
Instrumenter.<MessageWithDestination, Void>newBuilder( Instrumenter.<MessageWithDestination, Void>newBuilder(
otel, INSTRUMENTATION_NAME, spanNameExtractor) otel, INSTRUMENTATION_NAME, spanNameExtractor)
.addAttributesExtractor(attributesExtractor) .addAttributesExtractor(attributesExtractor)
.setTimeExtractors(MessageWithDestination::getStartTime, response -> Instant.now()) .setTimeExtractors(
MessageWithDestination::getStartTime, (request, response) -> Instant.now())
.newInstrumenter(SpanKindExtractor.alwaysConsumer()); .newInstrumenter(SpanKindExtractor.alwaysConsumer());
LISTENER_INSTRUMENTER = LISTENER_INSTRUMENTER =
Instrumenter.<MessageWithDestination, Void>newBuilder( Instrumenter.<MessageWithDestination, Void>newBuilder(