Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
de5b793
new branch for solrj-plugin
May 22, 2019
1031356
rollback and add module solrj-plugin
May 22, 2019
0ce3017
preparing to pr
May 22, 2019
9151a87
to resolve reviewers' suggestions
May 23, 2019
109d478
Merge branch 'master' into solrj-plugin
May 23, 2019
10d923c
remove unused code
May 23, 2019
c1beec4
Merge branch 'solrj-plugin' of https://github.com/dmsolr/skywalking i…
May 23, 2019
e6b4d32
remove unused code
May 23, 2019
6727464
Merge branch 'master' into solrj-plugin
wu-sheng May 24, 2019
7bee1d6
remove unused comments
May 24, 2019
dea10a3
Merge branch 'master' into solrj-plugin
dmsolr May 24, 2019
a26b659
To avoid NPE
zteny May 24, 2019
cced8a7
fix typo
zteny May 24, 2019
bb2b5aa
Merge branch 'master' into solrj-plugin
wu-sheng May 25, 2019
57799c8
Change to JRE6
zteny May 25, 2019
dc7f960
Merge branch 'solrj-plugin' of https://github.com/dmsolr/skywalking i…
zteny May 25, 2019
4af0a8f
fix pom.xml merge incorrectly. and java.net.URL instead of Regex
zteny May 25, 2019
99c31f8
Using RuntimeContext instead of ThreadLocal
zteny May 26, 2019
f809f8c
To reduce unnecessary tags
zteny May 26, 2019
aa68f82
add test cases
dmsolr May 27, 2019
447da95
fix validation fail
dmsolr May 27, 2019
831dfa3
Merge branch 'master' into solrj-plugin
wu-sheng May 28, 2019
2404c78
add solrj-plugin into component-libraries & Supported-list
May 28, 2019
d3ce641
to trace all patch to avoid recheck status of span
May 29, 2019
65d7221
Merge branch 'master' into solrj-plugin
wu-sheng May 31, 2019
57d76d3
remove unnecessary properites
May 31, 2019
549be85
Merge branch 'master' into solrj-plugin
wu-sheng Jun 3, 2019
a310a94
remove unnecessary tags
Jun 3, 2019
0da3a38
Merge branch 'master' into solrj-plugin
wu-sheng Jun 4, 2019
74b1fc7
Add the config to document of setup
Jun 4, 2019
f24d46c
Merge branch 'master' into solrj-plugin
wu-sheng Jun 4, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ public class ComponentsDefine {

public static final OfficialComponent RESTEASY = new OfficialComponent(62, "RESTEasy");

public static final OfficialComponent SOLRJ = new OfficialComponent(63, "solrj");

private static ComponentsDefine INSTANCE = new ComponentsDefine();

private String[] components;
Expand Down Expand Up @@ -179,6 +181,7 @@ public ComponentsDefine() {
addComponent(VERTX);
addComponent(SPRING_CLOUD_GATEWAY);
addComponent(RESTEASY);
addComponent(SOLRJ);
}

private void addComponent(OfficialComponent component) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,17 @@ public static class Toolkit {
*/
public static boolean USE_QUALIFIED_NAME_AS_OPERATION_NAME = false;
}

public static class SolrJ {
/**
* If true, trace all the query parameters(include deleteByIds and deleteByQuery) in Solr query request, default is false.
*/
public static boolean TRACE_STATEMENT = false;

/**
* If true, trace all the operation parameters in Solr request, default is false.
*/
public static boolean TRACE_OPS_PARAMS = false;
}
}
}
3 changes: 2 additions & 1 deletion apm-sniffer/apm-sdk-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<module>dubbo-2.7.x-conflict-patch</module>
<module>vertx-plugins</module>
<module>resteasy-plugin</module>
<module>solrj-7.x-plugin</module>
Comment thread
wu-sheng marked this conversation as resolved.
</modules>
<packaging>pom</packaging>

