Skip to content

Commit 4d46646

Browse files
Add Visibility into Reactor FlatMap (#2281)
1 parent 7bfd208 commit 4d46646

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
*
3+
* * Copyright 2025 New Relic Corporation. All rights reserved.
4+
* * SPDX-License-Identifier: Apache-2.0
5+
*
6+
*/
7+
8+
package reactor.core.publisher;
9+
10+
import com.newrelic.agent.bridge.AgentBridge;
11+
import com.newrelic.api.agent.NewRelic;
12+
import com.newrelic.api.agent.weaver.MatchType;
13+
import com.newrelic.api.agent.weaver.NewField;
14+
import com.newrelic.api.agent.weaver.Weave;
15+
import com.newrelic.api.agent.Token;
16+
import com.newrelic.api.agent.weaver.WeaveAllConstructors;
17+
import com.newrelic.api.agent.weaver.Weaver;
18+
19+
@Weave(type = MatchType.ExactClass, originalName = "reactor.core.publisher.MonoFlatMap")
20+
abstract class MonoFlatMap_Instrumentation {
21+
22+
@Weave(type = MatchType.ExactClass, originalName = "reactor.core.publisher.MonoFlatMap$FlatMapMain")
23+
static final class FlatMapMain_Instrumentation<T, R> {
24+
25+
@NewField
26+
private Token token;
27+
28+
@WeaveAllConstructors
29+
FlatMapMain_Instrumentation() {
30+
if (AgentBridge.getAgent().getTransaction(false) != null && token == null) {
31+
Token existingToken = NewRelic.getAgent().getTransaction().getToken();
32+
token = (existingToken != null && existingToken.isActive()) ? existingToken : NewRelic.getAgent().getTransaction().getToken();
33+
}
34+
}
35+
36+
public void onNext(T t) {
37+
if (token != null && token.isActive()) {
38+
token.link();
39+
}
40+
Weaver.callOriginal();
41+
}
42+
43+
public void onComplete() {
44+
if (token != null && token.isActive()) {
45+
token.linkAndExpire();
46+
token = null;
47+
}
48+
Weaver.callOriginal();
49+
}
50+
51+
public void onError(Throwable t) {
52+
if (token != null && token.isActive()) {
53+
token.linkAndExpire();
54+
token = null;
55+
}
56+
Weaver.callOriginal();
57+
}
58+
59+
public void cancel() {
60+
if (token != null && token.isActive()) {
61+
token.linkAndExpire();
62+
token = null;
63+
}
64+
Weaver.callOriginal();
65+
}
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
*
3+
* * Copyright 2025 New Relic Corporation. All rights reserved.
4+
* * SPDX-License-Identifier: Apache-2.0
5+
*
6+
*/
7+
8+
package reactor.core.publisher;
9+
10+
import com.newrelic.agent.bridge.AgentBridge;
11+
import com.newrelic.api.agent.NewRelic;
12+
import com.newrelic.api.agent.Token;
13+
import com.newrelic.api.agent.weaver.MatchType;
14+
import com.newrelic.api.agent.weaver.NewField;
15+
import com.newrelic.api.agent.weaver.Weave;
16+
import com.newrelic.api.agent.weaver.WeaveAllConstructors;
17+
import com.newrelic.api.agent.weaver.Weaver;
18+
19+
@Weave(type = MatchType.ExactClass, originalName = "reactor.core.publisher.MonoFlatMap")
20+
abstract class MonoFlatMap_Instrumentation {
21+
22+
@Weave(type = MatchType.ExactClass, originalName = "reactor.core.publisher.MonoFlatMap$FlatMapMain")
23+
static final class FlatMapMain_Instrumentation<T, R> {
24+
25+
@NewField
26+
private Token token;
27+
28+
@WeaveAllConstructors
29+
FlatMapMain_Instrumentation() {
30+
if (AgentBridge.getAgent().getTransaction(false) != null && token == null) {
31+
Token existingToken = NewRelic.getAgent().getTransaction().getToken();
32+
token = (existingToken != null && existingToken.isActive()) ? existingToken : NewRelic.getAgent().getTransaction().getToken();
33+
}
34+
}
35+
36+
public void onNext(T t) {
37+
if (token != null && token.isActive()) {
38+
token.link();
39+
}
40+
Weaver.callOriginal();
41+
}
42+
43+
public void onComplete() {
44+
if (token != null && token.isActive()) {
45+
token.linkAndExpire();
46+
token = null;
47+
}
48+
Weaver.callOriginal();
49+
}
50+
51+
public void onError(Throwable t) {
52+
if (token != null && token.isActive()) {
53+
token.linkAndExpire();
54+
token = null;
55+
}
56+
Weaver.callOriginal();
57+
}
58+
59+
public void cancel() {
60+
if (token != null && token.isActive()) {
61+
token.linkAndExpire();
62+
token = null;
63+
}
64+
Weaver.callOriginal();
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)