├── .gitignore ├── README.md ├── bom └── pom.xml ├── collect ├── pom.xml └── src │ ├── main │ ├── java │ │ └── cn │ │ │ └── bytes1024 │ │ │ └── hound │ │ │ └── collect │ │ │ ├── AgentCollector.java │ │ │ ├── context │ │ │ ├── ActiveTracerSpan.java │ │ │ ├── ApplicationContext.java │ │ │ ├── DefaultActiveTracerSpan.java │ │ │ ├── DefaultApplicationContext.java │ │ │ ├── DefaultReporter.java │ │ │ └── DefaultTraceContext.java │ │ │ ├── enhance │ │ │ ├── DefaultEnhanceFactory.java │ │ │ ├── DefaultEnhanceRuleChainProxy.java │ │ │ ├── DefaultInterceptorFactory.java │ │ │ ├── EnhanceFactory.java │ │ │ ├── EnhanceRuleChainProxy.java │ │ │ ├── InterceptorFactory.java │ │ │ ├── delegation │ │ │ │ ├── ConstructorInterceptDelegation.java │ │ │ │ └── MethodsInterceptWithDelegation.java │ │ │ └── rule │ │ │ │ ├── AbstractEnhanceRule.java │ │ │ │ ├── DefaultEnhanceRuleChain.java │ │ │ │ ├── EnhanceConstructorRule.java │ │ │ │ ├── EnhanceMethodRule.java │ │ │ │ ├── EnhanceRule.java │ │ │ │ ├── EnhanceRuleChain.java │ │ │ │ ├── EnhanceSourceRule.java │ │ │ │ └── EnhanceStaticMethodRule.java │ │ │ ├── exception │ │ │ ├── BaseException.java │ │ │ ├── ConstructorException.java │ │ │ └── NotFoundPluginLoader.java │ │ │ ├── module │ │ │ ├── CollectModuleFactory.java │ │ │ ├── DefineModule.java │ │ │ ├── ModuleFactory.java │ │ │ └── providers │ │ │ │ ├── AgentOptionProvider.java │ │ │ │ ├── ApplicationContextProvider.java │ │ │ │ ├── EnhanceFactoryProvider.java │ │ │ │ ├── EnhanceRuleChainProxyProvider.java │ │ │ │ ├── InterceptorFactoryProvider.java │ │ │ │ ├── ServerReporterProvider.java │ │ │ │ ├── SofaTracerProvider.java │ │ │ │ ├── TraceContextProvider.java │ │ │ │ └── TransferBufferProvider.java │ │ │ └── processor │ │ │ ├── AbstractProcessor.java │ │ │ ├── AgentCollectProcessor.java │ │ │ ├── MetricsCollectProcessor.java │ │ │ └── Processor.java │ └── resources │ │ └── collect.properties │ └── test │ └── java │ └── cn │ └── bytes1024 │ └── hound │ └── collect │ └── test │ ├── ConfigLoadingTest.java │ └── NettyTimerWheelTest.java ├── commons ├── pom.xml └── src │ └── main │ └── java │ └── cn │ └── bytes1024 │ └── hound │ └── commons │ ├── enums │ ├── ProcessorStatus.java │ └── ProcessorType.java │ ├── option │ ├── ConfigOption.java │ ├── ConfigOptionDefine.java │ └── DefaultConfigOption.java │ └── util │ ├── NamedThreadLocal.java │ ├── RefClassUtil.java │ ├── ThreadPoolUtils.java │ └── TimerWheel.java ├── loader ├── pom.xml └── src │ └── main │ └── java │ └── cn │ └── bytes1024 │ └── hound │ └── loader │ ├── ExtensionLoader.java │ └── Holder.java ├── plugins ├── plugins-bom │ └── pom.xml ├── plugins-define │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── cn │ │ └── bytes1024 │ │ └── hound │ │ └── plugins │ │ └── define │ │ ├── AbstractPluginDefine.java │ │ ├── EnhanceContext.java │ │ ├── EnhancedDefine.java │ │ ├── InterceptContext.java │ │ ├── InterceptContextFilter.java │ │ ├── PluginDefine.java │ │ ├── TraceContext.java │ │ ├── filter │ │ ├── DefaultTraceContextFilterOption.java │ │ └── TraceContextFilterOption.java │ │ └── interceptor │ │ ├── ConstructorInterceptor.java │ │ ├── IgnoreInterceptor.java │ │ ├── InterceptorPluginAware.java │ │ ├── MethodAroundInterceptor.java │ │ ├── StaticMethodAroundInterceptor.java │ │ └── supper │ │ ├── AbstractConstructorInterceptor.java │ │ ├── AbstractMethodAroundInterceptor.java │ │ ├── AbstractTransmissionMethodAroundInterceptor.java │ │ ├── RemoteTransmission.java │ │ └── async │ │ └── AbstractAsyncMethodAroundInterceptor.java ├── plugins-druid │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── cn │ │ │ └── bytes1024 │ │ │ └── hound │ │ │ └── plugins │ │ │ └── druid │ │ │ ├── DruidDataSourcePlugin.java │ │ │ ├── DruidPooledConnectionPlugin.java │ │ │ └── interceptor │ │ │ └── DruidPluginInterceptor.java │ │ └── resources │ │ └── META-INF │ │ └── plugins │ │ └── cn.bytes1024.hound.plugins.define.PluginDefine ├── plugins-fastjson │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── cn │ │ │ └── bytes1024 │ │ │ └── hound │ │ │ └── plugins │ │ │ └── fastjson │ │ │ ├── FastJsonPlugin.java │ │ │ └── interceptor │ │ │ └── FastJsonMethodInterceptor.java │ │ └── resources │ │ └── META-INF │ │ └── plugins │ │ └── cn.bytes1024.hound.plugins.define.PluginDefine ├── plugins-gson │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── cn │ │ │ └── bytes1024 │ │ │ └── hound │ │ │ └── plugins │ │ │ └── gson │ │ │ ├── GsonPlugin.java │ │ │ └── intercepotr │ │ │ └── GsonMethodInterceptor.java │ │ └── resources │ │ └── META-INF │ │ └── plugins │ │ └── cn.bytes1024.hound.plugins.define.PluginDefine ├── plugins-httpclient │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── cn │ │ │ └── bytes1024 │ │ │ └── hound │ │ │ └── plugins │ │ │ └── httpclient │ │ │ ├── HttpClientPlugin.java │ │ │ └── interceptor │ │ │ └── HttpClientMethodInterceptor.java │ │ └── resources │ │ └── META-INF │ │ └── plugins │ │ └── cn.bytes1024.hound.plugins.define.PluginDefine ├── plugins-mybatis │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── cn │ │ │ └── bytes1024 │ │ │ └── hound │ │ │ └── plugins │ │ │ └── mybatis │ │ │ ├── MybatisAnnotationMethodPlugin.java │ │ │ ├── MybatisBoundSqlPlugin.java │ │ │ ├── MybatisPlugin.java │ │ │ └── interceptor │ │ │ ├── MybatisBoundSqlPluginInterceptor.java │ │ │ └── MybatisPluginInterceptor.java │ │ └── resources │ │ └── META-INF │ │ └── plugins │ │ └── cn.bytes1024.hound.plugins.define.PluginDefine ├── plugins-mysql-jdbc │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── cn │ │ │ └── bytes1024 │ │ │ └── hound │ │ │ └── plugins │ │ │ └── mysql │ │ │ └── jdbc │ │ │ ├── MysqlConnectionTransformerPlugin.java │ │ │ ├── MysqlJdbcPlugin.java │ │ │ ├── MysqlJdbcPreparedCallPlugin.java │ │ │ ├── MysqlJdbcPreparedStatementPlugin.java │ │ │ └── interceptor │ │ │ ├── MysqlJdbcInterceptor.java │ │ │ ├── MysqlJdbcPreparedCallInterceptor.java │ │ │ └── MysqlJdbcPreparedStatementInterceptor.java │ │ └── resources │ │ └── META-INF │ │ └── plugins │ │ └── cn.bytes1024.hound.plugins.define.PluginDefine ├── plugins-okhttp │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── cn │ │ │ └── bytes1024 │ │ │ └── hound │ │ │ └── plugins │ │ │ └── okhttp │ │ │ ├── Containts.java │ │ │ ├── OkHttpClientPlugin.java │ │ │ ├── OkHttpRealCallPlugin.java │ │ │ └── interceptor │ │ │ ├── OkHttpClientMethodInterceptor.java │ │ │ └── OkHttpRealCallMethodInterceptor.java │ │ └── resources │ │ └── META-INF │ │ └── plugins │ │ └── cn.bytes1024.hound.plugins.define.PluginDefine ├── plugins-spring │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── cn │ │ │ └── bytes1024 │ │ │ └── hound │ │ │ └── plugins │ │ │ └── spring │ │ │ ├── SpringBeanPlugin.java │ │ │ ├── SpringExecutorExecutorsPlugin.java │ │ │ ├── SpringWebMvcPlugin.java │ │ │ └── interceptor │ │ │ └── SpringPluginMethodInterceptor.java │ │ └── resources │ │ └── META-INF │ │ └── plugins │ │ └── cn.bytes1024.hound.plugins.define.PluginDefine ├── plugins-tomcat │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── cn │ │ │ └── bytes1024 │ │ │ └── hound │ │ │ └── plugins │ │ │ └── tomcat │ │ │ ├── Tomcat7xPlugin.java │ │ │ └── interceptor │ │ │ └── TomcatMethodInterceptor.java │ │ └── resources │ │ └── META-INF │ │ └── plugins │ │ └── cn.bytes1024.hound.plugins.define.PluginDefine ├── plugins-webflux │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── cn │ │ │ └── bytes1024 │ │ │ └── hound │ │ │ └── plugins │ │ │ └── webflux │ │ │ ├── WebFluxOnInboundNextPlugin.java │ │ │ ├── WebFluxOnOutboundCompletePlugin.java │ │ │ └── interceptor │ │ │ ├── OnInboundNextMethodInterceptor.java │ │ │ └── OnOutboundCompleteMethodInterceptor.java │ │ └── resources │ │ └── META-INF │ │ └── plugins │ │ └── cn.bytes1024.hound.plugins.define.PluginDefine └── pom.xml ├── pom.xml ├── server ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── bytes1024 │ │ └── hound │ │ └── server │ │ ├── ApplicationServer.java │ │ └── api │ │ └── IndexApi.java │ └── resources │ └── application.yml └── transfers ├── pom.xml ├── transfers-bom └── pom.xml ├── transfers-define ├── pom.xml └── src │ └── main │ └── java │ └── cn │ └── bytes1024 │ └── hound │ └── transfers │ └── define │ ├── DefineTransmitContent.java │ ├── TransferDefine.java │ ├── TransmitMetricsContent.java │ ├── TransmitTraceContent.java │ └── buffer │ ├── DefaultTransferBuffer.java │ ├── NotifyListener.java │ └── TransferBuffer.java └── transfers-web ├── pom.xml └── src └── main ├── java └── cn │ └── bytes1024 │ └── hound │ └── transfers │ └── web │ ├── AbstractRestApiTransfer.java │ └── OkHttpRestApiTransfer.java └── resources └── META-INF └── plugins └── cn.bytes1024.hound.transfers.define.TransferDefine /.gitignore: -------------------------------------------------------------------------------- 1 | ## .gitignore for Grails 1.2 and 1.3 2 | 3 | # .gitignore for maven 4 | target/ 5 | *.releaseBackup 6 | 7 | classes.* 8 | 9 | # web application files 10 | #/web-app/WEB-INF 11 | 12 | # IDE support files 13 | /.classpath 14 | /.launch 15 | /.project 16 | /.settings 17 | /*.launch 18 | /*.tmproj 19 | /ivy* 20 | /eclipse 21 | 22 | # default HSQL database files for production mode 23 | /prodDb.* 24 | 25 | # general HSQL database files 26 | *Db.properties 27 | *Db.script 28 | 29 | # logs 30 | /stacktrace.log 31 | /test/reports 32 | /logs 33 | *.log 34 | *.log.* 35 | 36 | # project release file 37 | /*.war 38 | 39 | # plugin release file 40 | /*.zip 41 | /*.zip.sha1 42 | 43 | # older plugin install locations 44 | /web-app/plugins 45 | /web-app/WEB-INF/classes 46 | 47 | # "temporary" build files 48 | target/ 49 | out/ 50 | build/ 51 | 52 | # other 53 | *.iws 54 | 55 | #.gitignore for java 56 | *.class 57 | 58 | # Package Files # 59 | *.jar 60 | *.war 61 | *.ear 62 | 63 | ## .gitignore for eclipse 64 | 65 | *.pydevproject 66 | .project 67 | .metadata 68 | bin/** 69 | tmp/** 70 | tmp/**/* 71 | *.tmp 72 | *.bak 73 | *.swp 74 | *~.nib 75 | local.properties 76 | .classpath 77 | .settings/ 78 | .loadpath 79 | 80 | # External tool builders 81 | .externalToolBuilders/ 82 | 83 | # Locally stored "Eclipse launch configurations" 84 | *.launch 85 | 86 | # CDT-specific 87 | .cproject 88 | 89 | # PDT-specific 90 | .buildpath 91 | 92 | ## .gitignore for intellij 93 | 94 | *.iml 95 | *.ipr 96 | *.iws 97 | .idea/ 98 | 99 | ## .gitignore for linux 100 | .* 101 | !.gitignore 102 | *~ 103 | 104 | ## .gitignore for windows 105 | 106 | # Windows image file caches 107 | Thumbs.db 108 | ehthumbs.db 109 | 110 | # Folder config file 111 | Desktop.ini 112 | 113 | # Recycle Bin used on file shares 114 | $RECYCLE.BIN/ 115 | 116 | ## .gitignore for mac os x 117 | 118 | .DS_Store 119 | .AppleDouble 120 | .LSOverride 121 | Icon 122 | 123 | 124 | # Thumbnails 125 | ._* 126 | 127 | # Files that might appear on external disk 128 | .Spotlight-V100 129 | .Trashes 130 | 131 | ## hack for graddle wrapper 132 | !wrapper/*.jar 133 | !**/wrapper/*.jar -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ##### 使用 2 | * 下载 3 | * TODO 4 | * 本地编译 5 | ``` 6 | mvn clean package -Dmaven.test.skip=true 7 | 8 | //在 hound-collect 目录下面生成对应的jar文件 9 | //项目启动后面添加参数 -javaagent:xxx\collect.jar 10 | //config 配置目前用处不大 11 | 12 | ``` 13 | * 本地测试预览 14 | ```$xslt 15 | 如下,目前使用的三个测试插件数据 16 | {"bizBaggage":{},"endTime":1562249883956,"operationName":"plugin-springBean","parentId":"0.1.1","sampled":true,"spanId":"0.1.1.1","startTime":1562249883951,"sysBaggage":{"method":"servce1","class":"com.example.demo.service.Service"},"traceId":"c0a89a011562249883949100217996"} 5 ms 17 | {"bizBaggage":{},"endTime":1562249883956,"operationName":"plugin-springWebmvc","parentId":"0.1","sampled":true,"spanId":"0.1.1","startTime":1562249883951,"sysBaggage":{"method":"test1","class":"com.example.demo.TestController"},"traceId":"c0a89a011562249883949100217996"} 5 ms 18 | {"bizBaggage":{},"endTime":1562249883960,"operationName":"plugin-tomcat","parentId":"0","sampled":true,"spanId":"0.1","startTime":1562249883949,"sysBaggage":{"method":"invoke","class":"org.apache.catalina.core.StandardHostValve"},"traceId":"c0a89a011562249883949100217996"} 11 ms 19 | 20 | ``` 21 | ##### 系统设计 22 | ![jgt](https://github.com/bytes1024/files/blob/master/hound/images/jgt.png) 23 | 24 | ##### 配置参数 25 | `-javaagent:collect.jar=collect.properties` 26 | ``` 27 | #增强代码输出目录 28 | # agent.plugin.debug.path="E:\\" 29 | 30 | ########################################################################## 31 | ###探针编号 32 | ### 33 | bytes.hound.agent.id=agent1 34 | ###是否开启远程传输 35 | bytes.hound.transfer.enabled=true 36 | ###是否开启传输内容打印 37 | bytes.hound.transfer.content.show.enabled=true 38 | ###传输类型 39 | bytes.hound.transfer.type=web 40 | ###服务地址 41 | bytes.hound.transfer.web.address=http://127.0.0.1:8888/v1/transfer/receive 42 | #other config TODO........... 43 | ########################################################################### 44 | 45 | ``` 46 | 47 | ##### 功能 48 | ##### [任务列表](https://github.com/bytes1024/hound/wiki/%E5%BC%80%E5%8F%91%E4%BB%BB%E5%8A%A1%E5%88%97%E8%A1%A8) 49 | ##### 支持插件 50 | 名称|版本|粒度 51 | |---|---|---| 52 | |tomcat|6+|粗| 53 | |webflux|5.0.0.release|粗| 54 | |okHttp|3.7.0|粗 55 | |spring|5.0.0.RELEASE|粗 56 | |httpClient|4.5.2|粗 57 | |dubbo|TODO| 58 | |gson|2.8.0|粗 59 | |fastjson|1.2.49|粗 60 | |jackson|TODO 61 | |hystrix|TODO 62 | |mybatis|3.5.2|粗 63 | |hibernate(jpa)|TODO 64 | |mysql-jdbc|TODO 65 | |redis-lettuce|TODO 66 | |spring-data-*(jpa,redis,es,....)|TODO 67 | |rabbitmq|TODO 68 | |rocketmq|TODO 69 | |mongodb|TODO 70 | |log4j|TODO 71 | |dbcp|TODO 72 | |druid|1.1.10|粗 73 | |hikaricp|TODO 74 | |TODO 75 | ##### [文档](https://github.com/bytes1024/hound/wiki) 76 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/AgentCollector.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect; 2 | 3 | 4 | import cn.bytes1024.hound.collect.module.CollectModuleFactory; 5 | 6 | import java.lang.instrument.Instrumentation; 7 | 8 | /** 9 | * 收集器探针 10 | * 11 | * @author 江浩 12 | */ 13 | public class AgentCollector { 14 | 15 | /** 16 | * agent 拦截 17 | * 18 | * @param agentArgs 19 | * @param instrumentation 20 | */ 21 | public static void premain(String agentArgs, Instrumentation instrumentation) { 22 | CollectModuleFactory applicationContextModuleFactory = new CollectModuleFactory(); 23 | applicationContextModuleFactory.init(agentArgs, instrumentation); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/context/ActiveTracerSpan.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.context; 2 | 3 | 4 | import com.alipay.common.tracer.core.span.SofaTracerSpan; 5 | 6 | /** 7 | * 单个线程活跃span的操作信息 8 | *

9 | * LIFO 先进后出 栈原则操作 10 | *

