1+ /*
2+ * Licensed to the Apache Software Foundation (ASF) under one or more
3+ * contributor license agreements. See the NOTICE file distributed with
4+ * this work for additional information regarding copyright ownership.
5+ * The ASF licenses this file to You under the Apache License, Version 2.0
6+ * (the "License"); you may not use this file except in compliance with
7+ * the License. You may obtain a copy of the License at
8+ *
9+ * http://www.apache.org/licenses/LICENSE-2.0
10+ *
11+ * Unless required by applicable law or agreed to in writing, software
12+ * distributed under the License is distributed on an "AS IS" BASIS,
13+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+ * See the License for the specific language governing permissions and
15+ * limitations under the License.
16+ */
17+
18+ package org .apache .skywalking .apm .plugin .aerospike ;
19+
20+ import org .apache .skywalking .apm .agent .core .context .ContextManager ;
21+ import org .apache .skywalking .apm .agent .core .context .tag .Tags ;
22+ import org .apache .skywalking .apm .agent .core .context .trace .AbstractSpan ;
23+ import org .apache .skywalking .apm .agent .core .context .trace .SpanLayer ;
24+ import org .apache .skywalking .apm .agent .core .plugin .interceptor .enhance .EnhancedInstance ;
25+ import org .apache .skywalking .apm .agent .core .plugin .interceptor .enhance .InstanceMethodsAroundInterceptor ;
26+ import org .apache .skywalking .apm .agent .core .plugin .interceptor .enhance .MethodInterceptResult ;
27+ import org .apache .skywalking .apm .network .trace .component .ComponentsDefine ;
28+
29+ import java .lang .reflect .Method ;
30+ import java .util .Arrays ;
31+ import java .util .HashSet ;
32+ import java .util .Optional ;
33+ import java .util .Set ;
34+
35+ public class AerospikeClientMethodInterceptor implements InstanceMethodsAroundInterceptor {
36+ private static final Set <String > OPERATION_MAPPING_READ = new HashSet <>(Arrays .asList (
37+ "get" ,
38+ "prepend" ,
39+ "exists" ,
40+ "getHeader" ,
41+ "scanAll" ,
42+ "scanNode" ,
43+ "scanPartitions" ,
44+ "getLargeList" ,
45+ "getLargeMap" ,
46+ "getLargeSet" ,
47+ "getLargeStack" ,
48+ "query" ,
49+ "queryNode" ,
50+ "queryPartitions" ,
51+ "queryAggregate" ,
52+ "queryAggregateNode" ,
53+ "info"
54+ ));
55+
56+ private static final Set <String > OPERATION_MAPPING_WRITE = new HashSet <>(Arrays .asList (
57+ "append" ,
58+ "put" ,
59+ "add" ,
60+ "delete" ,
61+ "touch" ,
62+ "operate" ,
63+ "register" ,
64+ "registerUdfString" ,
65+ "removeUdf" ,
66+ "execute"
67+ ));
68+
69+ @ Override
70+ public void beforeMethod (EnhancedInstance objInst , Method method , Object [] allArguments , Class <?>[] argumentsTypes ,
71+ MethodInterceptResult result ) throws Throwable {
72+ String peer = String .valueOf (objInst .getSkyWalkingDynamicField ());
73+ String methodName = method .getName ();
74+ AbstractSpan span = ContextManager .createExitSpan ("Aerospike/" + methodName , peer );
75+ span .setComponent (ComponentsDefine .AEROSPIKE );
76+ Tags .CACHE_TYPE .set (span , "Aerospike" );
77+ SpanLayer .asCache (span );
78+ parseOperation (methodName ).ifPresent (op -> Tags .CACHE_OP .set (span , op ));
79+ }
80+
81+ @ Override
82+ public Object afterMethod (EnhancedInstance objInst , Method method , Object [] allArguments , Class <?>[] argumentsTypes ,
83+ Object ret ) throws Throwable {
84+ ContextManager .stopSpan ();
85+ return ret ;
86+ }
87+
88+ @ Override
89+ public void handleMethodException (EnhancedInstance objInst , Method method , Object [] allArguments ,
90+ Class <?>[] argumentsTypes , Throwable t ) {
91+ AbstractSpan span = ContextManager .activeSpan ();
92+ span .log (t );
93+ }
94+
95+ private Optional <String > parseOperation (String cmd ) {
96+ if (OPERATION_MAPPING_READ .contains (cmd )) {
97+ return Optional .of ("read" );
98+ }
99+ if (OPERATION_MAPPING_WRITE .contains (cmd )) {
100+ return Optional .of ("write" );
101+ }
102+ return Optional .empty ();
103+ }
104+ }
0 commit comments