Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions com.creditease.uav.hook.redis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,53 @@
-->
</exclusions>
</dependency>
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>5.0.5.RELEASE</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty-common</artifactId>
</exclusion>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty-transport</artifactId>
</exclusion>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty-handler</artifactId>
</exclusion>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty-codec</artifactId>
</exclusion>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty-transport-native-epoll</artifactId>
</exclusion>
<!--
<exclusion>
<groupId>io.reactivex</groupId>
<artifactId>rxjava</artifactId>
</exclusion>
<exclusion>
<groupId>org.latencyutils</groupId>
<artifactId>LatencyUtils</artifactId>
</exclusion>

<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</exclusion>
-->
</exclusions>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
Expand Down Expand Up @@ -144,6 +191,17 @@
</includes>
</configuration>
</execution>
<execution>
<id>lettuce5x</id>
<goals><goal>jar</goal></goals>
<phase>package</phase>
<configuration>
<classifier>lettuce5x</classifier>
<includes>
<include>**/com/creditease/uav/hook/redis/lettuce5x/**</include>
</includes>
</configuration>
</execution>
<execution>
<id>aredis</id>
<goals><goal>jar</goal></goals>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*-
* <<
* UAVStack
* ==
* Copyright (C) 2016 - 2018 UAVStack
* ==
* Licensed 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 com.creditease.uav.hook.redis.lettuce5x;

import java.util.Map;

import com.creditease.monitor.appfra.hook.spi.HookConstants;
import com.creditease.monitor.appfra.hook.spi.HookContext;
import com.creditease.monitor.appfra.hook.spi.HookProxy;
import com.creditease.monitor.interceptframework.spi.InterceptConstants;
import com.creditease.monitor.interceptframework.spi.InterceptContext;
import com.creditease.monitor.interceptframework.spi.InterceptContext.Event;
import com.creditease.uav.monitorframework.dproxy.DynamicProxyInstaller;
import com.creditease.uav.monitorframework.dproxy.DynamicProxyProcessor;
import com.creditease.uav.monitorframework.dproxy.bytecode.DPClass;
import com.creditease.uav.monitorframework.dproxy.bytecode.DPMethod;
import com.creditease.uav.util.MonitorServerUtil;

/**
* Lettuce5HookProxy description: ???
*
*/
public class Lettuce5HookProxy extends HookProxy {
private DynamicProxyInstaller dpInstaller;

public Lettuce5HookProxy(String id, @SuppressWarnings("rawtypes") Map config) {

super(id, config);
dpInstaller = new DynamicProxyInstaller();
}

protected void insertIntercepter(HookContext context, ClassLoader webapploader) {

if (isHookEventDone("insertIntercepter")) {
return;
}

InterceptContext ic = (InterceptContext) context.get(HookConstants.INTERCEPTCONTEXT);

String contextPath = (String) ic.get(InterceptConstants.CONTEXTPATH);
String basePath = (String) ic.get(InterceptConstants.BASEPATH);

final String appid = MonitorServerUtil.getApplicationId(contextPath, basePath);

doProxyInstall(webapploader, appid);
}

public void doProxyInstall(ClassLoader webapploader, final String appid) {

/**
* set the webapploader is the target classloader
*/
dpInstaller.setTargetClassLoader(webapploader);

dpInstaller.installProxy("io.lettuce.core.protocol.CommandHandler",
new String[] { "com.creditease.uav.hook.redis.lettuce5x.interceptors" }, new DynamicProxyProcessor() {

@Override
public void process(DPMethod m) throws Exception {

if ("write".equals(m.getName())) {
DPClass[] ccs = m.getParameterTypes();
if (ccs.length == 3) {

m.insertBefore("{Lettuce5CommandHandlerIT.start(\"" + appid
+ "\", new Object[]{$2, ((java.net.InetSocketAddress)remote()).getHostName(), new Integer(((java.net.InetSocketAddress)remote()).getPort())});}");
m.insertAfter("{Lettuce5CommandHandlerIT.end(null);}");

dpInstaller.addCatch(m, "Lettuce5CommandHandlerIT.end(new Object[]{$e});");
}
else {
// TODO
// logger.debug("write with more args", null);
}
}
}
}, false);
}

@Override
public void start(HookContext context, ClassLoader webapploader) {

Event evt = context.get(Event.class);

switch (evt) {
case SPRING_BEAN_REGIST:
case WEBCONTAINER_INIT:
insertIntercepter(context, webapploader);
break;
// when servlet init
case AFTER_SERVET_INIT:
break;
case BEFORE_SERVLET_DESTROY:
break;
case WEBCONTAINER_STARTED:
break;
case WEBCONTAINER_STOPPED:
break;
default:
break;
}
}

@Override
public void stop(HookContext context, ClassLoader webapploader) {

Event evt = context.get(Event.class);

switch (evt) {
// when servlet init
case AFTER_SERVET_INIT:
break;
case BEFORE_SERVLET_DESTROY:
break;
case WEBCONTAINER_STARTED:
break;
case WEBCONTAINER_STOPPED:
break;
default:
break;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*-
* <<
* UAVStack
* ==
* Copyright (C) 2016 - 2018 UAVStack
* ==
* Licensed 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 com.creditease.uav.hook.redis.lettuce5x.interceptors;

import java.util.HashMap;
import java.util.Map;

import com.creditease.monitor.UAVServer;
import com.creditease.monitor.captureframework.spi.CaptureConstants;
import com.creditease.monitor.captureframework.spi.Monitor;
import com.creditease.uav.apm.invokechain.spi.InvokeChainConstants;
import com.creditease.uav.common.BaseComponent;
import com.creditease.uav.hook.redis.lettuce5x.invokeChain.Lettuce5ClientAdapter;

import io.lettuce.core.protocol.ProtocolKeyword;
import io.lettuce.core.protocol.RedisCommand;

/**
* Lettuce5CommandHandlerIT description: ???
*
*/
public class Lettuce5CommandHandlerIT extends BaseComponent{
private static ThreadLocal<Lettuce5CommandHandlerIT> tl = new ThreadLocal<Lettuce5CommandHandlerIT>();

private String appid;

private Map<String, Object> ivcContextParams = new HashMap<String, Object>();

public Lettuce5CommandHandlerIT(String appid) {
this.appid = appid;
}

public static void start(String appid, Object[] args) {

Lettuce5CommandHandlerIT it = new Lettuce5CommandHandlerIT(appid);
it.doWriteStart(args);
tl.set(it);
}

public static void end(Object[] args) {

Lettuce5CommandHandlerIT it = tl.get();
tl.remove();

if (it == null) {
return;
}
it.doWriteEnd(args);
}

@SuppressWarnings({ "unchecked", "rawtypes" })
public Object doWriteStart(Object[] args) {

RedisCommand cmdRc = (RedisCommand) args[0];
ProtocolKeyword cmd = (ProtocolKeyword)cmdRc.getType();
String host = (String) args[1];
Integer port = (Integer) args[2];
String targetURL = "redis://" + host + ":" + port;
String redisAction = cmd.toString();

if (logger.isDebugable()) {
logger.debug("REDIS INVOKE START: " + targetURL + " action: " + redisAction, null);
}

Map<String, Object> params = new HashMap<String, Object>();

params.put(CaptureConstants.INFO_CLIENT_REQUEST_URL, targetURL);
params.put(CaptureConstants.INFO_CLIENT_REQUEST_ACTION, redisAction);
params.put(CaptureConstants.INFO_CLIENT_APPID, appid);
params.put(CaptureConstants.INFO_CLIENT_TYPE, "redis.client.Lettuce");

// register adapter
UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.InvokeChainSupporter", "registerAdapter",
Lettuce5ClientAdapter.class);

ivcContextParams = (Map<String, Object>) UAVServer.instance().runSupporter(
"com.creditease.uav.apm.supporters.InvokeChainSupporter", "runCap",
InvokeChainConstants.CHAIN_APP_CLIENT, InvokeChainConstants.CapturePhase.PRECAP, params,
Lettuce5ClientAdapter.class, args);

UAVServer.instance().runMonitorCaptureOnServerCapPoint(CaptureConstants.CAPPOINT_APP_CLIENT,
Monitor.CapturePhase.PRECAP, params);

return null;
}

public Object doWriteEnd(Object[] args) {

int rc = -1;

if (args == null) {
rc = 1;
}

if (args != null && args.length > 0) {
if (!Throwable.class.isAssignableFrom(args[0].getClass())) {
rc = 1;
}
}

if (logger.isDebugable()) {
logger.debug("REDIS INVOKE END: " + rc, null);
}

Map<String, Object> params = new HashMap<String, Object>();
params.put(CaptureConstants.INFO_CLIENT_TARGETSERVER, "redis");
params.put(CaptureConstants.INFO_CLIENT_RESPONSECODE, rc);

UAVServer.instance().runMonitorCaptureOnServerCapPoint(CaptureConstants.CAPPOINT_APP_CLIENT,
Monitor.CapturePhase.DOCAP, params);

if (rc == -1) {
params.put(CaptureConstants.INFO_CLIENT_RESPONSESTATE, ((Throwable) args[0]).toString());
}
if (ivcContextParams != null) {
ivcContextParams.putAll(params);
}

UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.InvokeChainSupporter", "runCap",
InvokeChainConstants.CHAIN_APP_CLIENT, InvokeChainConstants.CapturePhase.DOCAP, ivcContextParams,
Lettuce5ClientAdapter.class, args);

return null;
}
}
Loading