-
Notifications
You must be signed in to change notification settings - Fork 873
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create spans for multiple subscriptions to traced Reactor publisher
- Loading branch information
Showing
5 changed files
with
308 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
.../library/src/main/java/io/opentelemetry/instrumentation/reactor/InstrumentedOperator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.reactor; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Function; | ||
import org.reactivestreams.Publisher; | ||
import reactor.core.CoreSubscriber; | ||
import reactor.core.Scannable; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
import reactor.core.publisher.Operators; | ||
|
||
final class InstrumentedOperator<REQUEST, RESPONSE, T> | ||
implements BiFunction<Scannable, CoreSubscriber<? super T>, CoreSubscriber<? super T>> { | ||
|
||
private final Instrumenter<REQUEST, RESPONSE> instrumenter; | ||
private final Context context; | ||
private final REQUEST request; | ||
private final Class<RESPONSE> responseType; | ||
private final boolean captureExperimentalSpanAttributes; | ||
private final AtomicBoolean firstSubscriber = new AtomicBoolean(true); | ||
|
||
static <REQUEST, RESPONSE, T> Mono<T> transformMono( | ||
Mono<T> mono, | ||
Instrumenter<REQUEST, RESPONSE> instrumenter, | ||
Context context, | ||
REQUEST request, | ||
Class<RESPONSE> responseType, | ||
boolean captureExperimentalSpanAttributes) { | ||
|
||
return mono.transform( | ||
InstrumentedOperator.<REQUEST, RESPONSE, T>tracingLift( | ||
instrumenter, context, request, responseType, captureExperimentalSpanAttributes)); | ||
} | ||
|
||
static <REQUEST, RESPONSE, T> Flux<T> transformFlux( | ||
Flux<T> flux, | ||
Instrumenter<REQUEST, RESPONSE> instrumenter, | ||
Context context, | ||
REQUEST request, | ||
Class<RESPONSE> responseType, | ||
boolean captureExperimentalSpanAttributes) { | ||
|
||
return flux.transform( | ||
InstrumentedOperator.<REQUEST, RESPONSE, T>tracingLift( | ||
instrumenter, context, request, responseType, captureExperimentalSpanAttributes)); | ||
} | ||
|
||
private static <REQUEST, RESPONSE, T> | ||
Function<? super Publisher<T>, ? extends Publisher<T>> tracingLift( | ||
Instrumenter<REQUEST, RESPONSE> instrumenter, | ||
Context context, | ||
REQUEST request, | ||
Class<RESPONSE> responseType, | ||
boolean captureExperimentalSpanAttributes) { | ||
|
||
return Operators.lift( | ||
new InstrumentedOperator<>( | ||
instrumenter, context, request, responseType, captureExperimentalSpanAttributes)); | ||
} | ||
|
||
private InstrumentedOperator( | ||
Instrumenter<REQUEST, RESPONSE> instrumenter, | ||
Context context, | ||
REQUEST request, | ||
Class<RESPONSE> responseType, | ||
boolean captureExperimentalSpanAttributes) { | ||
this.instrumenter = instrumenter; | ||
this.context = context; | ||
this.request = request; | ||
this.responseType = responseType; | ||
this.captureExperimentalSpanAttributes = captureExperimentalSpanAttributes; | ||
} | ||
|
||
@Override | ||
public CoreSubscriber<? super T> apply( | ||
Scannable scannable, CoreSubscriber<? super T> coreSubscriber) { | ||
|
||
if (isFirstSubscriber()) { | ||
return new InstrumentedSubscriber<>( | ||
instrumenter, | ||
context, | ||
request, | ||
responseType, | ||
captureExperimentalSpanAttributes, | ||
coreSubscriber); | ||
} | ||
|
||
Context parentContext = Context.current(); | ||
if (instrumenter.shouldStart(parentContext, request)) { | ||
Context context = instrumenter.start(parentContext, request); | ||
return new InstrumentedSubscriber<>( | ||
instrumenter, | ||
context, | ||
request, | ||
responseType, | ||
captureExperimentalSpanAttributes, | ||
coreSubscriber); | ||
} | ||
return coreSubscriber; | ||
} | ||
|
||
private boolean isFirstSubscriber() { | ||
return firstSubscriber.compareAndSet(true, false); | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
...ibrary/src/main/java/io/opentelemetry/instrumentation/reactor/InstrumentedSubscriber.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.reactor; | ||
|
||
import static io.opentelemetry.instrumentation.api.annotation.support.async.AsyncOperationEndSupport.tryToGetResponse; | ||
|
||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; | ||
import org.reactivestreams.Subscription; | ||
import reactor.core.CoreSubscriber; | ||
import reactor.core.publisher.Operators; | ||
|
||
final class InstrumentedSubscriber<REQUEST, RESPONSE, T> | ||
implements CoreSubscriber<T>, Subscription { | ||
|
||
private static final AttributeKey<Boolean> CANCELED_ATTRIBUTE_KEY = | ||
AttributeKey.booleanKey("reactor.canceled"); | ||
|
||
private final Instrumenter<REQUEST, RESPONSE> instrumenter; | ||
private final Context context; | ||
private final REQUEST request; | ||
private final Class<RESPONSE> responseType; | ||
private final boolean captureExperimentalSpanAttributes; | ||
private final CoreSubscriber<T> actual; | ||
private Subscription subscription; | ||
private T value; | ||
|
||
InstrumentedSubscriber( | ||
Instrumenter<REQUEST, RESPONSE> instrumenter, | ||
Context context, | ||
REQUEST request, | ||
Class<RESPONSE> responseType, | ||
boolean captureExperimentalSpanAttributes, | ||
CoreSubscriber<T> actual) { | ||
|
||
this.instrumenter = instrumenter; | ||
this.context = context; | ||
this.request = request; | ||
this.responseType = responseType; | ||
this.captureExperimentalSpanAttributes = captureExperimentalSpanAttributes; | ||
this.actual = actual; | ||
} | ||
|
||
@Override | ||
public void onSubscribe(Subscription subscription) { | ||
if (Operators.validate(this.subscription, subscription)) { | ||
this.subscription = subscription; | ||
actual.onSubscribe(this); | ||
} | ||
} | ||
|
||
@Override | ||
public void request(long count) { | ||
if (subscription != null) { | ||
subscription.request(count); | ||
} | ||
} | ||
|
||
@Override | ||
public void cancel() { | ||
if (subscription != null) { | ||
if (captureExperimentalSpanAttributes) { | ||
Span.fromContext(context).setAttribute(CANCELED_ATTRIBUTE_KEY, true); | ||
} | ||
instrumenter.end(context, request, null, null); | ||
subscription.cancel(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onNext(T value) { | ||
this.value = value; | ||
actual.onNext(value); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable error) { | ||
instrumenter.end(context, request, null, error); | ||
actual.onError(error); | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
instrumenter.end(context, request, tryToGetResponse(responseType, value), null); | ||
actual.onComplete(); | ||
} | ||
|
||
@Override | ||
public reactor.util.context.Context currentContext() { | ||
return actual.currentContext(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.