Skip to content

Commit 095a885

Browse files
committed
[pinpoint-apm#10940] Fix mongodb mongodbClient constructor
1 parent 3de57c1 commit 095a885

File tree

3 files changed

+77
-11
lines changed

3 files changed

+77
-11
lines changed

agent-module/plugins/mongodb/src/main/java/com/navercorp/pinpoint/plugin/mongo/MongoPlugin.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import com.navercorp.pinpoint.plugin.mongo.interceptor.MongoClientGetDatabaseInterceptor;
5858
import com.navercorp.pinpoint.plugin.mongo.interceptor.MongoClientImplConstructorInterceptor;
5959
import com.navercorp.pinpoint.plugin.mongo.interceptor.MongoClientImplGetDatabaseInterceptor;
60+
import com.navercorp.pinpoint.plugin.mongo.interceptor.MongoClientV4ConstructorInterceptor;
6061
import com.navercorp.pinpoint.plugin.mongo.interceptor.MongoClientsInterceptor;
6162
import com.navercorp.pinpoint.plugin.mongo.interceptor.MongoCollectionImplConstructorInterceptor;
6263
import com.navercorp.pinpoint.plugin.mongo.interceptor.MongoCollectionImplReadOperationInterceptor;
@@ -238,7 +239,7 @@ public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, Strin
238239
// MongoClient(final MongoClientSettings settings, @Nullable final MongoClientOptions options, @Nullable final MongoDriverInformation mongoDriverInformation)
239240
final InstrumentMethod constructorMethod13 = target.getConstructor("com.mongodb.MongoClientSettings", "com.mongodb.MongoClientOptions", "com.mongodb.MongoDriverInformation");
240241
if (constructorMethod13 != null) {
241-
constructorMethod13.addInterceptor(MongoClientConstructorInterceptor.class);
242+
constructorMethod13.addInterceptor(MongoClientV4ConstructorInterceptor.class);
242243
}
243244

244245
final InstrumentMethod getDatabaseMethod = target.getDeclaredMethod("getDatabase", "java.lang.String");

agent-module/plugins/mongodb/src/main/java/com/navercorp/pinpoint/plugin/mongo/interceptor/MongoClientConstructorInterceptor.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@
1616

1717
package com.navercorp.pinpoint.plugin.mongo.interceptor;
1818

19-
import com.mongodb.MongoClientSettings;
2019
import com.mongodb.ServerAddress;
2120
import com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor;
2221
import com.navercorp.pinpoint.bootstrap.logging.PluginLogManager;
2322
import com.navercorp.pinpoint.bootstrap.logging.PluginLogger;
2423
import com.navercorp.pinpoint.common.plugin.util.HostAndPort;
2524
import com.navercorp.pinpoint.common.util.ArrayArgumentUtils;
2625
import com.navercorp.pinpoint.plugin.mongo.HostListAccessor;
27-
import com.navercorp.pinpoint.plugin.mongo.MongoUtil;
2826

2927
import java.util.ArrayList;
3028
import java.util.List;
@@ -55,14 +53,6 @@ public void after(Object target, Object[] args, Object result, Throwable throwab
5553
}
5654

5755
try {
58-
// over 4.2
59-
final MongoClientSettings mongoClientSettings = ArrayArgumentUtils.getArgument(args, 0, MongoClientSettings.class);
60-
if (mongoClientSettings != null) {
61-
List<String> list = MongoUtil.getHostList(mongoClientSettings);
62-
setHostList(target, list);
63-
return;
64-
}
65-
6656
final List<String> hostList = new ArrayList<>();
6757
// arg0 is ServerAddress
6858
final ServerAddress serverAddress = ArrayArgumentUtils.getArgument(args, 0, ServerAddress.class);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.plugin.mongo.interceptor;
18+
19+
import com.mongodb.MongoClientSettings;
20+
import com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor;
21+
import com.navercorp.pinpoint.bootstrap.logging.PluginLogManager;
22+
import com.navercorp.pinpoint.bootstrap.logging.PluginLogger;
23+
import com.navercorp.pinpoint.common.util.ArrayArgumentUtils;
24+
import com.navercorp.pinpoint.plugin.mongo.HostListAccessor;
25+
import com.navercorp.pinpoint.plugin.mongo.MongoUtil;
26+
27+
import java.util.List;
28+
29+
public class MongoClientV4ConstructorInterceptor implements AroundInterceptor {
30+
private final PluginLogger logger = PluginLogManager.getLogger(this.getClass());
31+
private final boolean isDebug = logger.isDebugEnabled();
32+
33+
public MongoClientV4ConstructorInterceptor() {
34+
}
35+
36+
@Override
37+
public void before(Object target, Object[] args) {
38+
}
39+
40+
@Override
41+
public void after(Object target, Object[] args, Object result, Throwable throwable) {
42+
if (isDebug) {
43+
logger.afterInterceptor(target, args, result, throwable);
44+
}
45+
46+
if (throwable != null) {
47+
return;
48+
}
49+
50+
if (Boolean.FALSE == (target instanceof HostListAccessor)) {
51+
return;
52+
}
53+
54+
try {
55+
// over 4.2
56+
final MongoClientSettings mongoClientSettings = ArrayArgumentUtils.getArgument(args, 0, MongoClientSettings.class);
57+
if (mongoClientSettings != null) {
58+
List<String> list = MongoUtil.getHostList(mongoClientSettings);
59+
setHostList(target, list);
60+
return;
61+
}
62+
} catch (Throwable th) {
63+
if (logger.isWarnEnabled()) {
64+
logger.warn("AFTER error. Caused:{}", th.getMessage(), th);
65+
}
66+
}
67+
}
68+
69+
private void setHostList(Object target, List<String> hostList) {
70+
((HostListAccessor) target)._$PINPOINT$_setHostList(hostList);
71+
if (isDebug) {
72+
logger.debug("Set hostList={}", hostList);
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)