-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Provide plugin for Solr-7.x(client) #2730
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
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
1031356
rollback and add module solrj-plugin
0ce3017
preparing to pr
9151a87
to resolve reviewers' suggestions
109d478
Merge branch 'master' into solrj-plugin
10d923c
remove unused code
c1beec4
Merge branch 'solrj-plugin' of https://github.com/dmsolr/skywalking i…
e6b4d32
remove unused code
6727464
Merge branch 'master' into solrj-plugin
wu-sheng 7bee1d6
remove unused comments
dea10a3
Merge branch 'master' into solrj-plugin
dmsolr a26b659
To avoid NPE
zteny cced8a7
fix typo
zteny bb2b5aa
Merge branch 'master' into solrj-plugin
wu-sheng 57799c8
Change to JRE6
zteny dc7f960
Merge branch 'solrj-plugin' of https://github.com/dmsolr/skywalking i…
zteny 4af0a8f
fix pom.xml merge incorrectly. and java.net.URL instead of Regex
zteny 99c31f8
Using RuntimeContext instead of ThreadLocal
zteny f809f8c
To reduce unnecessary tags
zteny aa68f82
add test cases
dmsolr 447da95
fix validation fail
dmsolr 831dfa3
Merge branch 'master' into solrj-plugin
wu-sheng 2404c78
add solrj-plugin into component-libraries & Supported-list
d3ce641
to trace all patch to avoid recheck status of span
65d7221
Merge branch 'master' into solrj-plugin
wu-sheng 57d76d3
remove unnecessary properites
549be85
Merge branch 'master' into solrj-plugin
wu-sheng a310a94
remove unnecessary tags
0da3a38
Merge branch 'master' into solrj-plugin
wu-sheng 74b1fc7
Add the config to document of setup
f24d46c
Merge branch 'master' into solrj-plugin
wu-sheng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
229 changes: 229 additions & 0 deletions
229
....x-plugin/src/main/java/org/apache/skywalking/apm/plugin/solrj/SolrClientInterceptor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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())) { | ||
|
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()); | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Above codes include a lot of |
||
| 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()) { | ||
|
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(); | ||
| } | ||
| } | ||
59 changes: 59 additions & 0 deletions
59
...plugin/src/main/java/org/apache/skywalking/apm/plugin/solrj/SolrConnectorInterceptor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.