optimize lettuce plugin (#17)

This commit is contained in:
Darcy 2021-09-02 20:38:55 +08:00 committed by GitHub
parent 2b51932055
commit c872adc20c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 474 additions and 751 deletions

View File

@ -14,7 +14,8 @@ Release Notes.
* Fix kafka-reporter-plugin shade package conflict
* Add all config items to `agent.conf` file for convenient containerization use cases.
* Advanced Kafka Producer configuration enhancement.
* Suport mTLS for gRPC channel.
* Support mTLS for gRPC channel.
* fix the bug that plugin record wrong time elapse for lettuce plugin
#### Documentation

View File

@ -1,66 +0,0 @@
/*
* 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.lettuce.v5;
import io.lettuce.core.protocol.AsyncCommand;
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.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
import java.lang.reflect.Method;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
public class AsyncCommandMethodInterceptor implements InstanceMethodsAroundInterceptor {
@Override
@SuppressWarnings("unchecked")
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
AsyncCommand asyncCommand = (AsyncCommand) objInst;
String operationName = "Lettuce/" + asyncCommand.getType().name();
AbstractSpan span = ContextManager.createLocalSpan(operationName + "/onComplete");
span.setComponent(ComponentsDefine.LETTUCE);
Tags.DB_TYPE.set(span, "Redis");
SpanLayer.asCache(span);
if (allArguments[0] instanceof Consumer) {
allArguments[0] = new SWConsumer((Consumer) allArguments[0], ContextManager.capture(), operationName);
} else {
allArguments[0] = new SWBiConsumer((BiConsumer) allArguments[0], ContextManager.capture(), operationName);
}
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
ContextManager.stopSpan();
return ret;
}
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
ContextManager.activeSpan().log(t);
}
}

View File

@ -1,33 +0,0 @@
/*
* 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.lettuce.v5;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor;
/**
* ClientOptions is the link between RedisChannelWriter and AbstractRedisClient. to enhance ClientOptions for bring
* peer(the cluster configuration information) in AbstractRedisClient to RedisChannelWriter.
*/
public class ClientOptionsConstructorInterceptor implements InstanceConstructorInterceptor {
@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
}
}

View File

