From 0929ff3453c3178db82a4b5399054900ab84bba0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8E=E7=8E=89=E6=A1=94?= <769213327@qq.com> Date: Fri, 15 Feb 2019 22:13:44 +0800 Subject: [PATCH] Support Zookeeper plugin (#2211) * Zookeeper plugin --- .../trace/component/ComponentsDefine.java | 5 +- apm-sniffer/optional-plugins/pom.xml | 1 + .../zookeeper-3.4.x-plugin/pom.xml | 45 ++++++ .../zookeeper/ClientCnxnInterceptor.java | 96 ++++++++++++ .../EventThreadMethodInterceptor.java | 73 +++++++++ .../apm/plugin/zookeeper/ZooOpt.java | 140 ++++++++++++++++++ .../define/ClientCnxnInstrumentation.java | 88 +++++++++++ .../define/EventThreadInstrumentation.java | 73 +++++++++ .../src/main/resources/skywalking-plugin.def | 18 +++ .../zookeeper/ClientCnxnInterceptorTest.java | 114 ++++++++++++++ docker/config/component-libraries.yml | 4 + docs/en/guides/Component-library-settings.md | 1 + .../setup/service-agent/java-agent/README.md | 1 + .../java-agent/Supported-list.md | 2 + .../cn/guides/Component-library-settings.md | 1 + .../test/resources/component-libraries.yml | 4 + .../main/resources/component-libraries.yml | 4 + 17 files changed, 669 insertions(+), 1 deletion(-) create mode 100644 apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/pom.xml create mode 100644 apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/zookeeper/ClientCnxnInterceptor.java create mode 100644 apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/zookeeper/EventThreadMethodInterceptor.java create mode 100644 apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/zookeeper/ZooOpt.java create mode 100644 apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/zookeeper/define/ClientCnxnInstrumentation.java create mode 100644 apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/zookeeper/define/EventThreadInstrumentation.java create mode 100644 apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/src/main/resources/skywalking-plugin.def create mode 100644 apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/zookeeper/ClientCnxnInterceptorTest.java diff --git a/apm-protocol/apm-network/src/main/java/org/apache/skywalking/apm/network/trace/component/ComponentsDefine.java b/apm-protocol/apm-network/src/main/java/org/apache/skywalking/apm/network/trace/component/ComponentsDefine.java index c0784d145a..ce9cb5f626 100644 --- a/apm-protocol/apm-network/src/main/java/org/apache/skywalking/apm/network/trace/component/ComponentsDefine.java +++ b/apm-protocol/apm-network/src/main/java/org/apache/skywalking/apm/network/trace/component/ComponentsDefine.java @@ -112,6 +112,8 @@ public class ComponentsDefine { public static final OfficialComponent LETTUCE = new OfficialComponent(57, "Lettuce"); + public static final OfficialComponent ZOOKEEPER = new OfficialComponent(58, "Zookeeper"); + private static ComponentsDefine INSTANCE = new ComponentsDefine(); private String[] components; @@ -121,7 +123,7 @@ public class ComponentsDefine { } public ComponentsDefine() { - components = new String[58]; + components = new String[59]; addComponent(TOMCAT); addComponent(HTTPCLIENT); addComponent(DUBBO); @@ -164,6 +166,7 @@ public class ComponentsDefine { addComponent(GSON); addComponent(REDISSON); addComponent(LETTUCE); + addComponent(ZOOKEEPER); } private void addComponent(OfficialComponent component) { diff --git a/apm-sniffer/optional-plugins/pom.xml b/apm-sniffer/optional-plugins/pom.xml index e462a2c26d..be50e205bd 100644 --- a/apm-sniffer/optional-plugins/pom.xml +++ b/apm-sniffer/optional-plugins/pom.xml @@ -45,6 +45,7 @@ trace-ignore-plugin gson-2.8.x-plugin lettuce-5.x-plugin + zookeeper-3.4.x-plugin diff --git a/apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/pom.xml b/apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/pom.xml new file mode 100644 index 0000000000..92841ea4e5 --- /dev/null +++ b/apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/pom.xml @@ -0,0 +1,45 @@ + + + + + 4.0.0 + + org.apache.skywalking + optional-plugins + 6.1.0-SNAPSHOT + + + apm-zookeeper-3.4.x-plugin + jar + + zookeeper-3.4.x-plugin + http://maven.apache.org + + 3.4.13 + + + + + org.apache.zookeeper + zookeeper + ${zookeeper.version} + provided + + + \ No newline at end of file diff --git a/apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/zookeeper/ClientCnxnInterceptor.java b/apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/zookeeper/ClientCnxnInterceptor.java new file mode 100644 index 0000000000..7e5d3a30d5 --- /dev/null +++ b/apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/zookeeper/ClientCnxnInterceptor.java @@ -0,0 +1,96 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin.zookeeper; + +import org.apache.jute.Record; +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.context.tag.Tags; +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; +import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; +import org.apache.skywalking.apm.agent.core.logging.api.ILog; +import org.apache.skywalking.apm.agent.core.logging.api.LogManager; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; +import org.apache.zookeeper.client.StaticHostProvider; +import org.apache.zookeeper.proto.RequestHeader; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.net.InetSocketAddress; +import java.util.List; + +/** + * @author zhaoyuguang + */ +public class ClientCnxnInterceptor implements InstanceMethodsAroundInterceptor, InstanceConstructorInterceptor { + + private static final ILog logger = LogManager.getLogger(ClientCnxnInterceptor.class); + + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + MethodInterceptResult result) throws Throwable { + String peer = (String) objInst.getSkyWalkingDynamicField(); + RequestHeader header = (RequestHeader) allArguments[0]; + String operationName = ZooOpt.getOperationName(header.getType()); + AbstractSpan span = ContextManager.createExitSpan("Zookeeper/" + operationName, peer); + span.setComponent(ComponentsDefine.ZOOKEEPER); + Tags.DB_TYPE.set(span, "Zookeeper"); + ZooOpt.setTags(span, (Record) allArguments[2]); + SpanLayer.asCache(span); + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Object ret) throws Throwable { + ContextManager.stopSpan(); + return ret; + } + + @Override + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Throwable t) { + AbstractSpan span = ContextManager.activeSpan(); + span.errorOccurred(); + span.log(t); + } + + @Override + public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { + StaticHostProvider hostProvider = (StaticHostProvider) allArguments[1]; + try { + Field field = StaticHostProvider.class.getDeclaredField("serverAddresses"); + field.setAccessible(true); + @SuppressWarnings("unchecked") + List serverAddresses = (List) field.get(hostProvider); + StringBuilder peer = new StringBuilder(); + for (InetSocketAddress address : serverAddresses) { + peer.append(address.getHostName()).append(":").append(address.getPort()).append(";"); + } + objInst.setSkyWalkingDynamicField(peer.toString()); + } catch (NoSuchFieldException e) { + logger.warn("NoSuchFieldException, not be compatible with this version of zookeeper", e); + } catch (IllegalAccessException e) { + logger.warn("IllegalAccessException, not be compatible with this version of zookeeper", e); + } + } +} diff --git a/apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/zookeeper/EventThreadMethodInterceptor.java b/apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/zookeeper/EventThreadMethodInterceptor.java new file mode 100644 index 0000000000..64184003e7 --- /dev/null +++ b/apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/zookeeper/EventThreadMethodInterceptor.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin.zookeeper; + +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.context.tag.Tags; +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; +import org.apache.zookeeper.WatchedEvent; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +/** + * @author zhaoyuguang + */ +public class EventThreadMethodInterceptor implements InstanceMethodsAroundInterceptor { + + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + MethodInterceptResult result) throws Throwable { + if (isWatchedEvent(allArguments[0])) { + Field field = allArguments[0].getClass().getDeclaredField("event"); + field.setAccessible(true); + WatchedEvent event = (WatchedEvent) field.get(allArguments[0]); + AbstractSpan span = ContextManager.createEntrySpan("Zookeeper/WatchedEvent/" + event.getType().name(), null); + ZooOpt.setTags(span, event); + span.setComponent(ComponentsDefine.ZOOKEEPER); + Tags.DB_TYPE.set(span, "Zookeeper"); + } + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Object ret) throws Throwable { + if (isWatchedEvent(allArguments[0])) { + ContextManager.stopSpan(); + } + return ret; + } + + @Override + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Throwable t) { + if (isWatchedEvent(allArguments[0])) { + ContextManager.activeSpan().errorOccurred().log(t); + } + } + + private boolean isWatchedEvent(Object event) { + return event != null && "org.apache.zookeeper.ClientCnxn$WatcherSetEventPair".equals(event.getClass().getName()); + } +} diff --git a/apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/zookeeper/ZooOpt.java b/apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/zookeeper/ZooOpt.java new file mode 100644 index 0000000000..70ae72d98b --- /dev/null +++ b/apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/zookeeper/ZooOpt.java @@ -0,0 +1,140 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin.zookeeper; + +import org.apache.jute.Record; +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.proto.*; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author zhaoyuguang + */ +class ZooOpt { + + private static final Map OPTS = new HashMap(); + private static final String PATH = "path"; + private static final String VERSION = "version"; + private static final String WATCH = "watch"; + private static final String MAX_CHILDREN = "max"; + private static final String KEEPER_STATE = "state"; + + static { + OPTS.put(ZooDefs.OpCode.notification, "notification"); + OPTS.put(ZooDefs.OpCode.create, "create"); + OPTS.put(ZooDefs.OpCode.delete, "delete"); + OPTS.put(ZooDefs.OpCode.exists, "exists"); + OPTS.put(ZooDefs.OpCode.getData, "getData"); + OPTS.put(ZooDefs.OpCode.setData, "setData"); + OPTS.put(ZooDefs.OpCode.getACL, "getACL"); + OPTS.put(ZooDefs.OpCode.setACL, "setACL"); + OPTS.put(ZooDefs.OpCode.getChildren, "getChildren"); + OPTS.put(ZooDefs.OpCode.sync, "sync"); + OPTS.put(ZooDefs.OpCode.ping, "ping"); + OPTS.put(ZooDefs.OpCode.getChildren2, "getChildren2"); + OPTS.put(ZooDefs.OpCode.check, "check"); + OPTS.put(ZooDefs.OpCode.multi, "multi"); + OPTS.put(ZooDefs.OpCode.auth, "auth"); + OPTS.put(ZooDefs.OpCode.setWatches, "setWatches"); + OPTS.put(ZooDefs.OpCode.sasl, "sasl"); + OPTS.put(ZooDefs.OpCode.createSession, "createSession"); + OPTS.put(ZooDefs.OpCode.closeSession, "closeSession"); + OPTS.put(ZooDefs.OpCode.error, "error"); + } + + static String getOperationName(Integer opCode) { + String operationName = OPTS.get(opCode); + return operationName == null ? "unknown" : operationName; + } + + /** + * Add the tag attribute only for the implementation of the Request suffix + * except ConnectRequest.class because no very important attributes + * except GetSASLRequest.class because no very important attributes + * except SetSASLRequest.class because no very important attributes + * + * @param span SkyWalking AbstractSpan.class + * @param record Zookeeper Record.class + */ + static void setTags(AbstractSpan span, Record record) { + if (record instanceof CheckVersionRequest) { + CheckVersionRequest recordImpl = (CheckVersionRequest) record; + span.tag(PATH, recordImpl.getPath()); + } else if (record instanceof CreateRequest) { + CreateRequest recordImpl = (CreateRequest) record; + span.tag(PATH, recordImpl.getPath()); + } else if (record instanceof DeleteRequest) { + DeleteRequest recordImpl = (DeleteRequest) record; + span.tag(PATH, recordImpl.getPath()); + span.tag(VERSION, String.valueOf(recordImpl.getVersion())); + } else if (record instanceof ExistsRequest) { + ExistsRequest recordImpl = (ExistsRequest) record; + span.tag(PATH, recordImpl.getPath()); + span.tag(WATCH, String.valueOf(recordImpl.getWatch())); + } else if (record instanceof GetACLRequest) { + GetACLRequest recordImpl = (GetACLRequest) record; + span.tag(PATH, recordImpl.getPath()); + } else if (record instanceof GetChildren2Request) { + GetChildren2Request recordImpl = (GetChildren2Request) record; + span.tag(PATH, recordImpl.getPath()); + span.tag(WATCH, String.valueOf(recordImpl.getWatch())); + } else if (record instanceof GetChildrenRequest) { + GetChildrenRequest recordImpl = (GetChildrenRequest) record; + span.tag(PATH, recordImpl.getPath()); + span.tag(WATCH, String.valueOf(recordImpl.getWatch())); + } else if (record instanceof GetDataRequest) { + GetDataRequest recordImpl = (GetDataRequest) record; + span.tag(PATH, recordImpl.getPath()); + span.tag(WATCH, String.valueOf(recordImpl.getWatch())); + } else if (record instanceof GetMaxChildrenRequest) { + GetMaxChildrenRequest recordImpl = (GetMaxChildrenRequest) record; + span.tag(PATH, recordImpl.getPath()); + } else if (record instanceof SetACLRequest) { + SetACLRequest recordImpl = (SetACLRequest) record; + span.tag(PATH, recordImpl.getPath()); + span.tag(VERSION, String.valueOf(recordImpl.getVersion())); + } else if (record instanceof SetDataRequest) { + SetDataRequest recordImpl = (SetDataRequest) record; + span.tag(PATH, recordImpl.getPath()); + span.tag(VERSION, String.valueOf(recordImpl.getVersion())); + } else if (record instanceof SetMaxChildrenRequest) { + SetMaxChildrenRequest recordImpl = (SetMaxChildrenRequest) record; + span.tag(PATH, recordImpl.getPath()); + span.tag(MAX_CHILDREN, String.valueOf(recordImpl.getMax())); + } else if (record instanceof SyncRequest) { + SyncRequest recordImpl = (SyncRequest) record; + span.tag(PATH, recordImpl.getPath()); + } + } + + /** + * Add the necessary tags for the WatchedEvent + * + * @param span SkyWalking AbstractSpan.class + * @param event Zookeeper WatchedEvent.class + */ + static void setTags(AbstractSpan span, WatchedEvent event) { + span.tag(PATH, event.getPath()); + span.tag(KEEPER_STATE, event.getState().name()); + } +} diff --git a/apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/zookeeper/define/ClientCnxnInstrumentation.java b/apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/zookeeper/define/ClientCnxnInstrumentation.java new file mode 100644 index 0000000000..9247f79e3d --- /dev/null +++ b/apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/zookeeper/define/ClientCnxnInstrumentation.java @@ -0,0 +1,88 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin.zookeeper.define; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; + +import static net.bytebuddy.matcher.ElementMatchers.named; +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; +import static org.apache.skywalking.apm.agent.core.plugin.bytebuddy.ArgumentTypeNameMatch.takesArgumentWithType; +import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName; + +/** + * @author zhaoyuguang + */ +public class ClientCnxnInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "org.apache.zookeeper.ClientCnxn"; + + private static final String CLIENT_CNXN_INTERCEPTOR = "org.apache.skywalking.apm.plugin.zookeeper.ClientCnxnInterceptor"; + + @Override + protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return takesArguments(9); + } + + @Override + public String getConstructorInterceptor() { + return CLIENT_CNXN_INTERCEPTOR; + } + } + }; + } + + @Override + protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[]{ + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("queuePacket") + .and(takesArgumentWithType(0,"org.apache.zookeeper.proto.RequestHeader")) + .and(takesArgumentWithType(2,"org.apache.jute.Record")); + } + + @Override + public String getMethodsInterceptor() { + return CLIENT_CNXN_INTERCEPTOR; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + } + }; + } + + @Override + public ClassMatch enhanceClass() { + return byName(ENHANCE_CLASS); + } +} diff --git a/apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/zookeeper/define/EventThreadInstrumentation.java b/apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/zookeeper/define/EventThreadInstrumentation.java new file mode 100644 index 0000000000..288ab10b70 --- /dev/null +++ b/apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/zookeeper/define/EventThreadInstrumentation.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin.zookeeper.define; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; + +import static net.bytebuddy.matcher.ElementMatchers.named; +import static org.apache.skywalking.apm.agent.core.plugin.bytebuddy.ArgumentTypeNameMatch.takesArgumentWithType; +import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName; + +/** + * @author zhaoyuguang + */ +public class EventThreadInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "org.apache.zookeeper.ClientCnxn$EventThread"; + + private static final String EVENT_THREAD_METHOD_INTERCEPTOR = "org.apache.skywalking.apm.plugin.zookeeper.EventThreadMethodInterceptor"; + + @Override + protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[0]; + } + + @Override + protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[]{ + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("processEvent").and(takesArgumentWithType(0,"java.lang.Object")); + } + + @Override + public String getMethodsInterceptor() { + return EVENT_THREAD_METHOD_INTERCEPTOR; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + } + }; + } + + @Override + public ClassMatch enhanceClass() { + return byName(ENHANCE_CLASS); + } +} diff --git a/apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/src/main/resources/skywalking-plugin.def b/apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/src/main/resources/skywalking-plugin.def new file mode 100644 index 0000000000..c2ae7c5130 --- /dev/null +++ b/apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/src/main/resources/skywalking-plugin.def @@ -0,0 +1,18 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +zookeeper-3.4.x=org.apache.skywalking.apm.plugin.zookeeper.define.ClientCnxnInstrumentation +zookeeper-3.4.x=org.apache.skywalking.apm.plugin.zookeeper.define.EventThreadInstrumentation \ No newline at end of file diff --git a/apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/zookeeper/ClientCnxnInterceptorTest.java b/apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/zookeeper/ClientCnxnInterceptorTest.java new file mode 100644 index 0000000000..fc3c36d68c --- /dev/null +++ b/apm-sniffer/optional-plugins/zookeeper-3.4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/zookeeper/ClientCnxnInterceptorTest.java @@ -0,0 +1,114 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin.zookeeper; + +import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan; +import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; +import org.apache.skywalking.apm.agent.core.context.trace.TraceSegment; +import org.apache.skywalking.apm.agent.core.context.util.TagValuePair; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.test.helper.SegmentHelper; +import org.apache.skywalking.apm.agent.test.helper.SpanHelper; +import org.apache.skywalking.apm.agent.test.tools.AgentServiceRule; +import org.apache.skywalking.apm.agent.test.tools.SegmentStorage; +import org.apache.skywalking.apm.agent.test.tools.SegmentStoragePoint; +import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner; +import org.apache.zookeeper.client.StaticHostProvider; +import org.apache.zookeeper.proto.CreateRequest; +import org.apache.zookeeper.proto.RequestHeader; +import org.hamcrest.CoreMatchers; +import org.hamcrest.MatcherAssert; +import org.hamcrest.core.Is; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.modules.junit4.PowerMockRunnerDelegate; + +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.List; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +/** + * @author zhaoyuguang + */ +@RunWith(PowerMockRunner.class) +@PowerMockRunnerDelegate(TracingSegmentRunner.class) +public class ClientCnxnInterceptorTest { + + @SegmentStoragePoint + private SegmentStorage segmentStorage; + + @Rule + public AgentServiceRule serviceRule = new AgentServiceRule(); + + @Mock + private MockInstance instance; + + private ClientCnxnInterceptor interceptor; + + private class MockInstance implements EnhancedInstance { + private Object object; + + @Override + public Object getSkyWalkingDynamicField() { + return object; + } + + @Override + public void setSkyWalkingDynamicField(Object value) { + this.object = value; + } + } + + @SuppressWarnings({"rawtypes", "unchecked"}) + @Before + public void setUp() throws Exception { + instance = new MockInstance(); + interceptor = new ClientCnxnInterceptor(); + } + + @Test + public void testInterceptor() throws Throwable { + InetSocketAddress address = new InetSocketAddress("localhost", 2800); + List serverAddresses = new ArrayList(); + serverAddresses.add(address); + StaticHostProvider provider = new StaticHostProvider(serverAddresses); + interceptor.onConstruct(instance, new Object[]{null, provider}); + RequestHeader header = new RequestHeader(1, 1); + CreateRequest createRequest = new CreateRequest("/path", null, null, 0); + interceptor.beforeMethod(instance, null, new Object[]{header, null, createRequest}, null, null); + interceptor.afterMethod(instance, null, null, null, null); + MatcherAssert.assertThat((String) instance.getSkyWalkingDynamicField(), Is.is("localhost:2800;")); + TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0); + List spans = SegmentHelper.getSpans(traceSegment); + assertThat(spans.size(), is(1)); + assertThat(spans.get(0).getOperationName(), is("Zookeeper/create")); + assertThat(spans.get(0).isExit(), is(true)); + assertThat(SpanHelper.getComponentId(spans.get(0)), is(58)); + List tags = SpanHelper.getTags(spans.get(0)); + assertThat(tags.get(0).getValue(), is("Zookeeper")); + assertThat(SpanHelper.getLayer(spans.get(0)), CoreMatchers.is(SpanLayer.CACHE)); + } +} diff --git a/docker/config/component-libraries.yml b/docker/config/component-libraries.yml index 1ea9f1f577..ff516c875e 100644 --- a/docker/config/component-libraries.yml +++ b/docker/config/component-libraries.yml @@ -201,6 +201,9 @@ Redisson: Lettuce: id: 57 languages: Java +Zookeeper: + id: 58 + languages: Java # .NET/.NET Core components @@ -287,6 +290,7 @@ Component-Server-Mappings: Jedis: Redis Redisson: Redis Lettuce: Redis + Zookeeper: Zookeeper StackExchange.Redis: Redis SqlClient: SqlServer Npgsql: PostgreSQL diff --git a/docs/en/guides/Component-library-settings.md b/docs/en/guides/Component-library-settings.md index ea320412d7..27d9a4361e 100644 --- a/docs/en/guides/Component-library-settings.md +++ b/docs/en/guides/Component-library-settings.md @@ -57,6 +57,7 @@ Component-Server-Mappings: StackExchange.Redis: Redis Redisson: Redis Lettuce: Redis + Zookeeper: Zookeeper SqlClient: SqlServer Npgsql: PostgreSQL MySqlConnector: Mysql diff --git a/docs/en/setup/service-agent/java-agent/README.md b/docs/en/setup/service-agent/java-agent/README.md index d563210774..8208197837 100644 --- a/docs/en/setup/service-agent/java-agent/README.md +++ b/docs/en/setup/service-agent/java-agent/README.md @@ -88,6 +88,7 @@ Now, we have the following known optional plugins. * [Filter traces through specified endpoint name patterns](agent-optional-plugins/trace-ignore-plugin.md) * Gson serialization lib in optional plugin folder * Lettuce 5.x(JRE1.8+) in optional plugin folder +* Zookeeper 3.4.x in optional plugin folder. The reason of being optional plugin is, many business irrelevant traces are generated, which cause extra payload to agents and backends. At the same time, those traces may be just heartbeat(s). ## Advanced Features * Set the settings through system properties for config file override. Read [setting override](Setting-override.md). diff --git a/docs/en/setup/service-agent/java-agent/Supported-list.md b/docs/en/setup/service-agent/java-agent/Supported-list.md index 2c04fe69e0..144c75357f 100644 --- a/docs/en/setup/service-agent/java-agent/Supported-list.md +++ b/docs/en/setup/service-agent/java-agent/Supported-list.md @@ -50,6 +50,8 @@ * [transport-client](https://github.com/elastic/elasticsearch/tree/master/client/transport) 5.2.x-5.6.x * Service Discovery * [Netflix Eureka](https://github.com/Netflix/eureka) +* Distributed Coordination + * [Zookeeper](https://github.com/apache/zookeeper) 3.4.x (Optional² & Except 3.4.4) * Spring Ecosystem * Spring Bean annotations(@Bean, @Service, @Component, @Repository) 3.x and 4.x (Optional²) * Spring Core Async SuccessCallback/FailureCallback/ListenableFutureCallback 4.x diff --git a/docs/others/cn/guides/Component-library-settings.md b/docs/others/cn/guides/Component-library-settings.md index 1d531dfaaf..fbb74bfce6 100644 --- a/docs/others/cn/guides/Component-library-settings.md +++ b/docs/others/cn/guides/Component-library-settings.md @@ -57,6 +57,7 @@ Component-Server-Mappings: StackExchange.Redis: Redis Redisson: Redis Lettuce: Redis + Zookeeper: Zookeeper SqlClient: SqlServer Npgsql: PostgreSQL MySqlConnector: Mysql diff --git a/oap-server/server-core/src/test/resources/component-libraries.yml b/oap-server/server-core/src/test/resources/component-libraries.yml index 6023696756..587c08ba2e 100644 --- a/oap-server/server-core/src/test/resources/component-libraries.yml +++ b/oap-server/server-core/src/test/resources/component-libraries.yml @@ -183,6 +183,9 @@ Redisson: Lettuce: id: 57 languages: Java +Zookeeper: + id: 58 + languages: Java # .NET/.NET Core components # [3000, 4000) for C#/.NET only @@ -266,6 +269,7 @@ Component-Server-Mappings: Jedis: Redis Redisson: Redis Lettuce: Redis + Zookeeper: Zookeeper StackExchange.Redis: Redis SqlClient: SqlServer Npgsql: PostgreSQL diff --git a/oap-server/server-starter/src/main/resources/component-libraries.yml b/oap-server/server-starter/src/main/resources/component-libraries.yml index bae435c6f5..3d439fa42d 100644 --- a/oap-server/server-starter/src/main/resources/component-libraries.yml +++ b/oap-server/server-starter/src/main/resources/component-libraries.yml @@ -201,6 +201,9 @@ Redisson: Lettuce: id: 57 languages: Java +Zookeeper: + id: 58 + languages: Java # .NET/.NET Core components @@ -288,6 +291,7 @@ Component-Server-Mappings: StackExchange.Redis: Redis Redisson: Redis Lettuce: Redis + Zookeeper: Zookeeper SqlClient: SqlServer Npgsql: PostgreSQL MySqlConnector: Mysql -- GitLab