Skip to content

Commit

Permalink
Change TracingOperator and TracingAssembly to accept configuration fr…
Browse files Browse the repository at this point in the history
…om Javaagent
  • Loading branch information
HaloFour committed Jun 1, 2021
1 parent af4313e commit ba4e686
Show file tree
Hide file tree
Showing 15 changed files with 219 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static net.bytebuddy.matcher.ElementMatchers.isTypeInitializer;
import static net.bytebuddy.matcher.ElementMatchers.named;

import io.opentelemetry.instrumentation.api.config.Config;
import io.opentelemetry.instrumentation.reactor.TracingOperator;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
Expand All @@ -31,7 +32,13 @@ public void transform(TypeTransformer transformer) {
public static class ResetOnEachOperatorAdvice {
@Advice.OnMethodExit(suppress = Throwable.class)
public static void postStaticInitializer() {
TracingOperator.registerOnEachOperator();
TracingOperator.newBuilder()
.setCaptureExperimentalSpanAttributes(
Config.get()
.getBooleanProperty(
"otel.instrumentation.reactor.experimental-span-attributes", false))
.build()
.registerOnEachOperator();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
package io.opentelemetry.instrumentation.reactor;

import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.config.Config;
import io.opentelemetry.instrumentation.api.tracer.async.AsyncSpanEndStrategies;
import java.util.function.BiFunction;
import java.util.function.Function;
Expand All @@ -35,28 +34,39 @@
import reactor.core.publisher.Operators;

/** Based on Spring Sleuth's Reactor instrumentation. */
public class TracingOperator {
public final class TracingOperator {

public static TracingOperator create() {
return newBuilder().build();
}

public static TracingOperatorBuilder newBuilder() {
return new TracingOperatorBuilder();
}

private final boolean captureExperimentalSpanAttributes;

TracingOperator(boolean captureExperimentalSpanAttributes) {
this.captureExperimentalSpanAttributes = captureExperimentalSpanAttributes;
}

/**
* Registers a hook that applies to every operator, propagating {@link Context} to downstream
* callbacks to ensure spans in the {@link Context} are available throughout the lifetime of a
* reactive stream. This should generally be called in a static initializer block in your
* application.
*/
public static void registerOnEachOperator() {
public void registerOnEachOperator() {
Hooks.onEachOperator(TracingSubscriber.class.getName(), tracingLift());
AsyncSpanEndStrategies.getInstance()
.registerStrategy(
ReactorAsyncSpanEndStrategy.newBuilder()
.setCaptureExperimentalSpanAttributes(
Config.get()
.getBooleanProperty(
"otel.instrumentation.reactor.experimental-span-attributes", false))
.setCaptureExperimentalSpanAttributes(captureExperimentalSpanAttributes)
.build());
}

/** Unregisters the hook registered by {@link #registerOnEachOperator()}. */
public static void resetOnEachOperator() {
public void resetOnEachOperator() {
Hooks.resetOnEachOperator(TracingSubscriber.class.getName());
AsyncSpanEndStrategies.getInstance().unregisterStrategy(ReactorAsyncSpanEndStrategy.class);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.reactor;

public final class TracingOperatorBuilder {
private boolean captureExperimentalSpanAttributes;

TracingOperatorBuilder() {}

public TracingOperatorBuilder setCaptureExperimentalSpanAttributes(
boolean captureExperimentalSpanAttributes) {
this.captureExperimentalSpanAttributes = captureExperimentalSpanAttributes;
return this;
}

public TracingOperator build() {
return new TracingOperator(captureExperimentalSpanAttributes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class HooksTest extends LibraryInstrumentationSpecification {

def "can reset out hooks"() {
setup:
def underTest = TracingOperator.create()
AtomicReference<CoreSubscriber> subscriber = new AtomicReference<>()

when: "no hook registered"
Expand All @@ -23,14 +24,14 @@ class HooksTest extends LibraryInstrumentationSpecification {
!(subscriber.get() instanceof TracingSubscriber)

when: "hook registered"
TracingOperator.registerOnEachOperator()
underTest.registerOnEachOperator()
new CapturingMono(subscriber).map { it + 1 }.subscribe()

then:
subscriber.get() instanceof TracingSubscriber

when: "hook reset"
TracingOperator.resetOnEachOperator()
underTest.resetOnEachOperator()
new CapturingMono(subscriber).map { it + 1 }.subscribe()

then:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
package io.opentelemetry.instrumentation.reactor

import io.opentelemetry.instrumentation.test.LibraryTestTrait
import spock.lang.Shared

class SubscriptionTest extends AbstractSubscriptionTest implements LibraryTestTrait {
@Shared
TracingOperator tracingOperator = TracingOperator.create()

def setupSpec() {
TracingOperator.registerOnEachOperator()
tracingOperator.registerOnEachOperator()
}

def cleanupSpec() {
TracingOperator.resetOnEachOperator()
tracingOperator.resetOnEachOperator()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.opentelemetry.instrumentation.rxjava2;

import io.opentelemetry.instrumentation.api.config.Config;
import java.util.concurrent.atomic.AtomicBoolean;

public final class TracingAssemblyActivation {
Expand All @@ -19,7 +20,13 @@ protected AtomicBoolean computeValue(Class<?> type) {

public static void activate(Class<?> clz) {
if (activated.get(clz).compareAndSet(false, true)) {
TracingAssembly.enable();
TracingAssembly.newBuilder()
.setCaptureExperimentalSpanAttributes(
Config.get()
.getBooleanProperty(
"otel.instrumentation.rxjava.experimental-span-attributes", false))
.build()
.enable();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.api.config.Config;
import io.opentelemetry.instrumentation.api.tracer.async.AsyncSpanEndStrategies;
import io.reactivex.Completable;
import io.reactivex.CompletableObserver;
Expand Down Expand Up @@ -90,50 +89,66 @@ public final class TracingAssembly {
@GuardedBy("TracingAssembly.class")
private static boolean enabled;

private TracingAssembly() {}
public static TracingAssembly create() {
return newBuilder().build();
}

public static synchronized void enable() {
if (enabled) {
return;
}
public static TracingAssemblyBuilder newBuilder() {
return new TracingAssemblyBuilder();
}

enableObservable();
private final boolean captureExperimentalSpanAttributes;

enableCompletable();
TracingAssembly(boolean captureExperimentalSpanAttributes) {
this.captureExperimentalSpanAttributes = captureExperimentalSpanAttributes;
}

enableSingle();
public void enable() {
synchronized (TracingAssembly.class) {
if (enabled) {
return;
}

enableMaybe();
enableObservable();

enableFlowable();
enableCompletable();

enableParallel();
enableSingle();

enableWithSpanStrategy();
enableMaybe();

enabled = true;
}
enableFlowable();

public static synchronized void disable() {
if (!enabled) {
return;
enableParallel();

enableWithSpanStrategy(captureExperimentalSpanAttributes);

enabled = true;
}
}

disableObservable();
public void disable() {
synchronized (TracingAssembly.class) {
if (!enabled) {
return;
}

disableCompletable();
disableObservable();

disableSingle();
disableCompletable();

disableMaybe();
disableSingle();

disableFlowable();
disableMaybe();

disableParallel();
disableFlowable();

disableWithSpanStrategy();
disableParallel();

enabled = false;
disableWithSpanStrategy();

enabled = false;
}
}

@SuppressWarnings({"rawtypes", "unchecked"})
Expand Down Expand Up @@ -223,14 +238,11 @@ private static void enableMaybe() {
}));
}

private static void enableWithSpanStrategy() {
private static void enableWithSpanStrategy(boolean captureExperimentalSpanAttributes) {
AsyncSpanEndStrategies.getInstance()
.registerStrategy(
RxJava2AsyncSpanEndStrategy.newBuilder()
.setCaptureExperimentalSpanAttributes(
Config.get()
.getBooleanProperty(
"otel.instrumentation.rxjava.experimental-span-attributes", false))
.setCaptureExperimentalSpanAttributes(captureExperimentalSpanAttributes)
.build());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.rxjava2;

public final class TracingAssemblyBuilder {
private boolean captureExperimentalSpanAttributes;

TracingAssemblyBuilder() {}

public TracingAssemblyBuilder setCaptureExperimentalSpanAttributes(
boolean captureExperimentalSpanAttributes) {
this.captureExperimentalSpanAttributes = captureExperimentalSpanAttributes;
return this;
}

public TracingAssembly build() {
return new TracingAssembly(captureExperimentalSpanAttributes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
import io.opentelemetry.instrumentation.rxjava2.AbstractRxJava2SubscriptionTest
import io.opentelemetry.instrumentation.rxjava2.TracingAssembly
import io.opentelemetry.instrumentation.test.LibraryTestTrait
import spock.lang.Shared

class RxJava2SubscriptionTest extends AbstractRxJava2SubscriptionTest implements LibraryTestTrait {
@Shared
TracingAssembly tracingAssembly = TracingAssembly.create()

def setupSpec() {
TracingAssembly.enable()
tracingAssembly.enable()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
import io.opentelemetry.instrumentation.rxjava2.AbstractRxJava2Test
import io.opentelemetry.instrumentation.rxjava2.TracingAssembly
import io.opentelemetry.instrumentation.test.LibraryTestTrait
import spock.lang.Shared

class RxJava2Test extends AbstractRxJava2Test implements LibraryTestTrait {
@Shared
TracingAssembly tracingAssembly = TracingAssembly.create()

def setupSpec() {
TracingAssembly.enable()
tracingAssembly.enable()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.opentelemetry.instrumentation.rxjava3;

import io.opentelemetry.instrumentation.api.config.Config;
import java.util.concurrent.atomic.AtomicBoolean;

public final class TracingAssemblyActivation {
Expand All @@ -19,7 +20,13 @@ protected AtomicBoolean computeValue(Class<?> type) {

public static void activate(Class<?> clz) {
if (activated.get(clz).compareAndSet(false, true)) {
TracingAssembly.enable();
TracingAssembly.newBuilder()
.setCaptureExperimentalSpanAttributes(
Config.get()
.getBooleanProperty(
"otel.instrumentation.rxjava.experimental-span-attributes", false))
.build()
.enable();
}
}

Expand Down
Loading

0 comments on commit ba4e686

Please sign in to comment.