Search in sources :

Example 6 with Token

use of com.newrelic.agent.bridge.Token in project newrelic-java-agent by newrelic.

the class TransactionPropagationTest method testEmptyFluxOnComplete.

@Test
public void testEmptyFluxOnComplete() {
    AtomicBoolean hadTransaction = new AtomicBoolean();
    inTransaction(() -> {
        Token token = createToken();
        Flux.empty().subscribeOn(Schedulers.elastic()).doOnComplete(() -> checkTransaction(hadTransaction)).subscriberContext(with(token)).blockFirst();
        token.expire();
    });
    assertCapturedData(hadTransaction);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Token(com.newrelic.agent.bridge.Token) Test(org.junit.Test)

Example 7 with Token

use of com.newrelic.agent.bridge.Token in project newrelic-java-agent by newrelic.

the class TransactionPropagationTest method testMonoRetryOnSuccess.

@Test
public void testMonoRetryOnSuccess() {
    AtomicBoolean hadTransaction = new AtomicBoolean();
    inTransaction(() -> {
        Token token = createToken();
        AtomicBoolean firstCall = new AtomicBoolean(true);
        Mono.create(monoSink -> inAnotherThread(() -> {
            if (firstCall.getAndSet(false))
                monoSink.error(new RuntimeException("failing the first call"));
            else
                monoSink.success(A_VALUE);
        })).doOnSuccess(v -> checkTransaction(hadTransaction)).retry(2).subscriberContext(with(token)).block();
        token.expire();
    });
    assertCapturedData(hadTransaction);
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) TokenLinkingSubscriber(com.nr.instrumentation.reactor.netty.TokenLinkingSubscriber) Trace(com.newrelic.api.agent.Trace) BeforeClass(org.junit.BeforeClass) Token(com.newrelic.agent.bridge.Token) Context(reactor.util.context.Context) RunWith(org.junit.runner.RunWith) Introspector(com.newrelic.agent.introspec.Introspector) Hooks(reactor.core.publisher.Hooks) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Matchers(org.hamcrest.Matchers) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) TokenLinkingSubscriber.tokenLift(com.nr.instrumentation.reactor.netty.TokenLinkingSubscriber.tokenLift) Consumer(java.util.function.Consumer) Flux(reactor.core.publisher.Flux) CountDownLatch(java.util.concurrent.CountDownLatch) AgentBridge(com.newrelic.agent.bridge.AgentBridge) Duration(java.time.Duration) Schedulers(reactor.core.scheduler.Schedulers) InstrumentationTestConfig(com.newrelic.agent.introspec.InstrumentationTestConfig) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) InstrumentationTestRunner(com.newrelic.agent.introspec.InstrumentationTestRunner) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Token(com.newrelic.agent.bridge.Token) Test(org.junit.Test)

Example 8 with Token

use of com.newrelic.agent.bridge.Token in project newrelic-java-agent by newrelic.

the class AbstractServerResponse_Instrumentation method writeTo.

public Mono<Void> writeTo(ServerWebExchange exchange, ServerResponse.Context context) {
    final Token token = (Token) exchange.getAttribute(Util.NR_TOKEN);
    if (token != null) {
        final Object pathPattern = exchange.getAttribute(RouterFunctions.MATCHING_PATTERN_ATTRIBUTE);
        String txnName = exchange.getAttribute(Util.NR_TXN_NAME);
        if (pathPattern != null) {
            // If the pattern string provided by Spring is available we should use it
            if (pathPattern instanceof PathPattern) {
                txnName = ((PathPattern) pathPattern).getPatternString();
            } else if (pathPattern instanceof String) {
                txnName = (String) pathPattern;
            }
        }
        final String methodName = " (" + exchange.getRequest().getMethod() + ")";
        if (txnName != null && statusCode != 404) {
            final String txnNameWithMethod = removeTrailingSlash(txnName) + methodName;
            token.getTransaction().setTransactionName(TransactionNamePriority.FRAMEWORK_HIGH, true, "Spring", txnNameWithMethod);
        } else {
            token.getTransaction().setTransactionName(TransactionNamePriority.FRAMEWORK_LOW, true, "Spring", "Unknown Route" + methodName);
        }
    }
    return Weaver.callOriginal();
}
Also used : PathPattern(org.springframework.web.util.pattern.PathPattern) Token(com.newrelic.agent.bridge.Token)