@ -0,0 +1,56 @@
/*
* 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.lettuce.v5;
import io.netty.channel.Channel;
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;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
public class DefaultEndpointChannelActiveInterceptor implements InstanceMethodsAroundInterceptor {
public static final String IP_AND_PORT_DELIMITER = ":";
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
Channel channel = (Channel) allArguments[0];
if (channel == null) {
return;
}
SocketAddress socketAddress = channel.remoteAddress();
if (socketAddress instanceof InetSocketAddress) {
InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
String peer = inetSocketAddress.getHostString() + IP_AND_PORT_DELIMITER + inetSocketAddress.getPort();
objInst.setSkyWalkingDynamicField(peer);
}
}
@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) {
}
}

View File

@ -18,25 +18,26 @@
package org.apache.skywalking.apm.plugin.lettuce.v5;
import java.lang.reflect.Method;
import java.util.Collection;
import io.lettuce.core.protocol.CommandArgs;
import io.lettuce.core.protocol.DecoratedCommand;
import io.lettuce.core.protocol.RedisCommand;
import org.apache.skywalking.apm.agent.core.conf.Constants;
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.util.StringUtil;
import io.lettuce.core.protocol.CommandArgs;
import io.lettuce.core.protocol.RedisCommand;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
public class RedisChannelWriterInterceptor implements InstanceMethodsAroundInterceptor, InstanceConstructorInterceptor {
@SuppressWarnings("unchecked")
public class RedisChannelWriterInterceptor implements InstanceMethodsAroundInterceptor {
private static final String PASSWORD_MASK = "******";
private static final String ABBR = "...";
@ -44,15 +45,28 @@ public class RedisChannelWriterInterceptor implements InstanceMethodsAroundInter
private static final String AUTH = "AUTH";
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) {
String peer = (String) objInst.getSkyWalkingDynamicField();
RedisCommand<?, ?, ?> spanCarrierCommand = getSpanCarrierCommand(allArguments[0]);
if (spanCarrierCommand == null) {
return;
}
EnhancedInstance enhancedCommand = (EnhancedInstance) spanCarrierCommand;
// command has been handle by another channel writer (cluster or sentinel case)
if (enhancedCommand.getSkyWalkingDynamicField() != null) {
//set peer in last channel writer (delegate)
if (peer != null) {
AbstractSpan span = (AbstractSpan) enhancedCommand.getSkyWalkingDynamicField();
span.setPeer(peer);
}
return;
}
StringBuilder dbStatement = new StringBuilder();
String operationName = "Lettuce/";
if (allArguments[0] instanceof RedisCommand) {
RedisCommand redisCommand = (RedisCommand) allArguments[0];
RedisCommand<?, ?, ?> redisCommand = (RedisCommand<?, ?, ?>) allArguments[0];
String command = redisCommand.getType().name();
operationName = operationName + command;
dbStatement.append(command);
@ -60,26 +74,28 @@ public class RedisChannelWriterInterceptor implements InstanceMethodsAroundInter
dbStatement.append(DELIMITER_SPACE).append(getArgsStatement(redisCommand));
}
} else if (allArguments[0] instanceof Collection) {
@SuppressWarnings("unchecked") Collection<RedisCommand> redisCommands = (Collection<RedisCommand>) allArguments[0];
Collection<RedisCommand<?, ?, ?>> redisCommands = (Collection<RedisCommand<?, ?, ?>>) allArguments[0];
operationName = operationName + "BATCH_WRITE";
for (RedisCommand redisCommand : redisCommands) {
for (RedisCommand<?, ?, ?> redisCommand : redisCommands) {
dbStatement.append(redisCommand.getType().name()).append(";");
}
}
AbstractSpan span = ContextManager.createExitSpan(operationName, peer);
span.setComponent(ComponentsDefine.LETTUCE);
Tags.DB_TYPE.set(span, "Redis");
Tags.DB_STATEMENT.set(span, dbStatement.toString());
SpanLayer.asCache(span);
span.prepareForAsync();
ContextManager.stopSpan();
enhancedCommand.setSkyWalkingDynamicField(span);
}
private String getArgsStatement(RedisCommand redisCommand) {
private String getArgsStatement(RedisCommand<?, ?, ?> redisCommand) {
String statement;
if (AUTH.equalsIgnoreCase(redisCommand.getType().name())) {
statement = PASSWORD_MASK;
} else {
CommandArgs args = redisCommand.getArgs();
CommandArgs<?, ?> args = redisCommand.getArgs();
statement = (args != null) ? args.toCommandString() : Constants.EMPTY_STRING;
}
if (StringUtil.isNotEmpty(statement) && statement.length() > LettucePluginConfig.Plugin.Lettuce.REDIS_PARAMETER_MAX_LENGTH) {
@ -89,22 +105,43 @@ public class RedisChannelWriterInterceptor implements InstanceMethodsAroundInter
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
ContextManager.stopSpan();
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) {
return ret;
}
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
AbstractSpan span = ContextManager.activeSpan();
span.log(t);
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
RedisCommand<?, ?, ?> redisCommand = getSpanCarrierCommand(allArguments[0]);
if (redisCommand instanceof EnhancedInstance && ((EnhancedInstance) redisCommand).getSkyWalkingDynamicField() != null) {
EnhancedInstance enhancedRedisCommand = (EnhancedInstance) redisCommand;
AbstractSpan abstractSpan = (AbstractSpan) enhancedRedisCommand.getSkyWalkingDynamicField();
enhancedRedisCommand.setSkyWalkingDynamicField(null);
abstractSpan.log(t);
abstractSpan.asyncFinish();
}
}
@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
EnhancedInstance optionsInst = (EnhancedInstance) allArguments[0];
objInst.setSkyWalkingDynamicField(optionsInst.getSkyWalkingDynamicField());
private static RedisCommand<?, ?, ?> getSpanCarrierCommand(Object o) {
RedisCommand<?, ?, ?> command = null;
if (o instanceof RedisCommand) {
command = (RedisCommand<?, ?, ?>) o;
} else if (o instanceof List) {
List<?> list = (List<?>) o;
command = list.isEmpty() ? null : (RedisCommand<?, ?, ?>) list.get(list.size() - 1);
} else if (o instanceof Collection) {
Collection<RedisCommand<?, ?, ?>> redisCommands = (Collection<RedisCommand<?, ?, ?>>) o;
RedisCommand<?, ?, ?> last = null;
for (RedisCommand<?, ?, ?> redisCommand : redisCommands) {
last = redisCommand;
}
command = last;
}
if (command instanceof DecoratedCommand) {
while (command instanceof DecoratedCommand) {
DecoratedCommand<?, ?, ?> wrapper = (DecoratedCommand<?, ?, ?>) command;
command = wrapper.getDelegate();
}
}
return command;
}
}

View File

@ -1,46 +0,0 @@
/*
* 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.lettuce.v5;
import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
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.util.StringUtil;
import java.util.stream.Collectors;
public class RedisClientConstructorInterceptor implements InstanceConstructorInterceptor {
@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
RedisURI redisURI = (RedisURI) allArguments[1];
RedisClient redisClient = (RedisClient) objInst;
EnhancedInstance optionsInst = (EnhancedInstance) redisClient.getOptions();
StringBuilder redisPeer = new StringBuilder();
if (StringUtil.isNotBlank(redisURI.getSentinelMasterId())) {
redisPeer.append(redisURI.getSentinelMasterId()).append("[").append(
redisURI.getSentinels().stream().map(r -> r.getHost() + ":" + r.getPort())
.collect(Collectors.joining(","))).append("]");
} else {
redisPeer.append(redisURI.getHost()).append(":").append(redisURI.getPort());
}
optionsInst.setSkyWalkingDynamicField(redisPeer.toString());
}
}

View File

@ -1,40 +0,0 @@
/*
* 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.lettuce.v5;
import io.lettuce.core.RedisURI;
import io.lettuce.core.cluster.RedisClusterClient;
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.context.util.PeerFormat;
public class RedisClusterClientConstructorInterceptor implements InstanceConstructorInterceptor {
@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
@SuppressWarnings("unchecked") Iterable<RedisURI> redisURIs = (Iterable<RedisURI>) allArguments[1];
RedisClusterClient redisClusterClient = (RedisClusterClient) objInst;
StringBuilder peer = new StringBuilder();
for (RedisURI redisURI : redisURIs) {
peer.append(redisURI.getHost()).append(":").append(redisURI.getPort()).append(";");
}
EnhancedInstance optionsInst = (EnhancedInstance) redisClusterClient.getOptions();
optionsInst.setSkyWalkingDynamicField(PeerFormat.shorten(peer.toString()));
}
}

View File

@ -0,0 +1,56 @@
/*
* 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.lettuce.v5;
import org.apache.skywalking.apm.agent.core.context.tag.StringTag;
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
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 RedisCommandCancelMethodInterceptor implements InstanceMethodsAroundInterceptor {
private static final String CANCEL_SIGNAL_TAG = "signalType";
private static final String COMMAND_CANCEL_VALUE = "cancel";
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) {
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) {
if (objInst.getSkyWalkingDynamicField() != null) {
AbstractSpan span = (AbstractSpan) objInst.getSkyWalkingDynamicField();
span.errorOccurred();
span.tag(new StringTag(CANCEL_SIGNAL_TAG), COMMAND_CANCEL_VALUE);
span.asyncFinish();
objInst.setSkyWalkingDynamicField(null);
}
return ret;
}
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
if (objInst.getSkyWalkingDynamicField() != null) {
AbstractSpan span = (AbstractSpan) objInst.getSkyWalkingDynamicField();
span.log(t);
}
}
}

View File

@ -0,0 +1,53 @@
/*
* 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.lettuce.v5;
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
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 RedisCommandCompleteExceptionallyMethodInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) {
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) {
if (objInst.getSkyWalkingDynamicField() != null) {
Throwable t = (Throwable) allArguments[0];
AbstractSpan span = (AbstractSpan) objInst.getSkyWalkingDynamicField();
span.log(t);
span.asyncFinish();
objInst.setSkyWalkingDynamicField(null);
}
return ret;
}
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
if (objInst.getSkyWalkingDynamicField() != null) {
AbstractSpan span = (AbstractSpan) objInst.getSkyWalkingDynamicField();
span.log(t);
}
}
}

View File

@ -18,37 +18,35 @@
package org.apache.skywalking.apm.plugin.lettuce.v5;
import io.lettuce.core.AbstractRedisClient;
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
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 AbstractRedisClientInterceptor implements InstanceMethodsAroundInterceptor {
public class RedisCommandCompleteMethodInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
EnhancedInstance clientOptions = (EnhancedInstance) allArguments[0];
if (clientOptions == null) {
return;
}
AbstractRedisClient client = (AbstractRedisClient) objInst;
if (client.getOptions() == null || ((EnhancedInstance) client.getOptions()).getSkyWalkingDynamicField() == null) {
return;
}
clientOptions.setSkyWalkingDynamicField(((EnhancedInstance) client.getOptions()).getSkyWalkingDynamicField());
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) {
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) {
if (objInst.getSkyWalkingDynamicField() != null) {
AbstractSpan span = (AbstractSpan) objInst.getSkyWalkingDynamicField();
span.asyncFinish();
objInst.setSkyWalkingDynamicField(null);
}
return ret;
}
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
Class<?>[] argumentsTypes, Throwable t) {
if (objInst.getSkyWalkingDynamicField() != null) {
AbstractSpan span = (AbstractSpan) objInst.getSkyWalkingDynamicField();
span.log(t);
}
}
}

View File

@ -1,57 +0,0 @@
/*
* 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.lettuce.v5;
import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.context.ContextSnapshot;
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.network.trace.component.ComponentsDefine;
import java.util.function.BiConsumer;
public class SWBiConsumer<T, U> implements BiConsumer<T, U> {
private BiConsumer<T, U> biConsumer;
private ContextSnapshot snapshot;
private String operationName;
SWBiConsumer(BiConsumer<T, U> biConsumer, ContextSnapshot snapshot, String operationName) {
this.biConsumer = biConsumer;
this.snapshot = snapshot;
this.operationName = operationName;
}
@Override
public void accept(T t, U u) {
AbstractSpan span = ContextManager.createLocalSpan(operationName + "/accept");
span.setComponent(ComponentsDefine.LETTUCE);
Tags.DB_TYPE.set(span, "Redis");
SpanLayer.asCache(span);
try {
ContextManager.continued(snapshot);
biConsumer.accept(t, u);
} catch (Throwable th) {
ContextManager.activeSpan().log(th);
} finally {
ContextManager.stopSpan();
}
}
}

View File

@ -1,57 +0,0 @@
/*
* 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.lettuce.v5;
import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.context.ContextSnapshot;
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.network.trace.component.ComponentsDefine;
import java.util.function.Consumer;
public class SWConsumer<T> implements Consumer<T> {
private Consumer<T> consumer;
private ContextSnapshot snapshot;
private String operationName;
SWConsumer(Consumer<T> consumer, ContextSnapshot snapshot, String operationName) {
this.consumer = consumer;
this.snapshot = snapshot;
this.operationName = operationName;
}
@Override
public void accept(T t) {
AbstractSpan span = ContextManager.createLocalSpan(operationName + "/accept");
span.setComponent(ComponentsDefine.LETTUCE);
Tags.DB_TYPE.set(span, "Redis");
SpanLayer.asCache(span);
try {
ContextManager.continued(snapshot);
consumer.accept(t);
} catch (Throwable th) {
ContextManager.activeSpan().log(th);
} finally {
ContextManager.stopSpan();
}
}
}

View File

@ -1,63 +0,0 @@
/*
* 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.lettuce.v5.define;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
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 static net.bytebuddy.matcher.ElementMatchers.any;
import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName;
public class ClientOptionsInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
private static final String ENHANCE_CLASS = "io.lettuce.core.ClientOptions";
private static final String CLIENT_OPTIONS_CONSTRUCTOR_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.lettuce.v5.ClientOptionsConstructorInterceptor";
@Override
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return new ConstructorInterceptPoint[] {
new ConstructorInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getConstructorMatcher() {
return any();
}
@Override
public String getConstructorInterceptor() {
return CLIENT_OPTIONS_CONSTRUCTOR_INTERCEPTOR_CLASS;
}
}
};
}
@Override
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[0];
}
@Override
public ClassMatch enhanceClass() {
return byName(ENHANCE_CLASS);
}
}

View File

@ -26,14 +26,14 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInst
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static org.apache.skywalking.apm.agent.core.plugin.bytebuddy.ArgumentTypeNameMatch.takesArgumentWithType;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName;
public class AbstractRedisClientInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
public class DefaultEndpointInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
private static final String ENHANCE_CLASS = "io.lettuce.core.AbstractRedisClient";
private static final String ENHANCE_CLASS = "io.lettuce.core.protocol.DefaultEndpoint";
private static final String ABSTRACT_REDIS_CLIENT_CONSTRUCTOR_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.lettuce.v5.AbstractRedisClientInterceptor";
private static final String DEFAULT_ENDPOINT_CHANNEL_ACTIVE_INTERCEPTOR = "org.apache.skywalking.apm.plugin.lettuce.v5.DefaultEndpointChannelActiveInterceptor";
@Override
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
@ -46,12 +46,12 @@ public class AbstractRedisClientInstrumentation extends ClassInstanceMethodsEnha
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named("setOptions").and(takesArgumentWithType(0, "io.lettuce.core.ClientOptions"));
return named("notifyChannelActive").and(takesArgument(0, named("io.netty.channel.Channel")));
}
@Override
public String getMethodsInterceptor() {
return ABSTRACT_REDIS_CLIENT_CONSTRUCTOR_INTERCEPTOR_CLASS;
return DEFAULT_ENDPOINT_CHANNEL_ACTIVE_INTERCEPTOR;
}
@Override

View File

@ -25,43 +25,34 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsIn
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
import java.util.List;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static org.apache.skywalking.apm.agent.core.plugin.bytebuddy.ArgumentTypeNameMatch.takesArgumentWithType;
import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import static org.apache.skywalking.apm.agent.core.plugin.match.HierarchyMatch.byHierarchyMatch;
/**
* The writeAndFlush method is used in versions lower than 5.0.2.RELEASE
*/
public class RedisChannelWriterInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
private static final String ENHANCE_CLASS = "io.lettuce.core.protocol.DefaultEndpoint";
private static final String ENHANCE_CLASS = "io.lettuce.core.RedisChannelWriter";
private static final String REDIS_CHANNEL_WRITER_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.lettuce.v5.RedisChannelWriterInterceptor";
@Override
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return new ConstructorInterceptPoint[] {
new ConstructorInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getConstructorMatcher() {
return takesArgumentWithType(0, "io.lettuce.core.ClientOptions");
}
@Override
public String getConstructorInterceptor() {
return REDIS_CHANNEL_WRITER_INTERCEPTOR_CLASS;
}
}
};
return new ConstructorInterceptPoint[0];
}
@Override
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[] {
return new InstanceMethodsInterceptPoint[]{
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named("writeToChannelAndFlush").or(named("writeAndFlush"));
return named("write").and(takesArgument(0, named("io.lettuce.core.protocol.RedisCommand")).or(
takesArgument(0, List.class)));
}
@Override
@ -73,12 +64,12 @@ public class RedisChannelWriterInstrumentation extends ClassInstanceMethodsEnhan
public boolean isOverrideArgs() {
return false;
}
}
},
};
}
@Override
public ClassMatch enhanceClass() {
return byName(ENHANCE_CLASS);
return byHierarchyMatch(ENHANCE_CLASS);
}
}

