GuanceCloud / dd-trace-java

Datadog APM client for Java
https://docs.datadoghq.com/tracing/languages/java
Apache License 2.0
9 stars 3 forks source link

查看 Redis 请求参数 #77

Open songlonqi-java opened 2 months ago

songlonqi-java commented 2 months ago

目前支持的: #19 #76

需要增加别的驱动支持例如:RedissonSpring Data Redis Redis Template

songlonqi-java commented 2 months ago

redisson比较特殊一点

Command 中获取参数的方法是 command.getParams() 方法返回的是Object[]。 这里要说下这个Object数组。

如果是命令,比如 set,get等,那么就是String类型。

如果是参数则是 byte[] 类型, 需要把ObjectByteBuf类型。这个ByteBuf有个特点,第0位是标识符00000011, 最后一位的最高位为1,例如:参数如果是 SET aaaaa ,那么参数列表为:Object["set",byte[3,a,a,a,a,a|0x7f]] , 最后一位打印二进制位:11100001 而 a的二进制却是 01100001。 所以在拿到ByteBuf类型 需要将第0位切掉,最后一位去掉高位的1。

部分java代码:

      for (Object val : args) {
        if (val instanceof ByteBuf) {
          ByteBuf buf = (ByteBuf) val;
          if (buf.hasArray()) {
            String data = new String(buf.array(), buf.arrayOffset() + buf.readerIndex(), buf.readableBytes(), StandardCharsets.UTF_8);
            sb.append(data).append(" ");
          } else {
            byte[] bytes = new byte[buf.readableBytes()];
            buf.getBytes(buf.readerIndex(), bytes);
            try{
              byte[] result = new byte[bytes.length - 1];
              System.arraycopy(bytes, 1, result, 0, bytes.length - 2);
              result[result.length - 1] = (byte) (bytes[bytes.length - 1] & 0x7F);
              String data = new String(result, StandardCharsets.UTF_8);
              sb.append(data).append(" ");
            }catch (Exception e){
              System.out.println(e);
            }
          }
        } else {
          sb.append(val.toString()).append(" ");
        }
      }