Expand Down Expand Up @@ -182,4 +183,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
47 changes: 47 additions & 0 deletions apm-sniffer/apm-sdk-plugin/solrj-7.x-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
~
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>apm-sdk-plugin</artifactId>
<groupId>org.apache.skywalking</groupId>
<version>6.2.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>apm-solrj-7.x-plugin</artifactId>
<packaging>jar</packaging>

<name>solrj-7.x-plugin</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<solr-solrj.version>7.7.1</solr-solrj.version>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>${solr-solrj.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
/*
* 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.solrj;

import org.apache.skywalking.apm.agent.core.conf.Config;
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.InstanceConstructorInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
import org.apache.skywalking.apm.plugin.solrj.commons.SolrjInstance;
import org.apache.skywalking.apm.plugin.solrj.commons.SolrjTags;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.request.AbstractUpdateRequest;
import org.apache.solr.client.solrj.request.QueryRequest;
import org.apache.solr.client.solrj.request.UpdateRequest;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.params.UpdateParams;
import org.apache.solr.common.util.NamedList;

import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class SolrClientInterceptor implements InstanceMethodsAroundInterceptor, InstanceConstructorInterceptor {
private static final String DB_TYPE = "Solr";

@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
SolrjInstance instance = new SolrjInstance();
HttpSolrClient client = (HttpSolrClient) objInst;

try {
URL url = new URL(client.getBaseURL());
instance.setRemotePeer(url.getHost() + ":" + url.getPort());

String path = url.getPath();
int idx = path.lastIndexOf('/');
if (idx > 0) {
instance.setCollection(path.substring(idx + 1));
}
} catch (MalformedURLException ignore) {
}
objInst.setSkyWalkingDynamicField(instance);
}

@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
SolrRequest<?> request = (SolrRequest<?>) allArguments[0];
SolrjInstance instance = (SolrjInstance) objInst.getSkyWalkingDynamicField();

SolrParams params = getParams(request.getParams());
String collection = getCollection(instance, allArguments[2]);

if ("/update".equals(request.getPath())) {
Comment thread
wu-sheng marked this conversation as resolved.
AbstractUpdateRequest update = (AbstractUpdateRequest) request;

AbstractUpdateRequest.ACTION action = update.getAction();
if (action == null) {
if (update instanceof UpdateRequest) {
AbstractSpan span = null;

UpdateRequest ur = (UpdateRequest) update;
List<SolrInputDocument> documents = ur.getDocuments();
if (documents == null) {
String actionName = "DELETE_BY_IDS";

List<String> deleteBy = ur.getDeleteById();
if (deleteBy == null) {
actionName = "DELETE_BY_QUERY";
deleteBy = ur.getDeleteQuery();
}
if (deleteBy == null) {
deleteBy = new ArrayList<String>();
}
String operator = getOperatorNameWithAction(collection, request.getPath(), actionName);
span = getSpan(operator, instance.getRemotePeer());
if (Config.Plugin.SolrJ.TRACE_STATEMENT) {
span.tag(Tags.DB_STATEMENT, deleteBy.toString());
}
} else {
String operator = getOperatorNameWithAction(collection, request.getPath(), "ADD");
span = getSpan(operator, instance.getRemotePeer());
if (Config.Plugin.SolrJ.TRACE_STATEMENT) {
span.tag(SolrjTags.TAG_DOCS_SIZE, String.valueOf(documents.size()));
}
}
if (Config.Plugin.SolrJ.TRACE_OPS_PARAMS) {
span.tag(SolrjTags.TAG_COMMIT_WITHIN, String.valueOf(ur.getCommitWithin()));
}
} else {
getSpan(getOperatorName(collection, request.getPath()), instance.getRemotePeer());
}
} else {
String operator = getOperatorNameWithAction(collection, request.getPath(), action.name());
AbstractSpan span = getSpan(operator, instance.getRemotePeer());

if (Config.Plugin.SolrJ.TRACE_OPS_PARAMS) {
if (action == AbstractUpdateRequest.ACTION.COMMIT) {
span.tag(SolrjTags.TAG_SOFT_COMMIT, params.get(UpdateParams.SOFT_COMMIT, ""));
} else {
span.tag(SolrjTags.TAG_MAX_OPTIMIZE_SEGMENTS, params.get(UpdateParams.MAX_OPTIMIZE_SEGMENTS, "1"));
}
}
}
} else if (request instanceof QueryRequest) {
AbstractSpan span = getSpan(getOperatorName(collection, request.getPath()), instance.getRemotePeer());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Above codes include a lot of instanceof, which would be an impact for performance, especially in JRE 6-8. If possible, please consider a better way to do this.

span.tag(SolrjTags.TAG_START, params.get(CommonParams.START, "0"));
span.tag(SolrjTags.TAG_QT, params.get(CommonParams.QT, request.getPath()));

if (Config.Plugin.SolrJ.TRACE_STATEMENT) {
span.tag(Tags.DB_STATEMENT, toQueryString(params));
}
} else {
getSpan(getOperatorName(collection, request.getPath()), instance.getRemotePeer());
}
}

@Override
@SuppressWarnings("unchecked")
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Object ret) throws Throwable {
if (!ContextManager.isActive()) {
return ret;
}

AbstractSpan span = ContextManager.activeSpan();
if (ret != null) {
NamedList<Object> result = (NamedList<Object>) ret;
NamedList<Object> header = (NamedList<Object>) result.get("responseHeader");

if (header != null) {
span.tag(SolrjTags.TAG_Q_TIME, String.valueOf(header.get("QTime")));
}
SolrDocumentList list = (SolrDocumentList) result.get("response");
if (list != null) {
span.tag(SolrjTags.TAG_NUM_FOUND, String.valueOf(list.getNumFound()));
}
}

ContextManager.stopSpan();
return ret;
}

@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
if (ContextManager.isActive()) {
Comment thread
wu-sheng marked this conversation as resolved.
AbstractSpan span = ContextManager.activeSpan();
int code = 500;
if (t instanceof SolrException) {
code = ((SolrException) t).code();
}
span.tag(SolrjTags.TAG_STATUS, String.valueOf(code));
span.errorOccurred().log(t);
}
}

private static final AbstractSpan getSpan(String operatorName, String remotePeer) {
return ContextManager.createExitSpan(operatorName, remotePeer)
.setComponent(ComponentsDefine.SOLRJ)
.setLayer(SpanLayer.DB)
.tag(Tags.DB_TYPE, DB_TYPE);
}

private static final String getOperatorNameWithAction(String collection, String path, String action) {
return String.format("solrJ/%s%s/%s", collection, path, action);
}

private static final String getOperatorName(String collection, String path) {
return String.format("solrJ/%s%s", collection, path);
}

private static final String getCollection(SolrjInstance instance, Object argument) {
if (null == argument) {
return instance.getCollection();
}
return String.valueOf(argument);
}

private static final SolrParams getParams(SolrParams params) {
if (params == null) {
return new ModifiableSolrParams();
}
return params;
}

private static final String toQueryString(SolrParams params) {
final StringBuilder sb = new StringBuilder(128);
boolean first = true;
for (final Iterator<String> it = params.getParameterNamesIterator(); it.hasNext();) {
final String name = it.next();
for (String val : params.getParams(name)) {
sb.append(first ? '?' : '&').append(name).append('=').append(val);
first = false;
}
}
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.solrj;

import org.apache.http.client.methods.HttpUriRequest;
import org.apache.skywalking.apm.agent.core.context.CarrierItem;
import org.apache.skywalking.apm.agent.core.context.ContextCarrier;
import org.apache.skywalking.apm.agent.core.context.ContextManager;
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 java.lang.reflect.Method;

public class SolrConnectorInterceptor implements InstanceMethodsAroundInterceptor {

@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
HttpUriRequest request = (HttpUriRequest) allArguments[0];

ContextCarrier carrier = new ContextCarrier();
ContextManager.inject(carrier);

CarrierItem items = carrier.items();
while (items.hasNext()) {
items = items.next();
request.setHeader(items.getHeadKey(), items.getHeadValue());
}
}

@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
return ret;
}

@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
if (ContextManager.isActive()) {
ContextManager.activeSpan().errorOccurred().log(t);
}
}
}
Loading