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.
*/
@FunctionalInterface
public interface EndTimeExtractor<RESPONSE> {
public interface EndTimeExtractor<REQUEST, RESPONSE> {
/** 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 ErrorCauseExtractor errorCauseExtractor;
@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 SpanSuppressionStrategy spanSuppressionStrategy;
@ -187,7 +187,7 @@ public class Instrumenter<REQUEST, RESPONSE> {
}
if (endTimeExtractor != null) {
span.end(endTimeExtractor.extract(response));
span.end(endTimeExtractor.extract(request, response));
} else {
span.end();
}

View File

@ -54,7 +54,7 @@ public final class InstrumenterBuilder<REQUEST, RESPONSE> {
SpanStatusExtractor.getDefault();
ErrorCauseExtractor errorCauseExtractor = ErrorCauseExtractor.jdk();
@Nullable StartTimeExtractor<REQUEST> startTimeExtractor = null;
@Nullable EndTimeExtractor<RESPONSE> endTimeExtractor = null;
@Nullable EndTimeExtractor<REQUEST, RESPONSE> endTimeExtractor = null;
boolean disabled = false;
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.
*/
public InstrumenterBuilder<REQUEST, RESPONSE> setTimeExtractors(
StartTimeExtractor<REQUEST> startTimeExtractor, EndTimeExtractor<RESPONSE> endTimeExtractor) {
StartTimeExtractor<REQUEST> startTimeExtractor,
EndTimeExtractor<REQUEST, RESPONSE> endTimeExtractor) {
this.startTimeExtractor = requireNonNull(startTimeExtractor);
this.endTimeExtractor = requireNonNull(endTimeExtractor);
return this;

View File

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

View File

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