Fix Redisson latestDep tests (#1507)

* Fix Redisson latestDep tests

* spotless

* Add comment
This commit is contained in:
Mateusz Rzeszutek 2020-10-28 16:18:13 +01:00 committed by GitHub
parent 58c78ec7d0
commit 9bb13bd8f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 12 deletions

View File

@ -5,13 +5,13 @@
package io.opentelemetry.javaagent.instrumentation.redisson;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.opentelemetry.instrumentation.api.tracer.DatabaseClientTracer;
import io.opentelemetry.javaagent.instrumentation.api.db.DbSystem;
import io.opentelemetry.javaagent.instrumentation.api.db.RedisCommandNormalizer;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.redisson.client.RedisConnection;
import org.redisson.client.protocol.CommandData;
@ -71,7 +71,21 @@ public class RedissonClientTracer extends DatabaseClientTracer<RedisConnection,
if (command.getCommand().getSubName() != null) {
args.add(command.getCommand().getSubName());
}
args.addAll(Arrays.asList(commandParams));
for (Object param : commandParams) {
if (param instanceof ByteBuf) {
try {
// slice() does not copy the actual byte buffer, it only returns a readable/writable
// "view" of the original buffer (i.e. read and write marks are not shared)
ByteBuf buf = ((ByteBuf) param).slice();
// state can be null here: no Decoders used by Codecs use it
args.add(command.getCodec().getValueDecoder().decode(buf, null));
} catch (Exception ignored) {
args.add("?");
}
} else {
args.add(param);
}
}
return RedisCommandNormalizer.normalize(command.getCommand().getName(), args);
}

View File

@ -4,6 +4,8 @@
*/
import static io.opentelemetry.trace.Span.Kind.CLIENT
import static java.util.regex.Pattern.compile
import static java.util.regex.Pattern.quote
import io.opentelemetry.instrumentation.test.AgentTestRunner
import io.opentelemetry.instrumentation.test.utils.PortUtils
@ -276,10 +278,9 @@ class RedissonClientTest extends AgentTestRunner {
assertTraces(2) {
trace(0, 1) {
span(0) {
def lockScript = "if (redis.call('exists', KEYS[1]) == 0) then redis.call('hset', KEYS[1], ARGV[2], 1);" +
" redis.call('pexpire', KEYS[1], ARGV[1]); return nil; end;" +
" if (redis.call('hexists', KEYS[1], ARGV[2]) == 1) then redis.call('hincrby', KEYS[1], ARGV[2], 1);" +
" redis.call('pexpire', KEYS[1], ARGV[1]); return nil; end; return redis.call('pttl', KEYS[1]);"
// Use .* to match the actual script, since it changes between redisson versions
// everything that does not change is quoted so that it's matched literally
def lockScriptPattern = compile("^" + quote("EVAL ") + ".*" + quote(" 1 lock ? ?") + "\$")
name "EVAL"
kind CLIENT
@ -289,16 +290,13 @@ class RedissonClientTest extends AgentTestRunner {
"$SemanticAttributes.NET_PEER_NAME.key" "localhost"
"$SemanticAttributes.DB_CONNECTION_STRING.key" "localhost:$port"
"$SemanticAttributes.NET_PEER_PORT.key" port
"$SemanticAttributes.DB_STATEMENT.key" "EVAL $lockScript 1 lock ? ?"
"$SemanticAttributes.DB_STATEMENT.key" { lockScriptPattern.matcher(it).matches() }
}
}
}
trace(1, 1) {
span(0) {
def unlockScript = "if (redis.call('exists', KEYS[1]) == 0) then redis.call('publish', KEYS[2], ARGV[1]);" +
" return 1; end;if (redis.call('hexists', KEYS[1], ARGV[3]) == 0) then return nil;end;" +
" local counter = redis.call('hincrby', KEYS[1], ARGV[3], -1); if (counter > 0) then redis.call('pexpire', KEYS[1], ARGV[2]);" +
" return 0; else redis.call('del', KEYS[1]); redis.call('publish', KEYS[2], ARGV[1]); return 1; end; return nil;"
def lockScriptPattern = compile("^" + quote("EVAL ") + ".*" + quote(" 2 lock ") + "\\S+" + quote(" ? ? ?") + "\$")
name "EVAL"
kind CLIENT
@ -308,7 +306,7 @@ class RedissonClientTest extends AgentTestRunner {
"$SemanticAttributes.NET_PEER_NAME.key" "localhost"
"$SemanticAttributes.DB_CONNECTION_STRING.key" "localhost:$port"
"$SemanticAttributes.NET_PEER_PORT.key" port
"$SemanticAttributes.DB_STATEMENT.key" "EVAL $unlockScript 2 lock redisson_lock__channel__{lock} ? ? ?"
"$SemanticAttributes.DB_STATEMENT.key" { lockScriptPattern.matcher(it).matches() }
}
}
}