From 1854ba110c6235f259c3e39a02ca261b8714657d Mon Sep 17 00:00:00 2001 From: Thomas Spring Date: Fri, 15 Jan 2021 09:19:47 -0800 Subject: [PATCH 1/3] Add instrumentation for LambdaMonoSubscriber --- .../netty-reactor-0.8.0/build.gradle | 2 +- .../LambdaMonoSubscriber_Instrumentation.java | 53 +++++++++ .../http/server/HttpServerMetricsHandler.java | 7 ++ .../netty-reactor-0.9.0/NOTICE.txt | 17 +++ .../netty-reactor-0.9.0/build.gradle | 27 +++++ .../instrumentation/NettyReactorConfig.java | 10 ++ .../reactor/netty/TokenLinkingSubscriber.java | 101 ++++++++++++++++++ .../core/publisher/Hooks_Instrumentation.java | 12 +++ .../LambdaMonoSubscriber_Instrumentation.java | 46 ++++++++ .../SchedulerTask_Instrumentation.java | 15 +++ .../scheduler/WorkerTask_Instrumentation.java | 14 +++ .../HttpTrafficHandler_Instrumentation.java | 27 +++++ ...tiveHttpOutputMessage_Instrumentation.java | 26 ----- ...tiveHttpOutputMessage_Instrumentation.java | 26 ----- settings.gradle | 1 + 15 files changed, 331 insertions(+), 53 deletions(-) create mode 100644 instrumentation/netty-reactor-0.8.0/src/main/java/reactor/core/publisher/LambdaMonoSubscriber_Instrumentation.java create mode 100644 instrumentation/netty-reactor-0.8.0/src/main/java/reactor/netty/http/server/HttpServerMetricsHandler.java create mode 100644 instrumentation/netty-reactor-0.9.0/NOTICE.txt create mode 100644 instrumentation/netty-reactor-0.9.0/build.gradle create mode 100644 instrumentation/netty-reactor-0.9.0/src/main/java/com/nr/instrumentation/NettyReactorConfig.java create mode 100644 instrumentation/netty-reactor-0.9.0/src/main/java/com/nr/instrumentation/reactor/netty/TokenLinkingSubscriber.java create mode 100644 instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/publisher/Hooks_Instrumentation.java create mode 100644 instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/publisher/LambdaMonoSubscriber_Instrumentation.java create mode 100644 instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/scheduler/SchedulerTask_Instrumentation.java create mode 100644 instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/scheduler/WorkerTask_Instrumentation.java create mode 100644 instrumentation/netty-reactor-0.9.0/src/main/java/reactor/netty/http/server/HttpTrafficHandler_Instrumentation.java diff --git a/instrumentation/netty-reactor-0.8.0/build.gradle b/instrumentation/netty-reactor-0.8.0/build.gradle index 66b0498c64..7864915a80 100644 --- a/instrumentation/netty-reactor-0.8.0/build.gradle +++ b/instrumentation/netty-reactor-0.8.0/build.gradle @@ -18,7 +18,7 @@ jar { } verifyInstrumentation { - passesOnly 'io.projectreactor.netty:reactor-netty:[0.8.0.RELEASE,)' + passesOnly 'io.projectreactor.netty:reactor-netty:[0.8.0.RELEASE,0.9.0.RELEASE)' } site { diff --git a/instrumentation/netty-reactor-0.8.0/src/main/java/reactor/core/publisher/LambdaMonoSubscriber_Instrumentation.java b/instrumentation/netty-reactor-0.8.0/src/main/java/reactor/core/publisher/LambdaMonoSubscriber_Instrumentation.java new file mode 100644 index 0000000000..072637df28 --- /dev/null +++ b/instrumentation/netty-reactor-0.8.0/src/main/java/reactor/core/publisher/LambdaMonoSubscriber_Instrumentation.java @@ -0,0 +1,53 @@ +//package reactor.core.publisher; +// +//import com.newrelic.agent.bridge.AgentBridge; +//import com.newrelic.api.agent.NewRelic; +//import com.newrelic.api.agent.Token; +//import com.newrelic.api.agent.weaver.NewField; +//import com.newrelic.api.agent.weaver.Weave; +//import com.newrelic.api.agent.weaver.WeaveAllConstructors; +//import com.newrelic.api.agent.weaver.Weaver; +//import reactor.core.CoreSubscriber; +//import reactor.util.context.Context; +// +//@Weave(originalName = "reactor.core.publisher.LambdaMonoSubscriber") +//abstract class LambdaMonoSubscriber_Instrumentation { +// @NewField +// private Context nrContext; +// +// //final Context initialContext = Weaver.callOriginal(); +// +// @WeaveAllConstructors +// protected LambdaMonoSubscriber_Instrumentation() { +// if (AgentBridge.getAgent().getTransaction(false) != null) { +// nrContext = Context.of("newrelic-token", NewRelic.getAgent().getTransaction().getToken()); +// } +// //this.initialContext = Weaver.callOriginal(); +//; +// } +// +//// public final void onSubscribe(Subscription s) { +//// Weaver.callOriginal(); +//// //((FluxContextWrite.ContextWriteSubscriber) ((FluxOnAssembly.OnAssemblySubscriber) subscription).s).currentContext(); +//// +//// } +// public final void onComplete() { +// Token token = this.currentContext().getOrDefault("newrelic-token", null); +// if (token != null) { +// token.expire(); +// token = null; +// this.nrContext = null; +// +// } +// } +// +// public Context currentContext() { +//// AgentBridge.activeToken.get(); +// if (nrContext != null) { +// return nrContext; +// //return initialContext.putAll(nrContext); +// } +// return Weaver.callOriginal(); +// } +// +//} diff --git a/instrumentation/netty-reactor-0.8.0/src/main/java/reactor/netty/http/server/HttpServerMetricsHandler.java b/instrumentation/netty-reactor-0.8.0/src/main/java/reactor/netty/http/server/HttpServerMetricsHandler.java new file mode 100644 index 0000000000..65a36cd64b --- /dev/null +++ b/instrumentation/netty-reactor-0.8.0/src/main/java/reactor/netty/http/server/HttpServerMetricsHandler.java @@ -0,0 +1,7 @@ +package reactor.netty.http.server; + +import com.newrelic.api.agent.weaver.SkipIfPresent; + +@SkipIfPresent +final class HttpServerMetricsHandler { +} diff --git a/instrumentation/netty-reactor-0.9.0/NOTICE.txt b/instrumentation/netty-reactor-0.9.0/NOTICE.txt new file mode 100644 index 0000000000..a59806f16b --- /dev/null +++ b/instrumentation/netty-reactor-0.9.0/NOTICE.txt @@ -0,0 +1,17 @@ +This product contains a modified part of OpenTelemetry: + + * License: + +Copyright 2019 The OpenTelemetry Authors + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except +in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License +is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express +or implied. See the License for the specific language governing permissions and limitations under +the License. + + * Homepage: https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/master/LICENSE diff --git a/instrumentation/netty-reactor-0.9.0/build.gradle b/instrumentation/netty-reactor-0.9.0/build.gradle new file mode 100644 index 0000000000..a8a651cdda --- /dev/null +++ b/instrumentation/netty-reactor-0.9.0/build.gradle @@ -0,0 +1,27 @@ +dependencies { + implementation(project(":agent-bridge")) + implementation("io.projectreactor.netty:reactor-netty:0.9.0.RELEASE") +} + +sourceCompatibility = JavaVersion.VERSION_1_8 +targetCompatibility = JavaVersion.VERSION_1_8 + +compileTestJava { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 +} + +compileJava.options.bootstrapClasspath = null + +jar { + manifest { attributes 'Implementation-Title': 'com.newrelic.instrumentation.netty-reactor-0.9.0' } +} + +verifyInstrumentation { + passesOnly 'io.projectreactor.netty:reactor-netty:[0.9.0.RELEASE,)' +} + +site { + title 'Netty Reactor' + type 'Appserver' +} \ No newline at end of file diff --git a/instrumentation/netty-reactor-0.9.0/src/main/java/com/nr/instrumentation/NettyReactorConfig.java b/instrumentation/netty-reactor-0.9.0/src/main/java/com/nr/instrumentation/NettyReactorConfig.java new file mode 100644 index 0000000000..e30e0f059f --- /dev/null +++ b/instrumentation/netty-reactor-0.9.0/src/main/java/com/nr/instrumentation/NettyReactorConfig.java @@ -0,0 +1,10 @@ +package com.nr.instrumentation; + +import com.newrelic.api.agent.NewRelic; + +public class NettyReactorConfig { + public static final boolean errorsEnabled = NewRelic.getAgent().getConfig().getValue("reactor-netty.errors.enabled", true); + + private NettyReactorConfig() { + } +} diff --git a/instrumentation/netty-reactor-0.9.0/src/main/java/com/nr/instrumentation/reactor/netty/TokenLinkingSubscriber.java b/instrumentation/netty-reactor-0.9.0/src/main/java/com/nr/instrumentation/reactor/netty/TokenLinkingSubscriber.java new file mode 100644 index 0000000000..b178c459c0 --- /dev/null +++ b/instrumentation/netty-reactor-0.9.0/src/main/java/com/nr/instrumentation/reactor/netty/TokenLinkingSubscriber.java @@ -0,0 +1,101 @@ +package com.nr.instrumentation.reactor.netty; + +import com.newrelic.agent.bridge.AgentBridge; +import com.newrelic.api.agent.NewRelic; +import com.newrelic.api.agent.Token; +import com.newrelic.api.agent.Trace; +import com.nr.instrumentation.NettyReactorConfig; +import org.reactivestreams.Publisher; +import org.reactivestreams.Subscriber; +import org.reactivestreams.Subscription; +import reactor.core.CoreSubscriber; +import reactor.core.Fuseable; +import reactor.core.Scannable; +import reactor.core.publisher.Operators; +import reactor.util.context.Context; + +import java.util.function.BiFunction; +import java.util.function.Function; + +// Based on OpenTelemetry code +// https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/master/instrumentation-core/reactor-3.1/src/main/java/io/opentelemetry/instrumentation/reactor/TracingSubscriber.java +public class TokenLinkingSubscriber implements CoreSubscriber { + private final Token token; + private final Subscriber subscriber; + private Context context; + + public TokenLinkingSubscriber(Subscriber subscriber, Context ctx) { + this.subscriber = subscriber; + this.context = ctx; + // newrelic-token is added by spring-webflux instrumentation + this.token = ctx.getOrDefault("newrelic-token", null); + } + + @Override + public void onSubscribe(Subscription subscription) { + withNRToken(() -> subscriber.onSubscribe(subscription)); + } + + @Override + public void onNext(T o) { + withNRToken(() -> subscriber.onNext(o)); + } + + @Override + public void onError(Throwable throwable) { + withNRError(() -> subscriber.onError(throwable), throwable); + } + + @Override + public void onComplete() { + subscriber.onComplete(); + } + + @Override + public Context currentContext() { + return context; + } + + @Trace(async = true, excludeFromTransactionTrace = true) + private void withNRToken(Runnable runnable) { + if (token != null && AgentBridge.getAgent().getTransaction(false) == null) { + token.link(); + } + runnable.run(); + } + + @Trace(async = true, excludeFromTransactionTrace = true) + private void withNRError(Runnable runnable, Throwable throwable) { + if (token != null && token.isActive()) { + token.linkAndExpire(); + if (NettyReactorConfig.errorsEnabled) { + NewRelic.noticeError(throwable); + } + } + runnable.run(); + } + + public static Function, ? extends Publisher> tokenLift() { + return Operators.lift(new TokenLifter<>()); + } + + private static class TokenLifter + implements BiFunction, CoreSubscriber> { + + public TokenLifter() { + } + + @Override + public CoreSubscriber apply(Scannable publisher, CoreSubscriber sub) { + // if Flux/Mono #just, #empty, #error + if (publisher instanceof Fuseable.ScalarCallable) { + return sub; + } + Token token = sub.currentContext().getOrDefault("newrelic-token", null); + if (token != null ) { + return new TokenLinkingSubscriber<>(sub, sub.currentContext()); + } + return sub; + } + } +} \ No newline at end of file diff --git a/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/publisher/Hooks_Instrumentation.java b/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/publisher/Hooks_Instrumentation.java new file mode 100644 index 0000000000..0ea6c1d178 --- /dev/null +++ b/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/publisher/Hooks_Instrumentation.java @@ -0,0 +1,12 @@ +package reactor.core.publisher; + +import com.newrelic.api.agent.weaver.NewField; +import com.newrelic.api.agent.weaver.Weave; + +import java.util.concurrent.atomic.AtomicBoolean; + +@Weave(originalName = "reactor.core.publisher.Hooks") +public abstract class Hooks_Instrumentation { + @NewField + public static AtomicBoolean instrumented = new AtomicBoolean(false); +} diff --git a/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/publisher/LambdaMonoSubscriber_Instrumentation.java b/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/publisher/LambdaMonoSubscriber_Instrumentation.java new file mode 100644 index 0000000000..24fd833930 --- /dev/null +++ b/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/publisher/LambdaMonoSubscriber_Instrumentation.java @@ -0,0 +1,46 @@ +package reactor.core.publisher; + +import com.newrelic.agent.bridge.AgentBridge; +import com.newrelic.api.agent.NewRelic; +import com.newrelic.api.agent.Token; +import com.newrelic.api.agent.weaver.NewField; +import com.newrelic.api.agent.weaver.Weave; +import com.newrelic.api.agent.weaver.WeaveAllConstructors; +import com.newrelic.api.agent.weaver.Weaver; +import reactor.core.CoreSubscriber; +import reactor.util.context.Context; + +@Weave(originalName = "reactor.core.publisher.LambdaMonoSubscriber") +abstract class LambdaMonoSubscriber_Instrumentation { + @NewField + private Context nrContext; + final Context initialContext = Weaver.callOriginal(); + + + @WeaveAllConstructors + protected LambdaMonoSubscriber_Instrumentation() { + if (AgentBridge.getAgent().getTransaction(false) != null + && initialContext.getOrDefault("newrelic-token", null) == null) { + nrContext = Context.of("newrelic-token", NewRelic.getAgent().getTransaction().getToken()); + } + } + + public final void onComplete() { + Token token = this.currentContext().getOrDefault("newrelic-token", null); + if (token != null) { + token.expire(); + token = null; + this.nrContext = null; + + } + } + + public Context currentContext() { + if (nrContext != null) { + //return nrContext; + return initialContext.putAll(nrContext); + } + return Weaver.callOriginal(); + } + +} diff --git a/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/scheduler/SchedulerTask_Instrumentation.java b/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/scheduler/SchedulerTask_Instrumentation.java new file mode 100644 index 0000000000..3b731b4a25 --- /dev/null +++ b/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/scheduler/SchedulerTask_Instrumentation.java @@ -0,0 +1,15 @@ +package reactor.core.scheduler; + +import com.newrelic.api.agent.Trace; +import com.newrelic.api.agent.weaver.Weave; +import com.newrelic.api.agent.weaver.Weaver; + +@Weave(originalName = "reactor.core.scheduler.SchedulerTask") +final class SchedulerTask_Instrumentation { + + // We need to be able to link the Token here when executing on a supplied Scheduler via Mono::publishOn + @Trace(async = true, excludeFromTransactionTrace = true) + public Void call() { + return Weaver.callOriginal(); + } +} diff --git a/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/scheduler/WorkerTask_Instrumentation.java b/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/scheduler/WorkerTask_Instrumentation.java new file mode 100644 index 0000000000..281f0c4356 --- /dev/null +++ b/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/scheduler/WorkerTask_Instrumentation.java @@ -0,0 +1,14 @@ +package reactor.core.scheduler; + +import com.newrelic.api.agent.Trace; +import com.newrelic.api.agent.weaver.Weave; +import com.newrelic.api.agent.weaver.Weaver; + +@Weave(originalName = "reactor.core.scheduler.WorkerTask") +final class WorkerTask_Instrumentation { + + @Trace(async = true, excludeFromTransactionTrace = true) + public Void call() { + return Weaver.callOriginal(); + } +} diff --git a/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/netty/http/server/HttpTrafficHandler_Instrumentation.java b/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/netty/http/server/HttpTrafficHandler_Instrumentation.java new file mode 100644 index 0000000000..caeb301cde --- /dev/null +++ b/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/netty/http/server/HttpTrafficHandler_Instrumentation.java @@ -0,0 +1,27 @@ +/* + * + * * Copyright 2020 New Relic Corporation. All rights reserved. + * * SPDX-License-Identifier: Apache-2.0 + * + */ + +package reactor.netty.http.server; + +import com.newrelic.api.agent.weaver.Weave; +import com.newrelic.api.agent.weaver.Weaver; +import com.nr.instrumentation.reactor.netty.TokenLinkingSubscriber; +import io.netty.channel.ChannelHandlerContext; +import reactor.core.publisher.Hooks; +import reactor.core.publisher.Hooks_Instrumentation; + +import static com.nr.instrumentation.reactor.netty.TokenLinkingSubscriber.tokenLift; + +@Weave(originalName = "reactor.netty.http.server.HttpTrafficHandler") +class HttpTrafficHandler_Instrumentation { + public void channelRead(ChannelHandlerContext ctx, Object msg) { + if (!Hooks_Instrumentation.instrumented.getAndSet(true)) { + Hooks.onEachOperator(TokenLinkingSubscriber.class.getName(), tokenLift()); + } + Weaver.callOriginal(); + } +} diff --git a/instrumentation/spring-webflux-5.1.0/src/main/java/org/springframework/http/ReactiveHttpOutputMessage_Instrumentation.java b/instrumentation/spring-webflux-5.1.0/src/main/java/org/springframework/http/ReactiveHttpOutputMessage_Instrumentation.java index ed7ce4f983..5f988a40f0 100644 --- a/instrumentation/spring-webflux-5.1.0/src/main/java/org/springframework/http/ReactiveHttpOutputMessage_Instrumentation.java +++ b/instrumentation/spring-webflux-5.1.0/src/main/java/org/springframework/http/ReactiveHttpOutputMessage_Instrumentation.java @@ -23,32 +23,6 @@ public class ReactiveHttpOutputMessage_Instrumentation { @NewField public Token token; - public Mono writeWith(Publisher body) { - try { - if (this.token != null) { - this.token.expire(); - this.token = null; - } - } catch (Throwable t) { - AgentBridge.instrumentation.noticeInstrumentationError(t, Weaver.getImplementationTitle()); - } - - return Weaver.callOriginal(); - } - - public Mono writeAndFlushWith( - Publisher> body) { - try { - if (this.token != null) { - this.token.expire(); - this.token = null; - } - } catch (Throwable t) { - AgentBridge.instrumentation.noticeInstrumentationError(t, Weaver.getImplementationTitle()); - } - return Weaver.callOriginal(); - } - public Mono setComplete() { try { if (this.token != null) { diff --git a/instrumentation/spring-webflux-5.3.0/src/main/java/org/springframework/http/ReactiveHttpOutputMessage_Instrumentation.java b/instrumentation/spring-webflux-5.3.0/src/main/java/org/springframework/http/ReactiveHttpOutputMessage_Instrumentation.java index ed7ce4f983..5f988a40f0 100644 --- a/instrumentation/spring-webflux-5.3.0/src/main/java/org/springframework/http/ReactiveHttpOutputMessage_Instrumentation.java +++ b/instrumentation/spring-webflux-5.3.0/src/main/java/org/springframework/http/ReactiveHttpOutputMessage_Instrumentation.java @@ -23,32 +23,6 @@ public class ReactiveHttpOutputMessage_Instrumentation { @NewField public Token token; - public Mono writeWith(Publisher body) { - try { - if (this.token != null) { - this.token.expire(); - this.token = null; - } - } catch (Throwable t) { - AgentBridge.instrumentation.noticeInstrumentationError(t, Weaver.getImplementationTitle()); - } - - return Weaver.callOriginal(); - } - - public Mono writeAndFlushWith( - Publisher> body) { - try { - if (this.token != null) { - this.token.expire(); - this.token = null; - } - } catch (Throwable t) { - AgentBridge.instrumentation.noticeInstrumentationError(t, Weaver.getImplementationTitle()); - } - return Weaver.callOriginal(); - } - public Mono setComplete() { try { if (this.token != null) { diff --git a/settings.gradle b/settings.gradle index 5082816197..7856d4f3de 100644 --- a/settings.gradle +++ b/settings.gradle @@ -174,6 +174,7 @@ include 'instrumentation:netty-4.0.0' include 'instrumentation:netty-4.0.8' include 'instrumentation:netty-reactor-0.7.0' include 'instrumentation:netty-reactor-0.8.0' +include 'instrumentation:netty-reactor-0.9.0' include 'instrumentation:okhttp-3.0.0' include 'instrumentation:okhttp-3.4.0' include 'instrumentation:okhttp-3.5.0' From cba93da8f3221a2ad9d2b950607c3278ae8c11ec Mon Sep 17 00:00:00 2001 From: Thomas Spring Date: Fri, 15 Jan 2021 12:16:52 -0800 Subject: [PATCH 2/3] Weave LambdaSubscriber for Flux --- .../LambdaMonoSubscriber_Instrumentation.java | 53 ------------------- .../LambdaMonoSubscriber_Instrumentation.java | 25 +++++---- .../LambdaSubscriber_Instrumentation.java | 44 +++++++++++++++ 3 files changed, 56 insertions(+), 66 deletions(-) delete mode 100644 instrumentation/netty-reactor-0.8.0/src/main/java/reactor/core/publisher/LambdaMonoSubscriber_Instrumentation.java create mode 100644 instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/publisher/LambdaSubscriber_Instrumentation.java diff --git a/instrumentation/netty-reactor-0.8.0/src/main/java/reactor/core/publisher/LambdaMonoSubscriber_Instrumentation.java b/instrumentation/netty-reactor-0.8.0/src/main/java/reactor/core/publisher/LambdaMonoSubscriber_Instrumentation.java deleted file mode 100644 index 072637df28..0000000000 --- a/instrumentation/netty-reactor-0.8.0/src/main/java/reactor/core/publisher/LambdaMonoSubscriber_Instrumentation.java +++ /dev/null @@ -1,53 +0,0 @@ -//package reactor.core.publisher; -// -//import com.newrelic.agent.bridge.AgentBridge; -//import com.newrelic.api.agent.NewRelic; -//import com.newrelic.api.agent.Token; -//import com.newrelic.api.agent.weaver.NewField; -//import com.newrelic.api.agent.weaver.Weave; -//import com.newrelic.api.agent.weaver.WeaveAllConstructors; -//import com.newrelic.api.agent.weaver.Weaver; -//import reactor.core.CoreSubscriber; -//import reactor.util.context.Context; -// -//@Weave(originalName = "reactor.core.publisher.LambdaMonoSubscriber") -//abstract class LambdaMonoSubscriber_Instrumentation { -// @NewField -// private Context nrContext; -// -// //final Context initialContext = Weaver.callOriginal(); -// -// @WeaveAllConstructors -// protected LambdaMonoSubscriber_Instrumentation() { -// if (AgentBridge.getAgent().getTransaction(false) != null) { -// nrContext = Context.of("newrelic-token", NewRelic.getAgent().getTransaction().getToken()); -// } -// //this.initialContext = Weaver.callOriginal(); -//; -// } -// -//// public final void onSubscribe(Subscription s) { -//// Weaver.callOriginal(); -//// //((FluxContextWrite.ContextWriteSubscriber) ((FluxOnAssembly.OnAssemblySubscriber) subscription).s).currentContext(); -//// -//// } -// public final void onComplete() { -// Token token = this.currentContext().getOrDefault("newrelic-token", null); -// if (token != null) { -// token.expire(); -// token = null; -// this.nrContext = null; -// -// } -// } -// -// public Context currentContext() { -//// AgentBridge.activeToken.get(); -// if (nrContext != null) { -// return nrContext; -// //return initialContext.putAll(nrContext); -// } -// return Weaver.callOriginal(); -// } -// -//} diff --git a/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/publisher/LambdaMonoSubscriber_Instrumentation.java b/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/publisher/LambdaMonoSubscriber_Instrumentation.java index 24fd833930..34d94bbbad 100644 --- a/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/publisher/LambdaMonoSubscriber_Instrumentation.java +++ b/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/publisher/LambdaMonoSubscriber_Instrumentation.java @@ -7,37 +7,36 @@ import com.newrelic.api.agent.weaver.Weave; import com.newrelic.api.agent.weaver.WeaveAllConstructors; import com.newrelic.api.agent.weaver.Weaver; -import reactor.core.CoreSubscriber; import reactor.util.context.Context; @Weave(originalName = "reactor.core.publisher.LambdaMonoSubscriber") -abstract class LambdaMonoSubscriber_Instrumentation { +abstract class LambdaMonoSubscriber_Instrumentation { @NewField - private Context nrContext; + private Context nrContext; final Context initialContext = Weaver.callOriginal(); - @WeaveAllConstructors protected LambdaMonoSubscriber_Instrumentation() { + // LamdaMonoSubscriber creates a new Context, so we create a new token and put it on the Context + // to be linked by TokenLinkingSubscriber but expired on onComplete here if (AgentBridge.getAgent().getTransaction(false) != null - && initialContext.getOrDefault("newrelic-token", null) == null) { + && initialContext.getOrDefault("newrelic-token", null) == null) { nrContext = Context.of("newrelic-token", NewRelic.getAgent().getTransaction().getToken()); } } - public final void onComplete() { - Token token = this.currentContext().getOrDefault("newrelic-token", null); - if (token != null) { - token.expire(); - token = null; - this.nrContext = null; + public final void onComplete() { + Token token = this.currentContext().getOrDefault("newrelic-token", null); + if (token != null) { + token.expire(); + token = null; + this.nrContext = null; - } } + } public Context currentContext() { if (nrContext != null) { - //return nrContext; return initialContext.putAll(nrContext); } return Weaver.callOriginal(); diff --git a/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/publisher/LambdaSubscriber_Instrumentation.java b/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/publisher/LambdaSubscriber_Instrumentation.java new file mode 100644 index 0000000000..71f559327f --- /dev/null +++ b/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/publisher/LambdaSubscriber_Instrumentation.java @@ -0,0 +1,44 @@ +package reactor.core.publisher; + +import com.newrelic.agent.bridge.AgentBridge; +import com.newrelic.api.agent.NewRelic; +import com.newrelic.api.agent.Token; +import com.newrelic.api.agent.weaver.NewField; +import com.newrelic.api.agent.weaver.Weave; +import com.newrelic.api.agent.weaver.WeaveAllConstructors; +import com.newrelic.api.agent.weaver.Weaver; +import reactor.util.context.Context; + +@Weave(originalName = "reactor.core.publisher.LambdaSubscriber") +abstract class LambdaSubscriber_Instrumentation { + final Context initialContext = Weaver.callOriginal(); + @NewField + private Context nrContext; + + @WeaveAllConstructors + protected LambdaSubscriber_Instrumentation() { + if (AgentBridge.getAgent().getTransaction(false) != null + && initialContext.getOrDefault("newrelic-token", null) == null) { + nrContext = Context.of("newrelic-token", NewRelic.getAgent().getTransaction().getToken()); + } + } + + public final void onComplete() { + Token token = this.currentContext().getOrDefault("newrelic-token", null); + if (token != null) { + token.expire(); + token = null; + this.nrContext = null; + + } + } + + public Context currentContext() { + if (nrContext != null) { + //return nrContext; + return initialContext.putAll(nrContext); + } + return Weaver.callOriginal(); + } + +} From 3bdf0992e62b444cf41099aa32c634891651f89e Mon Sep 17 00:00:00 2001 From: Thomas Spring Date: Wed, 20 Jan 2021 07:31:56 -0800 Subject: [PATCH 3/3] add license headers --- .../com/nr/instrumentation/NettyReactorConfig.java | 7 +++++++ .../reactor/core/publisher/Hooks_Instrumentation.java | 7 +++++++ .../com/nr/instrumentation/NettyReactorConfig.java | 10 +++++++++- .../reactor/core/publisher/Hooks_Instrumentation.java | 7 +++++++ .../core/scheduler/SchedulerTask_Instrumentation.java | 7 +++++++ .../core/scheduler/WorkerTask_Instrumentation.java | 7 +++++++ .../com/nr/instrumentation/NettyReactorConfig.java | 10 +++++++++- .../reactor/core/publisher/Hooks_Instrumentation.java | 7 +++++++ .../LambdaMonoSubscriber_Instrumentation.java | 8 +++++++- .../publisher/LambdaSubscriber_Instrumentation.java | 8 +++++++- .../core/scheduler/SchedulerTask_Instrumentation.java | 7 +++++++ .../core/scheduler/WorkerTask_Instrumentation.java | 7 +++++++ 12 files changed, 88 insertions(+), 4 deletions(-) diff --git a/instrumentation/netty-reactor-0.7.0/src/main/java/com/nr/instrumentation/NettyReactorConfig.java b/instrumentation/netty-reactor-0.7.0/src/main/java/com/nr/instrumentation/NettyReactorConfig.java index e30e0f059f..cf79de633f 100644 --- a/instrumentation/netty-reactor-0.7.0/src/main/java/com/nr/instrumentation/NettyReactorConfig.java +++ b/instrumentation/netty-reactor-0.7.0/src/main/java/com/nr/instrumentation/NettyReactorConfig.java @@ -1,3 +1,10 @@ +/* + * + * * Copyright 2020 New Relic Corporation. All rights reserved. + * * SPDX-License-Identifier: Apache-2.0 + * + */ + package com.nr.instrumentation; import com.newrelic.api.agent.NewRelic; diff --git a/instrumentation/netty-reactor-0.7.0/src/main/java/reactor/core/publisher/Hooks_Instrumentation.java b/instrumentation/netty-reactor-0.7.0/src/main/java/reactor/core/publisher/Hooks_Instrumentation.java index 0ea6c1d178..abe1cd448c 100644 --- a/instrumentation/netty-reactor-0.7.0/src/main/java/reactor/core/publisher/Hooks_Instrumentation.java +++ b/instrumentation/netty-reactor-0.7.0/src/main/java/reactor/core/publisher/Hooks_Instrumentation.java @@ -1,3 +1,10 @@ +/* + * + * * Copyright 2020 New Relic Corporation. All rights reserved. + * * SPDX-License-Identifier: Apache-2.0 + * + */ + package reactor.core.publisher; import com.newrelic.api.agent.weaver.NewField; diff --git a/instrumentation/netty-reactor-0.8.0/src/main/java/com/nr/instrumentation/NettyReactorConfig.java b/instrumentation/netty-reactor-0.8.0/src/main/java/com/nr/instrumentation/NettyReactorConfig.java index e30e0f059f..f2ad2502ef 100644 --- a/instrumentation/netty-reactor-0.8.0/src/main/java/com/nr/instrumentation/NettyReactorConfig.java +++ b/instrumentation/netty-reactor-0.8.0/src/main/java/com/nr/instrumentation/NettyReactorConfig.java @@ -1,9 +1,17 @@ +/* + * + * * Copyright 2020 New Relic Corporation. All rights reserved. + * * SPDX-License-Identifier: Apache-2.0 + * + */ + package com.nr.instrumentation; import com.newrelic.api.agent.NewRelic; public class NettyReactorConfig { - public static final boolean errorsEnabled = NewRelic.getAgent().getConfig().getValue("reactor-netty.errors.enabled", true); + public static final boolean errorsEnabled = NewRelic.getAgent().getConfig() + .getValue("reactor-netty.errors.enabled", true); private NettyReactorConfig() { } diff --git a/instrumentation/netty-reactor-0.8.0/src/main/java/reactor/core/publisher/Hooks_Instrumentation.java b/instrumentation/netty-reactor-0.8.0/src/main/java/reactor/core/publisher/Hooks_Instrumentation.java index 0ea6c1d178..abe1cd448c 100644 --- a/instrumentation/netty-reactor-0.8.0/src/main/java/reactor/core/publisher/Hooks_Instrumentation.java +++ b/instrumentation/netty-reactor-0.8.0/src/main/java/reactor/core/publisher/Hooks_Instrumentation.java @@ -1,3 +1,10 @@ +/* + * + * * Copyright 2020 New Relic Corporation. All rights reserved. + * * SPDX-License-Identifier: Apache-2.0 + * + */ + package reactor.core.publisher; import com.newrelic.api.agent.weaver.NewField; diff --git a/instrumentation/netty-reactor-0.8.0/src/main/java/reactor/core/scheduler/SchedulerTask_Instrumentation.java b/instrumentation/netty-reactor-0.8.0/src/main/java/reactor/core/scheduler/SchedulerTask_Instrumentation.java index 3b731b4a25..86d01bced8 100644 --- a/instrumentation/netty-reactor-0.8.0/src/main/java/reactor/core/scheduler/SchedulerTask_Instrumentation.java +++ b/instrumentation/netty-reactor-0.8.0/src/main/java/reactor/core/scheduler/SchedulerTask_Instrumentation.java @@ -1,3 +1,10 @@ +/* + * + * * Copyright 2020 New Relic Corporation. All rights reserved. + * * SPDX-License-Identifier: Apache-2.0 + * + */ + package reactor.core.scheduler; import com.newrelic.api.agent.Trace; diff --git a/instrumentation/netty-reactor-0.8.0/src/main/java/reactor/core/scheduler/WorkerTask_Instrumentation.java b/instrumentation/netty-reactor-0.8.0/src/main/java/reactor/core/scheduler/WorkerTask_Instrumentation.java index 281f0c4356..027fe89c06 100644 --- a/instrumentation/netty-reactor-0.8.0/src/main/java/reactor/core/scheduler/WorkerTask_Instrumentation.java +++ b/instrumentation/netty-reactor-0.8.0/src/main/java/reactor/core/scheduler/WorkerTask_Instrumentation.java @@ -1,3 +1,10 @@ +/* + * + * * Copyright 2020 New Relic Corporation. All rights reserved. + * * SPDX-License-Identifier: Apache-2.0 + * + */ + package reactor.core.scheduler; import com.newrelic.api.agent.Trace; diff --git a/instrumentation/netty-reactor-0.9.0/src/main/java/com/nr/instrumentation/NettyReactorConfig.java b/instrumentation/netty-reactor-0.9.0/src/main/java/com/nr/instrumentation/NettyReactorConfig.java index e30e0f059f..f2ad2502ef 100644 --- a/instrumentation/netty-reactor-0.9.0/src/main/java/com/nr/instrumentation/NettyReactorConfig.java +++ b/instrumentation/netty-reactor-0.9.0/src/main/java/com/nr/instrumentation/NettyReactorConfig.java @@ -1,9 +1,17 @@ +/* + * + * * Copyright 2020 New Relic Corporation. All rights reserved. + * * SPDX-License-Identifier: Apache-2.0 + * + */ + package com.nr.instrumentation; import com.newrelic.api.agent.NewRelic; public class NettyReactorConfig { - public static final boolean errorsEnabled = NewRelic.getAgent().getConfig().getValue("reactor-netty.errors.enabled", true); + public static final boolean errorsEnabled = NewRelic.getAgent().getConfig() + .getValue("reactor-netty.errors.enabled", true); private NettyReactorConfig() { } diff --git a/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/publisher/Hooks_Instrumentation.java b/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/publisher/Hooks_Instrumentation.java index 0ea6c1d178..abe1cd448c 100644 --- a/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/publisher/Hooks_Instrumentation.java +++ b/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/publisher/Hooks_Instrumentation.java @@ -1,3 +1,10 @@ +/* + * + * * Copyright 2020 New Relic Corporation. All rights reserved. + * * SPDX-License-Identifier: Apache-2.0 + * + */ + package reactor.core.publisher; import com.newrelic.api.agent.weaver.NewField; diff --git a/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/publisher/LambdaMonoSubscriber_Instrumentation.java b/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/publisher/LambdaMonoSubscriber_Instrumentation.java index 34d94bbbad..c62a3acf76 100644 --- a/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/publisher/LambdaMonoSubscriber_Instrumentation.java +++ b/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/publisher/LambdaMonoSubscriber_Instrumentation.java @@ -1,3 +1,10 @@ +/* + * + * * Copyright 2020 New Relic Corporation. All rights reserved. + * * SPDX-License-Identifier: Apache-2.0 + * + */ + package reactor.core.publisher; import com.newrelic.agent.bridge.AgentBridge; @@ -29,7 +36,6 @@ public final void onComplete() { Token token = this.currentContext().getOrDefault("newrelic-token", null); if (token != null) { token.expire(); - token = null; this.nrContext = null; } diff --git a/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/publisher/LambdaSubscriber_Instrumentation.java b/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/publisher/LambdaSubscriber_Instrumentation.java index 71f559327f..e2bd4310ad 100644 --- a/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/publisher/LambdaSubscriber_Instrumentation.java +++ b/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/publisher/LambdaSubscriber_Instrumentation.java @@ -1,3 +1,10 @@ +/* + * + * * Copyright 2020 New Relic Corporation. All rights reserved. + * * SPDX-License-Identifier: Apache-2.0 + * + */ + package reactor.core.publisher; import com.newrelic.agent.bridge.AgentBridge; @@ -27,7 +34,6 @@ public final void onComplete() { Token token = this.currentContext().getOrDefault("newrelic-token", null); if (token != null) { token.expire(); - token = null; this.nrContext = null; } diff --git a/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/scheduler/SchedulerTask_Instrumentation.java b/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/scheduler/SchedulerTask_Instrumentation.java index 3b731b4a25..86d01bced8 100644 --- a/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/scheduler/SchedulerTask_Instrumentation.java +++ b/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/scheduler/SchedulerTask_Instrumentation.java @@ -1,3 +1,10 @@ +/* + * + * * Copyright 2020 New Relic Corporation. All rights reserved. + * * SPDX-License-Identifier: Apache-2.0 + * + */ + package reactor.core.scheduler; import com.newrelic.api.agent.Trace; diff --git a/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/scheduler/WorkerTask_Instrumentation.java b/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/scheduler/WorkerTask_Instrumentation.java index 281f0c4356..027fe89c06 100644 --- a/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/scheduler/WorkerTask_Instrumentation.java +++ b/instrumentation/netty-reactor-0.9.0/src/main/java/reactor/core/scheduler/WorkerTask_Instrumentation.java @@ -1,3 +1,10 @@ +/* + * + * * Copyright 2020 New Relic Corporation. All rights reserved. + * * SPDX-License-Identifier: Apache-2.0 + * + */ + package reactor.core.scheduler; import com.newrelic.api.agent.Trace;