Skip to content

Commit 2e1f5cf

Browse files
committed
[#8941] Update reactor plugin for subscribeOrReturn method.
1 parent db30cc0 commit 2e1f5cf

File tree

12 files changed

+1158
-320
lines changed

12 files changed

+1158
-320
lines changed

bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/reactor/FluxAndMonoConstructorInterceptor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import com.navercorp.pinpoint.bootstrap.logging.PLogger;
2323
import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory;
2424

25-
public abstract class FluxAndMonoConstructorInterceptor implements AroundInterceptor {
25+
public class FluxAndMonoConstructorInterceptor implements AroundInterceptor {
2626
private final PLogger logger = PLoggerFactory.getLogger(getClass());
2727
private final boolean isDebug = logger.isDebugEnabled();
2828

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2022 NAVER Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.navercorp.pinpoint.bootstrap.plugin.reactor;
18+
19+
import com.navercorp.pinpoint.bootstrap.async.AsyncContextAccessorUtils;
20+
import com.navercorp.pinpoint.bootstrap.context.AsyncContext;
21+
import com.navercorp.pinpoint.bootstrap.context.MethodDescriptor;
22+
import com.navercorp.pinpoint.bootstrap.context.TraceContext;
23+
import com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor;
24+
import com.navercorp.pinpoint.bootstrap.logging.PLogger;
25+
import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory;
26+
27+
public class FluxAndMonoSubscribeOrReturnInterceptor implements AroundInterceptor {
28+
private final PLogger logger = PLoggerFactory.getLogger(getClass());
29+
private final boolean isDebug = logger.isDebugEnabled();
30+
31+
public FluxAndMonoSubscribeOrReturnInterceptor() {
32+
}
33+
34+
@Override
35+
public void before(Object target, Object[] args) {
36+
}
37+
38+
@Override
39+
public void after(Object target, Object[] args, Object result, Throwable throwable) {
40+
if (isDebug) {
41+
logger.afterInterceptor(target, args, result, throwable);
42+
}
43+
44+
if (throwable != null) {
45+
// Ignore if an error occurs.
46+
return;
47+
}
48+
if (result == null) {
49+
return;
50+
}
51+
52+
if (checkTargetReactorContextAccessor(target, args, result)) {
53+
return;
54+
}
55+
if (checkTargetAsyncContextAccessor(target, args, result)) {
56+
return;
57+
}
58+
if (checkSubscriberReactorContextAccessor(target, args, result)) {
59+
return;
60+
}
61+
}
62+
63+
boolean checkTargetReactorContextAccessor(final Object target, final Object[] args, final Object result) {
64+
final AsyncContext asyncContext = ReactorContextAccessorUtils.getAsyncContext(target);
65+
if (asyncContext != null) {
66+
setReactorContextToResult(asyncContext, result);
67+
return true;
68+
}
69+
return false;
70+
}
71+
72+
boolean checkTargetAsyncContextAccessor(final Object target, final Object[] args, final Object result) {
73+
final AsyncContext asyncContext = AsyncContextAccessorUtils.getAsyncContext(target);
74+
if (asyncContext != null) {
75+
setReactorContextToResult(asyncContext, result);
76+
return true;
77+
}
78+
return false;
79+
}
80+
81+
boolean checkSubscriberReactorContextAccessor(final Object target, final Object[] args, final Object result) {
82+
final AsyncContext asyncContext = ReactorContextAccessorUtils.getAsyncContext(args, 0);
83+
if (asyncContext != null) {
84+
setReactorContextToResult(asyncContext, result);
85+
return true;
86+
}
87+
return false;
88+
}
89+
90+
protected void setReactorContextToResult(AsyncContext asyncContext, Object result) {
91+
ReactorContextAccessorUtils.setAsyncContext(asyncContext, result);
92+
}
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2022 NAVER Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.navercorp.pinpoint.bootstrap.plugin.reactor;
18+
19+
import com.navercorp.pinpoint.bootstrap.context.AsyncContext;
20+
import org.junit.Test;
21+
22+
import static org.junit.Assert.*;
23+
import static org.mockito.Mockito.mock;
24+
25+
public class CoreSubscriberConstructorInterceptorTest {
26+
27+
@Test
28+
public void arg0ContainReactorContext() {
29+
AsyncContext mockAsyncContext = mock(AsyncContext.class);
30+
MockAsyncContextAndReactorContextImpl target = new MockAsyncContextAndReactorContextImpl();
31+
MockAsyncContextAndReactorContextImpl arg0 = new MockAsyncContextAndReactorContextImpl();
32+
CoreSubscriberConstructorInterceptor interceptor = new CoreSubscriberConstructorInterceptor();
33+
34+
// Set asyncContext to target
35+
arg0._$PINPOINT$_setReactorContext(mockAsyncContext);
36+
interceptor.after(target, new Object[]{arg0}, new Object(), null);
37+
38+
assertNotNull(target._$PINPOINT$_getReactorContext());
39+
assertEquals(target._$PINPOINT$_getReactorContext(), arg0._$PINPOINT$_getReactorContext());
40+
}
41+
42+
@Test
43+
public void argNotContainReactorContext() {
44+
MockAsyncContextAndReactorContextImpl target = new MockAsyncContextAndReactorContextImpl();
45+
MockAsyncContextAndReactorContextImpl arg0 = new MockAsyncContextAndReactorContextImpl();
46+
CoreSubscriberConstructorInterceptor interceptor = new CoreSubscriberConstructorInterceptor();
47+
48+
// Not set asyncContext to target
49+
interceptor.after(target, new Object[]{arg0}, new Object(), null);
50+
51+
assertNull(target._$PINPOINT$_getReactorContext());
52+
}
53+
54+
@Test
55+
public void arg1ContainReactorContext() {
56+
AsyncContext mockAsyncContext = mock(AsyncContext.class);
57+
MockAsyncContextAndReactorContextImpl target = new MockAsyncContextAndReactorContextImpl();
58+
MockAsyncContextAndReactorContextImpl arg0 = new MockAsyncContextAndReactorContextImpl();
59+
MockAsyncContextAndReactorContextImpl arg1 = new MockAsyncContextAndReactorContextImpl();
60+
CoreSubscriberConstructorInterceptor interceptor = new CoreSubscriberConstructorInterceptor();
61+
62+
// Set asyncContext to target
63+
arg1._$PINPOINT$_setReactorContext(mockAsyncContext);
64+
interceptor.after(target, new Object[]{arg0, arg1}, new Object(), null);
65+
66+
assertNotNull(target._$PINPOINT$_getReactorContext());
67+
assertEquals(target._$PINPOINT$_getReactorContext(), arg1._$PINPOINT$_getReactorContext());
68+
}
69+
70+
@Test
71+
public void throwableIsNotNull() {
72+
AsyncContext mockAsyncContext = mock(AsyncContext.class);
73+
MockAsyncContextAndReactorContextImpl target = new MockAsyncContextAndReactorContextImpl();
74+
MockAsyncContextAndReactorContextImpl arg0 = new MockAsyncContextAndReactorContextImpl();
75+
Throwable throwable = new Throwable("ERROR");
76+
CoreSubscriberConstructorInterceptor interceptor = new CoreSubscriberConstructorInterceptor();
77+
78+
arg0._$PINPOINT$_setReactorContext(mockAsyncContext);
79+
interceptor.after(target, new Object[]{arg0}, new Object(), throwable);
80+
81+
assertNull(target._$PINPOINT$_getReactorContext());
82+
}
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2022 NAVER Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.navercorp.pinpoint.bootstrap.plugin.reactor;
18+
19+
import com.navercorp.pinpoint.bootstrap.context.AsyncContext;
20+
import org.junit.Test;
21+
22+
import static org.junit.Assert.*;
23+
import static org.mockito.Mockito.mock;
24+
25+
public class FluxAndMonoConstructorInterceptorTest {
26+
27+
@Test
28+
public void arg0ContainAsyncContext() {
29+
AsyncContext mockAsyncContext = mock(AsyncContext.class);
30+
MockAsyncContextAndReactorContextImpl target = new MockAsyncContextAndReactorContextImpl();
31+
MockAsyncContextAndReactorContextImpl arg0 = new MockAsyncContextAndReactorContextImpl();
32+
FluxAndMonoConstructorInterceptor interceptor = new FluxAndMonoConstructorInterceptor();
33+
34+
// Set asyncContext to target
35+
arg0._$PINPOINT$_setAsyncContext(mockAsyncContext);
36+
interceptor.after(target, new Object[]{arg0}, new Object(), null);
37+
38+
assertNotNull(target._$PINPOINT$_getAsyncContext());
39+
assertEquals(target._$PINPOINT$_getAsyncContext(), arg0._$PINPOINT$_getAsyncContext());
40+
assertNotNull(target._$PINPOINT$_getReactorContext());
41+
assertEquals(target._$PINPOINT$_getReactorContext(), arg0._$PINPOINT$_getAsyncContext());
42+
}
43+
44+
@Test
45+
public void arg0NotContainAsyncContext() {
46+
MockAsyncContextAndReactorContextImpl target = new MockAsyncContextAndReactorContextImpl();
47+
MockAsyncContextAndReactorContextImpl arg0 = new MockAsyncContextAndReactorContextImpl();
48+
FluxAndMonoConstructorInterceptor interceptor = new FluxAndMonoConstructorInterceptor();
49+
50+
// Not set asyncContext to target
51+
interceptor.after(target, new Object[]{arg0}, new Object(), null);
52+
53+
assertNull(target._$PINPOINT$_getAsyncContext());
54+
assertNull(target._$PINPOINT$_getReactorContext());
55+
}
56+
57+
@Test
58+
public void arg1ContainAsyncContext() {
59+
AsyncContext mockAsyncContext = mock(AsyncContext.class);
60+
MockAsyncContextAndReactorContextImpl target = new MockAsyncContextAndReactorContextImpl();
61+
MockAsyncContextAndReactorContextImpl arg0 = new MockAsyncContextAndReactorContextImpl();
62+
MockAsyncContextAndReactorContextImpl arg1 = new MockAsyncContextAndReactorContextImpl();
63+
FluxAndMonoConstructorInterceptor interceptor = new FluxAndMonoConstructorInterceptor();
64+
65+
// Set asyncContext to target
66+
arg1._$PINPOINT$_setAsyncContext(mockAsyncContext);
67+
interceptor.after(target, new Object[]{arg0, arg1}, new Object(), null);
68+
69+
assertNotNull(target._$PINPOINT$_getAsyncContext());
70+
assertEquals(target._$PINPOINT$_getAsyncContext(), arg1._$PINPOINT$_getAsyncContext());
71+
assertNotNull(target._$PINPOINT$_getReactorContext());
72+
assertEquals(target._$PINPOINT$_getReactorContext(), arg1._$PINPOINT$_getAsyncContext());
73+
}
74+
75+
@Test
76+
public void throwableIsNotNull() {
77+
AsyncContext mockAsyncContext = mock(AsyncContext.class);
78+
MockAsyncContextAndReactorContextImpl target = new MockAsyncContextAndReactorContextImpl();
79+
MockAsyncContextAndReactorContextImpl arg0 = new MockAsyncContextAndReactorContextImpl();
80+
Throwable throwable = new Throwable("ERROR");
81+
FluxAndMonoConstructorInterceptor interceptor = new FluxAndMonoConstructorInterceptor();
82+
83+
interceptor.after(target, new Object[]{arg0}, new Object(), throwable);
84+
85+
assertNull(target._$PINPOINT$_getAsyncContext());
86+
assertNull(target._$PINPOINT$_getReactorContext());
87+
}
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2022 NAVER Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.navercorp.pinpoint.bootstrap.plugin.reactor;
18+
19+
import com.navercorp.pinpoint.bootstrap.context.AsyncContext;
20+
import org.junit.Test;
21+
22+
import static org.junit.Assert.*;
23+
import static org.mockito.Mockito.mock;
24+
25+
public class FluxAndMonoOperatorConstructorInterceptorTest {
26+
27+
@Test
28+
public void arg0ContainAsyncContext() {
29+
AsyncContext mockAsyncContext = mock(AsyncContext.class);
30+
MockAsyncContextAndReactorContextImpl target = new MockAsyncContextAndReactorContextImpl();
31+
MockAsyncContextAndReactorContextImpl arg0 = new MockAsyncContextAndReactorContextImpl();
32+
FluxAndMonoOperatorConstructorInterceptor interceptor = new FluxAndMonoOperatorConstructorInterceptor();
33+
34+
// Set asyncContext to target
35+
arg0._$PINPOINT$_setAsyncContext(mockAsyncContext);
36+
interceptor.after(target, new Object[]{arg0}, new Object(), null);
37+
38+
assertNull(target._$PINPOINT$_getAsyncContext());
39+
assertNotNull(target._$PINPOINT$_getReactorContext());
40+
assertEquals(target._$PINPOINT$_getReactorContext(), mockAsyncContext);
41+
}
42+
43+
@Test
44+
public void arg0ContainReactorContext() {
45+
AsyncContext mockAsyncContext = mock(AsyncContext.class);
46+
MockAsyncContextAndReactorContextImpl target = new MockAsyncContextAndReactorContextImpl();
47+
MockAsyncContextAndReactorContextImpl arg0 = new MockAsyncContextAndReactorContextImpl();
48+
FluxAndMonoOperatorConstructorInterceptor interceptor = new FluxAndMonoOperatorConstructorInterceptor();
49+
50+
// Set asyncContext to target
51+
arg0._$PINPOINT$_setReactorContext(mockAsyncContext);
52+
interceptor.after(target, new Object[]{arg0}, new Object(), null);
53+
54+
assertNull(target._$PINPOINT$_getAsyncContext());
55+
assertNotNull(target._$PINPOINT$_getReactorContext());
56+
assertEquals(target._$PINPOINT$_getReactorContext(), mockAsyncContext);
57+
}
58+
59+
@Test
60+
public void arg0NotContainAsyncContext() {
61+
MockAsyncContextAndReactorContextImpl target = new MockAsyncContextAndReactorContextImpl();
62+
MockAsyncContextAndReactorContextImpl arg0 = new MockAsyncContextAndReactorContextImpl();
63+
FluxAndMonoOperatorConstructorInterceptor interceptor = new FluxAndMonoOperatorConstructorInterceptor();
64+
65+
// Not set asyncContext to target
66+
interceptor.after(target, new Object[]{arg0}, new Object(), null);
67+
68+
assertNull(target._$PINPOINT$_getAsyncContext());
69+
assertNull(target._$PINPOINT$_getReactorContext());
70+
}
71+
72+
@Test
73+
public void arg1ContainAsyncContext() {
74+
AsyncContext mockAsyncContext = mock(AsyncContext.class);
75+
MockAsyncContextAndReactorContextImpl target = new MockAsyncContextAndReactorContextImpl();
76+
MockAsyncContextAndReactorContextImpl arg0 = new MockAsyncContextAndReactorContextImpl();
77+
MockAsyncContextAndReactorContextImpl arg1 = new MockAsyncContextAndReactorContextImpl();
78+
FluxAndMonoOperatorConstructorInterceptor interceptor = new FluxAndMonoOperatorConstructorInterceptor();
79+
80+
// Set asyncContext to target
81+
arg1._$PINPOINT$_setAsyncContext(mockAsyncContext);
82+
interceptor.after(target, new Object[]{arg0, arg1}, new Object(), null);
83+
84+
assertNotNull(target._$PINPOINT$_getAsyncContext());
85+
assertEquals(target._$PINPOINT$_getAsyncContext(), mockAsyncContext);
86+
assertNotNull(target._$PINPOINT$_getReactorContext());
87+
assertEquals(target._$PINPOINT$_getReactorContext(), mockAsyncContext);
88+
}
89+
90+
@Test
91+
public void throwableIsNotNull() {
92+
AsyncContext mockAsyncContext = mock(AsyncContext.class);
93+
MockAsyncContextAndReactorContextImpl target = new MockAsyncContextAndReactorContextImpl();
94+
MockAsyncContextAndReactorContextImpl arg0 = new MockAsyncContextAndReactorContextImpl();
95+
Throwable throwable = new Throwable("ERROR");
96+
FluxAndMonoOperatorConstructorInterceptor interceptor = new FluxAndMonoOperatorConstructorInterceptor();
97+
98+
interceptor.after(target, new Object[]{arg0}, new Object(), throwable);
99+
100+
assertNull(target._$PINPOINT$_getAsyncContext());
101+
assertNull(target._$PINPOINT$_getReactorContext());
102+
}
103+
}

0 commit comments

Comments
 (0)