Support configuration to collect redis parameters for jedis plugin (#91)

Co-authored-by: 温嘉鸣 <wenjiaming@shouqianba.com>
This commit is contained in:
ArjenDavid-sjtu 2022-01-14 00:50:40 +08:00 committed by GitHub
parent 634cf279a9
commit 8a1dae348c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 121 additions and 7 deletions

View File

@ -21,6 +21,7 @@ Release Notes.
* Compatible with the versions after dubbo-2.7.14
* Follow protocol grammar fix `GCPhrase -> GCPhase`.
* Support ZGC GC time and count metric collect. (Require 9.0.0 OAP)
* Support configuration for collecting redis parameters for jedis-2.x and redisson-3.x plugin.
#### Documentation

View File

@ -28,9 +28,13 @@ import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
import org.apache.skywalking.apm.agent.core.context.ContextManager;
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.util.StringUtil;
public class JedisMethodInterceptor implements InstanceMethodsAroundInterceptor {
private static final String ABBR = "...";
private static final String DELIMITER_SPACE = " ";
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
@ -41,12 +45,24 @@ public class JedisMethodInterceptor implements InstanceMethodsAroundInterceptor
SpanLayer.asCache(span);
if (allArguments.length > 0 && allArguments[0] instanceof String) {
Tags.DB_STATEMENT.set(span, method.getName() + " " + allArguments[0]);
Tags.DB_STATEMENT.set(span, getDBStatement(method.getName(), (String) allArguments[0]));
} else if (allArguments.length > 0 && allArguments[0] instanceof byte[]) {
Tags.DB_STATEMENT.set(span, method.getName());
}
}
private String getDBStatement(String methodName, String argument) {
StringBuilder dbStatement = new StringBuilder(methodName);
if (JedisPluginConfig.Plugin.Jedis.TRACE_REDIS_PARAMETERS && !StringUtil.isEmpty(argument)) {
dbStatement.append(DELIMITER_SPACE);
if (argument.length() > JedisPluginConfig.Plugin.Jedis.REDIS_PARAMETER_MAX_LENGTH) {
argument = argument.substring(0, JedisPluginConfig.Plugin.Jedis.REDIS_PARAMETER_MAX_LENGTH) + ABBR;
}
dbStatement.append(argument);
}
return dbStatement.toString();
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {

View File

@ -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.jedis.v2;
import org.apache.skywalking.apm.agent.core.boot.PluginConfig;
public class JedisPluginConfig {
public static class Plugin {
@PluginConfig(root = JedisPluginConfig.class)
public static class Jedis {
/**
* If set to true, the parameters of the Redis command would be collected.
*/
public static boolean TRACE_REDIS_PARAMETERS = false;
/**
* For the sake of performance, SkyWalking won't save Redis parameter string into the tag.
* If TRACE_REDIS_PARAMETERS is set to true, the first {@code REDIS_PARAMETER_MAX_LENGTH} parameter
* characters would be collected.
* <p>
* Set a negative number to save specified length of parameter string to the tag.
*/
public static int REDIS_PARAMETER_MAX_LENGTH = 128;
}
}
}

View File

@ -69,6 +69,8 @@ public class JedisMethodInterceptorTest {
@Before
public void setUp() throws Exception {
JedisPluginConfig.Plugin.Jedis.TRACE_REDIS_PARAMETERS = true;
allArgument = new Object[] {
"OperationKey",
"OperationValue"

View File

@ -44,6 +44,10 @@ public class RedisConnectionMethodInterceptor implements InstanceMethodsAroundIn
private static final ILog LOGGER = LogManager.getLogger(RedisConnectionMethodInterceptor.class);
private static final String ABBR = "...";
private static final String QUESTION_MARK = "?";
private static final String DELIMITER_SPACE = " ";
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
@ -81,9 +85,14 @@ public class RedisConnectionMethodInterceptor implements InstanceMethodsAroundIn
private void addCommandData(StringBuilder dbStatement, CommandData commandData) {
dbStatement.append(commandData.getCommand().getName());
if (commandData.getParams() != null) {
if (RedissonPluginConfig.Plugin.Redisson.TRACE_REDIS_PARAMETERS && commandData.getParams() != null) {
for (Object param : commandData.getParams()) {
dbStatement.append(" ").append(param instanceof ByteBuf ? "?" : String.valueOf(param.toString()));
dbStatement.append(DELIMITER_SPACE);
String paramStr = param instanceof ByteBuf ? QUESTION_MARK : String.valueOf(param.toString());
if (paramStr.length() > RedissonPluginConfig.Plugin.Redisson.REDIS_PARAMETER_MAX_LENGTH) {
paramStr = paramStr.substring(0, RedissonPluginConfig.Plugin.Redisson.REDIS_PARAMETER_MAX_LENGTH) + ABBR;
}
dbStatement.append(paramStr);
}
}
}

View File

@ -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.redisson.v3;
import org.apache.skywalking.apm.agent.core.boot.PluginConfig;
public class RedissonPluginConfig {
public static class Plugin {
@PluginConfig(root = RedissonPluginConfig.class)
public static class Redisson {
/**
* If set to true, the parameters of the Redis command would be collected.
*/
public static boolean TRACE_REDIS_PARAMETERS = false;
/**
* For the sake of performance, SkyWalking won't save Redis parameter string into the tag.
* If TRACE_REDIS_PARAMETERS is set to true, the first {@code REDIS_PARAMETER_MAX_LENGTH} parameter
* characters would be collected.
* <p>
* Set a negative number to save specified length of parameter string to the tag.
*/
public static int REDIS_PARAMETER_MAX_LENGTH = 128;
}
}
}

View File

@ -102,10 +102,15 @@ property key | Description | **System Environment Variable** | Default
`plugin.springannotation.classname_match_regex` | Match spring beans with regular expression for the class name. Multiple expressions could be separated by a comma. This only works when `Spring annotation plugin` has been activated. | SW_SPRINGANNOTATION_CLASSNAME_MATCH_REGEX | `All the spring beans tagged with @Bean,@Service,@Dao, or @Repository.`
`plugin.toolkit.log.transmit_formatted` | Whether or not to transmit logged data as formatted or un-formatted. | SW_PLUGIN_TOOLKIT_LOG_TRANSMIT_FORMATTED | `true`
`plugin.lettuce.trace_redis_parameters` | If set to true, the parameters of Redis commands would be collected by Lettuce agent.| SW_PLUGIN_LETTUCE_TRACE_REDIS_PARAMETERS | `false`
`plugin.lettuce.redis_parameter_max_length` | If set to positive number and `plugin.lettuce.trace_redis_parameters` is set to `true`, Redis command parameters would be collected and truncated to this length.| SW_PLUGIN_LETTUCE_REDIS_PARAMETER_MAX_LENGTH | `128`
`plugin.lettuce.redis_parameter_max_length` | If set to positive number and `plugin.lettuce.trace_redis_parameters` is set to `true`, Redis command parameters would be collected and truncated to this length.| SW_PLUGIN_LETTUCE_REDIS_PARAMETER_MAX_LENGTH | `128`
`plugin.jedis.trace_redis_parameters` | If set to true, the parameters of Redis commands would be collected by Jedis agent.| SW_PLUGIN_JEDIS_TRACE_REDIS_PARAMETERS | `false`
`plugin.jedis.redis_parameter_max_length` | If set to positive number and `plugin.jedis.trace_redis_parameters` is set to `true`, Redis command parameters would be collected and truncated to this length.| SW_PLUGIN_JEDIS_REDIS_PARAMETER_MAX_LENGTH | `128`
`plugin.redisson.trace_redis_parameters` | If set to true, the parameters of Redis commands would be collected by Redisson agent.| SW_PLUGIN_REDISSON_TRACE_REDIS_PARAMETERS | `false`
`plugin.redisson.redis_parameter_max_length` | If set to positive number and `plugin.redisson.trace_redis_parameters` is set to `true`, Redis command parameters would be collected and truncated to this length.| SW_PLUGIN_REDISSON_REDIS_PARAMETER_MAX_LENGTH | `128`
`plugin.neo4j.trace_cypher_parameters`|If set to true, the parameters of the cypher would be collected.|SW_PLUGIN_NEO4J_TRACE_CYPHER_PARAMETERS|`false`
`plugin.neo4j.cypher_parameters_max_length`|If set to positive number, the `db.cypher.parameters` would be truncated to this length, otherwise it would be completely saved, which may cause performance problem.|SW_PLUGIN_NEO4J_CYPHER_PARAMETERS_MAX_LENGTH|`512`
`plugin.neo4j.cypher_body_max_length`|If set to positive number, the `db.statement` would be truncated to this length, otherwise it would be completely saved, which may cause performance problem.|SW_PLUGIN_NEO4J_CYPHER_BODY_MAX_LENGTH|`2048`
# Dynamic Configurations
All configurations above are static, if you need to change some agent settings at runtime, please read [CDS - Configuration Discovery Service document](configuration-discovery.md) for more details.

View File

@ -18,5 +18,4 @@
home="$(cd "$(dirname $0)"; pwd)"
java -jar ${agent_opts} -Dredis.host=${REDIS_HOST} -Dredis.port=${REDIS_PORT} \
${home}/../libs/jedis-scenario.jar &
java -jar ${agent_opts} -Dredis.host=${REDIS_HOST} -Dredis.port=${REDIS_PORT} -Dskywalking.plugin.jedis.trace_redis_parameters=true ${home}/../libs/jedis-scenario.jar &

View File

@ -18,4 +18,4 @@
home="$(cd "$(dirname $0)"; pwd)"
java -Dredis.servers=${REDIS_SERVERS} -jar ${agent_opts} ${home}/../libs/redisson-scenario.jar &
java -Dredis.servers=${REDIS_SERVERS} -Dskywalking.plugin.redisson.trace_redis_parameters=true -jar ${agent_opts} ${home}/../libs/redisson-scenario.jar &