View File

@ -1,63 +0,0 @@
/*
* 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.lettuce.v5.define;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
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 static org.apache.skywalking.apm.agent.core.plugin.bytebuddy.ArgumentTypeNameMatch.takesArgumentWithType;
import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName;
public class RedisClientInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
private static final String ENHANCE_CLASS = "io.lettuce.core.RedisClient";
private static final String REDIS_CLIENT_CONSTRUCTOR_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.lettuce.v5.RedisClientConstructorInterceptor";
@Override
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return new ConstructorInterceptPoint[] {
new ConstructorInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getConstructorMatcher() {
return takesArgumentWithType(1, "io.lettuce.core.RedisURI");
}
@Override
public String getConstructorInterceptor() {
return REDIS_CLIENT_CONSTRUCTOR_INTERCEPTOR_CLASS;
}
}
};
}
@Override
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[0];
}
@Override
public ClassMatch enhanceClass() {
return byName(ENHANCE_CLASS);
}
}

View File

@ -1,63 +0,0 @@
/*
* 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.lettuce.v5.define;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
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 static org.apache.skywalking.apm.agent.core.plugin.bytebuddy.ArgumentTypeNameMatch.takesArgumentWithType;
import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName;
public class RedisClusterClientInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
private static final String ENHANCE_CLASS = "io.lettuce.core.cluster.RedisClusterClient";
private static final String REDIS_CLUSTER_CLIENT_CONSTRUCTOR_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.lettuce.v5.RedisClusterClientConstructorInterceptor";
@Override
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return new ConstructorInterceptPoint[] {
new ConstructorInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getConstructorMatcher() {
return takesArgumentWithType(1, "java.lang.Iterable");
}
@Override
public String getConstructorInterceptor() {
return REDIS_CLUSTER_CLIENT_CONSTRUCTOR_INTERCEPTOR_CLASS;
}
}
};
}
@Override
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[0];
}
@Override
public ClassMatch enhanceClass() {
return byName(ENHANCE_CLASS);
}
}

View File

@ -26,14 +26,17 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInst
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static org.apache.skywalking.apm.agent.core.plugin.bytebuddy.ArgumentTypeNameMatch.takesArgumentWithType;
import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
import static org.apache.skywalking.apm.agent.core.plugin.match.HierarchyMatch.byHierarchyMatch;
public class AsyncCommandInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
public class RedisCommandInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
private static final String ENHANCE_CLASS = "io.lettuce.core.protocol.AsyncCommand";
private static final String ENHANCE_CLASS = "io.lettuce.core.protocol.RedisCommand";
private static final String ASYNC_COMMAND_METHOD_INTERCEPTOR = "org.apache.skywalking.apm.plugin.lettuce.v5.AsyncCommandMethodInterceptor";
private static final String REDIS_COMMAND_COMPLETE_METHOD_INTERCEPTOR = "org.apache.skywalking.apm.plugin.lettuce.v5.RedisCommandCompleteMethodInterceptor";
private static final String REDIS_COMMAND_COMPLETE_EXCEPTIONALLY_METHOD_INTERCEPTOR = "org.apache.skywalking.apm.plugin.lettuce.v5.RedisCommandCompleteExceptionallyMethodInterceptor";
public static final String REDIS_COMMAND_CANCEL_METHOD_INTERCEPTOR = "org.apache.skywalking.apm.plugin.lettuce.v5.RedisCommandCancelMethodInterceptor";
@Override
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
@ -46,25 +49,56 @@ public class AsyncCommandInstrumentation extends ClassInstanceMethodsEnhancePlug
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return (named("onComplete").and(takesArgumentWithType(0, "java.util.function.Consumer"))).or(named("onComplete")
.and(takesArgumentWithType(0, "java.util.function.BiConsumer")));
return named("complete").and(takesArguments(0));
}
@Override
public String getMethodsInterceptor() {
return ASYNC_COMMAND_METHOD_INTERCEPTOR;
return REDIS_COMMAND_COMPLETE_METHOD_INTERCEPTOR;
}
@Override
public boolean isOverrideArgs() {
return true;
}
},
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named("completeExceptionally").and(takesArguments(1)).and(takesArgument(0, Throwable.class));
}
@Override
public String getMethodsInterceptor() {
return REDIS_COMMAND_COMPLETE_EXCEPTIONALLY_METHOD_INTERCEPTOR;
}
@Override
public boolean isOverrideArgs() {
return false;
}
},
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named("cancel").and(takesArguments(0));
}
@Override
public String getMethodsInterceptor() {
return REDIS_COMMAND_CANCEL_METHOD_INTERCEPTOR;
}
@Override
public boolean isOverrideArgs() {
return false;
}
}
};
}
@Override
public ClassMatch enhanceClass() {
return byName(ENHANCE_CLASS);
return byHierarchyMatch(ENHANCE_CLASS);
}
}

View File

@ -14,9 +14,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
lettuce-5.x=org.apache.skywalking.apm.plugin.lettuce.v5.define.AbstractRedisClientInstrumentation
lettuce-5.x=org.apache.skywalking.apm.plugin.lettuce.v5.define.AsyncCommandInstrumentation
lettuce-5.x=org.apache.skywalking.apm.plugin.lettuce.v5.define.ClientOptionsInstrumentation
lettuce-5.x=org.apache.skywalking.apm.plugin.lettuce.v5.define.DefaultEndpointInstrumentation
lettuce-5.x=org.apache.skywalking.apm.plugin.lettuce.v5.define.RedisChannelWriterInstrumentation
lettuce-5.x=org.apache.skywalking.apm.plugin.lettuce.v5.define.RedisClientInstrumentation
lettuce-5.x=org.apache.skywalking.apm.plugin.lettuce.v5.define.RedisClusterClientInstrumentation
lettuce-5.x=org.apache.skywalking.apm.plugin.lettuce.v5.define.RedisCommandInstrumentation

View File

@ -18,12 +18,13 @@
package org.apache.skywalking.apm.plugin.lettuce.v5;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import java.util.ArrayList;
import java.util.List;
import io.lettuce.core.codec.ByteArrayCodec;
import io.lettuce.core.output.CommandOutput;
import io.lettuce.core.protocol.Command;
import io.lettuce.core.protocol.CommandArgs;
import io.lettuce.core.protocol.CommandType;
import io.lettuce.core.protocol.ProtocolKeyword;
import io.lettuce.core.protocol.RedisCommand;
import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan;
import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer;
import org.apache.skywalking.apm.agent.core.context.trace.TraceSegment;
@ -35,11 +36,7 @@ import org.apache.skywalking.apm.agent.test.tools.AgentServiceRule;
import org.apache.skywalking.apm.agent.test.tools.SegmentStorage;
import org.apache.skywalking.apm.agent.test.tools.SegmentStoragePoint;
import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner;
import org.apache.skywalking.apm.plugin.lettuce.v5.mock.MockClientOptions;
import org.apache.skywalking.apm.plugin.lettuce.v5.mock.MockRedisClusterClient;
import org.hamcrest.CoreMatchers;
import org.hamcrest.MatcherAssert;
import org.hamcrest.core.Is;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@ -47,32 +44,37 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.powermock.reflect.Whitebox;
import io.lettuce.core.RedisURI;
import io.lettuce.core.codec.ByteArrayCodec;
import io.lettuce.core.protocol.Command;
import io.lettuce.core.protocol.CommandArgs;
import io.lettuce.core.protocol.CommandType;
import io.lettuce.core.protocol.RedisCommand;
import java.util.ArrayList;
import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(TracingSegmentRunner.class)
public class RedisChannelWriterInterceptorTest {
public static final String PEER = "192.168.1.12:6379";
@SegmentStoragePoint
private SegmentStorage segmentStorage;
@Rule
public AgentServiceRule serviceRule = new AgentServiceRule();
@Mock
private MockInstance mockClientOptionsInstance;
@Mock
private MockInstance mockRedisChannelWriterInstance;
private RedisChannelWriterInterceptor interceptor;
private class MockInstance implements EnhancedInstance {
private RedisCommandCompleteMethodInterceptor redisCommandCompleteMethodInterceptor;
private static class MockInstance implements EnhancedInstance {
private Object object;
@Override
@ -86,29 +88,48 @@ public class RedisChannelWriterInterceptorTest {
}
}
@SuppressWarnings({
"rawtypes",
"unchecked"
})
private static class MockRedisCommand<K, V, T> extends Command<K, V, T> implements EnhancedInstance {
private Object object;
public MockRedisCommand(ProtocolKeyword type, CommandOutput<K, V, T> output) {
super(type, output);
}
public MockRedisCommand(ProtocolKeyword type, CommandOutput<K, V, T> output, CommandArgs<K, V> args) {
super(type, output, args);
}
@Override
public Object getSkyWalkingDynamicField() {
return object;
}
@Override
public void setSkyWalkingDynamicField(Object value) {
this.object = value;
}
}
@Before
public void setUp() throws Exception {
public void setUp() {
LettucePluginConfig.Plugin.Lettuce.TRACE_REDIS_PARAMETERS = true;
mockRedisChannelWriterInstance = new MockInstance();
mockClientOptionsInstance = new MockInstance();
mockClientOptionsInstance.setSkyWalkingDynamicField("127.0.0.1:6379;127.0.0.1:6378;");
mockRedisChannelWriterInstance.setSkyWalkingDynamicField(PEER);
interceptor = new RedisChannelWriterInterceptor();
redisCommandCompleteMethodInterceptor = new RedisCommandCompleteMethodInterceptor();
}
@Test
public void testInterceptor() throws Throwable {
interceptor.onConstruct(mockRedisChannelWriterInstance, new Object[] {mockClientOptionsInstance});
CommandArgs args = new CommandArgs(new ByteArrayCodec()).addKey("name".getBytes()).addValue("Tom".getBytes());
RedisCommand redisCommand = new Command(CommandType.SET, null, args);
interceptor.beforeMethod(mockRedisChannelWriterInstance, null, new Object[] {redisCommand}, null, null);
public void testInterceptor() {
CommandArgs<?, ?> args = new CommandArgs<>(new ByteArrayCodec()).addKey("name".getBytes()).addValue("Tom".getBytes());
MockRedisCommand<?, ?, ?> redisCommand = new MockRedisCommand<>(CommandType.SET, null, args);
interceptor.beforeMethod(mockRedisChannelWriterInstance, null, new Object[]{redisCommand}, null, null);
interceptor.afterMethod(mockRedisChannelWriterInstance, null, null, null, null);
MatcherAssert.assertThat((String) mockRedisChannelWriterInstance.getSkyWalkingDynamicField(), Is.is("127.0.0.1:6379;127.0.0.1:6378;"));
redisCommandCompleteMethodInterceptor.afterMethod(redisCommand, null, null, null, null);
TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0);
List<AbstractTracingSpan> spans = SegmentHelper.getSpans(traceSegment);
assertNotNull(spans);
assertThat(spans.size(), is(1));
assertThat(spans.get(0).getOperationName(), is("Lettuce/SET"));
assertThat(spans.get(0).isExit(), is(true));
@ -117,22 +138,25 @@ public class RedisChannelWriterInterceptorTest {
assertThat(tags.get(0).getValue(), is("Redis"));
assertThat(tags.get(1).getValue(), CoreMatchers.containsString("Tom"));
assertThat(SpanHelper.getLayer(spans.get(0)), CoreMatchers.is(SpanLayer.CACHE));
assertThat(SpanHelper.getPeer(spans.get(0)), is(PEER));
}
@Test
public void testOnHugeClusterConsumerConfig() {
List<RedisURI> redisURIs = new ArrayList<>(100);
for (int i = 0; i < 100; i++) {
redisURIs.add(RedisURI.create("localhost", i));
public void testGetSpanCarrierCommand() throws Exception {
Command<?, ?, ?> command = new Command<>(CommandType.SET, null, null);
RedisCommand<?, ?, ?> redisCommand = Whitebox.invokeMethod(interceptor, "getSpanCarrierCommand", command);
assertEquals(command, redisCommand);
List<RedisCommand<?, ?, ?>> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list.add(new Command<>(CommandType.SET, null, null));
}
MockRedisClusterClient mockRedisClusterClient = new MockRedisClusterClient();
MockClientOptions options = new MockClientOptions();
mockRedisClusterClient.setOptions(options);
RedisClusterClientConstructorInterceptor constructorInterceptor = new RedisClusterClientConstructorInterceptor();
constructorInterceptor.onConstruct(mockRedisClusterClient, new Object[] {
null,
redisURIs
});
assertThat(options.getSkyWalkingDynamicField().toString().length(), Is.is(200));
list.add(command);
RedisCommand<?, ?, ?> last = Whitebox.invokeMethod(interceptor, "getSpanCarrierCommand", list);
assertEquals(command, last);
RedisCommand<?, ?, ?> nullValue1 = Whitebox.invokeMethod(interceptor, "getSpanCarrierCommand", (Object) null);
assertNull(nullValue1);
list.add(null);
RedisCommand<?, ?, ?> nullValue2 = Whitebox.invokeMethod(interceptor, "getSpanCarrierCommand", list);
assertNull(nullValue2);
}
}

View File

@ -1,45 +0,0 @@
/*
* 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.lettuce.v5.mock;
import io.lettuce.core.cluster.ClusterClientOptions;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
public class MockClientOptions extends ClusterClientOptions implements EnhancedInstance {
private Object object;
public MockClientOptions() {
this(ClusterClientOptions.builder());
}
protected MockClientOptions(Builder builder) {
super(builder);
}
@Override
public Object getSkyWalkingDynamicField() {
return object;
}
@Override
public void setSkyWalkingDynamicField(Object value) {
this.object = value;
}
}

View File

@ -1,37 +0,0 @@
/*
* 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.lettuce.v5.mock;
import io.lettuce.core.cluster.RedisClusterClient;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
public class MockRedisClusterClient extends RedisClusterClient implements EnhancedInstance {
private Object object;
@Override
public Object getSkyWalkingDynamicField() {
return object;
}
@Override
public void setSkyWalkingDynamicField(Object value) {
this.object = value;
}
}

View File

@ -40,6 +40,19 @@ public class SpanHelper {
return -9999;
}
public static String getPeer(AbstractSpan tracingSpan) {
try {
return FieldGetter.get2LevelParentFieldValue(tracingSpan, "peer");
} catch (Exception e) {
try {
return FieldGetter.getParentFieldValue(tracingSpan, "peer");
} catch (Exception e1) {
}
}
return null;
}
public static List<LogDataEntity> getLogs(AbstractSpan tracingSpan) {
try {
List<LogDataEntity> logs = FieldGetter.get2LevelParentFieldValue(tracingSpan, "logs");

View File

@ -14,53 +14,85 @@
# See the License for the specific language governing permissions and
# limitations under the License.
segmentItems:
- serviceName: lettuce-scenario
segmentSize: nq 0
segments:
- segmentId: not null
spans:
- operationName: Lettuce/GET
operationId: 0
parentSpanId: 0
spanId: 1
spanLayer: Cache
startTime: not null
endTime: not null
componentId: 57
isError: false
spanType: Exit
peer: not null
tags:
- {key: db.type, value: Redis}
- {key: db.statement, value: GET key<key>}
skipAnalysis: 'false'
- operationName: Lettuce/BATCH_WRITE
operationId: 0
parentSpanId: 0
spanId: 2
spanLayer: Cache
startTime: not null
endTime: not null
componentId: 57
isError: false
spanType: Exit
peer: not null
tags:
- {key: db.type, value: Redis}
- {key: db.statement, value: SET;SET;}
skipAnalysis: 'false'
- operationName: /lettuce-scenario/case/lettuce-case
operationId: 0
parentSpanId: -1
spanId: 0
spanLayer: Http
startTime: not null
endTime: not null
componentId: 1
isError: false
spanType: Entry
peer: ''
tags:
- {key: url, value: not null}
- {key: http.method, value: GET}
skipAnalysis: 'false'
- serviceName: lettuce-scenario
segmentSize: nq 0
segments:
- segmentId: not null
spans:
- operationName: /lettuce-scenario/case/healthCheck
operationId: 0
parentSpanId: -1
spanId: 0
spanLayer: Http
startTime: not null
endTime: not null
componentId: 1
isError: false
spanType: Entry
peer: ''
skipAnalysis: false
tags:
- {key: url, value: 'http://localhost:8080/lettuce-scenario/case/healthCheck'}
- {key: http.method, value: HEAD}
- segmentId: not null
spans:
- operationName: Lettuce/GET
operationId: 0
parentSpanId: 0
spanId: 1
spanLayer: Cache
startTime: not null
endTime: not null
componentId: 57
isError: false
spanType: Exit
peer: not null
skipAnalysis: false
tags:
- {key: db.type, value: Redis}
- {key: db.statement, value: GET key<key>}
- operationName: Lettuce/SET
operationId: 0
parentSpanId: 0
spanId: 2
spanLayer: Cache
startTime: not null
endTime: not null
componentId: 57
isError: false
spanType: Exit
peer: not null
skipAnalysis: false
tags:
- {key: db.type, value: Redis}
- {key: db.statement, value: SET key<key0> value<value0>}
- operationName: Lettuce/SET
operationId: 0
parentSpanId: 0
spanId: 3
spanLayer: Cache
startTime: not null
endTime: not null
componentId: 57
isError: false
spanType: Exit
peer: not null
skipAnalysis: false
tags:
- {key: db.type, value: Redis}
- {key: db.statement, value: SET key<key1> value<value1>}
- operationName: /lettuce-scenario/case/lettuce-case
operationId: 0
parentSpanId: -1
spanId: 0
spanLayer: Http
startTime: not null
endTime: not null
componentId: 1
isError: false
spanType: Entry
peer: ''
skipAnalysis: false
tags:
- {key: url, value: 'http://localhost:8080/lettuce-scenario/case/lettuce-case'}
- {key: http.method, value: GET}

View File

@ -16,4 +16,5 @@
5.2.1.RELEASE
5.1.8.RELEASE
5.0.5.RELEASE
5.0.5.RELEASE
6.1.4.RELEASE