11 | * 12 | * @author 江浩 13 | */ 14 | public interface ActiveTracerSpan { 15 | 16 | 17 | /** 18 | * 添加在栈头部 19 | * 20 | * @param sofaTracerSpan 21 | */ 22 | void addFirst(SofaTracerSpan sofaTracerSpan); 23 | 24 | 25 | /** 26 | * 当前线程执行长度 27 | * 28 | * @return : java.lang.Integer 29 | * @author 江浩 30 | */ 31 | Integer size(); 32 | 33 | /** 34 | * 最后一个提交的,LIFO 原则 35 | * 36 | * @return : com.alipay.common.tracer.core.span.SofaTracerSpan 37 | * @author 江浩 38 | */ 39 | SofaTracerSpan peekFirst(); 40 | 41 | /** 42 | * 停止最后一个提交信息 43 | * 44 | * @return : com.alipay.common.tracer.core.span.SofaTracerSpan 45 | * @author 江浩 46 | */ 47 | SofaTracerSpan pollFirst(); 48 | } 49 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/context/ApplicationContext.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.context; 2 | 3 | /** 4 | * 初始容器环境 5 | * @author 江浩 6 | */ 7 | public interface ApplicationContext { 8 | 9 | /** 10 | * 容器启动 11 | * @return : void 12 | * @author 江浩 13 | */ 14 | void start(); 15 | 16 | /** 17 | * 容器关闭 18 | * @return : void 19 | * @author 江浩 20 | */ 21 | void close(); 22 | } 23 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/context/DefaultActiveTracerSpan.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.context; 2 | 3 | import com.alipay.common.tracer.core.span.SofaTracerSpan; 4 | import com.google.common.collect.Lists; 5 | 6 | import java.util.LinkedList; 7 | import java.util.Objects; 8 | 9 | /** 10 | * 线程默认活跃span 的实现 11 | * 12 | * @author 江浩 13 | */ 14 | public class DefaultActiveTracerSpan implements ActiveTracerSpan { 15 | 16 | private LinkedList tracerSpans = Lists.newLinkedList(); 17 | 18 | 19 | @Override 20 | public void addFirst(SofaTracerSpan sofaTracerSpan) { 21 | if (!Objects.isNull(sofaTracerSpan)) { 22 | tracerSpans.addFirst(sofaTracerSpan); 23 | } 24 | } 25 | 26 | @Override 27 | public Integer size() { 28 | return tracerSpans.size(); 29 | } 30 | 31 | @Override 32 | public SofaTracerSpan peekFirst() { 33 | return tracerSpans.getFirst(); 34 | } 35 | 36 | @Override 37 | public SofaTracerSpan pollFirst() { 38 | return tracerSpans.removeFirst(); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/context/DefaultApplicationContext.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.context; 2 | 3 | import cn.bytes1024.hound.collect.processor.Processor; 4 | import cn.bytes1024.hound.commons.option.ConfigOption; 5 | import cn.bytes1024.hound.commons.util.ThreadPoolUtils; 6 | import lombok.extern.slf4j.Slf4j; 7 | import org.apache.commons.collections.CollectionUtils; 8 | 9 | import java.util.List; 10 | import java.util.concurrent.CountDownLatch; 11 | import java.util.concurrent.ThreadPoolExecutor; 12 | import java.util.concurrent.TimeUnit; 13 | 14 | /** 15 | * 默认容器环境 16 | * 17 | * @author 江浩 18 | */ 19 | @Slf4j 20 | public class DefaultApplicationContext implements ApplicationContext { 21 | 22 | private ConfigOption configOption; 23 | 24 | private List processors; 25 | 26 | private ThreadPoolExecutor poolExecutor = ThreadPoolUtils.newFixedThreadPool(3, DefaultApplicationContext.class.getName()); 27 | 28 | public DefaultApplicationContext(ConfigOption configOption, 29 | List processors) { 30 | this.configOption = configOption; 31 | this.processors = processors; 32 | } 33 | 34 | @Override 35 | public void start() { 36 | 37 | if (CollectionUtils.isNotEmpty(processors)) { 38 | 39 | CountDownLatch countDownLatch = new CountDownLatch(this.processors.size()); 40 | 41 | processors.forEach(handler -> poolExecutor.execute(() -> { 42 | try { 43 | handler.start(configOption, countDownLatch); 44 | } catch (Exception e) { 45 | e.printStackTrace(); 46 | } 47 | })); 48 | 49 | try { 50 | countDownLatch.await(60, TimeUnit.SECONDS); 51 | } catch (InterruptedException e) { 52 | log.error("handler start error : {}", e); 53 | this.close(); 54 | } 55 | } 56 | 57 | } 58 | 59 | @Override 60 | public void close() { 61 | if (CollectionUtils.isNotEmpty(processors)) { 62 | processors.forEach(Processor::destroy); 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/context/DefaultReporter.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.context; 2 | 3 | import cn.bytes1024.hound.commons.option.ConfigOption; 4 | import cn.bytes1024.hound.commons.option.ConfigOptionDefine; 5 | import cn.bytes1024.hound.transfers.define.DefineTransmitContent; 6 | import cn.bytes1024.hound.transfers.define.TransferDefine; 7 | import cn.bytes1024.hound.transfers.define.TransmitTraceContent; 8 | import cn.bytes1024.hound.transfers.define.buffer.NotifyListener; 9 | import cn.bytes1024.hound.transfers.define.buffer.TransferBuffer; 10 | import com.alipay.common.tracer.core.context.span.SofaTracerSpanContext; 11 | import com.alipay.common.tracer.core.reporter.facade.Reporter; 12 | import com.alipay.common.tracer.core.span.SofaTracerSpan; 13 | import lombok.extern.slf4j.Slf4j; 14 | 15 | import java.util.Objects; 16 | 17 | /** 18 | * 默认的上报器 19 | *

20 | * 1.上报器目前结合 {@link TransferBuffer} {@link NotifyListener} 使用 21 | *

22 | * 23 | * @author 江浩 24 | */ 25 | @Slf4j 26 | public class DefaultReporter implements Reporter, NotifyListener { 27 | 28 | private TransferDefine transferDefine; 29 | 30 | private TransferBuffer transferBuffer; 31 | 32 | private ConfigOption configOption; 33 | 34 | public DefaultReporter(TransferDefine transferDefine, TransferBuffer transferBuffer, ConfigOption configOption) { 35 | this.transferDefine = transferDefine; 36 | this.transferBuffer = transferBuffer; 37 | this.configOption = configOption; 38 | if (Objects.nonNull(this.transferBuffer)) { 39 | log.info("缓冲器启动...."); 40 | this.transferBuffer.register(this); 41 | } 42 | } 43 | 44 | @Override 45 | public String getReporterType() { 46 | return null; 47 | } 48 | 49 | @Override 50 | public void report(SofaTracerSpan sofaTracerSpan) { 51 | 52 | if (Objects.isNull(sofaTracerSpan)) { 53 | return; 54 | } 55 | 56 | SofaTracerSpanContext sofaTracerSpanContext = sofaTracerSpan.getSofaTracerSpanContext(); 57 | 58 | TransmitTraceContent transmitTraceContent = new TransmitTraceContent() 59 | .setOperationName(sofaTracerSpan.getOperationName()) 60 | .setTraceId(sofaTracerSpanContext.getTraceId()) 61 | .setParentId(sofaTracerSpanContext.getParentId()) 62 | .setSpanId(sofaTracerSpanContext.getSpanId()) 63 | .setBizBaggage(sofaTracerSpanContext.getBizBaggage()) 64 | .setSysBaggage(sofaTracerSpanContext.getSysBaggage()) 65 | .setStartTime(sofaTracerSpan.getStartTime()) 66 | .setEndTime(sofaTracerSpan.getEndTime()) 67 | .setSampled(sofaTracerSpanContext.isSampled()); 68 | 69 | if (ConfigOptionDefine.isOpenTransmitContentView(configOption)) { 70 | log.info("{}", transmitTraceContent); 71 | } 72 | if (Objects.nonNull(transferBuffer)) { 73 | this.transferBuffer.push(transmitTraceContent); 74 | } 75 | } 76 | 77 | @Override 78 | public void close() { 79 | 80 | } 81 | 82 | @Override 83 | public void notify(T message) { 84 | if (!Objects.isNull(transferDefine)) { 85 | this.transferDefine.transmit(configOption, message); 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/context/DefaultTraceContext.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.context; 2 | 3 | import cn.bytes1024.hound.commons.util.NamedThreadLocal; 4 | import cn.bytes1024.hound.plugins.define.InterceptContext; 5 | import cn.bytes1024.hound.plugins.define.TraceContext; 6 | import cn.bytes1024.hound.plugins.define.filter.TraceContextFilterOption; 7 | import com.alipay.common.tracer.core.SofaTracer; 8 | import com.alipay.common.tracer.core.context.span.SofaTracerSpanContext; 9 | import com.alipay.common.tracer.core.context.trace.SofaTracerThreadLocalTraceContext; 10 | import com.alipay.common.tracer.core.span.SofaTracerSpan; 11 | import lombok.Getter; 12 | import lombok.extern.slf4j.Slf4j; 13 | 14 | import java.util.EmptyStackException; 15 | import java.util.Objects; 16 | 17 | /** 18 | * 默认的追踪上线文 19 | * 20 | * @author 江浩 21 | */ 22 | @Getter 23 | @Slf4j 24 | public class DefaultTraceContext extends SofaTracerThreadLocalTraceContext implements TraceContext, TraceContextFilterOption { 25 | 26 | 27 | private SofaTracer sofaTracer; 28 | 29 | /** 30 | * 当前线程中活跃的span信息 31 | * 32 | * @author 江浩 33 | */ 34 | private final NamedThreadLocal activeTracerSpans = new NamedThreadLocal<>("activeTracerSpans"); 35 | 36 | private TraceContextFilterOption traceContextFilterOption; 37 | 38 | public DefaultTraceContext(SofaTracer sofaTracer, TraceContextFilterOption traceContextFilterOption) { 39 | this.sofaTracer = sofaTracer; 40 | this.traceContextFilterOption = traceContextFilterOption; 41 | } 42 | 43 | @Override 44 | public SofaTracer getSofaTracer() { 45 | return this.sofaTracer; 46 | } 47 | 48 | 49 | @Override 50 | public SofaTracerSpan stopCurrentTracerSpan(InterceptContext interceptContext) { 51 | SofaTracerSpan sofaTracerSpan = this.pop(); 52 | if (Objects.isNull(sofaTracerSpan)) { 53 | return null; 54 | } 55 | 56 | SofaTracerSpanContext sofaTracerSpanContext = sofaTracerSpan.getSofaTracerSpanContext(); 57 | 58 | this.filterOption(sofaTracerSpanContext, interceptContext); 59 | 60 | sofaTracerSpanContext.addBizBaggage(interceptContext.getProps()); 61 | sofaTracerSpan.finish(); 62 | 63 | return sofaTracerSpan; 64 | } 65 | 66 | 67 | @Override 68 | public void push(SofaTracerSpan span) { 69 | if (span == null) { 70 | return; 71 | } 72 | //当前线程 73 | ActiveTracerSpan activeTracerSpan = activeTracerSpans.get(); 74 | if (Objects.isNull(activeTracerSpan)) { 75 | activeTracerSpan = new DefaultActiveTracerSpan(); 76 | activeTracerSpans.set(activeTracerSpan); 77 | } 78 | activeTracerSpan.addFirst(span); 79 | } 80 | 81 | @Override 82 | public SofaTracerSpan getCurrentSpan() throws EmptyStackException { 83 | if (this.isEmpty()) { 84 | return null; 85 | } 86 | 87 | ActiveTracerSpan activeTracerSpan = activeTracerSpans.get(); 88 | if (Objects.isNull(activeTracerSpan)) { 89 | return null; 90 | } 91 | return activeTracerSpan.peekFirst(); 92 | } 93 | 94 | @Override 95 | public SofaTracerSpan pop() throws EmptyStackException { 96 | if (this.isEmpty()) { 97 | return null; 98 | } 99 | ActiveTracerSpan activeTracerSpan = activeTracerSpans.get(); 100 | if (Objects.isNull(activeTracerSpan)) { 101 | return null; 102 | } 103 | 104 | SofaTracerSpan sofaTracerSpan = activeTracerSpan.pollFirst(); 105 | if (activeTracerSpan.size() <= 0) { 106 | activeTracerSpans.remove(); 107 | } 108 | return sofaTracerSpan; 109 | } 110 | 111 | @Override 112 | public int getThreadLocalSpanSize() { 113 | ActiveTracerSpan activeTracerSpan = activeTracerSpans.get(); 114 | return activeTracerSpan == null ? 0 : activeTracerSpan.size(); 115 | } 116 | 117 | @Override 118 | public boolean isEmpty() { 119 | ActiveTracerSpan activeTracerSpan = activeTracerSpans.get(); 120 | return activeTracerSpan == null || activeTracerSpan.size() <= 0; 121 | } 122 | 123 | @Override 124 | public void clear() { 125 | activeTracerSpans.remove(); 126 | } 127 | 128 | 129 | @Override 130 | public void filterOption(SofaTracerSpanContext sofaTracerSpanContext, InterceptContext interceptContext) { 131 | this.traceContextFilterOption.filterOption(sofaTracerSpanContext, interceptContext); 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/enhance/DefaultEnhanceFactory.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.enhance; 2 | 3 | import cn.bytes1024.hound.collect.enhance.rule.DefaultEnhanceRuleChain; 4 | import cn.bytes1024.hound.collect.enhance.rule.EnhanceRule; 5 | import cn.bytes1024.hound.plugins.define.EnhanceContext; 6 | import lombok.extern.slf4j.Slf4j; 7 | import net.bytebuddy.dynamic.DynamicType; 8 | import org.apache.commons.collections.CollectionUtils; 9 | 10 | import java.util.List; 11 | 12 | /** 13 | * 默认method delegation构建工厂 14 | * 15 | * @author 16 | */ 17 | @Slf4j 18 | public class DefaultEnhanceFactory implements EnhanceFactory { 19 | 20 | 21 | private List enhanceRules; 22 | 23 | private InterceptorFactory interceptorFactory; 24 | 25 | 26 | public DefaultEnhanceFactory(List enhanceRules, InterceptorFactory interceptorFactory) { 27 | this.enhanceRules = enhanceRules; 28 | this.interceptorFactory = interceptorFactory; 29 | } 30 | 31 | 32 | private DynamicType.Builder enhance(DynamicType.Builder builder, EnhanceContext enhanceContext) { 33 | return new DefaultEnhanceRuleChain(this.enhanceRules).enhance(builder, new DefaultEnhanceRuleOption(enhanceContext, this.interceptorFactory)); 34 | } 35 | 36 | /** 37 | * 增强处理 38 | * 39 | * @param builder : 40 | * @param enhanceContexts : 41 | * @param index : 42 | * @return : net.bytebuddy.dynamic.DynamicType.Builder 43 | * @author 江浩 44 | */ 45 | private DynamicType.Builder enhance(DynamicType.Builder builder, List enhanceContexts, int index) { 46 | //#to long bug ? 47 | if (CollectionUtils.isEmpty(enhanceContexts) || index >= enhanceContexts.size()) { 48 | return builder; 49 | } 50 | EnhanceContext enhanceContext = enhanceContexts.get(index); 51 | builder = this.enhance(builder, enhanceContext); 52 | return enhance(builder, enhanceContexts, ++index); 53 | } 54 | 55 | @Override 56 | public DynamicType.Builder enhance(DynamicType.Builder builder, List enhanceContexts) { 57 | 58 | return this.enhance(builder, enhanceContexts, 0); 59 | } 60 | 61 | 62 | /** 63 | * callback 64 | */ 65 | public static class DefaultEnhanceRuleOption implements EnhanceRule.EnhanceRuleOption { 66 | 67 | private InterceptorFactory interceptorFactory; 68 | 69 | private EnhanceContext enhanceContext; 70 | 71 | 72 | public DefaultEnhanceRuleOption(EnhanceContext enhanceContext, 73 | InterceptorFactory interceptorFactory) { 74 | this.interceptorFactory = interceptorFactory; 75 | this.enhanceContext = enhanceContext; 76 | } 77 | 78 | @Override 79 | public InterceptorFactory getInterceptorFactory() { 80 | return this.interceptorFactory; 81 | } 82 | 83 | @Override 84 | public EnhanceContext getEnhanceContext() { 85 | return this.enhanceContext; 86 | } 87 | 88 | 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/enhance/DefaultEnhanceRuleChainProxy.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.enhance; 2 | 3 | import cn.bytes1024.hound.collect.enhance.rule.DefaultEnhanceRuleChain; 4 | import cn.bytes1024.hound.collect.enhance.rule.EnhanceRule; 5 | import cn.bytes1024.hound.collect.enhance.rule.EnhanceRuleChain; 6 | import cn.bytes1024.hound.plugins.define.EnhanceContext; 7 | import lombok.extern.slf4j.Slf4j; 8 | import net.bytebuddy.dynamic.DynamicType; 9 | import org.apache.commons.collections.CollectionUtils; 10 | 11 | import java.util.List; 12 | 13 | /** 14 | * 增强链路代理 15 | * 16 | * @author 17 | */ 18 | @Slf4j 19 | public class DefaultEnhanceRuleChainProxy implements EnhanceRuleChainProxy, EnhanceRuleChain { 20 | 21 | 22 | private List enhanceRules; 23 | 24 | private InterceptorFactory interceptorFactory; 25 | 26 | public DefaultEnhanceRuleChainProxy(List enhanceRules, InterceptorFactory interceptorFactory) { 27 | this.enhanceRules = enhanceRules; 28 | this.interceptorFactory = interceptorFactory; 29 | } 30 | 31 | 32 | private DynamicType.Builder enhance(DynamicType.Builder builder, EnhanceContext enhanceContext) { 33 | return this.enhance(builder, new DefaultEnhanceRuleOption(enhanceContext, this.interceptorFactory)); 34 | } 35 | 36 | /** 37 | * 增强处理 38 | * 39 | * @param builder : 40 | * @param enhanceContexts : 41 | * @param index : 42 | * @return : net.bytebuddy.dynamic.DynamicType.Builder 43 | * @author 江浩 44 | */ 45 | private DynamicType.Builder enhance(DynamicType.Builder builder, List enhanceContexts, int index) { 46 | //#to long bug ? 47 | if (CollectionUtils.isEmpty(enhanceContexts) || index >= enhanceContexts.size()) { 48 | return builder; 49 | } 50 | EnhanceContext enhanceContext = enhanceContexts.get(index); 51 | builder = this.enhance(builder, enhanceContext); 52 | return enhance(builder, enhanceContexts, ++index); 53 | } 54 | 55 | @Override 56 | public DynamicType.Builder enhance(DynamicType.Builder builder, List enhanceContexts) { 57 | 58 | return this.enhance(builder, enhanceContexts, 0); 59 | } 60 | 61 | @Override 62 | public DynamicType.Builder enhance(DynamicType.Builder builder, EnhanceRule.EnhanceRuleOption enhanceRuleOption) { 63 | return new DefaultEnhanceRuleChain(this.enhanceRules).enhance(builder, enhanceRuleOption); 64 | } 65 | 66 | 67 | /** 68 | * callback 69 | */ 70 | public static class DefaultEnhanceRuleOption implements EnhanceRule.EnhanceRuleOption { 71 | 72 | private InterceptorFactory interceptorFactory; 73 | 74 | private EnhanceContext enhanceContext; 75 | 76 | public DefaultEnhanceRuleOption(EnhanceContext enhanceContext, 77 | InterceptorFactory interceptorFactory) { 78 | this.interceptorFactory = interceptorFactory; 79 | this.enhanceContext = enhanceContext; 80 | } 81 | 82 | @Override 83 | public InterceptorFactory getInterceptorFactory() { 84 | return this.interceptorFactory; 85 | } 86 | 87 | @Override 88 | public EnhanceContext getEnhanceContext() { 89 | return this.enhanceContext; 90 | } 91 | 92 | 93 | } 94 | 95 | } 96 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/enhance/DefaultInterceptorFactory.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.enhance; 2 | 3 | import cn.bytes1024.hound.collect.exception.ConstructorException; 4 | import cn.bytes1024.hound.plugins.define.EnhanceContext; 5 | import cn.bytes1024.hound.plugins.define.TraceContext; 6 | import cn.bytes1024.hound.plugins.define.interceptor.InterceptorPluginAware; 7 | import lombok.extern.slf4j.Slf4j; 8 | import org.apache.commons.lang3.ClassUtils; 9 | 10 | import java.lang.reflect.Constructor; 11 | import java.lang.reflect.Method; 12 | import java.util.Objects; 13 | 14 | /** 15 | * 默认拦截器处理 16 | * 17 | * @author 江浩 18 | */ 19 | @Slf4j 20 | public class DefaultInterceptorFactory implements InterceptorFactory { 21 | 22 | private TraceContext traceContext; 23 | 24 | public DefaultInterceptorFactory(TraceContext traceContext) { 25 | this.traceContext = traceContext; 26 | } 27 | 28 | @Override 29 | public Object newInterceptorObject(EnhanceContext enhanceContext) { 30 | 31 | final String interceptorClassName = enhanceContext.getInterceptorClassName(); 32 | final String pluginName = enhanceContext.getRefPluginName(); 33 | try { 34 | Class aClass = ClassUtils.getClass(interceptorClassName); 35 | Object object = null; 36 | try { 37 | Constructor constructor = aClass.getDeclaredConstructor(TraceContext.class); 38 | object = constructor.newInstance(this.traceContext); 39 | invokeMethod(pluginName, aClass, object); 40 | 41 | } catch (NoSuchMethodException ignored) { 42 | } 43 | if (Objects.isNull(object)) { 44 | throw builder(interceptorClassName); 45 | } 46 | 47 | return object; 48 | } catch (Exception e) { 49 | throw builder(interceptorClassName); 50 | } 51 | } 52 | 53 | /** 54 | * invoker {@link InterceptorPluginAware#defineName(String)} 55 | * 56 | * @param pluginName : 57 | * @param aClass : 58 | * @param object : 59 | * @return : void 60 | * @author 江浩 61 | */ 62 | private void invokeMethod(String pluginName, Class aClass, Object object) { 63 | 64 | if (InterceptorPluginAware.class.isAssignableFrom(aClass)) { 65 | try { 66 | Method method = aClass.getMethod("defineName", String.class); 67 | method.invoke(object, pluginName); 68 | } catch (Exception ignored) { 69 | } 70 | } 71 | } 72 | 73 | private ConstructorException builder(String className) { 74 | return new ConstructorException(String.format("AroundInterceptor Constructor format: %s", 75 | className + "(com.support.monitor.agent.core.context.trace.TraceContext arg0)")); 76 | } 77 | 78 | 79 | } 80 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/enhance/EnhanceFactory.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.enhance; 2 | 3 | import cn.bytes1024.hound.plugins.define.EnhanceContext; 4 | import net.bytebuddy.dynamic.DynamicType; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | * 增强实现工厂 10 | * 11 | * @author 江浩 12 | */ 13 | public interface EnhanceFactory { 14 | 15 | /** 16 | * 增强实现 17 | * @param builder : 18 | * @param enhanceContexts : 19 | * @return : net.bytebuddy.dynamic.DynamicType.Builder 20 | * @author 江浩 21 | */ 22 | DynamicType.Builder enhance(DynamicType.Builder builder, List enhanceContexts); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/enhance/EnhanceRuleChainProxy.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.enhance; 2 | 3 | import cn.bytes1024.hound.collect.enhance.rule.EnhanceRule; 4 | import cn.bytes1024.hound.plugins.define.EnhanceContext; 5 | import net.bytebuddy.dynamic.DynamicType; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | * 增强规则链路代理 11 | *

12 | * 1.该代理实现最终只会执行{@link cn.bytes1024.hound.collect.enhance.rule.EnhanceRuleChain#enhance(DynamicType.Builder, EnhanceRule.EnhanceRuleOption)} 13 | *

14 | * 15 | * @author 江浩 16 | */ 17 | public interface EnhanceRuleChainProxy { 18 | 19 | /** 20 | * 增强实现 21 | * 22 | * @param builder : 23 | * @param enhanceContexts : 24 | * @return : net.bytebuddy.dynamic.DynamicType.Builder 25 | * @author 江浩 26 | */ 27 | DynamicType.Builder enhance(DynamicType.Builder builder, List enhanceContexts); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/enhance/InterceptorFactory.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.enhance; 2 | 3 | 4 | import cn.bytes1024.hound.plugins.define.EnhanceContext; 5 | 6 | /** 7 | * @author admin 8 | */ 9 | public interface InterceptorFactory { 10 | 11 | 12 | /** 13 | * 创建拦截器实体 14 | * 15 | * @param className : 16 | * @return : java.lang.Object 17 | * @author 江浩 18 | */ 19 | Object newInterceptorObject(EnhanceContext className); 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/enhance/delegation/ConstructorInterceptDelegation.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.enhance.delegation; 2 | 3 | import cn.bytes1024.hound.plugins.define.EnhancedDefine; 4 | import cn.bytes1024.hound.plugins.define.interceptor.ConstructorInterceptor; 5 | import lombok.extern.slf4j.Slf4j; 6 | import net.bytebuddy.implementation.bind.annotation.AllArguments; 7 | import net.bytebuddy.implementation.bind.annotation.RuntimeType; 8 | import net.bytebuddy.implementation.bind.annotation.This; 9 | 10 | /** 11 | * @author 江浩 12 | */ 13 | @Slf4j 14 | public class ConstructorInterceptDelegation { 15 | 16 | private ConstructorInterceptor constructorInterceptor; 17 | 18 | public ConstructorInterceptDelegation(ConstructorInterceptor constructorInterceptor) { 19 | this.constructorInterceptor = constructorInterceptor; 20 | } 21 | 22 | @RuntimeType 23 | public void intercept(@This Object obj, 24 | @AllArguments Object[] allArguments) { 25 | try { 26 | EnhancedDefine targetObject = (EnhancedDefine) obj; 27 | constructorInterceptor.onConstruct(targetObject, allArguments); 28 | } catch (Throwable t) { 29 | log.error("ConstructorInter failure.", t); 30 | } 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/enhance/delegation/MethodsInterceptWithDelegation.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.enhance.delegation; 2 | 3 | import cn.bytes1024.hound.plugins.define.InterceptContext; 4 | import cn.bytes1024.hound.plugins.define.interceptor.MethodAroundInterceptor; 5 | import lombok.extern.slf4j.Slf4j; 6 | import net.bytebuddy.implementation.bind.annotation.*; 7 | 8 | import java.lang.reflect.Method; 9 | import java.util.concurrent.Callable; 10 | 11 | /** 12 | * 方法拦截 13 | * 14 | * @author 江浩 15 | */ 16 | @Slf4j 17 | public class MethodsInterceptWithDelegation { 18 | 19 | private MethodAroundInterceptor methodAroundInterceptor; 20 | 21 | public MethodsInterceptWithDelegation(MethodAroundInterceptor methodAroundInterceptor) { 22 | this.methodAroundInterceptor = methodAroundInterceptor; 23 | } 24 | 25 | @RuntimeType 26 | public Object intercept( 27 | @This Object object, 28 | @AllArguments Object[] allArguments, 29 | @SuperCall Callable callable, 30 | @Origin Method method) { 31 | 32 | Object result = null; 33 | Throwable throwable = null; 34 | InterceptContext interceptContext = InterceptContext.builder() 35 | .target(object) 36 | .method(method) 37 | .args(allArguments) 38 | .build(); 39 | try { 40 | methodAroundInterceptor.before(interceptContext); 41 | result = callable.call(); 42 | } catch (Exception e) { 43 | throwable = e; 44 | } finally { 45 | interceptContext.setResult(result); 46 | interceptContext.setThrowable(throwable); 47 | methodAroundInterceptor.after(interceptContext); 48 | } 49 | 50 | return result; 51 | } 52 | 53 | 54 | } 55 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/enhance/rule/AbstractEnhanceRule.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.enhance.rule; 2 | 3 | import cn.bytes1024.hound.collect.enhance.InterceptorFactory; 4 | import cn.bytes1024.hound.commons.util.RefClassUtil; 5 | import cn.bytes1024.hound.plugins.define.EnhanceContext; 6 | import lombok.extern.slf4j.Slf4j; 7 | import net.bytebuddy.dynamic.DynamicType; 8 | 9 | import java.util.Objects; 10 | 11 | /** 12 | * 增强链路默认增强对象应该参与所有的增强方式 13 | *

14 | * 具体是否增强应该由子类自行判断 15 | *

16 | * 17 | * @author 江浩 18 | */ 19 | @Slf4j 20 | public abstract class AbstractEnhanceRule implements EnhanceRule { 21 | 22 | private Class rClass; 23 | 24 | 25 | public AbstractEnhanceRule() { 26 | rClass = RefClassUtil.getSuperClassGenricType(this.getClass(), 0); 27 | } 28 | 29 | @Override 30 | public DynamicType.Builder enhance(EnhanceRuleChain chan, DynamicType.Builder builder, EnhanceRuleOption enhanceRuleOption) { 31 | if (Objects.isNull(enhanceRuleOption)) { 32 | return builder; 33 | } 34 | 35 | try { 36 | InterceptorFactory interceptorFactory = enhanceRuleOption.getInterceptorFactory(); 37 | EnhanceContext enhanceContext = enhanceRuleOption.getEnhanceContext(); 38 | 39 | Object object = interceptorFactory.newInterceptorObject(enhanceContext); 40 | 41 | if (rClass.isAssignableFrom(object.getClass()) || need()) { 42 | DynamicType.Builder newBuilder = this.enhanceDefine(builder, (R) object, enhanceContext); 43 | if (!Objects.isNull(newBuilder)) { 44 | builder = newBuilder; 45 | } 46 | } 47 | } catch (Exception e) { 48 | log.error("enhance error : {}", e); 49 | } 50 | 51 | return chan.enhance(builder, enhanceRuleOption); 52 | } 53 | 54 | /** 55 | * 默认增强方式 56 | * 57 | * @param builder : 58 | * @param interceptPoint 59 | * @param enhanceContext : 60 | * @return : net.bytebuddy.dynamic.DynamicType.Builder 61 | * @author 江浩 62 | */ 63 | protected abstract DynamicType.Builder enhanceDefine(DynamicType.Builder builder, R interceptPoint, EnhanceContext enhanceContext); 64 | } 65 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/enhance/rule/DefaultEnhanceRuleChain.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.enhance.rule; 2 | 3 | import net.bytebuddy.dynamic.DynamicType; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * 默认增强实现链路 9 | * 10 | * @author 江浩 11 | */ 12 | public class DefaultEnhanceRuleChain implements EnhanceRuleChain { 13 | 14 | private int index; 15 | private List enhanceRules; 16 | 17 | public DefaultEnhanceRuleChain(List enhanceRules) { 18 | this.enhanceRules = enhanceRules; 19 | } 20 | 21 | public DefaultEnhanceRuleChain(DefaultEnhanceRuleChain parent, int index) { 22 | this.enhanceRules = parent.enhanceRules; 23 | this.index = index; 24 | } 25 | 26 | 27 | private DefaultEnhanceRuleChain chan() { 28 | return new DefaultEnhanceRuleChain(this, this.index + 1); 29 | } 30 | 31 | 32 | @Override 33 | public DynamicType.Builder enhance(DynamicType.Builder builder, EnhanceRule.EnhanceRuleOption enhanceRuleOption) { 34 | if (index < this.enhanceRules.size()) { 35 | return this.enhanceRules.get(index).enhance(chan(), builder, enhanceRuleOption); 36 | } 37 | //default builder 38 | return builder; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/enhance/rule/EnhanceConstructorRule.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.enhance.rule; 2 | 3 | import cn.bytes1024.hound.collect.enhance.delegation.ConstructorInterceptDelegation; 4 | import cn.bytes1024.hound.plugins.define.EnhanceContext; 5 | import cn.bytes1024.hound.plugins.define.interceptor.ConstructorInterceptor; 6 | import net.bytebuddy.dynamic.DynamicType; 7 | import net.bytebuddy.implementation.MethodDelegation; 8 | import net.bytebuddy.implementation.SuperMethodCall; 9 | 10 | /** 11 | * 构造器增强 12 | * 13 | * @author 江浩 14 | */ 15 | public class EnhanceConstructorRule extends AbstractEnhanceRule { 16 | 17 | @Override 18 | protected DynamicType.Builder enhanceDefine(DynamicType.Builder builder, ConstructorInterceptor interceptPoint, EnhanceContext enhanceContext) { 19 | 20 | MethodDelegation methodDelegation = constructorDelegation(interceptPoint); 21 | return builder.constructor(enhanceContext.getMethodDescription()).intercept(SuperMethodCall.INSTANCE 22 | .andThen(methodDelegation) 23 | ); 24 | } 25 | 26 | private MethodDelegation constructorDelegation(ConstructorInterceptor constructorInterceptor) { 27 | return 28 | MethodDelegation.withDefaultConfiguration() 29 | .to(new ConstructorInterceptDelegation(constructorInterceptor)); 30 | } 31 | 32 | 33 | } 34 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/enhance/rule/EnhanceMethodRule.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.enhance.rule; 2 | 3 | import cn.bytes1024.hound.collect.enhance.delegation.MethodsInterceptWithDelegation; 4 | import cn.bytes1024.hound.plugins.define.EnhanceContext; 5 | import cn.bytes1024.hound.plugins.define.interceptor.MethodAroundInterceptor; 6 | import net.bytebuddy.dynamic.DynamicType; 7 | import net.bytebuddy.implementation.MethodDelegation; 8 | import net.bytebuddy.matcher.ElementMatchers; 9 | 10 | import static net.bytebuddy.matcher.ElementMatchers.isStatic; 11 | 12 | /** 13 | * 方法增强的处理方式 14 | * 15 | * @author 江浩 16 | */ 17 | public class EnhanceMethodRule extends AbstractEnhanceRule { 18 | 19 | 20 | @Override 21 | protected DynamicType.Builder enhanceDefine(DynamicType.Builder builder, MethodAroundInterceptor 22 | interceptPoint, EnhanceContext enhanceContext) { 23 | MethodDelegation methodDelegation = methodsWithDelegation(interceptPoint); 24 | return builder.method(ElementMatchers.not(isStatic()).and(enhanceContext.getMethodDescription())).intercept(methodDelegation); 25 | 26 | } 27 | 28 | /** 29 | * 方法委托处理 30 | * 31 | * @param methodsAroundInterceptor : 32 | * @return : net.bytebuddy.implementation.MethodDelegation 33 | * @author 江浩 34 | */ 35 | private MethodDelegation methodsWithDelegation(MethodAroundInterceptor methodsAroundInterceptor) { 36 | return MethodDelegation.withDefaultConfiguration() 37 | // .withBinders( 38 | // Morph.Binder.install(DefaultCallable.class) 39 | // ) 40 | .to(new MethodsInterceptWithDelegation(methodsAroundInterceptor)); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/enhance/rule/EnhanceRule.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.enhance.rule; 2 | 3 | import cn.bytes1024.hound.collect.enhance.InterceptorFactory; 4 | import cn.bytes1024.hound.plugins.define.EnhanceContext; 5 | import net.bytebuddy.dynamic.DynamicType; 6 | 7 | /** 8 | * 规则信息 9 | * 10 | * @author 江浩 11 | */ 12 | public interface EnhanceRule { 13 | 14 | 15 | /** 16 | * 标识 17 | * 18 | * @author 江浩 19 | */ 20 | class Key { 21 | public static final String SOURCE = "source"; 22 | public static final String METHOD = "method"; 23 | public static final String CONSTRUCTOR = "constructor"; 24 | public static final String STATIC_METHOD = "static_method"; 25 | } 26 | 27 | /** 28 | * 增强规则执行设置信息 29 | * 30 | * @author 江浩 31 | */ 32 | interface EnhanceRuleOption { 33 | 34 | /** 35 | * 拦截器工厂 36 | * 37 | * @return : cn.bytes1024.hound.collect.enhance.InterceptorFactory 38 | * @author 江浩 39 | */ 40 | InterceptorFactory getInterceptorFactory(); 41 | 42 | /** 43 | * 获取增强上线文环境 44 | * 45 | * @return : cn.bytes1024.hound.plugins.define.EnhanceContext 46 | * @author 江浩 47 | */ 48 | EnhanceContext getEnhanceContext(); 49 | 50 | } 51 | 52 | 53 | /** 54 | * 增强规则实现 55 | * 56 | * @param chan : 57 | * @param builder 58 | * @param enhanceRuleOption : 59 | * @return : net.bytebuddy.dynamic.DynamicType.Builder 60 | * @author 江浩 61 | */ 62 | DynamicType.Builder enhance(EnhanceRuleChain chan, 63 | DynamicType.Builder builder, 64 | EnhanceRuleOption enhanceRuleOption); 65 | 66 | /** 67 | * 过滤掉 68 | * 69 | * @return : boolean 70 | * @author 江浩 71 | */ 72 | default boolean need() { 73 | return false; 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/enhance/rule/EnhanceRuleChain.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.enhance.rule; 2 | 3 | import net.bytebuddy.dynamic.DynamicType; 4 | 5 | /** 6 | * @author 江浩 7 | */ 8 | public interface EnhanceRuleChain { 9 | 10 | /** 11 | * 增强实现 12 | * 13 | * @param builder 14 | * @param enhanceRuleOption : 15 | * @return : net.bytebuddy.dynamic.DynamicType.Builder 16 | * @author 江浩 17 | */ 18 | DynamicType.Builder enhance(DynamicType.Builder builder, EnhanceRule.EnhanceRuleOption enhanceRuleOption); 19 | } 20 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/enhance/rule/EnhanceSourceRule.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.enhance.rule; 2 | 3 | import cn.bytes1024.hound.plugins.define.EnhanceContext; 4 | import cn.bytes1024.hound.plugins.define.EnhancedDefine; 5 | import com.alipay.common.tracer.core.span.SofaTracerSpan; 6 | import net.bytebuddy.description.type.TypeList; 7 | import net.bytebuddy.dynamic.DynamicType; 8 | import net.bytebuddy.implementation.FieldAccessor; 9 | 10 | import java.util.Objects; 11 | 12 | import static net.bytebuddy.jar.asm.Opcodes.ACC_PRIVATE; 13 | import static net.bytebuddy.jar.asm.Opcodes.ACC_VOLATILE; 14 | 15 | /** 16 | * @author 江浩 17 | */ 18 | public class EnhanceSourceRule extends AbstractEnhanceRule { 19 | 20 | private static final String ENHANCE_CLASS_FIELD_NAME = "_$ENHANCE_CLASS_FIELD_NAME"; 21 | 22 | @Override 23 | protected DynamicType.Builder enhanceDefine(DynamicType.Builder builder, Object interceptPoint, EnhanceContext enhanceContext) { 24 | 25 | TypeList.Generic generics = builder.make().getTypeDescription().getInterfaces(); 26 | if (!Objects.isNull(generics) && !generics.isEmpty()) { 27 | for (int i = 0; i < generics.size(); i++) { 28 | if (generics.get(i).getTypeName().equalsIgnoreCase(EnhancedDefine.class.getName())) { 29 | return builder; 30 | } 31 | } 32 | } 33 | 34 | //EnhanceDefine handler param of TraceIdRecorder 35 | //添加字段,实现接口 36 | return builder.defineField(ENHANCE_CLASS_FIELD_NAME, SofaTracerSpan.class, ACC_PRIVATE | ACC_VOLATILE) 37 | .implement(EnhancedDefine.class) 38 | .intercept(FieldAccessor.ofField(ENHANCE_CLASS_FIELD_NAME)); 39 | 40 | } 41 | 42 | @Override 43 | public boolean need() { 44 | return true; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/enhance/rule/EnhanceStaticMethodRule.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.enhance.rule; 2 | 3 | import cn.bytes1024.hound.plugins.define.EnhanceContext; 4 | import cn.bytes1024.hound.plugins.define.interceptor.StaticMethodAroundInterceptor; 5 | import net.bytebuddy.dynamic.DynamicType; 6 | 7 | /** 8 | * 静态方法增强 9 | * 10 | * @author 江浩 11 | */ 12 | public class EnhanceStaticMethodRule extends AbstractEnhanceRule { 13 | 14 | @Override 15 | protected DynamicType.Builder enhanceDefine(DynamicType.Builder builder, StaticMethodAroundInterceptor interceptPoint, EnhanceContext enhanceContext) { 16 | 17 | //TODO 18 | return null; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/exception/BaseException.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.exception; 2 | 3 | import lombok.Data; 4 | 5 | /** 6 | * @author 江浩 7 | */ 8 | @Data 9 | public class BaseException extends RuntimeException { 10 | 11 | private String code; 12 | 13 | private String message; 14 | 15 | public BaseException(String message) { 16 | this.message = message; 17 | } 18 | 19 | public BaseException(String code, String message) { 20 | super(message); 21 | this.code = code; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/exception/ConstructorException.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.exception; 2 | 3 | public class ConstructorException extends BaseException { 4 | public ConstructorException(String message) { 5 | super(message); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/exception/NotFoundPluginLoader.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.exception; 2 | 3 | public class NotFoundPluginLoader extends BaseException { 4 | public NotFoundPluginLoader(String message) { 5 | super(message); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/module/CollectModuleFactory.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.module; 2 | 3 | import cn.bytes1024.hound.collect.context.ApplicationContext; 4 | import com.google.inject.AbstractModule; 5 | import com.google.inject.Guice; 6 | import com.google.inject.Injector; 7 | import com.google.inject.Stage; 8 | import com.google.inject.util.Modules; 9 | import lombok.extern.slf4j.Slf4j; 10 | import org.apache.commons.lang3.StringUtils; 11 | 12 | import java.lang.instrument.Instrumentation; 13 | import java.util.Arrays; 14 | import java.util.HashSet; 15 | import java.util.Objects; 16 | import java.util.Set; 17 | 18 | import static com.google.inject.name.Names.named; 19 | 20 | /** 21 | * 收集器模块工厂 22 | * 23 | * @author 江浩 24 | */ 25 | @Slf4j 26 | public class CollectModuleFactory implements ModuleFactory { 27 | 28 | private Set refModules = new HashSet<>(); 29 | 30 | private Injector injector; 31 | 32 | @Override 33 | public ModuleFactory add(com.google.inject.Module... modules) { 34 | if (Objects.isNull(modules)) { 35 | return this; 36 | } 37 | this.refModules.addAll(Arrays.asList(modules)); 38 | return this; 39 | } 40 | 41 | @Override 42 | public void init(String args, Instrumentation instrumentation) { 43 | 44 | defineModule(args, instrumentation); 45 | 46 | log.debug("init module length: {}", refModules.size()); 47 | this.injector = Guice.createInjector(Stage.PRODUCTION, Modules.combine(refModules)); 48 | 49 | this.injector.getInstance(ApplicationContext.class).start(); 50 | } 51 | 52 | /** 53 | * 模块信息注入 54 | * 55 | * @return : void 56 | * @author 江浩 57 | */ 58 | private void defineModule(String args, Instrumentation instrumentation) { 59 | 60 | com.google.inject.Module module = new AbstractModule() { 61 | @Override 62 | protected void configure() { 63 | bind(Instrumentation.class).toInstance(instrumentation); 64 | bind(String.class).annotatedWith(named("args")).toInstance(StringUtils.isBlank(args) ? "" : args); 65 | } 66 | }; 67 | 68 | DefineModule defineModule = new DefineModule(); 69 | 70 | this.add(module, defineModule); 71 | } 72 | 73 | 74 | } 75 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/module/DefineModule.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.module; 2 | 3 | import cn.bytes1024.hound.collect.context.ApplicationContext; 4 | import cn.bytes1024.hound.collect.enhance.EnhanceRuleChainProxy; 5 | import cn.bytes1024.hound.collect.enhance.InterceptorFactory; 6 | import cn.bytes1024.hound.collect.enhance.rule.*; 7 | import cn.bytes1024.hound.collect.module.providers.*; 8 | import cn.bytes1024.hound.collect.processor.AgentCollectProcessor; 9 | import cn.bytes1024.hound.collect.processor.MetricsCollectProcessor; 10 | import cn.bytes1024.hound.collect.processor.Processor; 11 | import cn.bytes1024.hound.commons.option.ConfigOption; 12 | import cn.bytes1024.hound.plugins.define.TraceContext; 13 | import cn.bytes1024.hound.plugins.define.filter.DefaultTraceContextFilterOption; 14 | import cn.bytes1024.hound.plugins.define.filter.TraceContextFilterOption; 15 | import cn.bytes1024.hound.transfers.define.buffer.TransferBuffer; 16 | import com.alipay.common.tracer.core.SofaTracer; 17 | import com.alipay.common.tracer.core.reporter.facade.Reporter; 18 | import com.google.inject.AbstractModule; 19 | import com.google.inject.Scopes; 20 | import com.google.inject.name.Names; 21 | import net.bytebuddy.agent.builder.AgentBuilder; 22 | 23 | /** 24 | * 初始定义的模块信息 25 | * 26 | * @author 江浩 27 | */ 28 | public class DefineModule extends AbstractModule { 29 | 30 | 31 | public static class Alias { 32 | public static final String AGENT = "agent"; 33 | public static final String METRICS = "metrics"; 34 | } 35 | 36 | 37 | @Override 38 | protected void configure() { 39 | bind(ApplicationContext.class).toProvider(ApplicationContextProvider.class).in(Scopes.SINGLETON); 40 | bind(ConfigOption.class).toProvider(AgentOptionProvider.class).in(Scopes.SINGLETON); 41 | 42 | bind(AgentBuilder.class).to(AgentBuilder.Default.class).in(Scopes.SINGLETON); 43 | bind(Processor.class).annotatedWith(Names.named(Alias.AGENT)) 44 | .to(AgentCollectProcessor.class).in(Scopes.SINGLETON); 45 | 46 | bind(Processor.class).annotatedWith(Names.named(Alias.METRICS)) 47 | .to(MetricsCollectProcessor.class).in(Scopes.SINGLETON); 48 | 49 | bind(Reporter.class).toProvider(ServerReporterProvider.class).in(Scopes.SINGLETON); 50 | 51 | bind(TransferBuffer.class).toProvider(TransferBufferProvider.class).in(Scopes.SINGLETON); 52 | bind(SofaTracer.class).toProvider(SofaTracerProvider.class).in(Scopes.SINGLETON); 53 | 54 | bind(TraceContextFilterOption.class).to(DefaultTraceContextFilterOption.class).in(Scopes.SINGLETON); 55 | bind(TraceContext.class).toProvider(TraceContextProvider.class).in(Scopes.SINGLETON); 56 | bind(InterceptorFactory.class).toProvider(InterceptorFactoryProvider.class).in(Scopes.SINGLETON); 57 | bind(EnhanceRuleChainProxy.class).toProvider(EnhanceRuleChainProxyProvider.class).in(Scopes.SINGLETON); 58 | 59 | bind(EnhanceRule.class).annotatedWith(Names.named(EnhanceRule.Key.METHOD)).to(EnhanceMethodRule.class).in(Scopes.SINGLETON); 60 | bind(EnhanceRule.class).annotatedWith(Names.named(EnhanceRule.Key.CONSTRUCTOR)).to(EnhanceConstructorRule.class).in(Scopes.SINGLETON); 61 | bind(EnhanceRule.class).annotatedWith(Names.named(EnhanceRule.Key.STATIC_METHOD)).to(EnhanceStaticMethodRule.class).in(Scopes.SINGLETON); 62 | bind(EnhanceRule.class).annotatedWith(Names.named(EnhanceRule.Key.SOURCE)).to(EnhanceSourceRule.class).in(Scopes.SINGLETON); 63 | 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/module/ModuleFactory.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.module; 2 | 3 | import com.google.inject.Module; 4 | 5 | import java.lang.instrument.Instrumentation; 6 | 7 | /** 8 | * 模块管理 9 | * @author 江浩 10 | */ 11 | public interface ModuleFactory { 12 | 13 | 14 | /** 15 | * 添加管理模块信息 16 | * @param modules : 17 | * @return : void 18 | * @author 江浩 19 | */ 20 | ModuleFactory add(Module ... modules); 21 | 22 | /** 23 | * 模块的初始加载 24 | * @param args : 25 | * @param instrumentation : 26 | * @return : void 27 | * @author 江浩 28 | */ 29 | void init(String args, Instrumentation instrumentation); 30 | 31 | } 32 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/module/providers/AgentOptionProvider.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.module.providers; 2 | 3 | import cn.bytes1024.hound.commons.option.ConfigOption; 4 | import cn.bytes1024.hound.commons.option.DefaultConfigOption; 5 | import com.google.inject.Inject; 6 | import com.google.inject.Provider; 7 | import com.google.inject.name.Named; 8 | 9 | /** 10 | * @author 江浩 11 | */ 12 | public class AgentOptionProvider implements Provider { 13 | 14 | private String args; 15 | 16 | @Inject 17 | public AgentOptionProvider(@Named("args") String args) { 18 | this.args = args; 19 | } 20 | 21 | @Override 22 | public ConfigOption get() { 23 | return new DefaultConfigOption(this.args); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/module/providers/ApplicationContextProvider.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.module.providers; 2 | 3 | import cn.bytes1024.hound.collect.context.ApplicationContext; 4 | import cn.bytes1024.hound.collect.context.DefaultApplicationContext; 5 | import cn.bytes1024.hound.collect.module.DefineModule; 6 | import cn.bytes1024.hound.collect.processor.Processor; 7 | import cn.bytes1024.hound.commons.option.ConfigOption; 8 | import com.google.inject.Inject; 9 | import com.google.inject.Provider; 10 | import com.google.inject.name.Named; 11 | 12 | import java.util.ArrayList; 13 | import java.util.List; 14 | 15 | /** 16 | * @author 江浩 17 | */ 18 | public class ApplicationContextProvider implements Provider { 19 | 20 | private ConfigOption configOption; 21 | 22 | /** 23 | * 加载处理器 24 | * 25 | * @author 江浩 26 | */ 27 | private List processors = new ArrayList<>(); 28 | 29 | @Inject 30 | public ApplicationContextProvider(ConfigOption configOption, 31 | @Named(DefineModule.Alias.AGENT) Processor agentProcessor, 32 | @Named(DefineModule.Alias.METRICS) Processor metricsProcesser 33 | ) { 34 | this.configOption = configOption; 35 | processors.add(agentProcessor); 36 | processors.add(metricsProcesser); 37 | } 38 | 39 | @Override 40 | public ApplicationContext get() { 41 | return new DefaultApplicationContext(this.configOption, this.processors); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/module/providers/EnhanceFactoryProvider.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.module.providers; 2 | 3 | import cn.bytes1024.hound.collect.enhance.DefaultEnhanceFactory; 4 | import cn.bytes1024.hound.collect.enhance.EnhanceFactory; 5 | import cn.bytes1024.hound.collect.enhance.InterceptorFactory; 6 | import cn.bytes1024.hound.collect.enhance.rule.EnhanceRule; 7 | import com.google.common.collect.Lists; 8 | import com.google.inject.Inject; 9 | import com.google.inject.Provider; 10 | import com.google.inject.name.Named; 11 | 12 | import java.util.List; 13 | 14 | /** 15 | * 增强处理工厂 16 | * 17 | * @author 江浩 18 | */ 19 | public class EnhanceFactoryProvider implements Provider { 20 | 21 | 22 | private List enhanceRules = Lists.newArrayList(); 23 | 24 | private InterceptorFactory interceptorFactory; 25 | 26 | @Inject 27 | public EnhanceFactoryProvider( 28 | InterceptorFactory interceptorFactory, 29 | @Named(EnhanceRule.Key.METHOD) EnhanceRule enhanceMethod, 30 | @Named(EnhanceRule.Key.CONSTRUCTOR) EnhanceRule enhanceConstructor, 31 | @Named(EnhanceRule.Key.STATIC_METHOD) EnhanceRule enhanceStatic, 32 | @Named(EnhanceRule.Key.SOURCE) EnhanceRule enhanceSource) { 33 | this.interceptorFactory = interceptorFactory; 34 | this.enhanceRules.add(enhanceConstructor); 35 | this.enhanceRules.add(enhanceMethod); 36 | this.enhanceRules.add(enhanceStatic); 37 | this.enhanceRules.add(enhanceSource); 38 | } 39 | 40 | @Override 41 | public EnhanceFactory get() { 42 | return new DefaultEnhanceFactory(this.enhanceRules, this.interceptorFactory); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/module/providers/EnhanceRuleChainProxyProvider.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.module.providers; 2 | 3 | import cn.bytes1024.hound.collect.enhance.DefaultEnhanceRuleChainProxy; 4 | import cn.bytes1024.hound.collect.enhance.EnhanceRuleChainProxy; 5 | import cn.bytes1024.hound.collect.enhance.InterceptorFactory; 6 | import cn.bytes1024.hound.collect.enhance.rule.EnhanceRule; 7 | import com.google.common.collect.Lists; 8 | import com.google.inject.Inject; 9 | import com.google.inject.Provider; 10 | import com.google.inject.name.Named; 11 | 12 | import java.util.List; 13 | 14 | /** 15 | * 增强处理工厂 16 | * 17 | * @author 江浩 18 | */ 19 | public class EnhanceRuleChainProxyProvider implements Provider { 20 | 21 | 22 | private List enhanceRules = Lists.newArrayList(); 23 | 24 | private InterceptorFactory interceptorFactory; 25 | 26 | @Inject 27 | public EnhanceRuleChainProxyProvider( 28 | InterceptorFactory interceptorFactory, 29 | @Named(EnhanceRule.Key.METHOD) EnhanceRule enhanceMethod, 30 | @Named(EnhanceRule.Key.CONSTRUCTOR) EnhanceRule enhanceConstructor, 31 | @Named(EnhanceRule.Key.STATIC_METHOD) EnhanceRule enhanceStatic, 32 | @Named(EnhanceRule.Key.SOURCE) EnhanceRule enhanceSource) { 33 | this.interceptorFactory = interceptorFactory; 34 | this.enhanceRules.add(enhanceConstructor); 35 | this.enhanceRules.add(enhanceMethod); 36 | this.enhanceRules.add(enhanceStatic); 37 | this.enhanceRules.add(enhanceSource); 38 | } 39 | 40 | @Override 41 | public EnhanceRuleChainProxy get() { 42 | return new DefaultEnhanceRuleChainProxy(this.enhanceRules, this.interceptorFactory); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/module/providers/InterceptorFactoryProvider.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.module.providers; 2 | 3 | 4 | import cn.bytes1024.hound.collect.enhance.DefaultInterceptorFactory; 5 | import cn.bytes1024.hound.collect.enhance.InterceptorFactory; 6 | import cn.bytes1024.hound.plugins.define.TraceContext; 7 | import com.google.inject.Inject; 8 | import com.google.inject.Provider; 9 | 10 | /** 11 | * 拦截器处理 12 | * 13 | * @author 江浩 14 | */ 15 | public class InterceptorFactoryProvider implements Provider { 16 | 17 | private TraceContext traceContext; 18 | 19 | @Inject 20 | public InterceptorFactoryProvider(TraceContext traceContext) { 21 | this.traceContext = traceContext; 22 | } 23 | 24 | @Override 25 | public InterceptorFactory get() { 26 | return new DefaultInterceptorFactory(this.traceContext); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/module/providers/ServerReporterProvider.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.module.providers; 2 | 3 | import cn.bytes1024.hound.collect.context.DefaultReporter; 4 | import cn.bytes1024.hound.commons.option.ConfigOption; 5 | import cn.bytes1024.hound.commons.option.ConfigOptionDefine; 6 | import cn.bytes1024.hound.loader.ExtensionLoader; 7 | import cn.bytes1024.hound.transfers.define.TransferDefine; 8 | import cn.bytes1024.hound.transfers.define.buffer.TransferBuffer; 9 | import com.alipay.common.tracer.core.reporter.facade.Reporter; 10 | import com.google.inject.Inject; 11 | import com.google.inject.Provider; 12 | import lombok.extern.slf4j.Slf4j; 13 | 14 | /** 15 | * TODO 服务端默认传输 16 | *

17 | *

18 | * 19 | * @author 江浩 20 | */ 21 | @Slf4j 22 | public class ServerReporterProvider implements Provider { 23 | 24 | private ExtensionLoader transferDefineExtensions = ExtensionLoader.getExtensionLoader(TransferDefine.class); 25 | 26 | private TransferDefine transferDefine = null; 27 | 28 | private TransferBuffer transferBuffer = null; 29 | 30 | private ConfigOption configOption; 31 | 32 | @Inject 33 | public ServerReporterProvider(ConfigOption configOption, TransferBuffer transferBuffer) { 34 | this.configOption = configOption; 35 | this.transferBuffer = transferBuffer; 36 | 37 | boolean transferEnabled = ConfigOptionDefine.isTransferEnabled(configOption); 38 | if (log.isDebugEnabled()) { 39 | log.debug("transfer enabled: {}", transferEnabled); 40 | } 41 | 42 | if (transferEnabled) { 43 | String transferType = ConfigOptionDefine.getTransferType(configOption); 44 | this.transferDefine = transferDefineExtensions.getExtension(transferType); 45 | if (log.isDebugEnabled()) { 46 | log.debug("transfer plugin : {},{}", transferType, this.transferDefine); 47 | } 48 | } 49 | } 50 | 51 | @Override 52 | public Reporter get() { 53 | return new DefaultReporter(this.transferDefine, this.transferBuffer, this.configOption); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/module/providers/SofaTracerProvider.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.module.providers; 2 | 3 | import cn.bytes1024.hound.commons.option.ConfigOption; 4 | import cn.bytes1024.hound.commons.option.ConfigOptionDefine; 5 | import com.alipay.common.tracer.core.SofaTracer; 6 | import com.alipay.common.tracer.core.reporter.facade.Reporter; 7 | import com.google.inject.Inject; 8 | import com.google.inject.Provider; 9 | 10 | /** 11 | * sofaTrace默认构建 12 | * 13 | * @author 江浩 14 | */ 15 | public class SofaTracerProvider implements Provider { 16 | 17 | private ConfigOption configOption; 18 | 19 | private Reporter reporter; 20 | 21 | @Inject 22 | public SofaTracerProvider(ConfigOption configOption, Reporter reporter) { 23 | this.configOption = configOption; 24 | this.reporter = reporter; 25 | } 26 | 27 | @Override 28 | public SofaTracer get() { 29 | return new SofaTracer.Builder(ConfigOptionDefine.getTracerType(configOption)) 30 | .withClientReporter(reporter) 31 | .withServerReporter(reporter) 32 | .build(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/module/providers/TraceContextProvider.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.module.providers; 2 | 3 | import cn.bytes1024.hound.collect.context.DefaultTraceContext; 4 | import cn.bytes1024.hound.plugins.define.TraceContext; 5 | import cn.bytes1024.hound.plugins.define.filter.TraceContextFilterOption; 6 | import com.alipay.common.tracer.core.SofaTracer; 7 | import com.google.inject.Inject; 8 | import com.google.inject.Provider; 9 | 10 | /** 11 | * TraceContext provider 12 | * 13 | * @author 江浩 14 | */ 15 | public class TraceContextProvider implements Provider { 16 | 17 | private SofaTracer sofaTracer; 18 | 19 | private TraceContextFilterOption traceContextFilterOption; 20 | 21 | @Inject 22 | public TraceContextProvider(SofaTracer sofaTracer, TraceContextFilterOption traceContextFilterOption) { 23 | this.sofaTracer = sofaTracer; 24 | this.traceContextFilterOption = traceContextFilterOption; 25 | } 26 | 27 | @Override 28 | public TraceContext get() { 29 | return new DefaultTraceContext(sofaTracer, this.traceContextFilterOption); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/module/providers/TransferBufferProvider.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.module.providers; 2 | 3 | import cn.bytes1024.hound.commons.option.ConfigOption; 4 | import cn.bytes1024.hound.transfers.define.buffer.DefaultTransferBuffer; 5 | import cn.bytes1024.hound.transfers.define.buffer.TransferBuffer; 6 | import com.google.inject.Inject; 7 | import com.google.inject.Provider; 8 | import lombok.extern.slf4j.Slf4j; 9 | 10 | @Slf4j 11 | public class TransferBufferProvider implements Provider { 12 | 13 | private ConfigOption configOption; 14 | 15 | @Inject 16 | public TransferBufferProvider(ConfigOption configOption) { 17 | this.configOption = configOption; 18 | } 19 | 20 | @Override 21 | public TransferBuffer get() { 22 | return new DefaultTransferBuffer(this.configOption); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/processor/AbstractProcessor.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.processor; 2 | 3 | import cn.bytes1024.hound.commons.option.ConfigOption; 4 | import lombok.extern.slf4j.Slf4j; 5 | import org.apache.commons.lang3.time.StopWatch; 6 | 7 | import java.util.concurrent.CountDownLatch; 8 | 9 | @Slf4j 10 | public abstract class AbstractProcessor implements Processor { 11 | 12 | @Override 13 | public void start(ConfigOption configOption, CountDownLatch countDownLatch) { 14 | StopWatch stopWatch = StopWatch.createStarted(); 15 | this.start(configOption); 16 | countDownLatch.countDown(); 17 | stopWatch.split(); 18 | log.info("processor {} started use {} ms", this.type(), stopWatch.getSplitTime()); 19 | } 20 | 21 | protected abstract void start(ConfigOption configOption); 22 | 23 | 24 | } 25 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/processor/AgentCollectProcessor.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.processor; 2 | 3 | import cn.bytes1024.hound.collect.enhance.EnhanceRuleChainProxy; 4 | import cn.bytes1024.hound.commons.enums.ProcessorStatus; 5 | import cn.bytes1024.hound.commons.enums.ProcessorType; 6 | import cn.bytes1024.hound.commons.option.ConfigOption; 7 | import cn.bytes1024.hound.commons.option.ConfigOptionDefine; 8 | import cn.bytes1024.hound.loader.ExtensionLoader; 9 | import cn.bytes1024.hound.plugins.define.EnhanceContext; 10 | import cn.bytes1024.hound.plugins.define.PluginDefine; 11 | import com.google.inject.Inject; 12 | import lombok.extern.slf4j.Slf4j; 13 | import net.bytebuddy.agent.builder.AgentBuilder; 14 | import net.bytebuddy.description.type.TypeDescription; 15 | import net.bytebuddy.matcher.ElementMatcher; 16 | import net.bytebuddy.matcher.ElementMatchers; 17 | import org.apache.commons.collections.CollectionUtils; 18 | import org.apache.commons.lang3.StringUtils; 19 | 20 | import java.lang.instrument.Instrumentation; 21 | import java.util.List; 22 | import java.util.concurrent.atomic.AtomicReference; 23 | 24 | import static net.bytebuddy.matcher.ElementMatchers.isInterface; 25 | import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith; 26 | 27 | /** 28 | * @author 江浩 29 | */ 30 | @Slf4j 31 | public class AgentCollectProcessor extends AbstractProcessor { 32 | 33 | private AgentBuilder agentBuilder; 34 | 35 | private Instrumentation instrumentation; 36 | 37 | private EnhanceRuleChainProxy enhanceRuleChainProxy; 38 | 39 | private ExtensionLoader extensionLoader = ExtensionLoader.getExtensionLoader(PluginDefine.class); 40 | 41 | private AtomicReference processorStatusReference = new AtomicReference<>(status()); 42 | 43 | 44 | @Inject 45 | public AgentCollectProcessor(AgentBuilder agentBuilder, 46 | Instrumentation instrumentation, 47 | EnhanceRuleChainProxy enhanceRuleChainProxy) { 48 | this.agentBuilder = agentBuilder; 49 | this.instrumentation = instrumentation; 50 | this.enhanceRuleChainProxy = enhanceRuleChainProxy; 51 | } 52 | 53 | @Override 54 | public ProcessorType type() { 55 | return ProcessorType.AGENT; 56 | } 57 | 58 | @Override 59 | public void start(ConfigOption configOption) { 60 | 61 | //是否独立启动 62 | 63 | if (!ConfigOptionDefine.isEnabledAgent(configOption)) { 64 | if (log.isDebugEnabled()) { 65 | log.debug("processor [{}] enabled = {} ", this.getClass().getSimpleName(), "false"); 66 | } 67 | return; 68 | } 69 | 70 | if (processorStatusReference.get().equals(ProcessorStatus.RUN)) { 71 | log.debug("processor [{}] is runing ", this.getClass().getSimpleName()); 72 | return; 73 | } 74 | 75 | //String agentId = configOption.getOption(ConfigOptionDefine.AGENT_ID, null); 76 | String agentId = ConfigOptionDefine.getAgentId(configOption); 77 | log.info("collect agent : {} starting", agentId); 78 | List plugins = extensionLoader.getSupportedVExtensions(); 79 | log.info("loading plugins {}...", plugins.size()); 80 | this.start(plugins, 0); 81 | 82 | processorStatusReference.set(ProcessorStatus.RUN); 83 | 84 | } 85 | 86 | 87 | private void start(List loadPlugins, int index) { 88 | 89 | if (index >= loadPlugins.size()) { 90 | return; 91 | } 92 | PluginDefine pluginDefine = loadPlugins.get(index); 93 | ElementMatcher classDescription = pluginDefine.classDescription(); 94 | 95 | String pluginName = pluginDefine.name(); 96 | if (StringUtils.isBlank(pluginName)) { 97 | log.info("plugin name is empty ignored: {}", pluginDefine); 98 | } else { 99 | List enhanceContexts = pluginDefine.enhanceContexts(); 100 | log.info("plugin name loading: {} len {}", pluginDefine.name(), CollectionUtils.isEmpty(enhanceContexts) ? 0 : enhanceContexts.size()); 101 | this.agentBuilder 102 | .ignore(nameStartsWith("cn.bytes1024.hound")) 103 | .type(ElementMatchers.not(isInterface()).and(classDescription)) 104 | .transform((builder, typeDescription, classLoader, module) -> enhanceRuleChainProxy.enhance(builder, enhanceContexts)) 105 | //TODO 106 | //.with(new AgentEnhanceLister(this.enhanceDebugFactory)) 107 | .installOn(this.instrumentation); 108 | } 109 | 110 | this.start(loadPlugins, ++index); 111 | } 112 | 113 | 114 | // private static class AgentEnhanceLister implements AgentBuilder.Listener { 115 | // 116 | // private EnhanceDebugFactory enhanceDebugFactory; 117 | // 118 | // public AgentEnhanceLister(EnhanceDebugFactory enhanceDebugFactory) { 119 | // this.enhanceDebugFactory = enhanceDebugFactory; 120 | // } 121 | // 122 | // @Override 123 | // public void onDiscovery(String typeName, ClassLoader classLoader, JavaModule module, boolean loaded) { 124 | // 125 | // } 126 | // 127 | // @Override 128 | // public void onTransformation(TypeDescription typeDescription, ClassLoader classLoader, JavaModule module, 129 | // boolean loaded, DynamicType dynamicType) { 130 | // enhanceDebugFactory.autoWrite(typeDescription, dynamicType); 131 | // } 132 | // 133 | // @Override 134 | // public void onIgnored(TypeDescription typeDescription, ClassLoader classLoader, JavaModule module, 135 | // boolean loaded) { 136 | // 137 | // } 138 | // 139 | // @Override 140 | // public void onError(String typeName, ClassLoader classLoader, JavaModule module, boolean loaded, 141 | // Throwable throwable) { 142 | // log.error("Enhance class " + typeName + " error.", throwable); 143 | // } 144 | // 145 | // @Override 146 | // public void onComplete(String typeName, ClassLoader classLoader, JavaModule module, boolean loaded) { 147 | // } 148 | // } 149 | 150 | 151 | @Override 152 | public void destroy() { 153 | 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/processor/MetricsCollectProcessor.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.processor; 2 | 3 | import cn.bytes1024.hound.commons.enums.ProcessorType; 4 | import cn.bytes1024.hound.commons.option.ConfigOption; 5 | import lombok.extern.slf4j.Slf4j; 6 | 7 | /** 8 | * 指标数据采集 9 | * 10 | * @author 江浩 11 | */ 12 | @Slf4j 13 | public class MetricsCollectProcessor extends AbstractProcessor { 14 | 15 | // TODO: 2019/7/21 时间轮盘 16 | // TODO: 2019/7/21 任务时间轮盘触发回执上报 17 | 18 | // TODO: 2019/8/2 数据如何传输?直接使用传输插件? 19 | 20 | @Override 21 | public ProcessorType type() { 22 | return ProcessorType.METRICS; 23 | } 24 | 25 | @Override 26 | public void start(ConfigOption configOption) { 27 | 28 | } 29 | 30 | @Override 31 | public void destroy() { 32 | 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /collect/src/main/java/cn/bytes1024/hound/collect/processor/Processor.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.processor; 2 | 3 | import cn.bytes1024.hound.commons.enums.ProcessorStatus; 4 | import cn.bytes1024.hound.commons.enums.ProcessorType; 5 | import cn.bytes1024.hound.commons.option.ConfigOption; 6 | 7 | import java.util.concurrent.CountDownLatch; 8 | 9 | /** 10 | * 具体的处理器 11 | * 12 | * @author 江浩 13 | */ 14 | public interface Processor { 15 | 16 | 17 | /** 18 | * 处理器类型 19 | * 20 | * @return : cn.bytes1024.hound.commons.enums.ProcessorType 21 | * @author 江浩 22 | */ 23 | ProcessorType type(); 24 | 25 | /** 26 | * 处理器状态 27 | * 28 | * @return : cn.bytes1024.hound.commons.enums.ProcessorStatus 29 | * @author 江浩 30 | */ 31 | default ProcessorStatus status() { 32 | return ProcessorStatus.STOP; 33 | } 34 | 35 | /** 36 | * 判定是否启动完全 37 | * 38 | * @param configOption : 39 | * @param countDownLatch : 40 | * @author 江浩 41 | */ 42 | void start(ConfigOption configOption, CountDownLatch countDownLatch); 43 | 44 | /** 45 | * 关闭 46 | * 47 | * @author 江浩 48 | */ 49 | void destroy(); 50 | } 51 | -------------------------------------------------------------------------------- /collect/src/main/resources/collect.properties: -------------------------------------------------------------------------------- 1 | #探针基础设置信息 2 | #bytes.hound.agent.id=default 3 | 4 | # tracer 5 | # 添加流控等参数信息 6 | #bytes.hound.tracer.type=default 7 | 8 | #传输器信息 9 | #bytes.hound.transfer.name= -------------------------------------------------------------------------------- /collect/src/test/java/cn/bytes1024/hound/collect/test/ConfigLoadingTest.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.test; 2 | 3 | import cn.bytes1024.hound.commons.option.ConfigOption; 4 | import cn.bytes1024.hound.commons.option.ConfigOptionDefine; 5 | import cn.bytes1024.hound.commons.option.DefaultConfigOption; 6 | import org.junit.Test; 7 | 8 | public class ConfigLoadingTest { 9 | 10 | @Test 11 | public void loadTest() { 12 | 13 | ConfigOption configOption = new DefaultConfigOption("E:\\test.properties"); 14 | 15 | System.out.println(ConfigOptionDefine.getAgentId(configOption)); 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /collect/src/test/java/cn/bytes1024/hound/collect/test/NettyTimerWheelTest.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.collect.test; 2 | 3 | import io.netty.util.HashedWheelTimer; 4 | import io.netty.util.Timeout; 5 | import io.netty.util.TimerTask; 6 | 7 | import java.util.concurrent.TimeUnit; 8 | 9 | public class NettyTimerWheelTest implements TimerTask { 10 | 11 | 12 | public static void main(String[] args) { 13 | /** 14 | * tick: 时间轮里每一格; 15 | * tickDuration: 每一格的时长; 16 | * ticksPerWheel: 时间轮总共有多少格. 17 | * newTimeout: 定时任务分配到时间轮 18 | */ 19 | HashedWheelTimer hashedWheelTimer = new HashedWheelTimer(3, TimeUnit.SECONDS, 10); 20 | NettyTimerWheelTest nettyTimerWheelTest = new NettyTimerWheelTest(); 21 | 22 | while (true) { 23 | hashedWheelTimer.newTimeout(nettyTimerWheelTest, 5, TimeUnit.SECONDS); 24 | try { 25 | TimeUnit.SECONDS.sleep(10); 26 | } catch (InterruptedException e) { 27 | e.printStackTrace(); 28 | } 29 | } 30 | } 31 | 32 | @Override 33 | public void run(Timeout timeout) throws Exception { 34 | 35 | System.out.println(timeout.isCancelled()); 36 | 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /commons/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | bytes-hound 6 | cn.bytes1024.hound 7 | 1.0.1-SNAPSHOT 8 | 9 | 4.0.0 10 | 11 | commons 12 | 13 | 14 | false 15 | 16 | 17 | 18 | 19 | ch.qos.logback 20 | logback-classic 21 | 22 | 23 | 24 | org.apache.commons 25 | commons-lang3 26 | 27 | 28 | org.projectlombok 29 | lombok 30 | 31 | 32 | 33 | 34 | io.netty 35 | netty-all 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /commons/src/main/java/cn/bytes1024/hound/commons/enums/ProcessorStatus.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.commons.enums; 2 | 3 | public enum ProcessorStatus { 4 | 5 | RUN, STOP 6 | 7 | } 8 | -------------------------------------------------------------------------------- /commons/src/main/java/cn/bytes1024/hound/commons/enums/ProcessorType.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.commons.enums; 2 | 3 | public enum ProcessorType { 4 | 5 | AGENT, METRICS 6 | 7 | } 8 | -------------------------------------------------------------------------------- /commons/src/main/java/cn/bytes1024/hound/commons/option/ConfigOption.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.commons.option; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * 配置加载信息 7 | * 8 | * @author 江浩 9 | */ 10 | public interface ConfigOption { 11 | 12 | 13 | /** 14 | * 加载配置信息 15 | * 16 | * @param path : 17 | * @return : void 18 | * @author 江浩 19 | */ 20 | void load(String path); 21 | 22 | 23 | // /** 24 | // * 获取探针编号 25 | // * 26 | // * @return : java.lang.String 27 | // * @author 江浩 28 | // */ 29 | // String getAgentId(); 30 | // 31 | // /** 32 | // * 获取传输方式 33 | // * 34 | // * @return 35 | // */ 36 | // String getTransfer(); 37 | // 38 | // /** 39 | // * tracer类型 40 | // * 41 | // * @return : java.lang.String 42 | // * @author 江浩 43 | // */ 44 | // String getTracerType(); 45 | 46 | /** 47 | * 获取属性信息 48 | * 49 | * @param key : 50 | * @param defaultValue : 51 | * @return : java.lang.String 52 | * @author 江浩 53 | */ 54 | String getOption(String key, String defaultValue); 55 | 56 | 57 | /** 58 | * 默认获取一批获取 59 | * 60 | * @param key : 61 | * @return : java.util.List 62 | * @author 江浩 63 | */ 64 | List getOptions(String key); 65 | 66 | 67 | } 68 | -------------------------------------------------------------------------------- /commons/src/main/java/cn/bytes1024/hound/commons/option/ConfigOptionDefine.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.commons.option; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | 5 | @Slf4j 6 | public class ConfigOptionDefine { 7 | 8 | private static final String PREFIX = "bytes.hound."; 9 | 10 | 11 | private static final String AGENT_ENABLED = PREFIX + "agent.enabled"; 12 | 13 | private static final String AGENT_ID = PREFIX + "agent.id"; 14 | 15 | private static final String TRACER_TYPE = PREFIX + "tracer.type"; 16 | 17 | private static final String TRANSFER_ENABLED = PREFIX + "transfer.enabled"; 18 | 19 | private static final String TRANSFER_CONTENT_VIEW_ENABLED = PREFIX + "transfer.content.show.enabled"; 20 | 21 | private static final String TRANSFER_TYPE = PREFIX + "transfer.type"; 22 | 23 | private static final String TRANSFER_WEB_ADDRESS = PREFIX + "transfer.web.address"; 24 | 25 | private static final String BUFFER_MAX = PREFIX + "transfer.batch.max"; 26 | 27 | 28 | public static String getAgentId(ConfigOption configOption) { 29 | return configOption.getOption(AGENT_ID, null); 30 | } 31 | 32 | 33 | /** 34 | * tracerType 必须有一个默认值 35 | * 36 | * @param configOption : 37 | * @return : java.lang.String 38 | * @author 江浩 39 | */ 40 | public static String getTracerType(ConfigOption configOption) { 41 | return configOption.getOption(TRACER_TYPE, "default"); 42 | } 43 | 44 | 45 | public static String getTransferType(ConfigOption configOption) { 46 | return configOption.getOption(TRANSFER_TYPE, null); 47 | } 48 | 49 | /** 50 | * 是否开启了远程传输 51 | * 52 | * @param configOption : 53 | * @return : boolean 54 | * @author 江浩 55 | */ 56 | public static boolean isTransferEnabled(ConfigOption configOption) { 57 | String bool = configOption.getOption(TRANSFER_ENABLED, "false"); 58 | try { 59 | return Boolean.parseBoolean(bool); 60 | } catch (Exception e) { 61 | if (log.isDebugEnabled()) { 62 | log.debug("transfer.enabled error:{}", bool); 63 | } 64 | return false; 65 | } 66 | } 67 | 68 | 69 | public static boolean isOpenTransmitContentView(ConfigOption configOption) { 70 | String bool = configOption.getOption(TRANSFER_CONTENT_VIEW_ENABLED, "false"); 71 | try { 72 | return Boolean.parseBoolean(bool); 73 | } catch (Exception e) { 74 | if (log.isDebugEnabled()) { 75 | log.debug("transfer.enabled error:{}", bool); 76 | } 77 | return false; 78 | } 79 | } 80 | 81 | public static String getTransferWebAddress(ConfigOption configOption) { 82 | return configOption.getOption(TRANSFER_WEB_ADDRESS, null); 83 | } 84 | 85 | 86 | public static boolean isEnabledAgent(ConfigOption configOption) { 87 | String bool = configOption.getOption(AGENT_ENABLED, "true"); 88 | try { 89 | return Boolean.parseBoolean(bool); 90 | } catch (Exception e) { 91 | if (log.isDebugEnabled()) { 92 | log.debug("transfer.enabled error:{}", bool); 93 | } 94 | return false; 95 | } 96 | } 97 | 98 | 99 | public static int getBufferMaxNumber(ConfigOption configOption) { 100 | String number = configOption.getOption(BUFFER_MAX, String.valueOf(Short.MAX_VALUE)); 101 | return Integer.parseInt(number); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /commons/src/main/java/cn/bytes1024/hound/commons/option/DefaultConfigOption.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.commons.option; 2 | 3 | 4 | import lombok.extern.slf4j.Slf4j; 5 | import org.apache.commons.lang3.StringUtils; 6 | 7 | import java.io.FileInputStream; 8 | import java.io.IOException; 9 | import java.io.InputStream; 10 | import java.util.ArrayList; 11 | import java.util.Arrays; 12 | import java.util.List; 13 | import java.util.Properties; 14 | 15 | /** 16 | * @author 江浩 17 | */ 18 | @Slf4j 19 | public class DefaultConfigOption extends Properties implements ConfigOption { 20 | 21 | private static final String DEFAULT = "collect.properties"; 22 | 23 | private String path; 24 | 25 | public DefaultConfigOption(String path) { 26 | this.path = path; 27 | this.load(this.path); 28 | } 29 | 30 | @Override 31 | public void load(String path) { 32 | try { 33 | InputStream inputStream = 34 | readFile(path) ? new FileInputStream(path) : 35 | this.getClass().getClassLoader().getResourceAsStream(DEFAULT); 36 | 37 | this.load(inputStream); 38 | } catch (IOException e) { 39 | e.printStackTrace(); 40 | } 41 | } 42 | 43 | private boolean readFile(String path) { 44 | return StringUtils.isNotBlank(path); 45 | } 46 | 47 | // @Override 48 | // public String getAgentId() { 49 | // return this.getProperty("bytes.hound.agent.id", ""); 50 | // } 51 | // 52 | // @Override 53 | // public String getTransfer() { 54 | // return this.getOption("bytes.hound.transfer.type", ""); 55 | // } 56 | // 57 | // @Override 58 | // public String getTracerType() { 59 | // return this.getOption("bytes.hound.tracer.type", UUID.randomUUID().toString()); 60 | // } 61 | 62 | 63 | @Override 64 | public String getOption(String key, String defaultValue) { 65 | return this.getProperty(key, defaultValue); 66 | } 67 | 68 | @Override 69 | public List getOptions(String key) { 70 | String values = this.getOption(key, null); 71 | try { 72 | return StringUtils.isBlank(values) ? new ArrayList<>() : 73 | Arrays.asList(StringUtils.split(values, ",")); 74 | } catch (Exception e) { 75 | log.error("handle key {} error values {}", key, values); 76 | return null; 77 | } 78 | } 79 | 80 | 81 | } 82 | -------------------------------------------------------------------------------- /commons/src/main/java/cn/bytes1024/hound/commons/util/NamedThreadLocal.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.commons.util; 2 | 3 | public class NamedThreadLocal extends ThreadLocal { 4 | private final String name; 5 | 6 | public NamedThreadLocal(String name) { 7 | this.name = name; 8 | } 9 | 10 | public String getName() { 11 | return name; 12 | } 13 | } -------------------------------------------------------------------------------- /commons/src/main/java/cn/bytes1024/hound/commons/util/RefClassUtil.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.commons.util; 2 | 3 | import java.lang.reflect.ParameterizedType; 4 | import java.lang.reflect.Type; 5 | 6 | /** 7 | * 获取泛型类型 8 | * 9 | * @author 江浩 10 | */ 11 | public class RefClassUtil { 12 | 13 | public static Class getSuperClassGenricType(Class clazz, int index) 14 | throws IndexOutOfBoundsException { 15 | Type genType = clazz.getGenericSuperclass(); 16 | if (!(genType instanceof ParameterizedType)) { 17 | return Object.class; 18 | } 19 | Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); 20 | if (index >= params.length || index < 0) { 21 | return Object.class; 22 | } 23 | if (!(params[index] instanceof Class)) { 24 | return Object.class; 25 | } 26 | return (Class) params[index]; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /loader/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | bytes-hound 6 | cn.bytes1024.hound 7 | 1.0.1-SNAPSHOT 8 | 9 | 4.0.0 10 | 11 | loader 12 | 13 | false 14 | 15 | 16 | 17 | org.projectlombok 18 | lombok 19 | 20 | 21 | 22 | ch.qos.logback 23 | logback-classic 24 | 25 | 26 | 27 | org.apache.commons 28 | commons-lang3 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /loader/src/main/java/cn/bytes1024/hound/loader/Holder.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.loader; 2 | 3 | public class Holder { 4 | 5 | private volatile T value; 6 | 7 | public void set(T value) { 8 | this.value = value; 9 | } 10 | 11 | public T get() { 12 | return value; 13 | } 14 | 15 | } -------------------------------------------------------------------------------- /plugins/plugins-bom/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | plugins 7 | cn.bytes1024.hound 8 | 1.0.1-SNAPSHOT 9 | 10 | 11 | 4.0.0 12 | plugins-bom 13 | pom 14 | 15 | 16 | 17 | 18 | 19 | cn.bytes1024.hound 20 | plugins-tomcat 21 | ${project.version} 22 | 23 | 24 | 25 | cn.bytes1024.hound 26 | plugins-webflux 27 | ${project.version} 28 | 29 | 30 | 31 | 32 | cn.bytes1024.hound 33 | plugins-httpclient 34 | ${project.version} 35 | 36 | 37 | 38 | 39 | cn.bytes1024.hound 40 | plugins-okhttp 41 | ${project.version} 42 | 43 | 44 | 45 | cn.bytes1024.hound 46 | plugins-spring 47 | ${project.version} 48 | 49 | 50 | 51 | cn.bytes1024.hound 52 | plugins-fastjson 53 | ${project.version} 54 | 55 | 56 | 57 | cn.bytes1024.hound 58 | plugins-gson 59 | ${project.version} 60 | 61 | 62 | 63 | cn.bytes1024.hound 64 | plugins-druid 65 | ${project.version} 66 | 67 | 68 | 69 | cn.bytes1024.hound 70 | plugins-mybatis 71 | ${project.version} 72 | 73 | 74 | 75 | cn.bytes1024.hound 76 | plugins-mysql-jdbc 77 | ${project.version} 78 | 79 | 80 | 81 | 82 | 83 | 84 | -------------------------------------------------------------------------------- /plugins/plugins-define/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | plugins 6 | cn.bytes1024.hound 7 | 1.0.1-SNAPSHOT 8 | 9 | 10 | 4.0.0 11 | 12 | plugins-define 13 | 14 | 15 | 16 | net.bytebuddy 17 | byte-buddy 18 | 19 | 20 | org.projectlombok 21 | lombok 22 | 23 | 24 | org.apache.commons 25 | commons-lang3 26 | 27 | 28 | 29 | com.alipay.sofa 30 | tracer-core 31 | 32 | 33 | 34 | ch.qos.logback 35 | logback-classic 36 | 37 | 38 | 39 | com.alibaba 40 | fastjson 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /plugins/plugins-define/src/main/java/cn/bytes1024/hound/plugins/define/AbstractPluginDefine.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.define; 2 | 3 | import cn.bytes1024.hound.plugins.define.interceptor.ConstructorInterceptor; 4 | import cn.bytes1024.hound.plugins.define.interceptor.MethodAroundInterceptor; 5 | import cn.bytes1024.hound.plugins.define.interceptor.StaticMethodAroundInterceptor; 6 | import lombok.Getter; 7 | import net.bytebuddy.description.method.MethodDescription; 8 | import net.bytebuddy.description.type.TypeDescription; 9 | import net.bytebuddy.matcher.ElementMatcher; 10 | import org.apache.commons.lang3.StringUtils; 11 | 12 | import java.util.ArrayList; 13 | import java.util.List; 14 | import java.util.Objects; 15 | import java.util.UUID; 16 | 17 | /** 18 | * @author 江浩 19 | */ 20 | public abstract class AbstractPluginDefine implements PluginDefine { 21 | 22 | protected static final String PREFIX = "plugin-"; 23 | 24 | protected String format(String name) { 25 | return String.format("%s%s", PREFIX, name); 26 | } 27 | 28 | private PluginDefineBuilder defineBuilder; 29 | 30 | public AbstractPluginDefine() { 31 | this.defineBuilder = new PluginDefineBuilder(); 32 | this.init(this.defineBuilder); 33 | } 34 | 35 | /** 36 | * 初始方法 37 | * 38 | * @param defineBuilder 39 | */ 40 | public abstract void init(PluginDefineBuilder defineBuilder); 41 | 42 | 43 | @Override 44 | public ElementMatcher classDescription() { 45 | return defineBuilder.getClassDescription(); 46 | } 47 | 48 | 49 | @Override 50 | public List enhanceContexts() { 51 | return defineBuilder.getInterceptorBuilder().getEnhanceContexts(); 52 | } 53 | 54 | 55 | @Override 56 | public String name() { 57 | final String name = defineBuilder.getName(); 58 | return StringUtils.isBlank(name) ? UUID.randomUUID().toString() : name; 59 | } 60 | 61 | 62 | @Getter 63 | public static class PluginDefineBuilder { 64 | 65 | private ElementMatcher classDescription; 66 | 67 | private String name; 68 | 69 | private InterceptorBuilder interceptorBuilder; 70 | 71 | public PluginDefineBuilder pointName(String name) { 72 | this.name = name; 73 | return this; 74 | } 75 | 76 | public InterceptorBuilder pointClass(ElementMatcher classDescription) { 77 | this.classDescription = classDescription; 78 | return interceptorBuilder = new InterceptorBuilder(this.name); 79 | } 80 | } 81 | 82 | 83 | @Getter 84 | public static class InterceptorBuilder { 85 | 86 | private List enhanceContexts = new ArrayList<>(); 87 | private String pluginName; 88 | 89 | private InterceptorBuilder() { 90 | } 91 | 92 | public InterceptorBuilder(String pluginName) { 93 | this.pluginName = pluginName; 94 | } 95 | 96 | public InterceptorBuilder pointMethod(ElementMatcher methodDescription, Class interceptorClass) { 97 | pointSetting(methodDescription, interceptorClass.getName()); 98 | return this; 99 | } 100 | 101 | public InterceptorBuilder pointStaticMethod(ElementMatcher methodDescription, Class interceptorClass) { 102 | pointSetting(methodDescription, interceptorClass.getName()); 103 | return this; 104 | } 105 | 106 | public InterceptorBuilder pointConstructor(ElementMatcher methodDescription, Class interceptorClass) { 107 | pointSetting(methodDescription, interceptorClass.getName()); 108 | return this; 109 | } 110 | 111 | private void pointSetting(ElementMatcher methodDescription, String interceptorClassName) { 112 | 113 | if (Objects.isNull(methodDescription) || StringUtils.isBlank(interceptorClassName)) { 114 | return; 115 | } 116 | 117 | EnhanceContext enhanceContext = new EnhanceContext(); 118 | enhanceContext.setMethodDescription(methodDescription); 119 | enhanceContext.setInterceptorClassName(interceptorClassName); 120 | enhanceContext.setRefPluginName(this.pluginName); 121 | 122 | binder(enhanceContext); 123 | } 124 | 125 | private void binder(EnhanceContext enhanceContext) { 126 | if (!enhanceContexts.contains(enhanceContext)) { 127 | enhanceContexts.add(enhanceContext); 128 | } 129 | } 130 | 131 | } 132 | 133 | 134 | } 135 | -------------------------------------------------------------------------------- /plugins/plugins-define/src/main/java/cn/bytes1024/hound/plugins/define/EnhanceContext.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.define; 2 | 3 | import lombok.Data; 4 | import lombok.EqualsAndHashCode; 5 | import net.bytebuddy.description.method.MethodDescription; 6 | import net.bytebuddy.matcher.ElementMatcher; 7 | 8 | /** 9 | * 增强配置块 10 | * 11 | * @author 江浩 12 | */ 13 | @Data 14 | @EqualsAndHashCode 15 | public class EnhanceContext { 16 | 17 | private ElementMatcher methodDescription; 18 | 19 | private String interceptorClassName; 20 | 21 | private String refPluginName; 22 | } 23 | -------------------------------------------------------------------------------- /plugins/plugins-define/src/main/java/cn/bytes1024/hound/plugins/define/EnhancedDefine.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.define; 2 | 3 | import com.alipay.common.tracer.core.span.SofaTracerSpan; 4 | 5 | /** 6 | * 增强,相当于目标添加 该接口 7 | * 8 | * @author 江浩 9 | */ 10 | public interface EnhancedDefine { 11 | 12 | /** 13 | * 给拦截目标设置属性 14 | * 15 | * @param sofaTracerSpan : 16 | * @return : void 17 | * @author 江浩 18 | */ 19 | void setEnhancedInstanceTraceContext(SofaTracerSpan sofaTracerSpan); 20 | 21 | /** 22 | * 获取目标对象异步trace属性 23 | * 24 | * @return 25 | */ 26 | SofaTracerSpan getEnhancedInstanceTraceContext(); 27 | 28 | } 29 | -------------------------------------------------------------------------------- /plugins/plugins-define/src/main/java/cn/bytes1024/hound/plugins/define/InterceptContext.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.define; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Builder; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | 8 | import java.io.Serializable; 9 | import java.lang.reflect.Method; 10 | import java.util.HashMap; 11 | import java.util.Map; 12 | 13 | /** 14 | * 拦截上下文信息 15 | * 16 | * @author 江浩 17 | */ 18 | @Data 19 | @Builder 20 | @AllArgsConstructor 21 | @NoArgsConstructor 22 | public class InterceptContext implements Serializable { 23 | 24 | 25 | private Object target; 26 | 27 | private Method method; 28 | 29 | @Builder.Default 30 | private Object[] args = {}; 31 | 32 | /** 33 | * 标识当前请求信息,如果过滤掉了 不做后续的处理 34 | */ 35 | private boolean ignored; 36 | 37 | private Object result; 38 | 39 | private Throwable throwable; 40 | 41 | private Map props = new HashMap<>(); 42 | 43 | } 44 | -------------------------------------------------------------------------------- /plugins/plugins-define/src/main/java/cn/bytes1024/hound/plugins/define/InterceptContextFilter.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.define; 2 | 3 | public enum InterceptContextFilter { 4 | } 5 | -------------------------------------------------------------------------------- /plugins/plugins-define/src/main/java/cn/bytes1024/hound/plugins/define/PluginDefine.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.define; 2 | 3 | import net.bytebuddy.description.type.TypeDescription; 4 | import net.bytebuddy.matcher.ElementMatcher; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | * 插件定义 10 | * @author 江浩 11 | */ 12 | public interface PluginDefine { 13 | 14 | /** 15 | * 插件名称 16 | * @return 17 | */ 18 | String name(); 19 | 20 | /** 21 | * 增强拦截类 22 | * 23 | * @return : net.bytebuddy.matcher.ElementMatcher 24 | * @author 江浩 25 | */ 26 | ElementMatcher classDescription(); 27 | 28 | 29 | /** 30 | * 增强内容 31 | * 32 | * @return : java.util.List 33 | * @author 江浩 34 | */ 35 | List enhanceContexts(); 36 | } 37 | -------------------------------------------------------------------------------- /plugins/plugins-define/src/main/java/cn/bytes1024/hound/plugins/define/TraceContext.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.define; 2 | 3 | import com.alipay.common.tracer.core.SofaTracer; 4 | import com.alipay.common.tracer.core.context.trace.SofaTraceContext; 5 | import com.alipay.common.tracer.core.span.SofaTracerSpan; 6 | 7 | /** 8 | * trace 上线文 9 | * 10 | * @author 江浩 11 | */ 12 | public interface TraceContext extends SofaTraceContext { 13 | 14 | 15 | /** 16 | * sofaTracer 17 | * 18 | * @return : com.alipay.common.tracer.core.SofaTracer 19 | * @author 江浩 20 | */ 21 | SofaTracer getSofaTracer(); 22 | 23 | /** 24 | * 记录当前span停止 25 | * 26 | * @param interceptContext 27 | * @return 28 | */ 29 | SofaTracerSpan stopCurrentTracerSpan(InterceptContext interceptContext); 30 | } 31 | -------------------------------------------------------------------------------- /plugins/plugins-define/src/main/java/cn/bytes1024/hound/plugins/define/filter/DefaultTraceContextFilterOption.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.define.filter; 2 | 3 | import cn.bytes1024.hound.plugins.define.InterceptContext; 4 | import com.alibaba.fastjson.JSONObject; 5 | import com.alipay.common.tracer.core.context.span.SofaTracerSpanContext; 6 | import lombok.Builder; 7 | import lombok.Data; 8 | 9 | import java.lang.reflect.Parameter; 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | import java.util.Objects; 13 | 14 | /** 15 | * 默认的拦截过滤设置 16 | * 17 | * @author 江浩 18 | */ 19 | public class DefaultTraceContextFilterOption implements TraceContextFilterOption { 20 | 21 | 22 | @Override 23 | public void filterOption(SofaTracerSpanContext sofaTracerSpanContext, InterceptContext interceptContext) { 24 | sofaTracerSpanContext.setSysBaggageItem(CLASS_NAME, interceptContext.getTarget().getClass().getName()); 25 | sofaTracerSpanContext.setSysBaggageItem(METHOD_NAME, interceptContext.getMethod().getName()); 26 | Throwable throwable = interceptContext.getThrowable(); 27 | sofaTracerSpanContext.setSysBaggageItem(EXCEPTION, Objects.isNull(throwable) ? "" : throwable.toString()); 28 | //TODO 参数跟返回值的传递 29 | //this.filterParamsOption(sofaTracerSpanContext, interceptContext.getMethod().getParameters(), interceptContext.getArgs()); 30 | //this.filterResultOption(sofaTracerSpanContext, interceptContext.getResult()); 31 | } 32 | 33 | /** 34 | * 过滤参数设置 35 | *

36 | * 1.目前插件中可能存在接口的定义 37 | *

38 | * 39 | * @param sofaTracerSpanContext : 40 | * @param parameters : 41 | * @param args : 42 | * @return : void 43 | * @author 江浩 44 | */ 45 | private void filterParamsOption(SofaTracerSpanContext sofaTracerSpanContext, Parameter[] parameters, Object[] args) { 46 | if (Objects.isNull(parameters)) { 47 | return; 48 | } 49 | 50 | List kvOptions = new ArrayList<>(); 51 | for (int i = 0; i < parameters.length; i++) { 52 | Parameter parameter = parameters[i]; 53 | Class aClass = parameter.getType(); 54 | KVOption.KVOptionBuilder kvOptionBuilder = KVOption.builder(); 55 | kvOptionBuilder.optionKey(aClass.getName()); 56 | if (!isIgnore(aClass)) { 57 | kvOptionBuilder.optionValue(args[i]); 58 | } 59 | kvOptions.add(kvOptionBuilder.build()); 60 | } 61 | //json? 62 | sofaTracerSpanContext.setSysBaggageItem(PARAMS, format(kvOptions)); 63 | } 64 | 65 | private boolean isIgnore(Class aClass) { 66 | return aClass.isInterface(); 67 | } 68 | 69 | 70 | private void filterResultOption(SofaTracerSpanContext sofaTracerSpanContext, Object result) { 71 | if (Objects.isNull(result)) { 72 | return; 73 | } 74 | Class rClass = result.getClass(); 75 | if (this.isIgnore(rClass)) { 76 | return; 77 | } 78 | 79 | String resultFormat = this.format(KVOption.builder() 80 | .optionKey(rClass.getName()) 81 | .optionValue(result).build()); 82 | sofaTracerSpanContext.setSysBaggageItem(RESULT, resultFormat); 83 | } 84 | 85 | 86 | private String format(KVOption kvOption) { 87 | try { 88 | return JSONObject.toJSONString(kvOption); 89 | } catch (Exception e) { 90 | return null; 91 | } 92 | } 93 | 94 | private String format(List kvOptions) { 95 | try { 96 | return JSONObject.toJSONString(kvOptions); 97 | } catch (Exception e) { 98 | return null; 99 | } 100 | } 101 | 102 | 103 | @Data 104 | @Builder 105 | final static class KVOption { 106 | private String optionKey; 107 | private Object optionValue; 108 | } 109 | 110 | 111 | } 112 | -------------------------------------------------------------------------------- /plugins/plugins-define/src/main/java/cn/bytes1024/hound/plugins/define/filter/TraceContextFilterOption.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.define.filter; 2 | 3 | import cn.bytes1024.hound.plugins.define.InterceptContext; 4 | import com.alipay.common.tracer.core.context.span.SofaTracerSpanContext; 5 | 6 | /** 7 | * TranContextFilterOption的过滤设置 8 | * 9 | * @author 江浩 10 | */ 11 | public interface TraceContextFilterOption { 12 | 13 | 14 | /** 15 | * 拦截的目标class 16 | * 17 | * @author 江浩 18 | */ 19 | String CLASS_NAME = "class"; 20 | 21 | /** 22 | * 拦截的方法 23 | * 24 | * @author 江浩 25 | */ 26 | String METHOD_NAME = "method"; 27 | 28 | /** 29 | * 使用的参数 30 | * 31 | * @author 江浩 32 | */ 33 | String PARAMS = "params"; 34 | 35 | /** 36 | * 返回值 37 | * 38 | * @author 江浩 39 | */ 40 | String RESULT = "result"; 41 | 42 | 43 | /** 44 | * 异常对应key 45 | */ 46 | String EXCEPTION = "exception"; 47 | 48 | /** 49 | * 拦截信息过滤设置 50 | * 51 | * @param sofaTracerSpanContext : 52 | * @param interceptContext : 53 | * @return : void 54 | * @author 江浩 55 | */ 56 | void filterOption(SofaTracerSpanContext sofaTracerSpanContext, InterceptContext interceptContext); 57 | } 58 | -------------------------------------------------------------------------------- /plugins/plugins-define/src/main/java/cn/bytes1024/hound/plugins/define/interceptor/ConstructorInterceptor.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.define.interceptor; 2 | 3 | 4 | import cn.bytes1024.hound.plugins.define.EnhancedDefine; 5 | 6 | /** 7 | * 构造器拦截器 8 | * 9 | * @author 江浩 10 | */ 11 | public interface ConstructorInterceptor { 12 | 13 | /** 14 | * 构造器执行 15 | * 16 | * @param enhancedDefine : 17 | * @param allArguments : 18 | * @return : void 19 | * @author 江浩 20 | */ 21 | void onConstruct(EnhancedDefine enhancedDefine, Object[] allArguments); 22 | } 23 | -------------------------------------------------------------------------------- /plugins/plugins-define/src/main/java/cn/bytes1024/hound/plugins/define/interceptor/IgnoreInterceptor.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.define.interceptor; 2 | 3 | /** 4 | * 处理支持过滤方式 5 | * 6 | * @author 江浩 7 | */ 8 | public interface IgnoreInterceptor { 9 | 10 | boolean ignore(R handler); 11 | } 12 | -------------------------------------------------------------------------------- /plugins/plugins-define/src/main/java/cn/bytes1024/hound/plugins/define/interceptor/InterceptorPluginAware.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.define.interceptor; 2 | 3 | /** 4 | * @author 江浩 5 | */ 6 | public interface InterceptorPluginAware { 7 | 8 | /** 9 | * 定义的名称 10 | * 11 | * @param pluginName : 12 | * @return : void 13 | * @author 江浩 14 | */ 15 | void defineName(String pluginName); 16 | } 17 | -------------------------------------------------------------------------------- /plugins/plugins-define/src/main/java/cn/bytes1024/hound/plugins/define/interceptor/MethodAroundInterceptor.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.define.interceptor; 2 | 3 | 4 | import cn.bytes1024.hound.plugins.define.InterceptContext; 5 | 6 | /** 7 | * 方法环绕拦截器 8 | * 9 | * @author 江浩 10 | */ 11 | public interface MethodAroundInterceptor { 12 | 13 | /** 14 | * 之前 15 | * 16 | * @param interceptContext : 17 | * @return : void 18 | * @author 江浩 19 | */ 20 | void before(InterceptContext interceptContext); 21 | 22 | /** 23 | * 执行之后 24 | * 25 | * @param interceptContext : 26 | * @return : void 27 | * @author 江浩 28 | */ 29 | void after(InterceptContext interceptContext); 30 | 31 | } 32 | -------------------------------------------------------------------------------- /plugins/plugins-define/src/main/java/cn/bytes1024/hound/plugins/define/interceptor/StaticMethodAroundInterceptor.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.define.interceptor; 2 | 3 | import java.lang.reflect.Method; 4 | 5 | /** 6 | * 静态方法 7 | * 8 | * @author 江浩 9 | */ 10 | public interface StaticMethodAroundInterceptor { 11 | 12 | /** 13 | * before 14 | * 15 | * @param clazz : 16 | * @param method : 17 | * @param allArguments : 18 | * @param parameterTypes : 19 | * @return : void 20 | * @author 江浩 21 | */ 22 | void before(Class clazz, Method method, Object[] allArguments, Class[] parameterTypes); 23 | 24 | /** 25 | * after 26 | * 27 | * @param clazz : 28 | * @param method : 29 | * @param allArguments : 30 | * @param parameterTypes : 31 | * @param ret : 32 | * @return : void 33 | * @author 江浩 34 | */ 35 | void after(Class clazz, Method method, Object[] allArguments, Class[] parameterTypes, Object ret); 36 | 37 | /** 38 | * exception 39 | * 40 | * @param clazz : 41 | * @param method : 42 | * @param allArguments : 43 | * @param parameterTypes : 44 | * @param t : 45 | * @return : void 46 | * @author 江浩 47 | */ 48 | void exception(Class clazz, Method method, Object[] allArguments, Class[] parameterTypes, 49 | Throwable t); 50 | } 51 | -------------------------------------------------------------------------------- /plugins/plugins-define/src/main/java/cn/bytes1024/hound/plugins/define/interceptor/supper/AbstractConstructorInterceptor.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.define.interceptor.supper; 2 | 3 | import cn.bytes1024.hound.plugins.define.EnhancedDefine; 4 | import cn.bytes1024.hound.plugins.define.TraceContext; 5 | import cn.bytes1024.hound.plugins.define.interceptor.ConstructorInterceptor; 6 | import com.alipay.common.tracer.core.span.SofaTracerSpan; 7 | 8 | import java.util.Objects; 9 | 10 | /** 11 | * @author 江浩 12 | */ 13 | public class AbstractConstructorInterceptor implements ConstructorInterceptor { 14 | 15 | protected TraceContext traceContext; 16 | 17 | public AbstractConstructorInterceptor(TraceContext traceContext) { 18 | this.traceContext = traceContext; 19 | } 20 | 21 | @Override 22 | public void onConstruct(EnhancedDefine enhancedDefine, Object[] allArguments) { 23 | SofaTracerSpan sofaTracerSpan = traceContext.getCurrentSpan(); 24 | if (!Objects.isNull(enhancedDefine)) { 25 | enhancedDefine.setEnhancedInstanceTraceContext(sofaTracerSpan); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /plugins/plugins-define/src/main/java/cn/bytes1024/hound/plugins/define/interceptor/supper/AbstractMethodAroundInterceptor.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.define.interceptor.supper; 2 | 3 | import cn.bytes1024.hound.plugins.define.InterceptContext; 4 | import cn.bytes1024.hound.plugins.define.TraceContext; 5 | import cn.bytes1024.hound.plugins.define.interceptor.InterceptorPluginAware; 6 | import cn.bytes1024.hound.plugins.define.interceptor.MethodAroundInterceptor; 7 | import com.alipay.common.tracer.core.context.span.SofaTracerSpanContext; 8 | import com.alipay.common.tracer.core.span.SofaTracerSpan; 9 | import lombok.Getter; 10 | import lombok.extern.slf4j.Slf4j; 11 | import org.apache.commons.lang3.StringUtils; 12 | 13 | import java.util.Objects; 14 | 15 | import static io.opentracing.tag.Tags.SPAN_KIND; 16 | import static io.opentracing.tag.Tags.SPAN_KIND_SERVER; 17 | 18 | /** 19 | * 方法拦截器 20 | * 21 | * @author 江浩 22 | */ 23 | @Getter 24 | @Slf4j 25 | public abstract class AbstractMethodAroundInterceptor implements MethodAroundInterceptor, InterceptorPluginAware { 26 | 27 | public static final String TRANSMISSION_KEY = "TRANSMISSION_KEY"; 28 | 29 | private TraceContext traceContext; 30 | 31 | private String pluginName; 32 | 33 | 34 | public AbstractMethodAroundInterceptor(TraceContext traceContext) { 35 | this.traceContext = traceContext; 36 | } 37 | 38 | 39 | @Override 40 | public void defineName(String pluginName) { 41 | this.pluginName = pluginName; 42 | } 43 | 44 | @Override 45 | public void before(InterceptContext interceptContext) { 46 | try { 47 | SofaTracerSpan sofaTracerSpan = traceContext.getCurrentSpan(); 48 | if (Objects.isNull(sofaTracerSpan)) { 49 | log.info("current span is empty ! {} \n\t\t{}\t{}", Thread.currentThread().getId() 50 | , interceptContext.getTarget().getClass(), interceptContext.getMethod().getName()); 51 | return; 52 | } 53 | this.currentTracerSpan(sofaTracerSpan, interceptContext); 54 | } catch (Exception e) { 55 | e.printStackTrace(); 56 | throw e; 57 | } 58 | } 59 | 60 | 61 | /** 62 | * 构建当前的span信息 63 | * 64 | * @param preSofaTracerSpan : 上一个span信息 65 | * @param interceptContext : 构建当前span请求信息 66 | * @return : com.alipay.common.tracer.core.span.SofaTracerSpan 67 | * @author 江浩 68 | */ 69 | public SofaTracerSpan currentTracerSpan(SofaTracerSpan preSofaTracerSpan, InterceptContext interceptContext) { 70 | SofaTracerSpan thatSofaTracerSpan = (SofaTracerSpan) getTraceContext() 71 | .getSofaTracer() 72 | .buildSpan(StringUtils.isBlank(pluginName) ? this.getClass().getName() : pluginName) 73 | .withTag(SPAN_KIND.getKey(), SPAN_KIND_SERVER) 74 | .asChildOf(preSofaTracerSpan) 75 | .start(); 76 | SofaTracerSpanContext sofaTracerSpanContext = thatSofaTracerSpan.getSofaTracerSpanContext(); 77 | sofaTracerSpanContext.addBizBaggage(interceptContext.getProps()); 78 | getTraceContext().push(thatSofaTracerSpan); 79 | return thatSofaTracerSpan; 80 | } 81 | 82 | 83 | @Override 84 | public void after(InterceptContext interceptContext) { 85 | getTraceContext().stopCurrentTracerSpan(interceptContext); 86 | } 87 | 88 | 89 | } 90 | -------------------------------------------------------------------------------- /plugins/plugins-define/src/main/java/cn/bytes1024/hound/plugins/define/interceptor/supper/AbstractTransmissionMethodAroundInterceptor.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.define.interceptor.supper; 2 | 3 | import cn.bytes1024.hound.plugins.define.InterceptContext; 4 | import cn.bytes1024.hound.plugins.define.TraceContext; 5 | import com.alibaba.fastjson.JSONObject; 6 | import com.alipay.common.tracer.core.context.span.SofaTracerSpanContext; 7 | import com.alipay.common.tracer.core.span.SofaTracerSpan; 8 | import lombok.Getter; 9 | import lombok.NonNull; 10 | import lombok.extern.slf4j.Slf4j; 11 | import org.apache.commons.lang3.StringUtils; 12 | 13 | import java.util.HashMap; 14 | import java.util.Objects; 15 | 16 | import static io.opentracing.tag.Tags.SPAN_KIND; 17 | import static io.opentracing.tag.Tags.SPAN_KIND_SERVER; 18 | 19 | /** 20 | * 远程传递操作方法 21 | *

22 | * 23 | * @author 江浩 24 | */ 25 | @Getter 26 | @Slf4j 27 | public abstract class AbstractTransmissionMethodAroundInterceptor extends AbstractMethodAroundInterceptor { 28 | 29 | public AbstractTransmissionMethodAroundInterceptor(TraceContext traceContext) { 30 | super(traceContext); 31 | } 32 | 33 | private H remoteHandle; 34 | 35 | private RemoteTransmission remoteTransmission; 36 | 37 | public void setRemoteTransmission(RemoteTransmission remoteTransmission) { 38 | this.remoteTransmission = remoteTransmission; 39 | } 40 | 41 | @Override 42 | public void before(InterceptContext interceptContext) { 43 | invoker(interceptContext); 44 | } 45 | 46 | 47 | public void before(@NonNull H handler, @NonNull InterceptContext interceptContext) { 48 | //setRemoteHandle(handler); 49 | this.remoteHandle = handler; 50 | invoker(interceptContext); 51 | } 52 | 53 | private void invoker(InterceptContext interceptContext) { 54 | SofaTracerSpan sofaTracerSpan = getTraceContext().getCurrentSpan(); 55 | if (!Objects.isNull(sofaTracerSpan)) { 56 | SofaTracerSpanContext sofaTracerSpanContext = sofaTracerSpan.getSofaTracerSpanContext(); 57 | remoteTransmission.transmission(remoteHandle, TRANSMISSION_KEY, sofaTracerSpanContext); 58 | } 59 | //before 60 | super.before(interceptContext); 61 | } 62 | 63 | 64 | /** 65 | * 构建当前span信息 66 | * 67 | * @param handler : 68 | * @param interceptContext : 69 | * @return : void 70 | * @author 江浩 71 | */ 72 | protected void currentTracerSpan(H handler, InterceptContext interceptContext) { 73 | try { 74 | SofaTracerSpanContext sofaTracerSpanContext = getSpanContextFrom(handler); 75 | this.currentTracerSpan(sofaTracerSpanContext, interceptContext); 76 | } catch (Exception e) { 77 | e.printStackTrace(); 78 | } 79 | } 80 | 81 | 82 | /** 83 | * 根据context 构建span信息 84 | * 85 | * @param sofaTracerSpanContext : 86 | * @param interceptContext : 87 | * @return : void 88 | * @author 江浩 89 | */ 90 | public void currentTracerSpan(SofaTracerSpanContext sofaTracerSpanContext, InterceptContext interceptContext) { 91 | SofaTracerSpan currentTracerSpan = new SofaTracerSpan(getTraceContext().getSofaTracer(), 92 | System.currentTimeMillis(), getPluginName(), sofaTracerSpanContext 93 | , new HashMap(5) { 94 | { 95 | put(SPAN_KIND.getKey(), SPAN_KIND_SERVER); 96 | } 97 | }); 98 | super.currentTracerSpan(currentTracerSpan, interceptContext); 99 | } 100 | 101 | /** 102 | * 转换context 103 | * 104 | * @param spanContextJson : 105 | * @return : com.alipay.common.tracer.core.context.span.SofaTracerSpanContext 106 | * @author 江浩 107 | */ 108 | public SofaTracerSpanContext convert(String spanContextJson) { 109 | if (StringUtils.isBlank(spanContextJson)) { 110 | return null; 111 | } 112 | try { 113 | return JSONObject.parseObject(spanContextJson, SofaTracerSpanContext.class); 114 | } catch (Exception e) { 115 | return null; 116 | } 117 | } 118 | 119 | 120 | public String convert(SofaTracerSpanContext sofaTracerSpanContext) { 121 | try { 122 | return JSONObject.toJSONString(sofaTracerSpanContext); 123 | } catch (Exception e) { 124 | return null; 125 | } 126 | } 127 | 128 | 129 | /** 130 | * 获取信息 131 | * 132 | * @param handler : 133 | * @return : com.alipay.common.tracer.core.context.span.SofaTracerSpanContext 134 | * @author 江浩 135 | */ 136 | private SofaTracerSpanContext getSpanContextFrom(H handler) { 137 | return this.remoteTransmission.receive(handler, TRANSMISSION_KEY); 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /plugins/plugins-define/src/main/java/cn/bytes1024/hound/plugins/define/interceptor/supper/RemoteTransmission.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.define.interceptor.supper; 2 | 3 | import com.alipay.common.tracer.core.context.span.SofaTracerSpanContext; 4 | import lombok.NonNull; 5 | 6 | /** 7 | * 远程传输 8 | * 9 | * @author 江浩 10 | */ 11 | public interface RemoteTransmission { 12 | 13 | /** 14 | * 传输 15 | * 16 | * @param handler : 17 | * @param key : 18 | * @param spanContext : 19 | * @return : void 20 | * @author 江浩 21 | */ 22 | default void transmission(@NonNull H handler, @NonNull String key, @NonNull SofaTracerSpanContext spanContext) { 23 | 24 | } 25 | 26 | /** 27 | * 接收处理信息 28 | * 29 | * @param handler : 30 | * @param key : 31 | * @return : com.alipay.common.tracer.core.context.span.SofaTracerSpanContext 32 | * @author 江浩 33 | */ 34 | default SofaTracerSpanContext receive(@NonNull H handler, @NonNull String key) { 35 | return null; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /plugins/plugins-define/src/main/java/cn/bytes1024/hound/plugins/define/interceptor/supper/async/AbstractAsyncMethodAroundInterceptor.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.define.interceptor.supper.async; 2 | 3 | import cn.bytes1024.hound.plugins.define.EnhancedDefine; 4 | import cn.bytes1024.hound.plugins.define.InterceptContext; 5 | import cn.bytes1024.hound.plugins.define.TraceContext; 6 | import cn.bytes1024.hound.plugins.define.interceptor.supper.AbstractMethodAroundInterceptor; 7 | import com.alipay.common.tracer.core.span.SofaTracerSpan; 8 | import lombok.Getter; 9 | 10 | import java.util.Objects; 11 | 12 | /** 13 | * 方法拦截器 14 | *

异步获取当前活跃span只是根据当前的Object中来获取

15 | *

16 | * 1.异步的传递目前只有构造器传递 17 | *

18 | * 19 | * @author 江浩 20 | */ 21 | @Getter 22 | public abstract class AbstractAsyncMethodAroundInterceptor extends AbstractMethodAroundInterceptor { 23 | 24 | public AbstractAsyncMethodAroundInterceptor(TraceContext traceContext) { 25 | super(traceContext); 26 | } 27 | 28 | @Override 29 | public void before(InterceptContext interceptContext) { 30 | if (Objects.isNull(interceptContext)) { 31 | return; 32 | } 33 | Object object = interceptContext.getTarget(); 34 | if (Objects.isNull(object)) { 35 | return; 36 | } 37 | if (object instanceof EnhancedDefine) { 38 | EnhancedDefine enhancedDefine = (EnhancedDefine) object; 39 | SofaTracerSpan sofaTracerSpan = enhancedDefine.getEnhancedInstanceTraceContext(); 40 | super.currentTracerSpan(sofaTracerSpan, interceptContext); 41 | } 42 | 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /plugins/plugins-druid/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | plugins 7 | cn.bytes1024.hound 8 | 1.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | plugins-druid 13 | 14 | 15 | 16 | 17 | 18 | cn.bytes1024.hound 19 | plugins-define 20 | 21 | 22 | 23 | com.alibaba 24 | druid 25 | 1.1.10 26 | compile 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /plugins/plugins-druid/src/main/java/cn/bytes1024/hound/plugins/druid/DruidDataSourcePlugin.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.druid; 2 | 3 | import cn.bytes1024.hound.plugins.define.AbstractPluginDefine; 4 | import cn.bytes1024.hound.plugins.druid.interceptor.DruidPluginInterceptor; 5 | 6 | import static net.bytebuddy.matcher.ElementMatchers.named; 7 | import static net.bytebuddy.matcher.ElementMatchers.takesArguments; 8 | 9 | /** 10 | * druid 数据源 11 | * 12 | * @author 江浩 13 | */ 14 | public class DruidDataSourcePlugin extends AbstractPluginDefine { 15 | @Override 16 | public void init(PluginDefineBuilder defineBuilder) { 17 | defineBuilder.pointName("plugin-druid-dataSource") 18 | .pointClass(named("com.alibaba.druid.pool.DruidDataSource")) 19 | .pointMethod(named("close") 20 | .or(named("getPooledConnection") 21 | .or(named("getConnection").and(takesArguments(1))) 22 | ) 23 | , DruidPluginInterceptor.class); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /plugins/plugins-druid/src/main/java/cn/bytes1024/hound/plugins/druid/DruidPooledConnectionPlugin.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.druid; 2 | 3 | import cn.bytes1024.hound.plugins.define.AbstractPluginDefine; 4 | import cn.bytes1024.hound.plugins.druid.interceptor.DruidPluginInterceptor; 5 | 6 | import static net.bytebuddy.matcher.ElementMatchers.named; 7 | 8 | /** 9 | * druid 数据源 10 | * 11 | * @author 江浩 12 | */ 13 | public class DruidPooledConnectionPlugin extends AbstractPluginDefine { 14 | @Override 15 | public void init(PluginDefineBuilder defineBuilder) { 16 | defineBuilder.pointName("plugin-druid-pooledConnection") 17 | .pointClass(named("com.alibaba.druid.pool.DruidPooledConnection")) 18 | .pointMethod(named("close"), DruidPluginInterceptor.class); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /plugins/plugins-druid/src/main/java/cn/bytes1024/hound/plugins/druid/interceptor/DruidPluginInterceptor.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.druid.interceptor; 2 | 3 | import cn.bytes1024.hound.plugins.define.TraceContext; 4 | import cn.bytes1024.hound.plugins.define.interceptor.supper.AbstractMethodAroundInterceptor; 5 | 6 | /** 7 | * druid 默认拦截器 8 | * 9 | * @author 江浩 10 | */ 11 | public class DruidPluginInterceptor extends AbstractMethodAroundInterceptor { 12 | public DruidPluginInterceptor(TraceContext traceContext) { 13 | super(traceContext); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /plugins/plugins-druid/src/main/resources/META-INF/plugins/cn.bytes1024.hound.plugins.define.PluginDefine: -------------------------------------------------------------------------------- 1 | plugin-druid-pooledConnection = cn.bytes1024.hound.plugins.druid.DruidPooledConnectionPlugin 2 | 3 | plugin-druid-dataSource = cn.bytes1024.hound.plugins.druid.DruidDataSourcePlugin 4 | -------------------------------------------------------------------------------- /plugins/plugins-fastjson/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | plugins 7 | cn.bytes1024.hound 8 | 1.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | plugins-fastjson 13 | 14 | 15 | 16 | 17 | 18 | cn.bytes1024.hound 19 | plugins-define 20 | 21 | 22 | 23 | com.alibaba 24 | fastjson 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /plugins/plugins-fastjson/src/main/java/cn/bytes1024/hound/plugins/fastjson/FastJsonPlugin.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.fastjson; 2 | 3 | import cn.bytes1024.hound.plugins.define.AbstractPluginDefine; 4 | import cn.bytes1024.hound.plugins.fastjson.interceptor.FastJsonMethodInterceptor; 5 | 6 | import static net.bytebuddy.matcher.ElementMatchers.named; 7 | 8 | /** 9 | * fastJson插件信息 10 | * 11 | * @author 江浩 12 | */ 13 | public class FastJsonPlugin extends AbstractPluginDefine { 14 | @Override 15 | public void init(PluginDefineBuilder defineBuilder) { 16 | 17 | defineBuilder.pointName("plugin-fastJson") 18 | .pointClass(named("com.alibaba.fastjson.JSON")) 19 | .pointMethod( 20 | named("parse") 21 | .or(named("parseObject")) 22 | .or(named("parseArray")) 23 | .or(named("toJSON")) 24 | .or(named("toJavaObject")) 25 | .or(named("toJSONString")) 26 | .or(named("toJSONBytes")) 27 | .or(named("writeJSONString")) 28 | , 29 | FastJsonMethodInterceptor.class 30 | 31 | ); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /plugins/plugins-fastjson/src/main/java/cn/bytes1024/hound/plugins/fastjson/interceptor/FastJsonMethodInterceptor.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.fastjson.interceptor; 2 | 3 | import cn.bytes1024.hound.plugins.define.TraceContext; 4 | import cn.bytes1024.hound.plugins.define.interceptor.supper.AbstractMethodAroundInterceptor; 5 | 6 | /** 7 | * fastjson插件拦截器 8 | * 9 | * @author 江浩 10 | */ 11 | public class FastJsonMethodInterceptor extends AbstractMethodAroundInterceptor { 12 | public FastJsonMethodInterceptor(TraceContext traceContext) { 13 | super(traceContext); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /plugins/plugins-fastjson/src/main/resources/META-INF/plugins/cn.bytes1024.hound.plugins.define.PluginDefine: -------------------------------------------------------------------------------- 1 | plugin-fastJson=cn.bytes1024.hound.plugins.fastjson.FastJsonPlugin -------------------------------------------------------------------------------- /plugins/plugins-gson/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | plugins 7 | cn.bytes1024.hound 8 | 1.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | plugins-gson 13 | 14 | 15 | 16 | 17 | cn.bytes1024.hound 18 | plugins-define 19 | 20 | 21 | 22 | com.google.code.gson 23 | gson 24 | 2.8.0 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /plugins/plugins-gson/src/main/java/cn/bytes1024/hound/plugins/gson/GsonPlugin.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.gson; 2 | 3 | import cn.bytes1024.hound.plugins.define.AbstractPluginDefine; 4 | import cn.bytes1024.hound.plugins.gson.intercepotr.GsonMethodInterceptor; 5 | 6 | import static net.bytebuddy.matcher.ElementMatchers.*; 7 | 8 | /** 9 | * gson插件 10 | * 11 | * @author 江浩 12 | */ 13 | public class GsonPlugin extends AbstractPluginDefine { 14 | @Override 15 | public void init(PluginDefineBuilder defineBuilder) { 16 | 17 | 18 | //Reader json, Type typeOfT 19 | //JsonElement jsonElement, Appendable writer 20 | // defineBuilder.pointName("plugin-gson") 21 | // .pointClass(named("com.google.gson.Gson")) 22 | // .pointMethod( 23 | // named("toJson") 24 | // .and(takesArgument(0, JsonElement.class) 25 | // .and(takesArgument(1, JsonWriter.class))) 26 | // .or(named("fromJson").and(takesArgument(0, Reader.class) 27 | // .and(takesArgument(1, Type.class)))) 28 | // , GsonMethodInterceptor.class); 29 | 30 | 31 | defineBuilder.pointName("plugin-gson") 32 | .pointClass(named("com.google.gson.Gson")) 33 | .pointMethod( 34 | named("toJson") 35 | .and(takesArguments(1)) 36 | .or(named("fromJson") 37 | .and(takesArgument(0, String.class)) 38 | .and(takesArgument(1, Class.class)) 39 | ) 40 | , GsonMethodInterceptor.class); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /plugins/plugins-gson/src/main/java/cn/bytes1024/hound/plugins/gson/intercepotr/GsonMethodInterceptor.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.gson.intercepotr; 2 | 3 | import cn.bytes1024.hound.plugins.define.TraceContext; 4 | import cn.bytes1024.hound.plugins.define.interceptor.supper.AbstractMethodAroundInterceptor; 5 | 6 | /** 7 | * Gson插件拦截器 8 | * 9 | * @author 江浩 10 | */ 11 | public class GsonMethodInterceptor extends AbstractMethodAroundInterceptor { 12 | public GsonMethodInterceptor(TraceContext traceContext) { 13 | super(traceContext); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /plugins/plugins-gson/src/main/resources/META-INF/plugins/cn.bytes1024.hound.plugins.define.PluginDefine: -------------------------------------------------------------------------------- 1 | plugin-gson=cn.bytes1024.hound.plugins.gson.GsonPlugin -------------------------------------------------------------------------------- /plugins/plugins-httpclient/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | plugins 7 | cn.bytes1024.hound 8 | 1.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | plugins-httpclient 13 | 14 | 15 | 16 | 17 | 18 | 19 | cn.bytes1024.hound 20 | plugins-define 21 | 22 | 23 | 24 | 25 | 26 | org.apache.httpcomponents 27 | httpclient 28 | 4.5.2 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /plugins/plugins-httpclient/src/main/java/cn/bytes1024/hound/plugins/httpclient/HttpClientPlugin.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.httpclient; 2 | 3 | import cn.bytes1024.hound.plugins.define.AbstractPluginDefine; 4 | import cn.bytes1024.hound.plugins.httpclient.interceptor.HttpClientMethodInterceptor; 5 | 6 | import static net.bytebuddy.matcher.ElementMatchers.named; 7 | 8 | /** 9 | * httpClient 10 | * 11 | * @author 江浩 12 | */ 13 | public class HttpClientPlugin extends AbstractPluginDefine { 14 | @Override 15 | public void init(PluginDefineBuilder defineBuilder) { 16 | defineBuilder.pointName("plugin-httpClient") 17 | .pointClass(named("org.apache.http.impl.client.MinimalHttpClient") 18 | .or(named("org.apache.http.impl.client.InternalHttpClient")) 19 | .or(named("org.apache.http.impl.client.AbstractHttpClient"))) 20 | .pointMethod(named("execute"), HttpClientMethodInterceptor.class); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /plugins/plugins-httpclient/src/main/java/cn/bytes1024/hound/plugins/httpclient/interceptor/HttpClientMethodInterceptor.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.httpclient.interceptor; 2 | 3 | import cn.bytes1024.hound.plugins.define.InterceptContext; 4 | import cn.bytes1024.hound.plugins.define.TraceContext; 5 | import cn.bytes1024.hound.plugins.define.interceptor.supper.AbstractTransmissionMethodAroundInterceptor; 6 | import cn.bytes1024.hound.plugins.define.interceptor.supper.RemoteTransmission; 7 | import com.alipay.common.tracer.core.context.span.SofaTracerSpanContext; 8 | import org.apache.http.HttpRequest; 9 | 10 | /** 11 | * httpclient 插件 12 | *

13 | * 需要远程传递 14 | *

15 | * 16 | * @author 江浩 17 | */ 18 | public class HttpClientMethodInterceptor extends AbstractTransmissionMethodAroundInterceptor { 19 | public HttpClientMethodInterceptor(TraceContext traceContext) { 20 | super(traceContext); 21 | setRemoteTransmission(new RemoteTransmission() { 22 | @Override 23 | public void transmission(HttpRequest handler, String key, SofaTracerSpanContext spanContext) { 24 | handler.setHeader(key, convert(spanContext)); 25 | } 26 | }); 27 | } 28 | 29 | 30 | @Override 31 | public void before(InterceptContext interceptContext) { 32 | HttpRequest httpRequest = getHttpRequest(interceptContext.getArgs()); 33 | super.before(httpRequest, interceptContext); 34 | } 35 | 36 | private HttpRequest getHttpRequest(Object[] args) { 37 | if (args != null && args.length >= 1 && args[0] != null && args[0] instanceof HttpRequest) { 38 | return (HttpRequest) args[0]; 39 | } 40 | return null; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /plugins/plugins-httpclient/src/main/resources/META-INF/plugins/cn.bytes1024.hound.plugins.define.PluginDefine: -------------------------------------------------------------------------------- 1 | plugin-httpclient=cn.bytes1024.hound.plugins.httpclient.HttpClientPlugin -------------------------------------------------------------------------------- /plugins/plugins-mybatis/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | plugins 7 | cn.bytes1024.hound 8 | 1.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | plugins-mybatis 13 | 14 | 15 | 16 | 17 | cn.bytes1024.hound 18 | plugins-define 19 | 20 | 21 | 22 | 23 | org.mybatis 24 | mybatis 25 | 3.5.2 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /plugins/plugins-mybatis/src/main/java/cn/bytes1024/hound/plugins/mybatis/MybatisAnnotationMethodPlugin.java: -------------------------------------------------------------------------------- 1 | //package cn.bytes1024.hound.plugins.mybatis; 2 | // 3 | //import cn.bytes1024.hound.plugins.define.AbstractPluginDefine; 4 | //import cn.bytes1024.hound.plugins.mybatis.interceptor.MybatisPluginInterceptor; 5 | //import org.apache.ibatis.annotations.Delete; 6 | //import org.apache.ibatis.annotations.Select; 7 | //import org.apache.ibatis.annotations.Update; 8 | // 9 | //import static net.bytebuddy.matcher.ElementMatchers.*; 10 | // 11 | ///** 12 | // * mybatis 插件 13 | // * 注解定义的是接口需要找到接口指定的入口 TODO 14 | // * @author 江浩 15 | // */ 16 | //public class MybatisAnnotationMethodPlugin extends AbstractPluginDefine { 17 | // @Override 18 | // public void init(PluginDefineBuilder defineBuilder) { 19 | // defineBuilder.pointName("plugin-mybatis-annotation") 20 | // .pointClass(any()) 21 | // .pointMethod(isAnnotatedWith(named(Select.class.getName())) 22 | // .or(isAnnotatedWith(named(Update.class.getName()))) 23 | // .or(isAnnotatedWith(named(Delete.class.getName()))) 24 | // , MybatisPluginInterceptor.class); 25 | // } 26 | //} 27 | -------------------------------------------------------------------------------- /plugins/plugins-mybatis/src/main/java/cn/bytes1024/hound/plugins/mybatis/MybatisBoundSqlPlugin.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.mybatis; 2 | 3 | import cn.bytes1024.hound.plugins.define.AbstractPluginDefine; 4 | import cn.bytes1024.hound.plugins.mybatis.interceptor.MybatisBoundSqlPluginInterceptor; 5 | 6 | import static net.bytebuddy.matcher.ElementMatchers.named; 7 | 8 | /** 9 | * 执行sql语句 10 | * 11 | * @author 江浩 12 | */ 13 | public class MybatisBoundSqlPlugin extends AbstractPluginDefine { 14 | @Override 15 | public void init(PluginDefineBuilder defineBuilder) { 16 | defineBuilder.pointName("plugin-mybatis-boundSql") 17 | .pointClass(named("org.apache.ibatis.mapping.MappedStatement")) 18 | .pointMethod( 19 | named("getBoundSql") 20 | , MybatisBoundSqlPluginInterceptor.class); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /plugins/plugins-mybatis/src/main/java/cn/bytes1024/hound/plugins/mybatis/MybatisPlugin.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.mybatis; 2 | 3 | import cn.bytes1024.hound.plugins.define.AbstractPluginDefine; 4 | import cn.bytes1024.hound.plugins.mybatis.interceptor.MybatisPluginInterceptor; 5 | 6 | import static net.bytebuddy.matcher.ElementMatchers.named; 7 | import static net.bytebuddy.matcher.ElementMatchers.takesArguments; 8 | 9 | /** 10 | * mybatis 插件 11 | * 12 | * @author 江浩 13 | */ 14 | public class MybatisPlugin extends AbstractPluginDefine { 15 | @Override 16 | public void init(PluginDefineBuilder defineBuilder) { 17 | 18 | defineBuilder.pointName("plugin-mybatis") 19 | .pointClass(named("org.apache.ibatis.session.defaults.DefaultSqlSession") 20 | .or(named("org.mybatis.spring.SqlSessionTemplate") 21 | ) 22 | ) 23 | .pointMethod( 24 | named("selectOne").and(takesArguments(2)), MybatisPluginInterceptor.class) 25 | .pointMethod(named("selectList").and(takesArguments(3)), MybatisPluginInterceptor.class) 26 | .pointMethod(named("selectMap").and(takesArguments(4)), MybatisPluginInterceptor.class) 27 | .pointMethod(named("select").and(takesArguments(3)), MybatisPluginInterceptor.class) 28 | .pointMethod(named("insert"), MybatisPluginInterceptor.class) 29 | .pointMethod(named("update"), MybatisPluginInterceptor.class) 30 | .pointMethod(named("delete"), MybatisPluginInterceptor.class); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /plugins/plugins-mybatis/src/main/java/cn/bytes1024/hound/plugins/mybatis/interceptor/MybatisBoundSqlPluginInterceptor.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.mybatis.interceptor; 2 | 3 | import cn.bytes1024.hound.plugins.define.InterceptContext; 4 | import cn.bytes1024.hound.plugins.define.TraceContext; 5 | import cn.bytes1024.hound.plugins.define.interceptor.supper.AbstractMethodAroundInterceptor; 6 | import lombok.extern.slf4j.Slf4j; 7 | import org.apache.ibatis.mapping.BoundSql; 8 | 9 | import java.util.HashMap; 10 | 11 | @Slf4j 12 | public class MybatisBoundSqlPluginInterceptor extends AbstractMethodAroundInterceptor { 13 | public MybatisBoundSqlPluginInterceptor(TraceContext traceContext) { 14 | super(traceContext); 15 | } 16 | 17 | @Override 18 | public void after(InterceptContext interceptContext) { 19 | try { 20 | Object object = interceptContext.getResult(); 21 | if (object instanceof BoundSql) { 22 | BoundSql boundSql = (BoundSql) object; 23 | interceptContext.setProps(new HashMap() {{ 24 | putIfAbsent("sql", boundSql.getSql()); 25 | }}); 26 | } 27 | } catch (Exception e) { 28 | } 29 | 30 | super.after(interceptContext); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /plugins/plugins-mybatis/src/main/java/cn/bytes1024/hound/plugins/mybatis/interceptor/MybatisPluginInterceptor.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.mybatis.interceptor; 2 | 3 | import cn.bytes1024.hound.plugins.define.InterceptContext; 4 | import cn.bytes1024.hound.plugins.define.TraceContext; 5 | import cn.bytes1024.hound.plugins.define.interceptor.supper.AbstractMethodAroundInterceptor; 6 | 7 | /** 8 | * mybatis 默认插件 9 | * 10 | * @author 江浩 11 | */ 12 | public class MybatisPluginInterceptor extends AbstractMethodAroundInterceptor { 13 | 14 | public MybatisPluginInterceptor(TraceContext traceContext) { 15 | super(traceContext); 16 | } 17 | 18 | 19 | @Override 20 | public void before(InterceptContext interceptContext) { 21 | // 22 | // Object[] args = interceptContext.getArgs(); 23 | // System.out.println(args[0]); 24 | super.before(interceptContext); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /plugins/plugins-mybatis/src/main/resources/META-INF/plugins/cn.bytes1024.hound.plugins.define.PluginDefine: -------------------------------------------------------------------------------- 1 | plugin-mybatis=cn.bytes1024.hound.plugins.mybatis.MybatisPlugin 2 | #plugin-mybatis-annotation=cn.bytes1024.hound.plugins.mybatis.MybatisAnnotationMethodPlugin 3 | plugin-mybatis-boundSql= cn.bytes1024.hound.plugins.mybatis.MybatisBoundSqlPlugin -------------------------------------------------------------------------------- /plugins/plugins-mysql-jdbc/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | plugins 7 | cn.bytes1024.hound 8 | 1.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | plugins-mysql-jdbc 13 | 14 | 15 | 16 | cn.bytes1024.hound 17 | plugins-define 18 | 19 | 20 | 21 | mysql 22 | mysql-connector-java 23 | 5.1.45 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /plugins/plugins-mysql-jdbc/src/main/java/cn/bytes1024/hound/plugins/mysql/jdbc/MysqlConnectionTransformerPlugin.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.mysql.jdbc; 2 | 3 | import cn.bytes1024.hound.plugins.define.AbstractPluginDefine; 4 | 5 | /** 6 | * @author 江浩 7 | */ 8 | public class MysqlConnectionTransformerPlugin extends AbstractPluginDefine { 9 | @Override 10 | public void init(PluginDefineBuilder defineBuilder) { 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /plugins/plugins-mysql-jdbc/src/main/java/cn/bytes1024/hound/plugins/mysql/jdbc/MysqlJdbcPlugin.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.mysql.jdbc; 2 | 3 | import cn.bytes1024.hound.plugins.define.AbstractPluginDefine; 4 | import cn.bytes1024.hound.plugins.mysql.jdbc.interceptor.MysqlJdbcInterceptor; 5 | 6 | import static net.bytebuddy.matcher.ElementMatchers.named; 7 | 8 | /** 9 | * mysql 插件 10 | * 11 | * @author 江浩 12 | */ 13 | public class MysqlJdbcPlugin extends AbstractPluginDefine { 14 | @Override 15 | public void init(PluginDefineBuilder defineBuilder) { 16 | 17 | defineBuilder.pointName(super.format("mysql")) 18 | .pointClass(named("com.mysql.cj.jdbc.PreparedStatement")) 19 | .pointMethod(named("execute") 20 | .or(named("executeQuery")) 21 | .or(named("executeUpdate")) 22 | .or(named("executeLargeUpdate")), MysqlJdbcInterceptor.class); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /plugins/plugins-mysql-jdbc/src/main/java/cn/bytes1024/hound/plugins/mysql/jdbc/MysqlJdbcPreparedCallPlugin.java: -------------------------------------------------------------------------------- 1 | //package cn.bytes1024.hound.plugins.mysql.jdbc; 2 | // 3 | //import cn.bytes1024.hound.plugins.define.AbstractPluginDefine; 4 | //import cn.bytes1024.hound.plugins.mysql.jdbc.interceptor.MysqlJdbcPreparedCallInterceptor; 5 | // 6 | //import static net.bytebuddy.matcher.ElementMatchers.named; 7 | //import static net.bytebuddy.matcher.ElementMatchers.takesArguments; 8 | // 9 | ///** 10 | // * mysql jdbc 插件 11 | // * 12 | // * @author 江浩 13 | // */ 14 | //public class MysqlJdbcPreparedCallPlugin extends AbstractPluginDefine { 15 | // @Override 16 | // public void init(PluginDefineBuilder defineBuilder) { 17 | // defineBuilder.pointName(this.format("mysql-jdbc-preparedCall")) 18 | // .pointClass( 19 | // named("com.mysql.jdbc.CallableStatement") 20 | // .or(named("com.mysql.cj.jdbc.CallableStatement")) 21 | // ) 22 | // .pointMethod( 23 | // named("execute") 24 | // .or(named("executeQuery")) 25 | // .or(named("executeUpdate")) 26 | // .or(named("registerOutParameter") 27 | // .and(takesArguments(int.class, int.class)) 28 | // ) 29 | // .or(named("registerOutParameter") 30 | // .and(takesArguments(int.class, int.class, int.class)) 31 | // ) 32 | // .or(named("registerOutParameter") 33 | // .and(takesArguments(int.class, int.class, String.class)) 34 | // ) 35 | // , MysqlJdbcPreparedCallInterceptor.class); 36 | // } 37 | //} 38 | -------------------------------------------------------------------------------- /plugins/plugins-mysql-jdbc/src/main/java/cn/bytes1024/hound/plugins/mysql/jdbc/MysqlJdbcPreparedStatementPlugin.java: -------------------------------------------------------------------------------- 1 | //package cn.bytes1024.hound.plugins.mysql.jdbc; 2 | // 3 | //import cn.bytes1024.hound.plugins.define.AbstractPluginDefine; 4 | //import cn.bytes1024.hound.plugins.mysql.jdbc.interceptor.MysqlJdbcPreparedStatementInterceptor; 5 | // 6 | //import static net.bytebuddy.matcher.ElementMatchers.named; 7 | // 8 | ///** 9 | // * mysql jdbc 插件 10 | // * 11 | // * @author 江浩 12 | // */ 13 | //public class MysqlJdbcPreparedStatementPlugin extends AbstractPluginDefine { 14 | // @Override 15 | // public void init(PluginDefineBuilder defineBuilder) { 16 | // defineBuilder.pointName(this.format("mysql-jdbc-preparedStatement")) 17 | // .pointClass( 18 | // named("com.mysql.jdbc.PreparedStatement") 19 | // .or(named("com.mysql.cj.jdbc.PreparedStatement")) 20 | // .or(named("com.mysql.cj.jdbc.ClientPreparedStatement")) 21 | // .or(named("com.mysql.cj.jdbc.ServerPreparedStatement")) 22 | // ) 23 | // .pointMethod(named("execute") 24 | // .or(named("executeQuery")) 25 | // .or(named("executeUpdate")) 26 | // , MysqlJdbcPreparedStatementInterceptor.class) 27 | // 28 | // ; 29 | // } 30 | //} 31 | -------------------------------------------------------------------------------- /plugins/plugins-mysql-jdbc/src/main/java/cn/bytes1024/hound/plugins/mysql/jdbc/interceptor/MysqlJdbcInterceptor.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.mysql.jdbc.interceptor; 2 | 3 | import cn.bytes1024.hound.plugins.define.InterceptContext; 4 | import cn.bytes1024.hound.plugins.define.TraceContext; 5 | import cn.bytes1024.hound.plugins.define.interceptor.supper.AbstractMethodAroundInterceptor; 6 | 7 | public class MysqlJdbcInterceptor extends AbstractMethodAroundInterceptor { 8 | 9 | public MysqlJdbcInterceptor(TraceContext traceContext) { 10 | super(traceContext); 11 | } 12 | 13 | 14 | @Override 15 | public void before(InterceptContext interceptContext) { 16 | super.before(interceptContext); 17 | 18 | System.out.println(interceptContext.getMethod() + "==============>>"); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /plugins/plugins-mysql-jdbc/src/main/java/cn/bytes1024/hound/plugins/mysql/jdbc/interceptor/MysqlJdbcPreparedCallInterceptor.java: -------------------------------------------------------------------------------- 1 | //package cn.bytes1024.hound.plugins.mysql.jdbc.interceptor; 2 | // 3 | //import cn.bytes1024.hound.plugins.define.InterceptContext; 4 | //import cn.bytes1024.hound.plugins.define.TraceContext; 5 | //import cn.bytes1024.hound.plugins.define.interceptor.supper.AbstractMethodAroundInterceptor; 6 | // 7 | ///** 8 | // * preared call 9 | // * 10 | // * @author 江浩 11 | // */ 12 | //public class MysqlJdbcPreparedCallInterceptor extends AbstractMethodAroundInterceptor { 13 | // public MysqlJdbcPreparedCallInterceptor(TraceContext traceContext) { 14 | // super(traceContext); 15 | // } 16 | // 17 | // @Override 18 | // public void before(InterceptContext interceptContext) { 19 | // 20 | // Object[] objects = interceptContext.getArgs(); 21 | // if (objects != null) { 22 | // for (Object object : objects) { 23 | // System.out.println(object); 24 | // } 25 | // } 26 | // 27 | // 28 | // super.before(interceptContext); 29 | // } 30 | //} 31 | -------------------------------------------------------------------------------- /plugins/plugins-mysql-jdbc/src/main/java/cn/bytes1024/hound/plugins/mysql/jdbc/interceptor/MysqlJdbcPreparedStatementInterceptor.java: -------------------------------------------------------------------------------- 1 | //package cn.bytes1024.hound.plugins.mysql.jdbc.interceptor; 2 | // 3 | //import cn.bytes1024.hound.plugins.define.TraceContext; 4 | //import cn.bytes1024.hound.plugins.define.interceptor.supper.AbstractMethodAroundInterceptor; 5 | // 6 | ///** 7 | // * mysql preparedStatement 8 | // * 9 | // * @author 江浩 10 | // */ 11 | //public class MysqlJdbcPreparedStatementInterceptor extends AbstractMethodAroundInterceptor { 12 | // 13 | // public MysqlJdbcPreparedStatementInterceptor(TraceContext traceContext) { 14 | // super(traceContext); 15 | // } 16 | // 17 | //} 18 | -------------------------------------------------------------------------------- /plugins/plugins-mysql-jdbc/src/main/resources/META-INF/plugins/cn.bytes1024.hound.plugins.define.PluginDefine: -------------------------------------------------------------------------------- 1 | # plugin-mysql-jdbc-preparedStatement = cn.bytes1024.hound.plugins.mysql.jdbc.MysqlJdbcPreparedStatementPlugin 2 | 3 | # plugin-mysql-jdbc-preparedCall = cn.bytes1024.hound.plugins.mysql.jdbc.MysqlJdbcPreparedCallPlugin 4 | 5 | plugin-mysql = cn.bytes1024.hound.plugins.mysql.jdbc.MysqlJdbcPlugin -------------------------------------------------------------------------------- /plugins/plugins-okhttp/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | plugins 6 | cn.bytes1024.hound 7 | 1.0.1-SNAPSHOT 8 | 9 | 4.0.0 10 | 11 | plugins-okhttp 12 | 13 | 14 | 15 | 16 | cn.bytes1024.hound 17 | plugins-define 18 | 19 | 20 | 21 | com.squareup.okhttp3 22 | okhttp 23 | 3.7.0 24 | provided 25 | 26 | 27 | 28 | commons-collections 29 | commons-collections 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /plugins/plugins-okhttp/src/main/java/cn/bytes1024/hound/plugins/okhttp/Containts.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.okhttp; 2 | 3 | public class Containts { 4 | public static final String IGNORE_HEADER_KEY = "OKHTTP-IGNORE-DEFINE"; 5 | } 6 | -------------------------------------------------------------------------------- /plugins/plugins-okhttp/src/main/java/cn/bytes1024/hound/plugins/okhttp/OkHttpClientPlugin.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.okhttp; 2 | 3 | 4 | import cn.bytes1024.hound.plugins.define.AbstractPluginDefine; 5 | import cn.bytes1024.hound.plugins.okhttp.interceptor.OkHttpClientMethodInterceptor; 6 | 7 | import static net.bytebuddy.matcher.ElementMatchers.*; 8 | 9 | /** 10 | * @author 江浩 11 | */ 12 | public class OkHttpClientPlugin extends AbstractPluginDefine { 13 | @Override 14 | public void init(AbstractPluginDefine.PluginDefineBuilder defineBuilder) { 15 | 16 | defineBuilder 17 | .pointName("plugin-okHttp") 18 | .pointClass(named("okhttp3.OkHttpClient")) 19 | .pointMethod(named("newCall") 20 | .and(takesArguments(1)) 21 | .and(takesArgument(0, named("okhttp3.Request"))) 22 | , OkHttpClientMethodInterceptor.class); 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /plugins/plugins-okhttp/src/main/java/cn/bytes1024/hound/plugins/okhttp/OkHttpRealCallPlugin.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.okhttp; 2 | 3 | 4 | import cn.bytes1024.hound.plugins.define.AbstractPluginDefine; 5 | import cn.bytes1024.hound.plugins.okhttp.interceptor.OkHttpRealCallMethodInterceptor; 6 | 7 | import static net.bytebuddy.matcher.ElementMatchers.named; 8 | 9 | /** 10 | * okhttp3.RealCall 11 | * 12 | * @author 江浩 13 | */ 14 | public class OkHttpRealCallPlugin extends AbstractPluginDefine { 15 | @Override 16 | public void init(AbstractPluginDefine.PluginDefineBuilder defineBuilder) { 17 | defineBuilder.pointName("plugin-okHttp-realCall") 18 | .pointClass(named("okhttp3.RealCall")) 19 | .pointMethod(named("execute") 20 | .or(named("enqueue")) 21 | .or(named("cancel")), 22 | OkHttpRealCallMethodInterceptor.class 23 | ); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /plugins/plugins-okhttp/src/main/java/cn/bytes1024/hound/plugins/okhttp/interceptor/OkHttpClientMethodInterceptor.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.okhttp.interceptor; 2 | 3 | import cn.bytes1024.hound.plugins.define.InterceptContext; 4 | import cn.bytes1024.hound.plugins.define.TraceContext; 5 | import cn.bytes1024.hound.plugins.define.interceptor.IgnoreInterceptor; 6 | import cn.bytes1024.hound.plugins.define.interceptor.supper.AbstractTransmissionMethodAroundInterceptor; 7 | import cn.bytes1024.hound.plugins.define.interceptor.supper.RemoteTransmission; 8 | import cn.bytes1024.hound.plugins.okhttp.Containts; 9 | import com.alipay.common.tracer.core.context.span.SofaTracerSpanContext; 10 | import okhttp3.Headers; 11 | import okhttp3.Request; 12 | import org.apache.commons.collections.MapUtils; 13 | import org.apache.commons.lang3.StringUtils; 14 | 15 | import java.lang.reflect.Field; 16 | import java.util.List; 17 | import java.util.Map; 18 | import java.util.Objects; 19 | 20 | /** 21 | * ok http 拦截器 22 | * 23 | * @author 江浩 24 | */ 25 | public class OkHttpClientMethodInterceptor extends AbstractTransmissionMethodAroundInterceptor implements IgnoreInterceptor { 26 | 27 | 28 | public OkHttpClientMethodInterceptor(TraceContext traceContext) { 29 | super(traceContext); 30 | 31 | setRemoteTransmission(new RemoteTransmission() { 32 | @Override 33 | public void transmission(Request handler, String key, SofaTracerSpanContext spanContext) { 34 | try { 35 | Field headersField = Request.class.getDeclaredField("headers"); 36 | headersField.setAccessible(true); 37 | Headers.Builder headerBuilder = handler.headers().newBuilder(); 38 | //原始header信息 39 | Headers headers = handler.headers(); 40 | if (!Objects.isNull(headers)) { 41 | Map> multimap = headers.toMultimap(); 42 | if (MapUtils.isNotEmpty(multimap)) { 43 | for (Map.Entry> headerEntry : multimap.entrySet()) { 44 | headerBuilder.add(headerEntry.getKey(), StringUtils.join(headerEntry.getValue())); 45 | } 46 | } 47 | } 48 | 49 | headerBuilder.add(key, convert(spanContext)); 50 | headersField.set(handler, headerBuilder.build()); 51 | } catch (NoSuchFieldException | IllegalAccessException e) { 52 | e.printStackTrace(); 53 | } 54 | } 55 | }); 56 | } 57 | 58 | 59 | @Override 60 | public void before(InterceptContext interceptContext) { 61 | Object[] args = interceptContext.getArgs(); 62 | 63 | Object arg0 = args[0]; 64 | if (Objects.isNull(arg0) || !(arg0 instanceof Request)) { 65 | interceptContext.setIgnored(true); 66 | return; 67 | } 68 | 69 | Request request = (Request) arg0; 70 | if (ignore(request)) { 71 | interceptContext.setIgnored(true); 72 | return; 73 | } 74 | super.before(request, interceptContext); 75 | 76 | } 77 | 78 | @Override 79 | public void after(InterceptContext interceptContext) { 80 | if (Objects.isNull(interceptContext) || interceptContext.isIgnored()) { 81 | return; 82 | } 83 | super.after(interceptContext); 84 | } 85 | 86 | @Override 87 | public boolean ignore(Request handler) { 88 | 89 | return StringUtils.isNotBlank(handler.header(Containts.IGNORE_HEADER_KEY)); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /plugins/plugins-okhttp/src/main/java/cn/bytes1024/hound/plugins/okhttp/interceptor/OkHttpRealCallMethodInterceptor.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.okhttp.interceptor; 2 | 3 | 4 | import cn.bytes1024.hound.plugins.define.InterceptContext; 5 | import cn.bytes1024.hound.plugins.define.TraceContext; 6 | import cn.bytes1024.hound.plugins.define.interceptor.IgnoreInterceptor; 7 | import cn.bytes1024.hound.plugins.define.interceptor.supper.AbstractMethodAroundInterceptor; 8 | import cn.bytes1024.hound.plugins.okhttp.Containts; 9 | import okhttp3.Request; 10 | import org.apache.commons.lang3.StringUtils; 11 | 12 | import java.lang.reflect.Field; 13 | 14 | /** 15 | * ok http 拦截器 16 | * 17 | * @author 江浩 18 | */ 19 | public class OkHttpRealCallMethodInterceptor extends AbstractMethodAroundInterceptor implements IgnoreInterceptor { 20 | 21 | public OkHttpRealCallMethodInterceptor(TraceContext traceContext) { 22 | super(traceContext); 23 | } 24 | 25 | @Override 26 | public void after(InterceptContext interceptContext) { 27 | Object result = interceptContext.getResult(); 28 | super.after(interceptContext); 29 | } 30 | 31 | 32 | @Override 33 | public void before(InterceptContext interceptContext) { 34 | 35 | try { 36 | Field field = interceptContext.getTarget().getClass().getDeclaredField("originalRequest"); 37 | field.setAccessible(true); 38 | if (ignore((Request) field.get(interceptContext.getTarget()))) { 39 | interceptContext.setIgnored(true); 40 | return; 41 | } 42 | super.before(interceptContext); 43 | } catch (Exception e) { 44 | e.printStackTrace(); 45 | } 46 | } 47 | 48 | @Override 49 | public boolean ignore(Request handler) { 50 | return StringUtils.isNotBlank(handler.header(Containts.IGNORE_HEADER_KEY)); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /plugins/plugins-okhttp/src/main/resources/META-INF/plugins/cn.bytes1024.hound.plugins.define.PluginDefine: -------------------------------------------------------------------------------- 1 | plugin-okHttp-client = cn.bytes1024.hound.plugins.okhttp.OkHttpClientPlugin 2 | 3 | plugin-okHttp-realCall = cn.bytes1024.hound.plugins.okhttp.OkHttpRealCallPlugin 4 | -------------------------------------------------------------------------------- /plugins/plugins-spring/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | plugins 7 | cn.bytes1024.hound 8 | 1.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | plugins-spring 13 | 14 | 5.0.0.RELEASE 15 | 5.0.0.RELEASE 16 | 5.0.0.RELEASE 17 | 3.0.1 18 | 19 | 20 | 21 | 22 | 23 | cn.bytes1024.hound 24 | plugins-define 25 | 26 | 27 | 28 | org.springframework 29 | spring-core 30 | ${spring-core.version} 31 | provided 32 | 33 | 34 | org.springframework 35 | spring-webmvc 36 | ${spring-webmvc.version} 37 | provided 38 | 39 | 40 | javax.servlet 41 | javax.servlet-api 42 | ${javax-servlet-api.version} 43 | provided 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /plugins/plugins-spring/src/main/java/cn/bytes1024/hound/plugins/spring/SpringBeanPlugin.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.spring; 2 | 3 | import cn.bytes1024.hound.plugins.define.AbstractPluginDefine; 4 | import cn.bytes1024.hound.plugins.spring.interceptor.SpringPluginMethodInterceptor; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.stereotype.Component; 7 | import org.springframework.stereotype.Repository; 8 | import org.springframework.stereotype.Service; 9 | 10 | import static net.bytebuddy.matcher.ElementMatchers.*; 11 | 12 | /** 13 | * spring bean 相关插件 14 | * 15 | * @author 江浩 16 | */ 17 | public class SpringBeanPlugin extends AbstractPluginDefine { 18 | @Override 19 | public void init(PluginDefineBuilder defineBuilder) { 20 | // named("org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver") 21 | // .and(takesArgument(0, HttpServletRequest.class)) 22 | // .and(takesArgument(1, HttpServletResponse.class)) 23 | 24 | 25 | defineBuilder 26 | .pointName("plugin-springBean") 27 | //@Service 28 | .pointClass( 29 | isAnnotatedWith(named(Service.class.getName())) 30 | //@Component 31 | .or(isAnnotatedWith(named(Component.class.getName()))) 32 | //@Repository 33 | .or(isAnnotatedWith(named(Repository.class.getName()))) 34 | .or(isAnnotatedWith(named(Bean.class.getName()))) 35 | //.or(named("org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver")) 36 | ) 37 | .pointMethod(any(), 38 | SpringPluginMethodInterceptor.class); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /plugins/plugins-spring/src/main/java/cn/bytes1024/hound/plugins/spring/SpringExecutorExecutorsPlugin.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.spring; 2 | 3 | 4 | import cn.bytes1024.hound.plugins.define.AbstractPluginDefine; 5 | import cn.bytes1024.hound.plugins.spring.interceptor.SpringPluginMethodInterceptor; 6 | 7 | import java.util.concurrent.Callable; 8 | 9 | import static net.bytebuddy.matcher.ElementMatchers.*; 10 | 11 | /** 12 | * spring executor相关处理 13 | * 14 | * @author 江浩 15 | */ 16 | public class SpringExecutorExecutorsPlugin extends AbstractPluginDefine { 17 | 18 | @Override 19 | public void init(PluginDefineBuilder defineBuilder) { 20 | defineBuilder 21 | .pointName("plugins-springExecutor") 22 | .pointClass(named("org.springframework.scheduling.concurrent.ConcurrentTaskExecutor") 23 | .or(named("org.springframework.core.task.SimpleAsyncTaskExecutor")) 24 | .or(named("org.springframework.scheduling.quartz.SimpleThreadPoolTaskExecutor")) 25 | .or(named("org.springframework.core.task.support.TaskExecutorAdapter")) 26 | .or(named("org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor")) 27 | .or(named("org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler")) 28 | .or(named("org.springframework.jca.work.WorkManagerTaskExecutor")) 29 | .or(named("org.springframework.scheduling.commonj.WorkManagerTaskExecutor")) 30 | ) 31 | .pointMethod(named("execute").and(takesArguments(1)) 32 | .and(takesArgument(0, Runnable.class).or(takesArgument(0, Callable.class))) 33 | , SpringPluginMethodInterceptor.class) 34 | 35 | .pointMethod(named("submit"), SpringPluginMethodInterceptor.class); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /plugins/plugins-spring/src/main/java/cn/bytes1024/hound/plugins/spring/SpringWebMvcPlugin.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.spring; 2 | 3 | import cn.bytes1024.hound.plugins.define.AbstractPluginDefine; 4 | import cn.bytes1024.hound.plugins.spring.interceptor.SpringPluginMethodInterceptor; 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.web.bind.annotation.*; 7 | 8 | import static net.bytebuddy.matcher.ElementMatchers.isAnnotatedWith; 9 | import static net.bytebuddy.matcher.ElementMatchers.named; 10 | 11 | /** 12 | * spring mvc controller 相关注解插件 13 | * 14 | * @author 江浩 15 | */ 16 | public class SpringWebMvcPlugin extends AbstractPluginDefine { 17 | @Override 18 | public void init(PluginDefineBuilder defineBuilder) { 19 | defineBuilder.pointName("plugin-springWebmvc") 20 | .pointClass(isAnnotatedWith(named(Controller.class.getName())) 21 | .or(isAnnotatedWith(named(RestController.class.getName())))) 22 | .pointMethod(isAnnotatedWith(named(GetMapping.class.getName())) 23 | , SpringPluginMethodInterceptor.class) 24 | .pointMethod( 25 | isAnnotatedWith(named(PostMapping.class.getName())) 26 | , SpringPluginMethodInterceptor.class) 27 | .pointMethod( 28 | isAnnotatedWith(named(PutMapping.class.getName())) 29 | , SpringPluginMethodInterceptor.class) 30 | .pointMethod( 31 | isAnnotatedWith(named(DeleteMapping.class.getName())) 32 | , SpringPluginMethodInterceptor.class) 33 | .pointMethod( 34 | isAnnotatedWith(named(PatchMapping.class.getName())) 35 | , SpringPluginMethodInterceptor.class); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /plugins/plugins-spring/src/main/java/cn/bytes1024/hound/plugins/spring/interceptor/SpringPluginMethodInterceptor.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.spring.interceptor; 2 | 3 | 4 | import cn.bytes1024.hound.plugins.define.TraceContext; 5 | import cn.bytes1024.hound.plugins.define.interceptor.supper.AbstractMethodAroundInterceptor; 6 | 7 | /** 8 | * spring 相关拦截器 9 | * 10 | * @author 江浩 11 | */ 12 | public class SpringPluginMethodInterceptor extends AbstractMethodAroundInterceptor { 13 | 14 | public SpringPluginMethodInterceptor(TraceContext traceContext) { 15 | super(traceContext); 16 | } 17 | 18 | 19 | } 20 | -------------------------------------------------------------------------------- /plugins/plugins-spring/src/main/resources/META-INF/plugins/cn.bytes1024.hound.plugins.define.PluginDefine: -------------------------------------------------------------------------------- 1 | plugin-springBean = cn.bytes1024.hound.plugins.spring.SpringBeanPlugin 2 | 3 | plugin-springExecutor = cn.bytes1024.hound.plugins.spring.SpringExecutorExecutorsPlugin 4 | 5 | plugin-springWebMvc = cn.bytes1024.hound.plugins.spring.SpringWebMvcPlugin 6 | -------------------------------------------------------------------------------- /plugins/plugins-tomcat/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | plugins 6 | cn.bytes1024.hound 7 | 1.0.1-SNAPSHOT 8 | 9 | 4.0.0 10 | 11 | plugins-tomcat 12 | 13 | 14 | 15 | 16 | cn.bytes1024.hound 17 | plugins-define 18 | 19 | 20 | 21 | javax.servlet 22 | javax.servlet-api 23 | 3.1.0 24 | provided 25 | 26 | 27 | 28 | org.apache.tomcat 29 | servlet-api 30 | 6.0.35 31 | provided 32 | 33 | 34 | org.apache.tomcat 35 | catalina 36 | 6.0.43 37 | provided 38 | 39 | 40 | 41 | org.apache.tomcat 42 | coyote 43 | 6.0.43 44 | test 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /plugins/plugins-tomcat/src/main/java/cn/bytes1024/hound/plugins/tomcat/Tomcat7xPlugin.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.tomcat; 2 | 3 | import cn.bytes1024.hound.plugins.define.AbstractPluginDefine; 4 | import cn.bytes1024.hound.plugins.tomcat.interceptor.TomcatMethodInterceptor; 5 | 6 | import static net.bytebuddy.matcher.ElementMatchers.*; 7 | 8 | /** 9 | * tomcat 7x+ 插件支持 10 | * 11 | * @author 江浩 12 | */ 13 | public class Tomcat7xPlugin extends AbstractPluginDefine { 14 | @Override 15 | public void init(AbstractPluginDefine.PluginDefineBuilder defineBuilder) { 16 | 17 | defineBuilder.pointName("plugin-tomcat") 18 | .pointClass(named("org.apache.catalina.core.StandardHostValve")) 19 | .pointMethod(named("invoke") 20 | .and(takesArguments(2)) 21 | .and(takesArgument(0, named("org.apache.catalina.connector.Request"))) 22 | .and(takesArgument(1, named("org.apache.catalina.connector.Response"))) 23 | , TomcatMethodInterceptor.class); 24 | 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /plugins/plugins-tomcat/src/main/java/cn/bytes1024/hound/plugins/tomcat/interceptor/TomcatMethodInterceptor.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.tomcat.interceptor; 2 | 3 | import cn.bytes1024.hound.plugins.define.InterceptContext; 4 | import cn.bytes1024.hound.plugins.define.TraceContext; 5 | import cn.bytes1024.hound.plugins.define.interceptor.supper.AbstractTransmissionMethodAroundInterceptor; 6 | import cn.bytes1024.hound.plugins.define.interceptor.supper.RemoteTransmission; 7 | import com.alipay.common.tracer.core.context.span.SofaTracerSpanContext; 8 | 9 | import javax.servlet.http.HttpServletRequest; 10 | import java.util.Objects; 11 | 12 | /** 13 | * tomcat 方法拦截 14 | * 15 | * @author 江浩 16 | */ 17 | public class TomcatMethodInterceptor extends AbstractTransmissionMethodAroundInterceptor { 18 | 19 | static final String IGNORED_URL = ".*favicon.ico.*"; 20 | 21 | public TomcatMethodInterceptor(TraceContext traceContext) { 22 | super(traceContext); 23 | 24 | setRemoteTransmission(new RemoteTransmission() { 25 | @Override 26 | public SofaTracerSpanContext receive(HttpServletRequest handler, String key) { 27 | String spanContext = handler.getHeader(key); 28 | return convert(spanContext); 29 | } 30 | }); 31 | } 32 | 33 | @Override 34 | public void before(InterceptContext interceptContext) { 35 | Object[] args = interceptContext.getArgs(); 36 | final Object request = args[0], response = args[1]; 37 | final HttpServletRequest httpServletRequest = (request instanceof HttpServletRequest) ? (HttpServletRequest) request : null; 38 | interceptContext.setIgnored(Objects.isNull(httpServletRequest) 39 | || httpServletRequest.getRequestURL().toString().matches(IGNORED_URL) 40 | ); 41 | if (interceptContext.isIgnored()) { 42 | return; 43 | } 44 | super.currentTracerSpan(httpServletRequest, interceptContext); 45 | 46 | } 47 | 48 | @Override 49 | public void after(InterceptContext interceptContext) { 50 | 51 | if (interceptContext.isIgnored()) { 52 | return; 53 | } 54 | 55 | super.after(interceptContext); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /plugins/plugins-tomcat/src/main/resources/META-INF/plugins/cn.bytes1024.hound.plugins.define.PluginDefine: -------------------------------------------------------------------------------- 1 | plugin-tomcat7x = cn.bytes1024.hound.plugins.tomcat.Tomcat7xPlugin -------------------------------------------------------------------------------- /plugins/plugins-webflux/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | plugins 6 | cn.bytes1024.hound 7 | 1.0.1-SNAPSHOT 8 | 9 | 10 | 4.0.0 11 | 12 | plugins-webflux 13 | 14 | 15 | 16 | 17 | cn.bytes1024.hound 18 | plugins-define 19 | 20 | 21 | 22 | org.springframework 23 | spring-web 24 | 5.0.0.RELEASE 25 | compile 26 | 27 | 28 | org.springframework 29 | spring-webflux 30 | 5.0.0.RELEASE 31 | compile 32 | 33 | 34 | 35 | io.netty 36 | netty-codec-http 37 | 4.1.30.Final 38 | compile 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /plugins/plugins-webflux/src/main/java/cn/bytes1024/hound/plugins/webflux/WebFluxOnInboundNextPlugin.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.webflux; 2 | 3 | 4 | import cn.bytes1024.hound.plugins.define.AbstractPluginDefine; 5 | import cn.bytes1024.hound.plugins.webflux.interceptor.OnInboundNextMethodInterceptor; 6 | 7 | import static net.bytebuddy.matcher.ElementMatchers.named; 8 | 9 | /** 10 | * reactor 11 | * 12 | * @author 江浩 13 | */ 14 | public class WebFluxOnInboundNextPlugin extends AbstractPluginDefine { 15 | @Override 16 | public void init(AbstractPluginDefine.PluginDefineBuilder defineBuilder) { 17 | 18 | defineBuilder 19 | .pointName("spring-web-flux-onInboundNext") 20 | .pointClass(named("reactor.netty.http.server.HttpServerOperations")) 21 | .pointMethod(named("onInboundNext"), OnInboundNextMethodInterceptor.class); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /plugins/plugins-webflux/src/main/java/cn/bytes1024/hound/plugins/webflux/WebFluxOnOutboundCompletePlugin.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.webflux; 2 | 3 | 4 | import cn.bytes1024.hound.plugins.define.AbstractPluginDefine; 5 | import cn.bytes1024.hound.plugins.webflux.interceptor.OnOutboundCompleteMethodInterceptor; 6 | 7 | import static net.bytebuddy.matcher.ElementMatchers.named; 8 | 9 | /** 10 | * reactor 11 | * 12 | * @author 江浩 13 | */ 14 | public class WebFluxOnOutboundCompletePlugin extends AbstractPluginDefine { 15 | @Override 16 | public void init(PluginDefineBuilder defineBuilder) { 17 | 18 | defineBuilder 19 | .pointName("spring-web-flux-onOutboundComplete") 20 | .pointClass(named("reactor.netty.http.server.HttpServerOperations")) 21 | .pointMethod(named("onOutboundComplete"), OnOutboundCompleteMethodInterceptor.class); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /plugins/plugins-webflux/src/main/java/cn/bytes1024/hound/plugins/webflux/interceptor/OnInboundNextMethodInterceptor.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.webflux.interceptor; 2 | 3 | 4 | import cn.bytes1024.hound.plugins.define.InterceptContext; 5 | import cn.bytes1024.hound.plugins.define.TraceContext; 6 | import cn.bytes1024.hound.plugins.define.interceptor.supper.AbstractTransmissionMethodAroundInterceptor; 7 | import cn.bytes1024.hound.plugins.define.interceptor.supper.RemoteTransmission; 8 | import com.alipay.common.tracer.core.context.span.SofaTracerSpanContext; 9 | import io.netty.handler.codec.http.HttpRequest; 10 | 11 | import java.util.Objects; 12 | 13 | /** 14 | * @author 江浩 15 | */ 16 | public class OnInboundNextMethodInterceptor extends AbstractTransmissionMethodAroundInterceptor { 17 | 18 | static final String IGNORED_URL = ".*favicon.ico.*"; 19 | 20 | public OnInboundNextMethodInterceptor(TraceContext traceContext) { 21 | super(traceContext); 22 | 23 | setRemoteTransmission(new RemoteTransmission() { 24 | @Override 25 | public SofaTracerSpanContext receive(HttpRequest handler, String key) { 26 | String spanContext = handler.headers().get(key); 27 | return convert(spanContext); 28 | } 29 | }); 30 | } 31 | 32 | 33 | @Override 34 | public void before(InterceptContext interceptContext) { 35 | Object[] args = interceptContext.getArgs(); 36 | 37 | if (Objects.isNull(args) || args.length <= 0) { 38 | return; 39 | } 40 | 41 | Object arg1 = args[1]; 42 | if (!(arg1 instanceof HttpRequest)) { 43 | interceptContext.setIgnored(true); 44 | return; 45 | } 46 | 47 | HttpRequest httpRequest = (HttpRequest) arg1; 48 | String URI = httpRequest.uri(); 49 | 50 | interceptContext.setIgnored(URI.matches(IGNORED_URL)); 51 | if (interceptContext.isIgnored()) { 52 | return; 53 | } 54 | super.currentTracerSpan(httpRequest, interceptContext); 55 | } 56 | 57 | @Override 58 | public void after(InterceptContext interceptContext) { 59 | 60 | if (interceptContext.isIgnored()) { 61 | return; 62 | } 63 | //TODO reactor 方式的处理逻辑? 64 | super.after(interceptContext); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /plugins/plugins-webflux/src/main/java/cn/bytes1024/hound/plugins/webflux/interceptor/OnOutboundCompleteMethodInterceptor.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.plugins.webflux.interceptor; 2 | 3 | 4 | import cn.bytes1024.hound.plugins.define.InterceptContext; 5 | import cn.bytes1024.hound.plugins.define.TraceContext; 6 | import cn.bytes1024.hound.plugins.define.interceptor.supper.AbstractMethodAroundInterceptor; 7 | 8 | public class OnOutboundCompleteMethodInterceptor extends AbstractMethodAroundInterceptor { 9 | public OnOutboundCompleteMethodInterceptor(TraceContext traceContext) { 10 | super(traceContext); 11 | } 12 | 13 | @Override 14 | public void before(InterceptContext interceptContext) { 15 | System.out.println("onOutboundComplete.........."); 16 | } 17 | 18 | @Override 19 | public void after(InterceptContext interceptContext) { 20 | System.out.println("onOutboundComplete after...."); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /plugins/plugins-webflux/src/main/resources/META-INF/plugins/cn.bytes1024.hound.plugins.define.PluginDefine: -------------------------------------------------------------------------------- 1 | 2 | plugin-webflux-onInbound = cn.bytes1024.hound.plugins.webflux.WebFluxOnInboundNextPlugin 3 | -------------------------------------------------------------------------------- /plugins/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | bytes-hound 7 | cn.bytes1024.hound 8 | 1.0.1-SNAPSHOT 9 | 10 | 11 | 4.0.0 12 | plugins 13 | pom 14 | 15 | 16 | plugins-define 17 | plugins-tomcat 18 | plugins-bom 19 | plugins-webflux 20 | plugins-okhttp 21 | plugins-spring 22 | plugins-httpclient 23 | plugins-fastjson 24 | plugins-gson 25 | plugins-druid 26 | plugins-mybatis 27 | plugins-mysql-jdbc 28 | 29 | 30 | 31 | 32 | UTF-8 33 | false 34 | 35 | 36 | 37 | 38 | 39 | cn.bytes1024.hound 40 | plugins-define 41 | ${project.version} 42 | 43 | 44 | 45 | cn.bytes1024.hound 46 | commons 47 | ${project.version} 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | cn.bytes1024.hound 7 | bytes-hound 8 | 1.0.1-SNAPSHOT 9 | pom 10 | 4.0.0 11 | 12 | 13 | bom 14 | collect 15 | loader 16 | plugins 17 | transfers 18 | commons 19 | server 20 | 21 | 22 | 23 | UTF-8 24 | 1.8 25 | 3.8.0 26 | true 27 | 28 | 29 | 30 | 31 | 32 | 33 | cn.bytes1024.hound 34 | bom 35 | 1.0.0.BUILD 36 | pom 37 | import 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.apache.maven.plugins 48 | maven-deploy-plugin 49 | 2.8.2 50 | 51 | ${deploy.skip} 52 | 53 | 54 | 55 | 56 | org.apache.maven.plugins 57 | maven-jar-plugin 58 | 3.1.2 59 | 60 | 61 | false 62 | 63 | 64 | 65 | 66 | 67 | maven-compiler-plugin 68 | ${maven-compiler-plugin.version} 69 | 70 | ${java.version} 71 | ${java.version} 72 | ${project.build.sourceEncoding} 73 | 74 | 75 | 76 | 77 | org.apache.maven.plugins 78 | maven-source-plugin 79 | 3.1.0 80 | 81 | 82 | attach-sources 83 | 84 | jar 85 | 86 | 87 | 88 | 89 | 90 | 91 | maven-release-plugin 92 | 2.5.3 93 | 94 | false 95 | release 96 | true 97 | @{project.version} 98 | deploy 99 | -DskipTests 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | org.springframework.boot 108 | spring-boot-maven-plugin 109 | 110 | 111 | 112 | repackage 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | bytes1024-profile 126 | http://nexus.bytes1024.cn/repository/maven-releases/ 127 | 128 | 129 | bytes1024-profile 130 | http://nexus.bytes1024.cn/repository/maven-snapshots/ 131 | 132 | 133 | 134 | 135 | 136 | https://github.com/bytes1024/hound.git 137 | scm:git:https://github.com/bytes1024/hound.git 138 | scm:git:https://github.com/bytes1024/hound.git 139 | HEAD 140 | 141 | 142 | 143 | 144 | 145 | bytes1024-profile 146 | 147 | true 148 | 149 | 150 | 151 | 152 | 153 | 154 | -------------------------------------------------------------------------------- /server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | bytes-hound 6 | cn.bytes1024.hound 7 | 1.0.1-SNAPSHOT 8 | 9 | 4.0.0 10 | hound-server 11 | 12 | 13 | 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-dependencies 18 | 2.1.0.RELEASE 19 | pom 20 | import 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-webflux 32 | 33 | 34 | 35 | cn.bytes1024.hound 36 | transfers-define 37 | 38 | 39 | 40 | com.alibaba 41 | fastjson 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /server/src/main/java/cn/bytes1024/hound/server/ApplicationServer.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.server; 2 | 3 | import org.springframework.boot.autoconfigure.SpringBootApplication; 4 | import org.springframework.boot.builder.SpringApplicationBuilder; 5 | import org.springframework.boot.context.ApplicationPidFileWriter; 6 | import org.springframework.boot.web.context.WebServerPortFileWriter; 7 | import org.springframework.context.ConfigurableApplicationContext; 8 | 9 | @SpringBootApplication 10 | public class ApplicationServer { 11 | public static void main(String[] args) { 12 | ConfigurableApplicationContext context = 13 | new SpringApplicationBuilder(ApplicationServer.class).run(args); 14 | context.addApplicationListener(new ApplicationPidFileWriter()); 15 | context.addApplicationListener(new WebServerPortFileWriter()); 16 | } 17 | } -------------------------------------------------------------------------------- /server/src/main/java/cn/bytes1024/hound/server/api/IndexApi.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.server.api; 2 | 3 | import org.springframework.web.bind.annotation.PostMapping; 4 | import org.springframework.web.bind.annotation.RequestBody; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | @RestController 9 | @RequestMapping("/v1/transfer") 10 | public class IndexApi { 11 | 12 | 13 | @PostMapping("/receive") 14 | public String receive(@RequestBody String content) { 15 | System.out.println(content); 16 | return "ok"; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /server/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8888 -------------------------------------------------------------------------------- /transfers/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | bytes-hound 7 | cn.bytes1024.hound 8 | 1.0.1-SNAPSHOT 9 | 10 | 11 | 4.0.0 12 | 13 | hound-transfers 14 | pom 15 | 16 | 17 | transfers-define 18 | transfers-web 19 | transfers-bom 20 | 21 | 22 | 23 | UTF-8 24 | false 25 | 26 | 27 | 28 | 29 | 30 | cn.bytes1024.hound 31 | commons 32 | ${project.version} 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /transfers/transfers-bom/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | hound-transfers 7 | cn.bytes1024.hound 8 | 1.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | transfers-bom 13 | pom 14 | 15 | 16 | 17 | 18 | 19 | cn.bytes1024.hound 20 | transfers-web 21 | ${project.version} 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /transfers/transfers-define/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | hound-transfers 6 | cn.bytes1024.hound 7 | 1.0.1-SNAPSHOT 8 | 9 | 10 | 4.0.0 11 | 12 | transfers-define 13 | 14 | 15 | org.projectlombok 16 | lombok 17 | 18 | 19 | 20 | com.alibaba 21 | fastjson 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /transfers/transfers-define/src/main/java/cn/bytes1024/hound/transfers/define/DefineTransmitContent.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.transfers.define; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | 5 | import java.io.Serializable; 6 | 7 | public class DefineTransmitContent implements Serializable { 8 | 9 | public DefineTransmitContent() { 10 | } 11 | 12 | public String encode() { 13 | return JSONObject.toJSONString(this); 14 | } 15 | 16 | public T decode(String content, Class tClass) { 17 | return JSONObject.parseObject(content, tClass); 18 | } 19 | 20 | 21 | } 22 | -------------------------------------------------------------------------------- /transfers/transfers-define/src/main/java/cn/bytes1024/hound/transfers/define/TransferDefine.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.transfers.define; 2 | 3 | import cn.bytes1024.hound.commons.option.ConfigOption; 4 | 5 | /** 6 | * 传输定义 7 | *

8 | * 1.传输对象有多个不一样,webflux,kafka,netty ... 9 | * 2.具体的连接实现需要根据当前插件加载来处理实现 10 | * ... 11 | * 3.如何加载每个服务不一样的配置信息 12 | *

13 | * 14 | * @author 江浩 15 | */ 16 | public interface TransferDefine { 17 | 18 | /** 19 | * 数据传输信息 20 | * 21 | * @param configOption : 22 | * @param transmitContent : 23 | * @return : void 24 | * @author 江浩 25 | */ 26 | void transmit(ConfigOption configOption, T transmitContent); 27 | } 28 | -------------------------------------------------------------------------------- /transfers/transfers-define/src/main/java/cn/bytes1024/hound/transfers/define/TransmitMetricsContent.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.transfers.define; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | import lombok.experimental.Accessors; 7 | 8 | import java.io.Serializable; 9 | 10 | /** 11 | * 传输指标数据 12 | * 13 | * @author 江浩 14 | */ 15 | @Data 16 | @AllArgsConstructor 17 | @NoArgsConstructor 18 | @Accessors(chain = true) 19 | public class TransmitMetricsContent implements Serializable { 20 | 21 | private Float cpu; 22 | 23 | private Float mem; 24 | 25 | } 26 | -------------------------------------------------------------------------------- /transfers/transfers-define/src/main/java/cn/bytes1024/hound/transfers/define/TransmitTraceContent.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.transfers.define; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.EqualsAndHashCode; 6 | import lombok.NoArgsConstructor; 7 | import lombok.experimental.Accessors; 8 | 9 | import java.util.Map; 10 | 11 | /** 12 | * 传输对象 13 | *

特定传输对象避免对个数据来源点数据不一致

14 | *

15 | * 1.agent 16 | * 2.sdk 17 | *

18 | * 19 | * @author 江浩 20 | */ 21 | @Data 22 | @AllArgsConstructor 23 | @NoArgsConstructor 24 | @Accessors(chain = true) 25 | @EqualsAndHashCode(callSuper = true) 26 | public class TransmitTraceContent extends DefineTransmitContent { 27 | 28 | private String operationName; 29 | 30 | private String traceId; 31 | 32 | private String spanId; 33 | 34 | private String parentId; 35 | 36 | private Boolean sampled; 37 | 38 | private Map bizBaggage; 39 | 40 | private Map sysBaggage; 41 | 42 | private Long startTime; 43 | 44 | private Long endTime; 45 | } 46 | -------------------------------------------------------------------------------- /transfers/transfers-define/src/main/java/cn/bytes1024/hound/transfers/define/buffer/DefaultTransferBuffer.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.transfers.define.buffer; 2 | 3 | import cn.bytes1024.hound.commons.option.ConfigOption; 4 | import cn.bytes1024.hound.commons.util.ThreadPoolUtils; 5 | import cn.bytes1024.hound.transfers.define.DefineTransmitContent; 6 | import lombok.extern.slf4j.Slf4j; 7 | 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | import java.util.Objects; 11 | import java.util.Queue; 12 | import java.util.concurrent.ConcurrentLinkedQueue; 13 | import java.util.concurrent.ThreadPoolExecutor; 14 | import java.util.concurrent.TimeUnit; 15 | 16 | /** 17 | * 默认的缓冲区域 18 | * 19 | * @author 江浩 20 | */ 21 | @Slf4j 22 | public class DefaultTransferBuffer implements TransferBuffer, Runnable { 23 | 24 | private List notifyListeners = new ArrayList<>(); 25 | 26 | private ThreadPoolExecutor poolExecutor = ThreadPoolUtils.newFixedThreadPool(5, this.getClass().getName()); 27 | 28 | private final Queue queue = new ConcurrentLinkedQueue<>(); 29 | 30 | private ConfigOption configOption; 31 | 32 | public DefaultTransferBuffer(ConfigOption configOption) { 33 | this.configOption = configOption; 34 | poolExecutor.execute(this); 35 | } 36 | 37 | @Override 38 | public void register(NotifyListener notifyListener) { 39 | notifyListeners.add(notifyListener); 40 | } 41 | 42 | @Override 43 | public void push(T transmitContent) { 44 | poolExecutor.execute(() -> { 45 | try { 46 | boolean bool = queue.add(transmitContent); 47 | if (!bool && log.isDebugEnabled()) { 48 | log.debug("数据缓冲添加失败: {}->{}", bool, transmitContent); 49 | } 50 | } catch (Exception e) { 51 | e.printStackTrace(); 52 | } 53 | }); 54 | } 55 | 56 | @Override 57 | public List notifyListener() { 58 | return this.notifyListeners; 59 | } 60 | 61 | @Override 62 | public void run() { 63 | while (true) { 64 | 65 | DefineTransmitContent content = queue.poll(); 66 | if (Objects.nonNull(content)) { 67 | try { 68 | for (NotifyListener notifyListener : notifyListeners) { 69 | notifyListener.notify(content); 70 | } 71 | TimeUnit.NANOSECONDS.sleep(50); 72 | } catch (InterruptedException e) { 73 | e.printStackTrace(); 74 | } 75 | } 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /transfers/transfers-define/src/main/java/cn/bytes1024/hound/transfers/define/buffer/NotifyListener.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.transfers.define.buffer; 2 | 3 | 4 | import cn.bytes1024.hound.transfers.define.DefineTransmitContent; 5 | 6 | public interface NotifyListener { 7 | /** 8 | * 通知消息监听 9 | * 10 | * @param message : 11 | * @return : void 12 | * @author 江浩 13 | */ 14 | void notify(T message); 15 | 16 | } 17 | -------------------------------------------------------------------------------- /transfers/transfers-define/src/main/java/cn/bytes1024/hound/transfers/define/buffer/TransferBuffer.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.transfers.define.buffer; 2 | 3 | import cn.bytes1024.hound.transfers.define.DefineTransmitContent; 4 | 5 | import java.util.List; 6 | 7 | public interface TransferBuffer { 8 | 9 | 10 | /** 11 | * 注册监听器 12 | * 13 | * @param notifyListener : 14 | * @return : void 15 | * @author 江浩 16 | */ 17 | void register(NotifyListener notifyListener); 18 | 19 | 20 | /** 21 | * 添加传输数据 22 | * 23 | * @param transmitContent : 24 | * @return : void 25 | * @author 江浩 26 | */ 27 | void push(T transmitContent); 28 | 29 | 30 | /** 31 | * 通知监听 32 | * 33 | * @return : cn.bytes1024.hound.transfers.define.buffer.CallableListener 34 | * @author 江浩 35 | */ 36 | List notifyListener(); 37 | 38 | } 39 | -------------------------------------------------------------------------------- /transfers/transfers-web/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | hound-transfers 6 | cn.bytes1024.hound 7 | 1.0.1-SNAPSHOT 8 | 9 | 4.0.0 10 | 11 | transfers-web 12 | 13 | 14 | 15 | 16 | cn.bytes1024.hound 17 | transfers-define 18 | 19 | 20 | 21 | com.squareup.okhttp3 22 | okhttp 23 | 24 | 25 | 26 | com.alibaba 27 | fastjson 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /transfers/transfers-web/src/main/java/cn/bytes1024/hound/transfers/web/AbstractRestApiTransfer.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.transfers.web; 2 | 3 | import cn.bytes1024.hound.transfers.define.TransferDefine; 4 | 5 | public abstract class AbstractRestApiTransfer implements TransferDefine { 6 | } 7 | -------------------------------------------------------------------------------- /transfers/transfers-web/src/main/java/cn/bytes1024/hound/transfers/web/OkHttpRestApiTransfer.java: -------------------------------------------------------------------------------- 1 | package cn.bytes1024.hound.transfers.web; 2 | 3 | import cn.bytes1024.hound.commons.option.ConfigOption; 4 | import cn.bytes1024.hound.commons.option.ConfigOptionDefine; 5 | import cn.bytes1024.hound.transfers.define.DefineTransmitContent; 6 | import lombok.extern.slf4j.Slf4j; 7 | import okhttp3.*; 8 | 9 | import java.io.IOException; 10 | import java.util.Objects; 11 | 12 | /** 13 | * rest api 数据传输器 14 | *

15 | * 1.目前内嵌传输使用 okhttp,但是他又是内嵌的插件之一,循环调用的问题 16 | *

17 | * 18 | * @author 江浩 19 | */ 20 | @Slf4j 21 | public class OkHttpRestApiTransfer extends AbstractRestApiTransfer { 22 | 23 | private OkHttpClient okHttpClient = new OkHttpClient(); 24 | 25 | private MediaType mediaType = MediaType.parse("application/json"); 26 | 27 | 28 | @Override 29 | public void transmit(ConfigOption configOption, T transmitContent) { 30 | RequestBody requestBody = RequestBody.create(mediaType, transmitContent.encode()); 31 | 32 | String responseJson = this.postResponseJson(getRemoteAddress(configOption), requestBody); 33 | System.out.println("数据提交:" + transmitContent.encode() + " " + responseJson); 34 | } 35 | 36 | public String getRemoteAddress(ConfigOption configOption) { 37 | return Objects.isNull(configOption) ? null : ConfigOptionDefine.getTransferWebAddress(configOption); 38 | } 39 | 40 | public String postResponseJson(String url, RequestBody requestBody) { 41 | Request request = new Request.Builder() 42 | .url(url) 43 | .post(requestBody) 44 | .header("OKHTTP-IGNORE-DEFINE", "true") 45 | .build(); 46 | 47 | Call call = okHttpClient.newCall(request); 48 | try { 49 | ResponseBody response = call.execute().body(); 50 | return Objects.isNull(response) ? null : response.string(); 51 | } catch (IOException e) { 52 | log.info("agent WebfluxTransfer error: {}", e); 53 | return null; 54 | } 55 | } 56 | 57 | 58 | } 59 | -------------------------------------------------------------------------------- /transfers/transfers-web/src/main/resources/META-INF/plugins/cn.bytes1024.hound.transfers.define.TransferDefine: -------------------------------------------------------------------------------- 1 | web = cn.bytes1024.hound.transfers.web.OkHttpRestApiTransfer --------------------------------------------------------------------------------