Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -124,6 +124,8 @@ public class ComponentsDefine {

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

public static final OfficialComponent SOLR = new OfficialComponent(64, "solr");

public static final OfficialComponent SPRING_ASYNC = new OfficialComponent(65, "SpringAsync");

private static ComponentsDefine INSTANCE = new ComponentsDefine();
Expand Down Expand Up @@ -184,6 +186,7 @@ public ComponentsDefine() {
addComponent(SPRING_CLOUD_GATEWAY);
addComponent(RESTEASY);
addComponent(SOLRJ);
addComponent(SOLR);
}

private void addComponent(OfficialComponent component) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,5 +226,18 @@ public static class SolrJ {
public static boolean TRACE_OPS_PARAMS = false;
}

public static class Solr {

/**
* If true, the Trace_ID will be put into MDC. Default is false.
*/
public static boolean PRINT_TRACE_ID_ON_LOG = false;

/**
* If true, not trace admin request, include /admin/ping, /admin/metrics etc. Default is true.
*/
public static boolean SKIP_ADMIN_REQUEST = true;
}

}
}
1 change: 1 addition & 0 deletions apm-sniffer/apm-sdk-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<module>vertx-plugins</module>
<module>resteasy-plugin</module>
<module>solrj-7.x-plugin</module>
<module>solr-7.x-plugin</module>
</modules>
<packaging>pom</packaging>

Expand Down
56 changes: 56 additions & 0 deletions apm-sniffer/apm-sdk-plugin/solr-7.x-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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-solr-7.x-plugin</artifactId>
<packaging>jar</packaging>

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

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

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

<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,64 @@
/*
* 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.solr;

import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatchers;
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.enhance.ClassInstanceMethodsEnhancePluginDefine;
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch;

public class RequestHandlerBaseInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {

@Override
protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return new ConstructorInterceptPoint[]{
};
}

@Override
protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[]{
new InstanceMethodsInterceptPoint() {
@Override
public boolean isOverrideArgs() {
return false;
}

@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return ElementMatchers.named("handleRequest");
}

@Override
public String getMethodsInterceptor() {
return "org.apache.skywalking.apm.plugin.solr.RequestHandlerBaseInterceptor";
}
}
};
}

@Override
protected ClassMatch enhanceClass() {
return NameMatch.byName("org.apache.solr.handler.RequestHandlerBase");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* 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.solr;

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.context.trace.AbstractSpan;
import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer;
import org.apache.skywalking.apm.agent.core.logging.api.ILog;
import org.apache.skywalking.apm.agent.core.logging.api.LogManager;
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.solr.common.params.ShardParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.update.processor.DistributingUpdateProcessorFactory;
import org.slf4j.MDC;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

import static org.apache.skywalking.apm.agent.core.conf.Config.Plugin.Solr.PRINT_TRACE_ID_ON_LOG;
import static org.apache.skywalking.apm.agent.core.conf.Config.Plugin.Solr.SKIP_ADMIN_REQUEST;
import static org.apache.skywalking.apm.plugin.solr.commons.Constants.*;

public class RequestHandlerBaseInterceptor implements InstanceMethodsAroundInterceptor {
private static final ILog LOG = LogManager.getLogger(RequestHandlerBaseInterceptor.class);

@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
final SolrQueryRequest request = (SolrQueryRequest) allArguments[0];
if (SKIP_ADMIN_REQUEST && request.getPath().startsWith(SKIP_ADMIN_PREF)) {
return;
}

HttpServletRequest req = request.getHttpSolrCall().getReq();
ContextCarrier carrier = new ContextCarrier();
CarrierItem items = carrier.items();
while (items.hasNext()) {
items = items.next();
items.setHeadValue(req.getHeader(items.getHeadKey()));
}

SolrParams params = request.getParams();
String operationPref = OPER_SOLR_PREF;

if ((params.getBool(ShardParams.IS_SHARD, false) && params.getInt(ShardParams.SHARDS_PURPOSE, 0) != 0)
|| params.get(DistributingUpdateProcessorFactory.DISTRIB_UPDATE_PARAM) != null) {
operationPref = OPER_SHARD_PREF;
}

createSpan(operationPref + req.getServletPath(), carrier);
if (PRINT_TRACE_ID_ON_LOG) {
MDC.put(MDC_KEY_TRACE_ID, TRACE_ID_PREF + ContextManager.getGlobalTraceId());
}
}

private final AbstractSpan createSpan(String operation, ContextCarrier carrier) {
AbstractSpan span = ContextManager.createEntrySpan(operation, carrier);
span.setComponent(ComponentsDefine.SOLR)
.setLayer(SpanLayer.HTTP);
return span;
}

@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
final SolrQueryRequest request = (SolrQueryRequest) allArguments[0];
if (SKIP_ADMIN_REQUEST && request.getPath().startsWith(SKIP_ADMIN_PREF)) {
return ret;
}

if (ContextManager.isActive()) {
SolrQueryResponse response = (SolrQueryResponse) allArguments[1];
Exception exp = response.getException();
if (exp != null) {
ContextManager.activeSpan().errorOccurred().log(exp);
}
ContextManager.stopSpan();
}
return ret;
}

@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
if (ContextManager.isActive()) {
ContextManager.activeSpan().errorOccurred().log(t);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.solr.commons;

public interface Constants {
String SW_ENHANCE_FLAG = "_sw_enhance_flag_";

String OPER_SHARD_PREF = "Solr/shard";

String OPER_SHARD_PREF_SLASH = "Solr/shard/";

String OPER_STAGE_PREF = "Solr/stage/";

String OPER_SOLR_PREF = "Solr";

String SKIP_ADMIN_PREF = "/admin/";

String TRACE_ID_PREF = "t:";

String MDC_KEY_CORE = "core";

String MDC_KEY_NODE_NAME = "node_name";

String MDC_KEY_TRACE_ID = "traceId";
}
Original file line number Diff line number Diff line change
@@ -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.solr.commons;

import org.apache.solr.handler.component.ShardRequest;

public class NamesMap {
private static final String[] PURPOSENAMES = {
"/PRIVATE",
"/GET_TERM_DFS",
"/GET_TOP_IDS",
"/REFINE_TOP_IDS",
"/GET_FACETS",
"/REFINE_FACETS",
"/GET_FIELDS",
"/GET_HIGHLIGHTS",
"/GET_DEBUG",
"/GET_STATS",
"/GET_TERMS",
"/GET_TOP_GROUPS",
"/GET_MLT_RESULTS",
"/REFINE_PIVOT_FACETS",
"/SET_TERM_STATS",
"/GET_TERM_STATS"
};

public static String getStagName(int stage) {
switch (stage) {
case 0:
return "START";
case 1000:
return "PARSE_QUERY";
case 1500:
return "TOP_GROUPS";
case 2000:
return "EXECUTE_QUERY";
case 3000:
return "GET_FIELDS";
case Integer.MAX_VALUE:
return "DONE";
default:
return "UNKNOWN";
}
}

public static String gePurposeName(int purpose) {
if (purpose < ShardRequest.PURPOSE_PRIVATE || purpose > ShardRequest.PURPOSE_GET_TERM_STATS)
return "/PENDING";
return PURPOSENAMES[Integer.numberOfTrailingZeros(purpose)];
}

}
Loading