Example 9 with Token

use of com.newrelic.agent.bridge.Token in project newrelic-java-agent by newrelic.

the class HttpWebHandlerAdapter_Instrumentation method createExchange.

protected ServerWebExchange createExchange(ServerHttpRequest request, ServerHttpResponse response) {
    final com.newrelic.agent.bridge.Transaction transaction = AgentBridge.getAgent().getTransaction(false);
    final Token token = transaction == null ? null : transaction.getToken();
    if (response instanceof ReactiveHttpOutputMessage_Instrumentation) {
        ReactiveHttpOutputMessage_Instrumentation reactiveResponse = (ReactiveHttpOutputMessage_Instrumentation) response;
        reactiveResponse.token = token;
    }
    ServerWebExchange exchange = Weaver.callOriginal();
    if (token != null) {
        exchange.getAttributes().put(Util.NR_TOKEN, token);
    }
    return exchange;
}
Also used : ReactiveHttpOutputMessage_Instrumentation(org.springframework.http.ReactiveHttpOutputMessage_Instrumentation) Token(com.newrelic.agent.bridge.Token)

Example 10 with Token

use of com.newrelic.agent.bridge.Token in project newrelic-java-agent by newrelic.

the class ServerResponse_Instrumentation method writeTo.

public Mono<Void> writeTo(ServerWebExchange exchange, ServerResponse.Context context) {
    final Token token = (Token) exchange.getAttribute(Util.NR_TOKEN);
    if (token != null) {
        final PathPattern pathPattern = exchange.getAttribute("org.springframework.web.reactive.function.server.RouterFunctions.matchingPattern");
        String txnName = exchange.getAttribute(Util.NR_TXN_NAME);
        if (pathPattern != null) {
            // If the pattern string provided by Spring is available we should use it
            txnName = pathPattern.getPatternString();
        }
        final String methodName = " (" + exchange.getRequest().getMethod() + ")";
        if (txnName != null && statusCode().value() != 404) {
            final String txnNameWithMethod = removeTrailingSlash(txnName) + methodName;
            token.getTransaction().setTransactionName(TransactionNamePriority.FRAMEWORK_HIGH, true, "Spring", txnNameWithMethod);
        } else {
            token.getTransaction().setTransactionName(TransactionNamePriority.FRAMEWORK_LOW, true, "Spring", "Unknown Route" + methodName);
        }
    }
    return Weaver.callOriginal();
}
Also used : PathPattern(org.springframework.web.util.pattern.PathPattern) Token(com.newrelic.agent.bridge.Token)

Aggregations

Token (com.newrelic.agent.bridge.Token)20 Test (org.junit.Test)15 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 CountDownLatch (java.util.concurrent.CountDownLatch)10 AgentBridge (com.newrelic.agent.bridge.AgentBridge)8 Trace (com.newrelic.api.agent.Trace)7 InstrumentationTestConfig (com.newrelic.agent.introspec.InstrumentationTestConfig)6 InstrumentationTestRunner (com.newrelic.agent.introspec.InstrumentationTestRunner)6 Introspector (com.newrelic.agent.introspec.Introspector)6 TokenLinkingSubscriber (com.nr.instrumentation.reactor.netty.TokenLinkingSubscriber)6 TokenLinkingSubscriber.tokenLift (com.nr.instrumentation.reactor.netty.TokenLinkingSubscriber.tokenLift)6 Duration (java.time.Duration)6 Consumer (java.util.function.Consumer)6 CoreMatchers.is (org.hamcrest.CoreMatchers.is)6 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)6 Matchers (org.hamcrest.Matchers)6 Assert.assertTrue (org.junit.Assert.assertTrue)6 BeforeClass (org.junit.BeforeClass)6 RunWith (org.junit.runner.RunWith)6 Flux (reactor.core.publisher.Flux)6