Skip to content

Commit

Permalink
Support lettuce plugin (apache#2152)
Browse files Browse the repository at this point in the history
* Support lettuce plugin

* lettuce plugin bugfix

* lettuce plugin fix license issue

* lettuce plugin fix cluster and config issue

* plugin lettuce compatible with low 5.0.x version

* lettuce plugin support version accurately describes on Supported-list.md

* plugin lettuce compatible with low 5.0.2 version

* lettuce plugin fix wrong batch method intercept point

* move lettuce to optional plugins

* fix ci issue

* fix ci issue

* Modify the description file

* Pom code optimization

* Optimization operation name

* Optimization operation name2
  • Loading branch information
zhaoyuguang authored and wu-sheng committed Jan 26, 2019
1 parent 3b0a1d3 commit a36394d
Show file tree
Hide file tree
Showing 24 changed files with 887 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ public class ComponentsDefine {

public static final OfficialComponent REDISSON = new OfficialComponent(56, "Redisson");

public static final OfficialComponent LETTUCE = new OfficialComponent(57, "Lettuce");

private static ComponentsDefine INSTANCE = new ComponentsDefine();

private String[] components;
Expand All @@ -119,7 +121,7 @@ public static ComponentsDefine getInstance() {
}

public ComponentsDefine() {
components = new String[57];
components = new String[58];
addComponent(TOMCAT);
addComponent(HTTPCLIENT);
addComponent(DUBBO);
Expand Down Expand Up @@ -161,6 +163,7 @@ public ComponentsDefine() {
addComponent(CANAL);
addComponent(GSON);
addComponent(REDISSON);
addComponent(LETTUCE);
}

private void addComponent(OfficialComponent component) {
Expand Down
46 changes: 46 additions & 0 deletions apm-sniffer/optional-plugins/lettuce-5.x-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.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.
~
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.skywalking</groupId>
<artifactId>optional-plugins</artifactId>
<version>6.1.0-SNAPSHOT</version>
</parent>

<artifactId>apm-lettuce-5.x-plugin</artifactId>
<packaging>jar</packaging>

<name>lettuce-5.x-plugin</name>
<url>http://maven.apache.org</url>
<properties>
<lettuce-core.version>5.1.3.RELEASE</lettuce-core.version>
<compiler.version>1.8</compiler.version>
</properties>

<dependencies>
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>${lettuce-core.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.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 org.apache.skywalking.apm.network.trace.component.ComponentsDefine;

import java.lang.reflect.Method;
import java.util.function.Consumer;

/**
* @author zhaoyuguang
*/
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);
allArguments[0] = new SWConsumer((Consumer) 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().errorOccurred().log(t);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.
*
* @author zhaoyuguang
*/
public class ClientOptionsConstructorInterceptor implements InstanceConstructorInterceptor {

@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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.RedisCommand;
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 java.lang.reflect.Method;
import java.util.Collection;

/**
* @author zhaoyuguang
*/
public class RedisChannelWriterInterceptor implements InstanceMethodsAroundInterceptor, InstanceConstructorInterceptor {

@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
String peer = (String) objInst.getSkyWalkingDynamicField();

StringBuilder dbStatement = new StringBuilder();
String operationName = "Lettuce/";

if (allArguments[0] instanceof RedisCommand) {
RedisCommand redisCommand = (RedisCommand) allArguments[0];
String command = redisCommand.getType().name();
operationName = operationName + command;
dbStatement.append(command);
} else if (allArguments[0] instanceof Collection) {
@SuppressWarnings("unchecked")
Collection<RedisCommand> redisCommands = (Collection<RedisCommand>) allArguments[0];
operationName = operationName + "BATCH_WRITE";
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);
}

@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) {
AbstractSpan span = ContextManager.activeSpan();
span.errorOccurred();
span.log(t);
}

@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
EnhancedInstance optionsInst = (EnhancedInstance) allArguments[0];
objInst.setSkyWalkingDynamicField(optionsInst.getSkyWalkingDynamicField());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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;

/**
* @author zhaoyuguang
*/
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();
optionsInst.setSkyWalkingDynamicField(redisURI.getHost() + ":" + redisURI.getPort());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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;

/**
* @author zhaoyuguang
*/
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(peer.toString());
}
}
Loading

0 comments on commit a36394d

Please sign in to comment.