未验证 提交 5e9582ee 编写于 作者: C CommissarXia 提交者: GitHub

Add interceptors of method(analyze, searchScroll, clearScroll, searchTemplate...

Add interceptors of method(analyze, searchScroll, clearScroll, searchTemplate and deleteByQuery) for elasticsearch-6.x-plugin. (#5962)
上级 6feeaf6e
......@@ -12,10 +12,12 @@ Release Notes.
#### Java Agent
* The operation name of quartz-scheduler plugin, has been changed as the `quartz-scheduler/${className}` format.
* Fix jdk-http and okhttp-3.x plugin did not overwrite the old trace header.
* Add interceptors of method(analyze, searchScroll, clearScroll, searchTemplate and deleteByQuery) for elasticsearch-6.x-plugin.
* Support collecting logs of log4j, log4j2, and logback in the tracing context with a new `logger-plugin`.
* Fix the unexpected RunningContext recreation in the Tomcat plugin.
* Fix the potential NPE when trace_sql_parameters is enabled.
#### OAP-Backend
* Make meter receiver support MAL.
* Support influxDB connection response format option. Fix some error when use JSON as influxDB response format.
......
......@@ -80,6 +80,22 @@ public class IndicesClientInstrumentation extends ClassEnhancePluginDefine {
return Constants.INDICES_CLIENT_DELETE_METHODS_INTERCEPTOR;
}
@Override
public boolean isOverrideArgs() {
return false;
}
},
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named("analyze").or(named("analyzeAsync"));
}
@Override
public String getMethodsInterceptor() {
return Constants.INDICES_CLIENT_ANALYZE_METHODS_INTERCEPTOR;
}
@Override
public boolean isOverrideArgs() {
return false;
......
......@@ -181,6 +181,70 @@ public class RestHighLevelClientInstrumentation extends ClassEnhancePluginDefine
return Constants.REST_HIGH_LEVEL_CLIENT_CLUSTER_METHODS_INTERCEPTOR;
}
@Override
public boolean isOverrideArgs() {
return false;
}
},
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named("scroll").or(named("scrollAsync"));
}
@Override
public String getMethodsInterceptor() {
return Constants.REST_HIGH_LEVEL_CLIENT_SEARCH_SCROLL_METHODS_INTERCEPTOR;
}
@Override
public boolean isOverrideArgs() {
return false;
}
},
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named("searchTemplate").or(named("searchTemplateAsync"));
}
@Override
public String getMethodsInterceptor() {
return Constants.REST_HIGH_LEVEL_CLIENT_SEARCH_TEMPLATE_METHODS_INTERCEPTOR;
}
@Override
public boolean isOverrideArgs() {
return false;
}
},
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named("clearScroll");
}
@Override
public String getMethodsInterceptor() {
return Constants.REST_HIGH_LEVEL_CLIENT_CLEAR_SCROLL_METHODS_INTERCEPTOR;
}
@Override
public boolean isOverrideArgs() {
return false;
}
},
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named("deleteByQuery").or(named("deleteByQueryAsync"));
}
@Override
public String getMethodsInterceptor() {
return Constants.REST_HIGH_LEVEL_CLIENT_DELETE_BY_QUERY_METHODS_INTERCEPTOR;
}
@Override
public boolean isOverrideArgs() {
return false;
......
......@@ -26,6 +26,11 @@ public class Constants {
public static final String REST_HIGH_LEVEL_CLIENT_CON_INTERCEPTOR = "org.apache.skywalking.apm.plugin.elasticsearch.v6.interceptor.RestHighLevelClientConInterceptor";
public static final String INDICES_CLIENT_CREATE_METHODS_INTERCEPTOR = "org.apache.skywalking.apm.plugin.elasticsearch.v6.interceptor.IndicesClientCreateMethodsInterceptor";
public static final String INDICES_CLIENT_DELETE_METHODS_INTERCEPTOR = "org.apache.skywalking.apm.plugin.elasticsearch.v6.interceptor.IndicesClientDeleteMethodsInterceptor";
public static final String INDICES_CLIENT_ANALYZE_METHODS_INTERCEPTOR = "org.apache.skywalking.apm.plugin.elasticsearch.v6.interceptor.IndicesClientAnalyzeMethodsInterceptor";
public static final String REST_HIGH_LEVEL_CLIENT_SEARCH_SCROLL_METHODS_INTERCEPTOR = "org.apache.skywalking.apm.plugin.elasticsearch.v6.interceptor.RestHighLevelClientSearchScrollMethodsInterceptor";
public static final String REST_HIGH_LEVEL_CLIENT_SEARCH_TEMPLATE_METHODS_INTERCEPTOR = "org.apache.skywalking.apm.plugin.elasticsearch.v6.interceptor.RestHighLevelClientSearchTemplateMethodsInterceptor";
public static final String REST_HIGH_LEVEL_CLIENT_CLEAR_SCROLL_METHODS_INTERCEPTOR = "org.apache.skywalking.apm.plugin.elasticsearch.v6.interceptor.RestHighLevelClientClearScrollMethodsInterceptor";
public static final String REST_HIGH_LEVEL_CLIENT_DELETE_BY_QUERY_METHODS_INTERCEPTOR = "org.apache.skywalking.apm.plugin.elasticsearch.v6.interceptor.RestHighLevelClientDeleteByQueryMethodsInterceptor";
public static final String REST_HIGH_LEVEL_CLIENT_GET_METHODS_INTERCEPTOR = "org.apache.skywalking.apm.plugin.elasticsearch.v6.interceptor.RestHighLevelClientGetMethodsInterceptor";
public static final String REST_HIGH_LEVEL_CLIENT_SEARCH_METHODS_INTERCEPTOR = "org.apache.skywalking.apm.plugin.elasticsearch.v6.interceptor.RestHighLevelClientSearchMethodsInterceptor";
public static final String REST_HIGH_LEVEL_CLIENT_UPDATE_METHODS_INTERCEPTOR = "org.apache.skywalking.apm.plugin.elasticsearch.v6.interceptor.RestHighLevelClientUpdateMethodsInterceptor";
......@@ -42,10 +47,15 @@ public class Constants {
//es operator name
public static final String CREATE_OPERATOR_NAME = "Elasticsearch/CreateRequest";
public static final String DELETE_OPERATOR_NAME = "Elasticsearch/DeleteRequest";
public static final String ANALYZE_OPERATOR_NAME = "Elasticsearch/AnalyzeRequest";
public static final String GET_OPERATOR_NAME = "Elasticsearch/GetRequest";
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 SEARCH_SCROLL_OPERATOR_NAME = "Elasticsearch/SearchScrollRequest";
public static final String SEARCH_TEMPLATE_OPERATOR_NAME = "Elasticsearch/SearchTemplateRequest";
public static final String CLEAR_SCROLL_OPERATOR_NAME = "Elasticsearch/ClearScrollRequest";
public static final String DELETE_BY_QUERY_OPERATOR_NAME = "Elasticsearch/DeleteByQueryRequest";
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";
......
/*
* 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 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.indices.analyze.AnalyzeRequest;
import java.lang.reflect.Method;
import static org.apache.skywalking.apm.plugin.elasticsearch.v6.ElasticsearchPluginConfig.Plugin.Elasticsearch.TRACE_DSL;
import static org.apache.skywalking.apm.plugin.elasticsearch.v6.interceptor.Constants.DB_TYPE;
public class IndicesClientAnalyzeMethodsInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
AnalyzeRequest analyzeRequest = (AnalyzeRequest) allArguments[0];
RestClientEnhanceInfo restClientEnhanceInfo = (RestClientEnhanceInfo) objInst.getSkyWalkingDynamicField();
if (restClientEnhanceInfo != null) {
AbstractSpan span = ContextManager.createExitSpan(Constants.ANALYZE_OPERATOR_NAME, restClientEnhanceInfo.getPeers());
span.setComponent(ComponentsDefine.REST_HIGH_LEVEL_CLIENT);
Tags.DB_TYPE.set(span, DB_TYPE);
span.tag(Tags.ofKey("analyzer"), analyzeRequest.analyzer());
if (TRACE_DSL) {
Tags.DB_STATEMENT.set(span, analyzeRequest.text()[0]);
}
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().log(t);
}
}
}
/*
* 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 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.search.ClearScrollRequest;
import java.lang.reflect.Method;
import java.util.stream.Collectors;
import static org.apache.skywalking.apm.plugin.elasticsearch.v6.ElasticsearchPluginConfig.Plugin.Elasticsearch.TRACE_DSL;
import static org.apache.skywalking.apm.plugin.elasticsearch.v6.interceptor.Constants.DB_TYPE;
public class RestHighLevelClientClearScrollMethodsInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
ClearScrollRequest searchScrollRequest = (ClearScrollRequest) allArguments[0];
RestClientEnhanceInfo restClientEnhanceInfo = (RestClientEnhanceInfo) objInst.getSkyWalkingDynamicField();
AbstractSpan span = ContextManager.createExitSpan(Constants.CLEAR_SCROLL_OPERATOR_NAME, restClientEnhanceInfo.getPeers());
span.setComponent(ComponentsDefine.REST_HIGH_LEVEL_CLIENT);
Tags.DB_TYPE.set(span, DB_TYPE);
if (TRACE_DSL) {
Tags.DB_STATEMENT.set(span, searchScrollRequest.scrollIds().stream().collect(Collectors.joining(",")));
}
SpanLayer.asDB(span);
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
ContextManager.stopSpan();
return ret;
}
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
ContextManager.activeSpan().log(t);
}
}
/*
* 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 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.index.reindex.DeleteByQueryRequest;
import java.lang.reflect.Method;
import java.util.Arrays;
import static org.apache.skywalking.apm.plugin.elasticsearch.v6.ElasticsearchPluginConfig.Plugin.Elasticsearch.TRACE_DSL;
import static org.apache.skywalking.apm.plugin.elasticsearch.v6.interceptor.Constants.DB_TYPE;
public class RestHighLevelClientDeleteByQueryMethodsInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
DeleteByQueryRequest deleteByQueryRequest = (DeleteByQueryRequest) allArguments[0];
RestClientEnhanceInfo restClientEnhanceInfo = (RestClientEnhanceInfo) objInst.getSkyWalkingDynamicField();
AbstractSpan span = ContextManager.createExitSpan(Constants.DELETE_BY_QUERY_OPERATOR_NAME, restClientEnhanceInfo.getPeers());
span.setComponent(ComponentsDefine.REST_HIGH_LEVEL_CLIENT);
Tags.DB_TYPE.set(span, DB_TYPE);
Tags.DB_INSTANCE.set(span, Arrays.asList(deleteByQueryRequest.indices()).toString());
if (TRACE_DSL) {
if (deleteByQueryRequest.getSearchRequest() != null) {
Tags.DB_STATEMENT.set(span, deleteByQueryRequest.getSearchRequest().toString());
} else {
Tags.DB_STATEMENT.set(span, deleteByQueryRequest.toString());
}
}
SpanLayer.asDB(span);
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
ContextManager.stopSpan();
return ret;
}
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
ContextManager.activeSpan().log(t);
}
}
/*
* 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 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.search.SearchScrollRequest;
import java.lang.reflect.Method;
import static org.apache.skywalking.apm.plugin.elasticsearch.v6.ElasticsearchPluginConfig.Plugin.Elasticsearch.TRACE_DSL;
import static org.apache.skywalking.apm.plugin.elasticsearch.v6.interceptor.Constants.DB_TYPE;
public class RestHighLevelClientSearchScrollMethodsInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
SearchScrollRequest searchScrollRequest = (SearchScrollRequest) allArguments[0];
RestClientEnhanceInfo restClientEnhanceInfo = (RestClientEnhanceInfo) objInst.getSkyWalkingDynamicField();
AbstractSpan span = ContextManager.createExitSpan(Constants.SEARCH_SCROLL_OPERATOR_NAME, restClientEnhanceInfo.getPeers());
span.setComponent(ComponentsDefine.REST_HIGH_LEVEL_CLIENT);
Tags.DB_TYPE.set(span, DB_TYPE);
if (TRACE_DSL) {
Tags.DB_STATEMENT.set(span, searchScrollRequest.toString());
}
SpanLayer.asDB(span);
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
ContextManager.stopSpan();
return ret;
}
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
ContextManager.activeSpan().log(t);
}
}
/*
* 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 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.script.mustache.SearchTemplateRequest;
import java.lang.reflect.Method;
import java.util.Arrays;
import static org.apache.skywalking.apm.plugin.elasticsearch.v6.ElasticsearchPluginConfig.Plugin.Elasticsearch.TRACE_DSL;
import static org.apache.skywalking.apm.plugin.elasticsearch.v6.interceptor.Constants.DB_TYPE;
public class RestHighLevelClientSearchTemplateMethodsInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
SearchTemplateRequest searchTemplateRequest = (SearchTemplateRequest) allArguments[0];
RestClientEnhanceInfo restClientEnhanceInfo = (RestClientEnhanceInfo) objInst.getSkyWalkingDynamicField();
AbstractSpan span = ContextManager.createExitSpan(Constants.SEARCH_TEMPLATE_OPERATOR_NAME, restClientEnhanceInfo.getPeers());
span.setComponent(ComponentsDefine.REST_HIGH_LEVEL_CLIENT);
Tags.DB_TYPE.set(span, DB_TYPE);
if (searchTemplateRequest.getRequest() != null) {
Tags.DB_INSTANCE.set(span, Arrays.asList(searchTemplateRequest.getRequest().indices()).toString());
}
if (TRACE_DSL) {
Tags.DB_STATEMENT.set(span, searchTemplateRequest.getScript());
}
SpanLayer.asDB(span);
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
ContextManager.stopSpan();
return ret;
}
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
ContextManager.activeSpan().log(t);
}
}
/*
* 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 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.indices.analyze.AnalyzeRequest;
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;
import java.util.List;
import static org.apache.skywalking.apm.plugin.elasticsearch.v6.ElasticsearchPluginConfig.Plugin.Elasticsearch.TRACE_DSL;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(TracingSegmentRunner.class)
public class IndicesClientAnalyzeMethodsInterceptorTest {
@SegmentStoragePoint
private SegmentStorage segmentStorage;
@Rule
public AgentServiceRule serviceRule = new AgentServiceRule();
@Mock
private EnhancedInstance enhancedInstance;
@Mock
private AnalyzeRequest analyzeRequest;
private Object[] allArguments;
@Mock
private RestClientEnhanceInfo restClientEnhanceInfo;
private IndicesClientAnalyzeMethodsInterceptor interceptor;
@Before
public void setUp() throws Exception {
when(restClientEnhanceInfo.getPeers()).thenReturn("127.0.0.1:9200");
allArguments = new Object[] {analyzeRequest};
when(analyzeRequest.analyzer()).thenReturn("analyzer");
when(analyzeRequest.text()).thenReturn(new String[] {"exampleText"});
when(enhancedInstance.getSkyWalkingDynamicField()).thenReturn(restClientEnhanceInfo);
interceptor = new IndicesClientAnalyzeMethodsInterceptor();
}
@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 analyzeSpan = SegmentHelper.getSpans(traceSegment).get(0);
assertAnalyzeSpan(analyzeSpan);
}
private void assertAnalyzeSpan(AbstractTracingSpan analyzeSpan) {
assertThat(analyzeSpan instanceof ExitSpan, is(true));
ExitSpan exitSpan = (ExitSpan) analyzeSpan;
assertThat(exitSpan.getOperationName(), is("Elasticsearch/AnalyzeRequest"));
assertThat(exitSpan.getPeer(), is("127.0.0.1:9200"));
assertThat(SpanHelper.getComponentId(exitSpan), is(77));
List<TagValuePair> tags = SpanHelper.getTags(exitSpan);
assertThat(tags.size(), is(3));
assertThat(tags.get(0).getValue(), is("Elasticsearch"));
assertThat(tags.get(1).getValue(), is("analyzer"));
assertThat(tags.get(2).getValue(), is("exampleText"));
}
@Test
public void testMethodsAroundError() throws Throwable {
TRACE_DSL = true;
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 analyzeSpan = SegmentHelper.getSpans(traceSegment).get(0);
assertAnalyzeSpan(analyzeSpan);
Assert.assertEquals(true, SpanHelper.getErrorOccurred(analyzeSpan));
SpanAssert.assertException(SpanHelper.getLogs(analyzeSpan).get(0), RuntimeException.class);
}
}
/*
* 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 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.search.ClearScrollRequest;
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;
import java.util.ArrayList;
import java.util.List;
import static org.apache.skywalking.apm.plugin.elasticsearch.v6.ElasticsearchPluginConfig.Plugin.Elasticsearch.TRACE_DSL;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(TracingSegmentRunner.class)
public class RestHighLevelClientClearScrollMethodsInterceptorTest {
@SegmentStoragePoint
private SegmentStorage segmentStorage;
@Rule
public AgentServiceRule serviceRule = new AgentServiceRule();
@Mock
private EnhancedInstance enhancedInstance;
@Mock
private ClearScrollRequest clearScrollRequest;
private Object[] allArguments;
@Mock
private RestClientEnhanceInfo restClientEnhanceInfo;
private RestHighLevelClientClearScrollMethodsInterceptor interceptor;
@Before
public void setUp() throws Exception {
when(restClientEnhanceInfo.getPeers()).thenReturn("127.0.0.1:9200");
allArguments = new Object[] {clearScrollRequest};
List<String> testList = new ArrayList<>(1);
testList.add("testScrollId");
when(clearScrollRequest.scrollIds()).thenReturn(testList);
when(enhancedInstance.getSkyWalkingDynamicField()).thenReturn(restClientEnhanceInfo);
interceptor = new RestHighLevelClientClearScrollMethodsInterceptor();
}
@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 clearScrollSpan = SegmentHelper.getSpans(traceSegment).get(0);
assertClearScrollSpan(clearScrollSpan);
}
private void assertClearScrollSpan(AbstractTracingSpan clearScrollSpan) {
assertThat(clearScrollSpan instanceof ExitSpan, is(true));
ExitSpan exitSpan = (ExitSpan) clearScrollSpan;
assertThat(exitSpan.getOperationName(), is("Elasticsearch/ClearScrollRequest"));
assertThat(exitSpan.getPeer(), is("127.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("testScrollId"));
}
@Test
public void testMethodsAroundError() throws Throwable {
TRACE_DSL = true;
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 clearScrollSpan = SegmentHelper.getSpans(traceSegment).get(0);
assertClearScrollSpan(clearScrollSpan);
Assert.assertEquals(true, SpanHelper.getErrorOccurred(clearScrollSpan));
SpanAssert.assertException(SpanHelper.getLogs(clearScrollSpan).get(0), RuntimeException.class);
}
}
/*
* 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 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.index.reindex.DeleteByQueryRequest;
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;
import java.util.List;
import static org.apache.skywalking.apm.plugin.elasticsearch.v6.ElasticsearchPluginConfig.Plugin.Elasticsearch.TRACE_DSL;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(TracingSegmentRunner.class)
public class RestHighLevelClientDeleteByQueryMethodsInterceptorTest {
@SegmentStoragePoint
private SegmentStorage segmentStorage;
@Rule
public AgentServiceRule serviceRule = new AgentServiceRule();
@Mock
private EnhancedInstance enhancedInstance;
@Mock
private DeleteByQueryRequest deleteByQueryRequest;
private Object[] allArguments;
@Mock
private RestClientEnhanceInfo restClientEnhanceInfo;
private RestHighLevelClientDeleteByQueryMethodsInterceptor interceptor;
@Before
public void setUp() throws Exception {
when(restClientEnhanceInfo.getPeers()).thenReturn("127.0.0.1:9200");
allArguments = new Object[] {deleteByQueryRequest};
when(deleteByQueryRequest.indices()).thenReturn(new String[] {"indexName"});
when(deleteByQueryRequest.toString()).thenReturn("deleteByQueryRequest");
when(enhancedInstance.getSkyWalkingDynamicField()).thenReturn(restClientEnhanceInfo);
interceptor = new RestHighLevelClientDeleteByQueryMethodsInterceptor();
}
@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 deleteByQuerySpan = SegmentHelper.getSpans(traceSegment).get(0);
assertDeleteByQuerySpan(deleteByQuerySpan);
}
private void assertDeleteByQuerySpan(AbstractTracingSpan deleteByQuerySpan) {
assertThat(deleteByQuerySpan instanceof ExitSpan, is(true));
ExitSpan exitSpan = (ExitSpan) deleteByQuerySpan;
assertThat(exitSpan.getOperationName(), is("Elasticsearch/DeleteByQueryRequest"));
assertThat(exitSpan.getPeer(), is("127.0.0.1:9200"));
assertThat(SpanHelper.getComponentId(exitSpan), is(77));
List<TagValuePair> tags = SpanHelper.getTags(exitSpan);
assertThat(tags.size(), is(3));
assertThat(tags.get(0).getValue(), is("Elasticsearch"));
assertThat(tags.get(1).getValue(), is("[indexName]"));
assertThat(tags.get(2).getValue(), is("deleteByQueryRequest"));
}
@Test
public void testMethodsAroundError() throws Throwable {
TRACE_DSL = true;
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 deleteByQuerySpan = SegmentHelper.getSpans(traceSegment).get(0);
assertDeleteByQuerySpan(deleteByQuerySpan);
Assert.assertEquals(true, SpanHelper.getErrorOccurred(deleteByQuerySpan));
SpanAssert.assertException(SpanHelper.getLogs(deleteByQuerySpan).get(0), RuntimeException.class);
}
}
/*
* 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 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.search.SearchScrollRequest;
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;
import java.util.List;
import static org.apache.skywalking.apm.plugin.elasticsearch.v6.ElasticsearchPluginConfig.Plugin.Elasticsearch.TRACE_DSL;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(TracingSegmentRunner.class)
public class RestHighLevelClientSearchScrollMethodsInterceptorTest {
@SegmentStoragePoint
private SegmentStorage segmentStorage;
@Rule
public AgentServiceRule serviceRule = new AgentServiceRule();
@Mock
private EnhancedInstance enhancedInstance;
@Mock
private SearchScrollRequest searchScrollRequest;
private Object[] allArguments;
@Mock
private RestClientEnhanceInfo restClientEnhanceInfo;
private RestHighLevelClientSearchScrollMethodsInterceptor interceptor;
@Before
public void setUp() throws Exception {
when(restClientEnhanceInfo.getPeers()).thenReturn("127.0.0.1:9200");
allArguments = new Object[] {searchScrollRequest};
when(searchScrollRequest.toString()).thenReturn("searchScrollRequest");
when(enhancedInstance.getSkyWalkingDynamicField()).thenReturn(restClientEnhanceInfo);
interceptor = new RestHighLevelClientSearchScrollMethodsInterceptor();
}
@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 searchScrollSpan = SegmentHelper.getSpans(traceSegment).get(0);
assertSearchScrollSpan(searchScrollSpan);
}
private void assertSearchScrollSpan(AbstractTracingSpan searchScrollSpan) {
assertThat(searchScrollSpan instanceof ExitSpan, is(true));
ExitSpan exitSpan = (ExitSpan) searchScrollSpan;
assertThat(exitSpan.getOperationName(), is("Elasticsearch/SearchScrollRequest"));
assertThat(exitSpan.getPeer(), is("127.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("searchScrollRequest"));
}
@Test
public void testMethodsAroundError() throws Throwable {
TRACE_DSL = true;
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 searchScrollSpan = SegmentHelper.getSpans(traceSegment).get(0);
assertSearchScrollSpan(searchScrollSpan);
Assert.assertEquals(true, SpanHelper.getErrorOccurred(searchScrollSpan));
SpanAssert.assertException(SpanHelper.getLogs(searchScrollSpan).get(0), RuntimeException.class);
}
}
/*
* 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 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.script.mustache.SearchTemplateRequest;
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;
import java.util.List;
import static org.apache.skywalking.apm.plugin.elasticsearch.v6.ElasticsearchPluginConfig.Plugin.Elasticsearch.TRACE_DSL;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(TracingSegmentRunner.class)
public class RestHighLevelClientSearchTemplateMethodsInterceptorTest {
@SegmentStoragePoint
private SegmentStorage segmentStorage;
@Rule
public AgentServiceRule serviceRule = new AgentServiceRule();
@Mock
private EnhancedInstance enhancedInstance;
@Mock
private SearchTemplateRequest searchTemplateRequest;
private Object[] allArguments;
@Mock
private RestClientEnhanceInfo restClientEnhanceInfo;
private RestHighLevelClientSearchTemplateMethodsInterceptor interceptor;
@Before
public void setUp() throws Exception {
when(restClientEnhanceInfo.getPeers()).thenReturn("127.0.0.1:9200");
allArguments = new Object[] {searchTemplateRequest};
when(searchTemplateRequest.getScript()).thenReturn("searchTemplateRequest");
when(enhancedInstance.getSkyWalkingDynamicField()).thenReturn(restClientEnhanceInfo);
interceptor = new RestHighLevelClientSearchTemplateMethodsInterceptor();
}
@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 searchTemplateSpan = SegmentHelper.getSpans(traceSegment).get(0);
assertSearchTemplateSpan(searchTemplateSpan);
}
private void assertSearchTemplateSpan(AbstractTracingSpan searchTemplateSpan) {
assertThat(searchTemplateSpan instanceof ExitSpan, is(true));
ExitSpan exitSpan = (ExitSpan) searchTemplateSpan;
assertThat(exitSpan.getOperationName(), is("Elasticsearch/SearchTemplateRequest"));
assertThat(exitSpan.getPeer(), is("127.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("searchTemplateRequest"));
}
@Test
public void testMethodsAroundError() throws Throwable {
TRACE_DSL = true;
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 searchTemplateSpan = SegmentHelper.getSpans(traceSegment).get(0);
assertSearchTemplateSpan(searchTemplateSpan);
Assert.assertEquals(true, SpanHelper.getErrorOccurred(searchTemplateSpan));
SpanAssert.assertException(SpanHelper.getLogs(searchTemplateSpan).get(0), RuntimeException.class);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册