Skip to content

Commit

Permalink
[#10940] Fix mongodb mongodbClient constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
jaehong-kim committed Apr 24, 2024
1 parent 91b75e3 commit 93aab08
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import com.navercorp.pinpoint.plugin.mongo.interceptor.MongoClientGetDatabaseInterceptor;
import com.navercorp.pinpoint.plugin.mongo.interceptor.MongoClientImplConstructorInterceptor;
import com.navercorp.pinpoint.plugin.mongo.interceptor.MongoClientImplGetDatabaseInterceptor;
import com.navercorp.pinpoint.plugin.mongo.interceptor.MongoClientV4ConstructorInterceptor;
import com.navercorp.pinpoint.plugin.mongo.interceptor.MongoClientsInterceptor;
import com.navercorp.pinpoint.plugin.mongo.interceptor.MongoCollectionImplConstructorInterceptor;
import com.navercorp.pinpoint.plugin.mongo.interceptor.MongoCollectionImplReadOperationInterceptor;
Expand Down Expand Up @@ -238,7 +239,7 @@ public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, Strin
// MongoClient(final MongoClientSettings settings, @Nullable final MongoClientOptions options, @Nullable final MongoDriverInformation mongoDriverInformation)
final InstrumentMethod constructorMethod13 = target.getConstructor("com.mongodb.MongoClientSettings", "com.mongodb.MongoClientOptions", "com.mongodb.MongoDriverInformation");
if (constructorMethod13 != null) {
constructorMethod13.addInterceptor(MongoClientConstructorInterceptor.class);
constructorMethod13.addInterceptor(MongoClientV4ConstructorInterceptor.class);
}

final InstrumentMethod getDatabaseMethod = target.getDeclaredMethod("getDatabase", "java.lang.String");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@

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

import com.mongodb.MongoClientSettings;
import com.mongodb.ServerAddress;
import com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor;
import com.navercorp.pinpoint.bootstrap.logging.PluginLogManager;
import com.navercorp.pinpoint.bootstrap.logging.PluginLogger;
import com.navercorp.pinpoint.common.plugin.util.HostAndPort;
import com.navercorp.pinpoint.common.util.ArrayArgumentUtils;
import com.navercorp.pinpoint.plugin.mongo.HostListAccessor;
import com.navercorp.pinpoint.plugin.mongo.MongoUtil;

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

try {
// over 4.2
final MongoClientSettings mongoClientSettings = ArrayArgumentUtils.getArgument(args, 0, MongoClientSettings.class);
if (mongoClientSettings != null) {
List<String> list = MongoUtil.getHostList(mongoClientSettings);
setHostList(target, list);
return;
}

final List<String> hostList = new ArrayList<>();
// arg0 is ServerAddress
final ServerAddress serverAddress = ArrayArgumentUtils.getArgument(args, 0, ServerAddress.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2022 NAVER Corp.
*
* 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.
*/

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

import com.mongodb.MongoClientSettings;
import com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor;
import com.navercorp.pinpoint.bootstrap.logging.PluginLogManager;
import com.navercorp.pinpoint.bootstrap.logging.PluginLogger;
import com.navercorp.pinpoint.common.util.ArrayArgumentUtils;
import com.navercorp.pinpoint.plugin.mongo.HostListAccessor;
import com.navercorp.pinpoint.plugin.mongo.MongoUtil;

import java.util.List;

public class MongoClientV4ConstructorInterceptor implements AroundInterceptor {
private final PluginLogger logger = PluginLogManager.getLogger(this.getClass());
private final boolean isDebug = logger.isDebugEnabled();

public MongoClientV4ConstructorInterceptor() {
}

@Override
public void before(Object target, Object[] args) {
}

@Override
public void after(Object target, Object[] args, Object result, Throwable throwable) {
if (isDebug) {
logger.afterInterceptor(target, args, result, throwable);
}

if (throwable != null) {
return;
}

if (Boolean.FALSE == (target instanceof HostListAccessor)) {
return;
}

try {
// over 4.2
final MongoClientSettings mongoClientSettings = ArrayArgumentUtils.getArgument(args, 0, MongoClientSettings.class);
if (mongoClientSettings != null) {
List<String> list = MongoUtil.getHostList(mongoClientSettings);
setHostList(target, list);
return;
}
} catch (Throwable th) {
if (logger.isWarnEnabled()) {
logger.warn("AFTER error. Caused:{}", th.getMessage(), th);
}
}
}

private void setHostList(Object target, List<String> hostList) {
((HostListAccessor) target)._$PINPOINT$_setHostList(hostList);
if (isDebug) {
logger.debug("Set hostList={}", hostList);
}
}
}

0 comments on commit 93aab08

Please sign in to comment.