All Projects → brutusin → instrumentation

brutusin / instrumentation

Licence: Apache-2.0 license
An extensible java agent framework that instruments (modifies the bytecode at class loading time) programs running on the JVM, with the purpose of capturing method invocation events (start, finish, errors ...) and notifying custom listeners.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to instrumentation

hasor
Hasor是一套基于 Java 语言的开发框架,区别于其它框架的是 Hasor 有着自己一套完整的体系,同时还可以和先有技术体系做到完美融合。它包含:IoC/Aop容器框架、Web框架、Jdbc框架、RSF分布式RPC框架、DataQL引擎,等几块。
Stars: ✭ 938 (+2305.13%)
Mutual labels:  aop
metrics-spring-boot
Spring Boot 基于Sentinel服务接口监控及其手自一体化限流
Stars: ✭ 17 (-56.41%)
Mutual labels:  aop
thinking-in-spring
Spring source code reading
Stars: ✭ 105 (+169.23%)
Mutual labels:  aop
contech
The Contech analysis framework provides the means for generating and analyzing task graphs that enable computer architects and programmers to gain a deeper understanding of parallel programs.
Stars: ✭ 43 (+10.26%)
Mutual labels:  instrumentation
Farseer.Net
Provides consistent standard use of common components of the .Net Core language
Stars: ✭ 42 (+7.69%)
Mutual labels:  aop
RemoteNET
Examine, create and interact with remote objects in other .NET processes.
Stars: ✭ 29 (-25.64%)
Mutual labels:  instrumentation
marathon
Cross-platform test runner written for Android and iOS projects
Stars: ✭ 398 (+920.51%)
Mutual labels:  instrumentation
DRouter
简单易用的支持多进程架构的组件化方案
Stars: ✭ 74 (+89.74%)
Mutual labels:  aop
pyccolo
Declarative instrumentation for Python.
Stars: ✭ 70 (+79.49%)
Mutual labels:  instrumentation
thundra-agent-python
Thundra Lambda Python Agent
Stars: ✭ 36 (-7.69%)
Mutual labels:  instrumentation
LSFramework
手写山寨版spring学习aop、ioc思想的demo,没看过spring的源码,因为实在是太庞大了,参考部分网上博客及开源代码完成。
Stars: ✭ 53 (+35.9%)
Mutual labels:  aop
tiny4j
IOC, AOP, REST...
Stars: ✭ 15 (-61.54%)
Mutual labels:  aop
egg-aop
AOP plugin for eggjs, add DI, AOP support.
Stars: ✭ 44 (+12.82%)
Mutual labels:  aop
MethodBoundaryAspect.Fody
A Fody weaver which allows to decorate methods and hook into method start, method end and method exceptions.
Stars: ✭ 173 (+343.59%)
Mutual labels:  aop
ComposableAsync
Create, compose and inject asynchronous behaviors in .Net Framework and .Net Core.
Stars: ✭ 28 (-28.21%)
Mutual labels:  aop
CrmWebApi
🎉 CRM后台管理系统(后端 WebApi)
Stars: ✭ 13 (-66.67%)
Mutual labels:  aop
rocket-pipes
Powerful pipes for TypeScript, that chain Promise and ADT for you 🚌 -> ⛰️ -> 🚠 -> 🏂 -> 🚀
Stars: ✭ 18 (-53.85%)
Mutual labels:  aop
live-platform
Add breakpoints, logs, metrics, and spans to live production applications
Stars: ✭ 37 (-5.13%)
Mutual labels:  aop
prometheus-phoenix
Prometheus.io Phoenix instrumenter
Stars: ✭ 66 (+69.23%)
Mutual labels:  instrumentation
objectiv-analytics
Powerful product analytics for data teams, with full control over data & models.
Stars: ✭ 399 (+923.08%)
Mutual labels:  instrumentation

Check out https://github.com/ShiftLeftSecurity/bctrace instead. A more mature and stable version of this project

org.brutusin:instrumentation Build Status Maven Central Latest Version

An extensible java agent framework that instruments programs running on the JVM (modifying the bytecode at class loading time), with the purpose of capturing method invocation events (start, finish, errors ...) and notifying custom listeners.

Table of Contents

How it works

The java instrumentation package introduced in JDK1.5, provides a simple way to transform java-class definition at loading time, consisting basically in a byte[] to byte[] transformation, by the so called "java agents".

This module provides an agent (org.brutusin.instrumentation.Agent) that creates an execution listener instance (from the name of a concrete class extending org.brutusin.instrumentation.Interceptor passed from the JVM agent arguments) and, making use of the ASM library, introduces a series of instructions in the method definitions of the classes to be loaded (classes and methods can be skipped) to notify these execution events to the listener.

From a simplified point of view, the dynamic transformation turns a method like this:

public Object foo(Object bar){
    return new Object();
}

into that:

public Object foo(Object bar){
    onStart(bar);
    try{
        Object ret = new Object();
        onFinished(ret);
        return ret;
    } catch(Throwable th){
        onThrowable(th);
        throw th; // at bytecode level this is legal
    }
}

allowing your custom listener to be notified.

Maven dependency

<dependency>
    <groupId>org.brutusin</groupId>
    <artifactId>instrumentation</artifactId>
</dependency>

Click here to see the latest available version released to the Maven Central Repository.

If you are not using maven and need help you can ask here.

Example

See logging-instrumentation for a complete working example.

Implementation

Create the following listener implementation:

package mypackage;

public class MyInterceptor extends Interceptor {

    @Override
    public void init(String arg) {
        System.out.println("Interceptor args: " + arg);
    }

    @Override
    public boolean interceptClass(String className, byte[] byteCode) {
        return true; // all classes can be intrumented
    }

    @Override
    public boolean interceptMethod(ClassNode cn, MethodNode mn) {
        return true; // all methods are instrumented
    }

    @Override
    protected void doOnStart(Method m, Object[] arg, String executionId) {
        System.out.println("doOnStart " + m + " " + executionId);
    }

    @Override
    protected void doOnThrowableThrown(Method m, Throwable throwable, String executionId) {
        System.out.println("doOnThrowableThrown " + m + " " + executionId);
    }

    @Override
    protected void doOnThrowableUncatched(Method m, Throwable throwable, String executionId) {
        System.out.println("doOnThrowableUncatched " + m + " " + executionId);
    }

    @Override
    protected void doOnFinish(Method m, Object result, String executionId) {
        System.out.println("doOnFinish " + m + " " + executionId);
    }
}

Packaging

Create a fat-jar with the previous class and its dependencies. Add the following attribute to the manifest of the agent JAR:

Premain-Class: org.brutusin.instrumentation.Agent

Suppose this jar to be named myagent.jar

JRE launching

Run (at least JRE 1.5) the desired java application with the following JVM options: (suppossing myagent.jar located in the working directory)

-javaagent:myagent.jar=mypackage.MyInterceptor;an_interceptor_optional_parameter

Main stack

This module could not be possible without:

Brutusin dependent modules

Support, bugs and requests

https://github.com/brutusin/instrumentation/issues

Authors

Contributions are always welcome and greatly appreciated!

License

Apache License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].