From 67b7f22a4074a8d714b603cb27400fdcb9ec31f4 Mon Sep 17 00:00:00 2001 From: aderm <394102339@qq.com> Date: Fri, 21 Feb 2020 14:00:19 +0800 Subject: [PATCH] =?UTF-8?q?1.add=20es=20agent=20resthighlevelclient=20of?= =?UTF-8?q?=20ClusterClientInstrumentation.2.=E2=80=A6=20(#4386)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 1.add es agent resthighlevelclient of ClusterClientInstrumentation.2.fix agent test removeOnExit parse error. * Update configuration.yml * revert removeOnExit param. Co-authored-by: Daming --- .../define/ClusterClientInstrumentation.java | 105 ++++++++++++++ .../RestHighLevelClientInstrumentation.java | 19 ++- ...erClientGetSettingsMethodsInterceptor.java | 68 +++++++++ ...ClusterClientHealthMethodsInterceptor.java | 67 +++++++++ ...erClientPutSettingsMethodsInterceptor.java | 87 +++++++++++ .../v6/interceptor/Constants.java | 7 + ...hLevelClientClusterMethodsInterceptor.java | 49 +++++++ .../src/main/resources/skywalking-plugin.def | 1 + ...ientGetSettingsMethodsInterceptorTest.java | 123 ++++++++++++++++ ...terClientHealthMethodsInterceptorTest.java | 123 ++++++++++++++++ ...ientPutSettingsMethodsInterceptorTest.java | 137 ++++++++++++++++++ .../config/expectedData.yaml | 68 +++++++-- .../controller/CaseController.java | 85 +++++++++-- 13 files changed, 912 insertions(+), 27 deletions(-) create mode 100644 apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/define/ClusterClientInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/ClusterClientGetSettingsMethodsInterceptor.java create mode 100644 apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/ClusterClientHealthMethodsInterceptor.java create mode 100644 apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/ClusterClientPutSettingsMethodsInterceptor.java create mode 100644 apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/RestHighLevelClientClusterMethodsInterceptor.java create mode 100644 apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/ClusterClientGetSettingsMethodsInterceptorTest.java create mode 100644 apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/ClusterClientHealthMethodsInterceptorTest.java create mode 100644 apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/ClusterClientPutSettingsMethodsInterceptorTest.java diff --git a/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/define/ClusterClientInstrumentation.java b/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/define/ClusterClientInstrumentation.java new file mode 100644 index 000000000..e62ef70e5 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/define/ClusterClientInstrumentation.java @@ -0,0 +1,105 @@ +/* + * 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.elasticsearch.v6.define; + +import static net.bytebuddy.matcher.ElementMatchers.named; +import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName; + +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.StaticMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.plugin.elasticsearch.v6.interceptor.Constants; + +public class ClusterClientInstrumentation extends ClassEnhancePluginDefine { + + public static final String ENHANCE_CLASS = "org.elasticsearch.client.ClusterClient"; + + @Override + protected ClassMatch enhanceClass() { + return byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[0]; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[] { + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("health").or(named("healthAsync")); + } + + @Override + public String getMethodsInterceptor() { + return Constants.CLUSTER_CLIENT_HEALTH_METHODS_INTERCEPTOR; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + }, + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("getSettings").or(named("getSettingsAsync")); + } + + @Override + public String getMethodsInterceptor() { + return Constants.CLUSTER_CLIENT_GET_SETTINGS_METHODS_INTERCEPTOR; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + }, + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("putSettings").or(named("putSettingsAsync")); + } + + @Override + public String getMethodsInterceptor() { + return Constants.CLUSTER_CLIENT_PUT_SETTINGS_METHODS_INTERCEPTOR; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + } + }; + } + + @Override + public StaticMethodsInterceptPoint[] getStaticMethodsInterceptPoints() { + return new StaticMethodsInterceptPoint[0]; + } +} diff --git a/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/define/RestHighLevelClientInstrumentation.java b/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/define/RestHighLevelClientInstrumentation.java index a6a05f1bb..91da040d5 100644 --- a/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/define/RestHighLevelClientInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/define/RestHighLevelClientInstrumentation.java @@ -19,6 +19,7 @@ package org.apache.skywalking.apm.plugin.elasticsearch.v6.define; 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; @@ -59,7 +60,7 @@ public class RestHighLevelClientInstrumentation extends ClassEnhancePluginDefine new ConstructorInterceptPoint() { @Override public ElementMatcher getConstructorMatcher() { - return takesArgumentWithType(0, "org.elasticsearch.client.RestClientBuilder"); + return takesArguments(1); } @Override @@ -164,6 +165,22 @@ public class RestHighLevelClientInstrumentation extends ClassEnhancePluginDefine return Constants.REST_HIGH_LEVEL_CLIENT_INDICES_METHODS_INTERCEPTOR; } + @Override + public boolean isOverrideArgs() { + return false; + } + }, + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("cluster"); + } + + @Override + public String getMethodsInterceptor() { + return Constants.REST_HIGH_LEVEL_CLIENT_CLUSTER_METHODS_INTERCEPTOR; + } + @Override public boolean isOverrideArgs() { return false; diff --git a/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/ClusterClientGetSettingsMethodsInterceptor.java b/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/ClusterClientGetSettingsMethodsInterceptor.java new file mode 100644 index 000000000..41a5d59ab --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/ClusterClientGetSettingsMethodsInterceptor.java @@ -0,0 +1,68 @@ +/* + * 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.elasticsearch.v6.interceptor; + +import static org.apache.skywalking.apm.plugin.elasticsearch.v6.interceptor.Constants.DB_TYPE; + +import java.lang.reflect.Method; +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.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.skywalking.apm.plugin.elasticsearch.v6.RestClientEnhanceInfo; + +public class ClusterClientGetSettingsMethodsInterceptor implements InstanceMethodsAroundInterceptor { + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { + + RestClientEnhanceInfo restClientEnhanceInfo = (RestClientEnhanceInfo) (objInst.getSkyWalkingDynamicField()); + if (restClientEnhanceInfo != null) { + AbstractSpan span = ContextManager + .createExitSpan(Constants.CLUSTER_GET_SETTINGS_NAME, restClientEnhanceInfo.getPeers()); + span.setComponent(ComponentsDefine.REST_HIGH_LEVEL_CLIENT); + + Tags.DB_TYPE.set(span, DB_TYPE); + SpanLayer.asDB(span); + } + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Object ret) throws Throwable { + RestClientEnhanceInfo restClientEnhanceInfo = (RestClientEnhanceInfo) (objInst.getSkyWalkingDynamicField()); + if (restClientEnhanceInfo != null) { + ContextManager.stopSpan(); + } + return ret; + } + + @Override + public void handleMethodException(EnhancedInstance objInst, Method method, + Object[] allArguments, Class[] argumentsTypes, Throwable t) { + RestClientEnhanceInfo restClientEnhanceInfo = (RestClientEnhanceInfo) (objInst.getSkyWalkingDynamicField()); + if (restClientEnhanceInfo != null) { + ContextManager.activeSpan().errorOccurred().log(t); + } + } +} diff --git a/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/ClusterClientHealthMethodsInterceptor.java b/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/ClusterClientHealthMethodsInterceptor.java new file mode 100644 index 000000000..fbcb72fe9 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/ClusterClientHealthMethodsInterceptor.java @@ -0,0 +1,67 @@ +/* + * 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.elasticsearch.v6.interceptor; + +import static org.apache.skywalking.apm.plugin.elasticsearch.v6.interceptor.Constants.DB_TYPE; + +import java.lang.reflect.Method; +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.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.skywalking.apm.plugin.elasticsearch.v6.RestClientEnhanceInfo; + +public class ClusterClientHealthMethodsInterceptor implements InstanceMethodsAroundInterceptor { + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { + + RestClientEnhanceInfo restClientEnhanceInfo = (RestClientEnhanceInfo) (objInst.getSkyWalkingDynamicField()); + if (restClientEnhanceInfo != null) { + AbstractSpan span = ContextManager.createExitSpan(Constants.CLUSTER_HEALTH_NAME, restClientEnhanceInfo.getPeers()); + span.setComponent(ComponentsDefine.REST_HIGH_LEVEL_CLIENT); + + Tags.DB_TYPE.set(span, DB_TYPE); + SpanLayer.asDB(span); + } + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Object ret) throws Throwable { + RestClientEnhanceInfo restClientEnhanceInfo = (RestClientEnhanceInfo) (objInst.getSkyWalkingDynamicField()); + if (restClientEnhanceInfo != null) { + ContextManager.stopSpan(); + } + return ret; + } + + @Override + public void handleMethodException(EnhancedInstance objInst, Method method, + Object[] allArguments, Class[] argumentsTypes, Throwable t) { + RestClientEnhanceInfo restClientEnhanceInfo = (RestClientEnhanceInfo) (objInst.getSkyWalkingDynamicField()); + if (restClientEnhanceInfo != null) { + ContextManager.activeSpan().errorOccurred().log(t); + } + } +} diff --git a/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/ClusterClientPutSettingsMethodsInterceptor.java b/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/ClusterClientPutSettingsMethodsInterceptor.java new file mode 100644 index 000000000..63aa133a0 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/ClusterClientPutSettingsMethodsInterceptor.java @@ -0,0 +1,87 @@ +/* + * 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.elasticsearch.v6.interceptor; + +import static org.apache.skywalking.apm.agent.core.conf.Config.Plugin.Elasticsearch.TRACE_DSL; +import static org.apache.skywalking.apm.plugin.elasticsearch.v6.interceptor.Constants.DB_TYPE; + +import java.lang.reflect.Method; +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.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.skywalking.apm.plugin.elasticsearch.v6.RestClientEnhanceInfo; +import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; +import org.elasticsearch.common.settings.Settings; + +public class ClusterClientPutSettingsMethodsInterceptor implements InstanceMethodsAroundInterceptor { + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { + ClusterUpdateSettingsRequest updateSettingsRequest = (ClusterUpdateSettingsRequest) (allArguments[0]); + + RestClientEnhanceInfo restClientEnhanceInfo = (RestClientEnhanceInfo) (objInst.getSkyWalkingDynamicField()); + if (restClientEnhanceInfo != null) { + AbstractSpan span = ContextManager + .createExitSpan(Constants.CLUSTER_PUT_SETTINGS_NAME, restClientEnhanceInfo.getPeers()); + span.setComponent(ComponentsDefine.REST_HIGH_LEVEL_CLIENT); + + Tags.DB_TYPE.set(span, DB_TYPE); + if (TRACE_DSL) { + StringBuilder sb = new StringBuilder("persistent:["); + Settings persist = updateSettingsRequest.persistentSettings(); + if (persist != null) { + sb.append(persist.toString()); + } + sb.append("]---transient:["); + Settings transi = updateSettingsRequest.transientSettings(); + if (transi != null) { + sb.append(transi.toString()); + } + sb.append("]"); + Tags.DB_STATEMENT.set(span, sb.toString()); + } + SpanLayer.asDB(span); + } + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Object ret) throws Throwable { + RestClientEnhanceInfo restClientEnhanceInfo = (RestClientEnhanceInfo) (objInst.getSkyWalkingDynamicField()); + if (restClientEnhanceInfo != null) { + ContextManager.stopSpan(); + } + return ret; + } + + @Override + public void handleMethodException(EnhancedInstance objInst, Method method, + Object[] allArguments, Class[] argumentsTypes, Throwable t) { + RestClientEnhanceInfo restClientEnhanceInfo = (RestClientEnhanceInfo) (objInst.getSkyWalkingDynamicField()); + if (restClientEnhanceInfo != null) { + ContextManager.activeSpan().errorOccurred().log(t); + } + } + +} diff --git a/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/Constants.java b/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/Constants.java index 583a52fbb..0c3c3f6f7 100644 --- a/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/Constants.java +++ b/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/Constants.java @@ -28,6 +28,10 @@ public class Constants { public static final String REST_HIGH_LEVEL_CLIENT_UPDATE_METHODS_INTERCEPTOR = "org.apache.skywalking.apm.plugin.elasticsearch.v6.interceptor.RestHighLevelClientUpdateMethodsInterceptor"; public static final String REST_HIGH_LEVEL_CLIENT_INDEX_METHODS_INTERCEPTOR = "org.apache.skywalking.apm.plugin.elasticsearch.v6.interceptor.RestHighLevelClientIndexMethodsInterceptor"; public static final String REST_HIGH_LEVEL_CLIENT_INDICES_METHODS_INTERCEPTOR = "org.apache.skywalking.apm.plugin.elasticsearch.v6.interceptor.RestHighLevelClientIndicesMethodsInterceptor"; + public static final String REST_HIGH_LEVEL_CLIENT_CLUSTER_METHODS_INTERCEPTOR = "org.apache.skywalking.apm.plugin.elasticsearch.v6.interceptor.RestHighLevelClientClusterMethodsInterceptor"; + public static final String CLUSTER_CLIENT_HEALTH_METHODS_INTERCEPTOR = "org.apache.skywalking.apm.plugin.elasticsearch.v6.interceptor.ClusterClientHealthMethodsInterceptor"; + public static final String CLUSTER_CLIENT_GET_SETTINGS_METHODS_INTERCEPTOR = "org.apache.skywalking.apm.plugin.elasticsearch.v6.interceptor.ClusterClientGetSettingsMethodsInterceptor"; + public static final String CLUSTER_CLIENT_PUT_SETTINGS_METHODS_INTERCEPTOR = "org.apache.skywalking.apm.plugin.elasticsearch.v6.interceptor.ClusterClientPutSettingsMethodsInterceptor"; //es operator name public static final String CREATE_OPERATOR_NAME = "Elasticsearch/CreateRequest"; @@ -36,6 +40,9 @@ public class Constants { public static final String INDEX_OPERATOR_NAME = "Elasticsearch/IndexRequest"; public static final String SEARCH_OPERATOR_NAME = "Elasticsearch/SearchRequest"; public static final String UPDATE_OPERATOR_NAME = "Elasticsearch/UpdateRequest"; + public static final String CLUSTER_HEALTH_NAME = "Elasticsearch/Health"; + public static final String CLUSTER_GET_SETTINGS_NAME = "Elasticsearch/GetSettings"; + public static final String CLUSTER_PUT_SETTINGS_NAME = "Elasticsearch/PutSettings"; public static final String DB_TYPE = "Elasticsearch"; } diff --git a/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/RestHighLevelClientClusterMethodsInterceptor.java b/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/RestHighLevelClientClusterMethodsInterceptor.java new file mode 100644 index 000000000..1bd434721 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/RestHighLevelClientClusterMethodsInterceptor.java @@ -0,0 +1,49 @@ +/* + * 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.elasticsearch.v6.interceptor; + +import java.lang.reflect.Method; +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.plugin.elasticsearch.v6.RestClientEnhanceInfo; + +public class RestHighLevelClientClusterMethodsInterceptor implements InstanceMethodsAroundInterceptor { + + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { + + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Object ret) throws Throwable { + if (ret instanceof EnhancedInstance) { + ((EnhancedInstance) ret).setSkyWalkingDynamicField((RestClientEnhanceInfo) (objInst.getSkyWalkingDynamicField())); + } + return ret; + } + + @Override + public void handleMethodException(EnhancedInstance objInst, Method method, + Object[] allArguments, Class[] argumentsTypes, Throwable t) { + + } +} diff --git a/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/resources/skywalking-plugin.def b/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/resources/skywalking-plugin.def index 3db969263..9752c2205 100644 --- a/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/resources/skywalking-plugin.def +++ b/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/main/resources/skywalking-plugin.def @@ -16,3 +16,4 @@ elasticsearch-6.x=org.apache.skywalking.apm.plugin.elasticsearch.v6.define.RestHighLevelClientInstrumentation elasticsearch-6.x=org.apache.skywalking.apm.plugin.elasticsearch.v6.define.IndicesClientInstrumentation +elasticsearch-6.x=org.apache.skywalking.apm.plugin.elasticsearch.v6.define.ClusterClientInstrumentation diff --git a/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/ClusterClientGetSettingsMethodsInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/ClusterClientGetSettingsMethodsInterceptorTest.java new file mode 100644 index 000000000..64aecd090 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/ClusterClientGetSettingsMethodsInterceptorTest.java @@ -0,0 +1,123 @@ +/* + * 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.elasticsearch.v6.interceptor; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.powermock.api.mockito.PowerMockito.when; + +import java.util.List; +import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan; +import org.apache.skywalking.apm.agent.core.context.trace.ExitSpan; +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.SpanAssert; +import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner; +import org.apache.skywalking.apm.plugin.elasticsearch.v6.RestClientEnhanceInfo; +import org.elasticsearch.action.admin.cluster.settings.ClusterGetSettingsRequest; +import org.junit.Assert; +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; + +@RunWith(PowerMockRunner.class) +@PowerMockRunnerDelegate(TracingSegmentRunner.class) +public class ClusterClientGetSettingsMethodsInterceptorTest { + + @SegmentStoragePoint + private SegmentStorage segmentStorage; + + @Rule + public AgentServiceRule serviceRule = new AgentServiceRule(); + + @Mock + private EnhancedInstance enhancedInstance; + + @Mock + private ClusterGetSettingsRequest getSettingsRequest; + + private Object[] allArguments; + + @Mock + private RestClientEnhanceInfo restClientEnhanceInfo; + + @Mock + private ClusterClientGetSettingsMethodsInterceptor interceptor; + + @Before + public void setUp() throws Exception { + when(restClientEnhanceInfo.getPeers()).thenReturn("172.0.0.1:9200"); + allArguments = new Object[] {getSettingsRequest}; + when(enhancedInstance.getSkyWalkingDynamicField()).thenReturn(restClientEnhanceInfo); + interceptor = new ClusterClientGetSettingsMethodsInterceptor(); + } + + @Test + public void testMethodsAround() throws Throwable { + interceptor.beforeMethod(enhancedInstance, null, allArguments, null, null); + interceptor.afterMethod(enhancedInstance, null, allArguments, null, null); + + List traceSegmentList = segmentStorage.getTraceSegments(); + Assert.assertThat(traceSegmentList.size(), is(1)); + TraceSegment traceSegment = traceSegmentList.get(0); + + AbstractTracingSpan indexSpan = SegmentHelper.getSpans(traceSegment).get(0); + assertIndexSpan(indexSpan); + } + + private void assertIndexSpan(AbstractTracingSpan getSpan) { + assertThat(getSpan instanceof ExitSpan, is(true)); + + ExitSpan exitSpan = (ExitSpan) getSpan; + assertThat(exitSpan.getOperationName(), is("Elasticsearch/GetSettings")); + assertThat(exitSpan.getPeer(), is("172.0.0.1:9200")); + assertThat(SpanHelper.getComponentId(exitSpan), is(77)); + + List tags = SpanHelper.getTags(exitSpan); + assertThat(tags.size(), is(1)); + assertThat(tags.get(0).getValue(), is("Elasticsearch")); + } + + @Test + public void testMethodsAroundError() throws Throwable { + interceptor.beforeMethod(enhancedInstance, null, allArguments, null, null); + interceptor.handleMethodException(enhancedInstance, null, allArguments, null, new RuntimeException()); + interceptor.afterMethod(enhancedInstance, null, allArguments, null, null); + + List traceSegmentList = segmentStorage.getTraceSegments(); + Assert.assertThat(traceSegmentList.size(), is(1)); + TraceSegment traceSegment = traceSegmentList.get(0); + + AbstractTracingSpan indexSpan = SegmentHelper.getSpans(traceSegment).get(0); + assertIndexSpan(indexSpan); + + Assert.assertEquals(true, SpanHelper.getErrorOccurred(indexSpan)); + SpanAssert.assertException(SpanHelper.getLogs(indexSpan).get(0), RuntimeException.class); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/ClusterClientHealthMethodsInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/ClusterClientHealthMethodsInterceptorTest.java new file mode 100644 index 000000000..a78157a5a --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/ClusterClientHealthMethodsInterceptorTest.java @@ -0,0 +1,123 @@ +/* + * 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.elasticsearch.v6.interceptor; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.powermock.api.mockito.PowerMockito.when; + +import java.util.List; +import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan; +import org.apache.skywalking.apm.agent.core.context.trace.ExitSpan; +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.SpanAssert; +import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner; +import org.apache.skywalking.apm.plugin.elasticsearch.v6.RestClientEnhanceInfo; +import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest; +import org.junit.Assert; +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; + +@RunWith(PowerMockRunner.class) +@PowerMockRunnerDelegate(TracingSegmentRunner.class) +public class ClusterClientHealthMethodsInterceptorTest { + + @SegmentStoragePoint + private SegmentStorage segmentStorage; + + @Rule + public AgentServiceRule serviceRule = new AgentServiceRule(); + + @Mock + private EnhancedInstance enhancedInstance; + + @Mock + private ClusterHealthRequest healthRequest; + + private Object[] allArguments; + + @Mock + private RestClientEnhanceInfo restClientEnhanceInfo; + + @Mock + private ClusterClientHealthMethodsInterceptor interceptor; + + @Before + public void setUp() throws Exception { + when(restClientEnhanceInfo.getPeers()).thenReturn("172.0.0.1:9200"); + allArguments = new Object[] {healthRequest}; + when(enhancedInstance.getSkyWalkingDynamicField()).thenReturn(restClientEnhanceInfo); + interceptor = new ClusterClientHealthMethodsInterceptor(); + } + + @Test + public void testMethodsAround() throws Throwable { + interceptor.beforeMethod(enhancedInstance, null, allArguments, null, null); + interceptor.afterMethod(enhancedInstance, null, allArguments, null, null); + + List traceSegmentList = segmentStorage.getTraceSegments(); + Assert.assertThat(traceSegmentList.size(), is(1)); + TraceSegment traceSegment = traceSegmentList.get(0); + + AbstractTracingSpan indexSpan = SegmentHelper.getSpans(traceSegment).get(0); + assertIndexSpan(indexSpan); + } + + private void assertIndexSpan(AbstractTracingSpan getSpan) { + assertThat(getSpan instanceof ExitSpan, is(true)); + + ExitSpan exitSpan = (ExitSpan) getSpan; + assertThat(exitSpan.getOperationName(), is("Elasticsearch/Health")); + assertThat(exitSpan.getPeer(), is("172.0.0.1:9200")); + assertThat(SpanHelper.getComponentId(exitSpan), is(77)); + + List tags = SpanHelper.getTags(exitSpan); + assertThat(tags.size(), is(1)); + assertThat(tags.get(0).getValue(), is("Elasticsearch")); + } + + @Test + public void testMethodsAroundError() throws Throwable { + interceptor.beforeMethod(enhancedInstance, null, allArguments, null, null); + interceptor.handleMethodException(enhancedInstance, null, allArguments, null, new RuntimeException()); + interceptor.afterMethod(enhancedInstance, null, allArguments, null, null); + + List traceSegmentList = segmentStorage.getTraceSegments(); + Assert.assertThat(traceSegmentList.size(), is(1)); + TraceSegment traceSegment = traceSegmentList.get(0); + + AbstractTracingSpan indexSpan = SegmentHelper.getSpans(traceSegment).get(0); + assertIndexSpan(indexSpan); + + Assert.assertEquals(true, SpanHelper.getErrorOccurred(indexSpan)); + SpanAssert.assertException(SpanHelper.getLogs(indexSpan).get(0), RuntimeException.class); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/ClusterClientPutSettingsMethodsInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/ClusterClientPutSettingsMethodsInterceptorTest.java new file mode 100644 index 000000000..fac1c2780 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/elasticsearch-6.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/elasticsearch/v6/interceptor/ClusterClientPutSettingsMethodsInterceptorTest.java @@ -0,0 +1,137 @@ +/* + * 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.elasticsearch.v6.interceptor; + +import static org.apache.skywalking.apm.agent.core.conf.Config.Plugin.Elasticsearch.TRACE_DSL; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.powermock.api.mockito.PowerMockito.when; + +import java.util.List; +import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan; +import org.apache.skywalking.apm.agent.core.context.trace.ExitSpan; +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.SpanAssert; +import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner; +import org.apache.skywalking.apm.plugin.elasticsearch.v6.RestClientEnhanceInfo; +import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; +import org.elasticsearch.common.settings.Settings; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.modules.junit4.PowerMockRunnerDelegate; + +@RunWith(PowerMockRunner.class) +@PowerMockRunnerDelegate(TracingSegmentRunner.class) +@PrepareForTest(value = { + Settings.class +}) +public class ClusterClientPutSettingsMethodsInterceptorTest { + + @SegmentStoragePoint + private SegmentStorage segmentStorage; + + @Rule + public AgentServiceRule serviceRule = new AgentServiceRule(); + + @Mock + private EnhancedInstance enhancedInstance; + + @Mock + private ClusterUpdateSettingsRequest updateSettingsRequest; + + private Object[] allArguments; + + @Mock + private RestClientEnhanceInfo restClientEnhanceInfo; + + @Mock + private ClusterClientPutSettingsMethodsInterceptor interceptor; + + @Mock + private Settings settings; + + @Before + public void setUp() throws Exception { + when(restClientEnhanceInfo.getPeers()).thenReturn("172.0.0.1:9200"); + allArguments = new Object[] {updateSettingsRequest}; + when(enhancedInstance.getSkyWalkingDynamicField()).thenReturn(restClientEnhanceInfo); + when(updateSettingsRequest.persistentSettings()).thenReturn(settings); + when(settings.toString()).thenReturn("settings"); + interceptor = new ClusterClientPutSettingsMethodsInterceptor(); + } + + @Test + public void testMethodsAround() throws Throwable { + TRACE_DSL = true; + interceptor.beforeMethod(enhancedInstance, null, allArguments, null, null); + interceptor.afterMethod(enhancedInstance, null, allArguments, null, null); + + List traceSegmentList = segmentStorage.getTraceSegments(); + Assert.assertThat(traceSegmentList.size(), is(1)); + TraceSegment traceSegment = traceSegmentList.get(0); + + AbstractTracingSpan indexSpan = SegmentHelper.getSpans(traceSegment).get(0); + assertIndexSpan(indexSpan); + } + + private void assertIndexSpan(AbstractTracingSpan getSpan) { + assertThat(getSpan instanceof ExitSpan, is(true)); + + ExitSpan exitSpan = (ExitSpan) getSpan; + assertThat(exitSpan.getOperationName(), is("Elasticsearch/PutSettings")); + assertThat(exitSpan.getPeer(), is("172.0.0.1:9200")); + assertThat(SpanHelper.getComponentId(exitSpan), is(77)); + + List tags = SpanHelper.getTags(exitSpan); + assertThat(tags.size(), is(2)); + assertThat(tags.get(0).getValue(), is("Elasticsearch")); + assertThat(tags.get(1).getValue(), is("persistent:[settings]---transient:[]")); + } + + @Test + public void testMethodsAroundError() throws Throwable { + interceptor.beforeMethod(enhancedInstance, null, allArguments, null, null); + interceptor.handleMethodException(enhancedInstance, null, allArguments, null, new RuntimeException()); + interceptor.afterMethod(enhancedInstance, null, allArguments, null, null); + + List traceSegmentList = segmentStorage.getTraceSegments(); + Assert.assertThat(traceSegmentList.size(), is(1)); + TraceSegment traceSegment = traceSegmentList.get(0); + + AbstractTracingSpan indexSpan = SegmentHelper.getSpans(traceSegment).get(0); + assertIndexSpan(indexSpan); + + Assert.assertEquals(true, SpanHelper.getErrorOccurred(indexSpan)); + SpanAssert.assertException(SpanHelper.getLogs(indexSpan).get(0), RuntimeException.class); + } + +} diff --git a/test/plugin/scenarios/elasticsearch-7.x-scenario/config/expectedData.yaml b/test/plugin/scenarios/elasticsearch-7.x-scenario/config/expectedData.yaml index d7454bf4f..0b40177c1 100644 --- a/test/plugin/scenarios/elasticsearch-7.x-scenario/config/expectedData.yaml +++ b/test/plugin/scenarios/elasticsearch-7.x-scenario/config/expectedData.yaml @@ -27,7 +27,7 @@ segmentItems: segments: - segmentId: not null spans: - - operationName: Elasticsearch/CreateRequest + - operationName: Elasticsearch/Health operationId: 0 parentSpanId: 0 spanId: 1 @@ -42,9 +42,7 @@ segmentItems: peerId: 0 tags: - {key: db.type, value: Elasticsearch} - - {key: db.instance, value: not null} - - {key: db.statement, value: not null} - - operationName: Elasticsearch/IndexRequest + - operationName: Elasticsearch/GetSettings operationId: 0 parentSpanId: 0 spanId: 2 @@ -59,9 +57,7 @@ segmentItems: peerId: 0 tags: - {key: db.type, value: Elasticsearch} - - {key: db.instance, value: not null} - - {key: db.statement, value: not null} - - operationName: Elasticsearch/GetRequest + - operationName: Elasticsearch/PutSettings operationId: 0 parentSpanId: 0 spanId: 3 @@ -76,9 +72,8 @@ segmentItems: peerId: 0 tags: - {key: db.type, value: Elasticsearch} - - {key: db.instance, value: not null} - {key: db.statement, value: not null} - - operationName: Elasticsearch/SearchRequest + - operationName: Elasticsearch/CreateRequest operationId: 0 parentSpanId: 0 spanId: 4 @@ -95,7 +90,7 @@ segmentItems: - {key: db.type, value: Elasticsearch} - {key: db.instance, value: not null} - {key: db.statement, value: not null} - - operationName: Elasticsearch/UpdateRequest + - operationName: Elasticsearch/IndexRequest operationId: 0 parentSpanId: 0 spanId: 5 @@ -112,7 +107,7 @@ segmentItems: - {key: db.type, value: Elasticsearch} - {key: db.instance, value: not null} - {key: db.statement, value: not null} - - operationName: Elasticsearch/DeleteRequest + - operationName: Elasticsearch/GetRequest operationId: 0 parentSpanId: 0 spanId: 6 @@ -128,6 +123,57 @@ segmentItems: tags: - {key: db.type, value: Elasticsearch} - {key: db.instance, value: not null} + - {key: db.statement, value: not null} + - operationName: Elasticsearch/SearchRequest + operationId: 0 + parentSpanId: 0 + spanId: 7 + spanLayer: Database + startTime: nq 0 + endTime: nq 0 + componentId: 77 + componentName: '' + isError: false + spanType: Exit + peer: not null + peerId: 0 + tags: + - {key: db.type, value: Elasticsearch} + - {key: db.instance, value: not null} + - {key: db.statement, value: not null} + - operationName: Elasticsearch/UpdateRequest + operationId: 0 + parentSpanId: 0 + spanId: 8 + spanLayer: Database + startTime: nq 0 + endTime: nq 0 + componentId: 77 + componentName: '' + isError: false + spanType: Exit + peer: not null + peerId: 0 + tags: + - {key: db.type, value: Elasticsearch} + - {key: db.instance, value: not null} + - {key: db.statement, value: not null} + - operationName: Elasticsearch/DeleteRequest + operationId: 0 + parentSpanId: 0 + spanId: 9 + spanLayer: Database + startTime: nq 0 + endTime: nq 0 + componentId: 77 + componentName: '' + isError: false + spanType: Exit + peer: not null + peerId: 0 + tags: + - {key: db.type, value: Elasticsearch} + - {key: db.instance, value: not null} - operationName: /elasticsearch-case/case/elasticsearch operationId: 0 parentSpanId: -1 diff --git a/test/plugin/scenarios/elasticsearch-7.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/elasticsearch/controller/CaseController.java b/test/plugin/scenarios/elasticsearch-7.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/elasticsearch/controller/CaseController.java index 0237cb917..ef71b7113 100644 --- a/test/plugin/scenarios/elasticsearch-7.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/elasticsearch/controller/CaseController.java +++ b/test/plugin/scenarios/elasticsearch-7.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/elasticsearch/controller/CaseController.java @@ -25,6 +25,10 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest; import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; +import org.elasticsearch.action.admin.cluster.settings.ClusterGetSettingsRequest; +import org.elasticsearch.action.admin.cluster.settings.ClusterGetSettingsResponse; +import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; +import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; import org.elasticsearch.action.get.GetRequest; @@ -42,10 +46,12 @@ import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.indices.recovery.RecoverySettings; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptType; import org.elasticsearch.search.builder.SearchSourceBuilder; @@ -66,7 +72,7 @@ public class CaseController { private RestHighLevelClient client; @GetMapping("/healthCheck") - public String healthcheck() throws Exception { + public String healthCheck() throws Exception { ClusterHealthRequest request = new ClusterHealthRequest(); request.timeout(TimeValue.timeValueSeconds(10)); request.waitForStatus(ClusterHealthStatus.GREEN); @@ -84,21 +90,34 @@ public class CaseController { public String elasticsearch() throws Exception { String indexName = UUID.randomUUID().toString(); try { - //create - createIndex(client, indexName); + // health + health(); + + // get settings + getSettings(); + + // put settings + putSettings(); + + // create + createIndex(indexName); + // index - index(client, indexName); + index(indexName); client.indices().refresh(new RefreshRequest(indexName), RequestOptions.DEFAULT); - //get - get(client, indexName); + // get + get(indexName); + // search - search(client, indexName); + search(indexName); + // update - update(client, indexName); + update(indexName); + // delete - delete(client, indexName); + delete(indexName); } finally { if (null != client) { client.close(); @@ -107,7 +126,43 @@ public class CaseController { return "Success"; } - private void createIndex(RestHighLevelClient client, String indexName) throws IOException { + private void health() throws IOException { + ClusterHealthRequest request = new ClusterHealthRequest(); + ClusterHealthResponse response = client.cluster().health(request, RequestOptions.DEFAULT); + if (response.isTimedOut()) { + String message = "elastic search health fail!"; + logger.error(message); + throw new RuntimeException(message); + } + } + + private void putSettings() throws IOException { + ClusterUpdateSettingsRequest request = new ClusterUpdateSettingsRequest(); + String transientSettingKey = + RecoverySettings.INDICES_RECOVERY_MAX_BYTES_PER_SEC_SETTING.getKey(); + int transientSettingValue = 10; + Settings transientSettings = Settings.builder() + .put(transientSettingKey, transientSettingValue, ByteSizeUnit.BYTES) + .build(); + request.transientSettings(transientSettings); + ClusterUpdateSettingsResponse response = client.cluster().putSettings(request, RequestOptions.DEFAULT); + if (response == null) { + String message = "elasticsearch put settings fail."; + logger.error(message); + throw new RuntimeException(message); + } + } + + private void getSettings() throws IOException { + ClusterGetSettingsResponse response = client.cluster().getSettings(new ClusterGetSettingsRequest(), RequestOptions.DEFAULT); + if (response == null) { + String message = "elasticsearch get settings fail."; + logger.error(message); + throw new RuntimeException(message); + } + } + + private void createIndex(String indexName) throws IOException { CreateIndexRequest request = new CreateIndexRequest(indexName); XContentBuilder builder = XContentFactory.jsonBuilder(); @@ -141,7 +196,7 @@ public class CaseController { } } - private void index(RestHighLevelClient client, String indexName) throws IOException { + private void index(String indexName) throws IOException { XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); { @@ -159,7 +214,7 @@ public class CaseController { } } - private void get(RestHighLevelClient client, String indexName) throws IOException { + private void get(String indexName) throws IOException { GetRequest getRequest = new GetRequest(indexName, "1"); GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT); @@ -170,7 +225,7 @@ public class CaseController { } } - private void update(RestHighLevelClient client, String indexName) throws IOException { + private void update(String indexName) throws IOException { UpdateRequest request = new UpdateRequest(indexName, "1"); Map parameters = singletonMap("title", "c++ programing."); Script inline = new Script(ScriptType.INLINE, "painless", "ctx._source.title = params.title", parameters); @@ -184,7 +239,7 @@ public class CaseController { } } - private void delete(RestHighLevelClient client, String indexName) throws IOException { + private void delete(String indexName) throws IOException { DeleteIndexRequest request = new DeleteIndexRequest(indexName); AcknowledgedResponse deleteIndexResponse = client.indices().delete(request, RequestOptions.DEFAULT); if (!deleteIndexResponse.isAcknowledged()) { @@ -194,7 +249,7 @@ public class CaseController { } } - private void search(RestHighLevelClient client, String indexName) throws IOException { + private void search(String indexName) throws IOException { SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.query(QueryBuilders.termQuery("author", "Marker")); sourceBuilder.from(0);