1.add es agent resthighlevelclient of ClusterClientInstrumentation.2.… (#4386)

* 1.add es agent resthighlevelclient of ClusterClientInstrumentation.2.fix agent test removeOnExit parse error.

* Update configuration.yml

* revert removeOnExit param.

Co-authored-by: Daming <zteny@foxmail.com>
This commit is contained in:
aderm 2020-02-21 14:00:19 +08:00 committed by GitHub
parent d02e472d14
commit 67b7f22a40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 912 additions and 27 deletions

View File

@ -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<MethodDescription> 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<MethodDescription> 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<MethodDescription> 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];
}
}

View File

@ -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<MethodDescription> 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<MethodDescription> getMethodsMatcher() {
return named("cluster");
}
@Override
public String getMethodsInterceptor() {
return Constants.REST_HIGH_LEVEL_CLIENT_CLUSTER_METHODS_INTERCEPTOR;
}
@Override
public boolean isOverrideArgs() {
return false;

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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";
}

View File

@ -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) {
}
}

View File

@ -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

View File

@ -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<TraceSegment> 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<TagValuePair> 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<TraceSegment> 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);
}
}

View File

@ -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<TraceSegment> 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<TagValuePair> 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<TraceSegment> 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);
}
}

View File

@ -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<TraceSegment> 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<TagValuePair> 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<TraceSegment> 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);
}
}

View File

@ -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

View File

@ -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<String, Object> 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);