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