Spring Metrik
Metrik
is lightweight Spring library to monitor your services execution.
Installation
@Metrik
release is published on the Maven Central,
so you can install it from your preferred dependency management tool : Gradle
or Maven
.
Maven
Include the following lines to your pom.xml
dependencies :
<dependencies>
<dependency>
<groupId>io.github.tibus29</groupId>
<artifactId>spring-metrik</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
Gradle
Include the following line to your build.gradle
dependencies :
compile 'io.github.tibus29:spring-metrik:1.2.0'
Usage
Using the library is very simple, you simply have to annotate the bean you want to monitor with@Metrik
:
@Service
@Metrik(value = "MY_SERVICE", traceMode = TraceMode.AUTO) // custom metrik group, TraceMode.AUTO will output all params and result
public class MyService {
public String sayHelloTo(String name) {
return "Hello, " + name + " !";
}
@Metrik(params = { "username" }) // prevent for logging clear password into logs !
public boolean authenticate(String username, String password) {
return true;
}
@Metrik(enabled = false)
public void doSomething() {
log.info("I do something...");
}
}
@RestController
@RequestMapping("/")
public class MyController {
@Inject
MyService myService;
class MyBean {
private String action = "Please clone me !";
}
@GetMapping
@Metrik(resultFields = { "toto" }) // the annotation can be placed on a single method
public MyBean index() {
log.info(this.myService.sayHelloTo("Foo"));
log.info("Authentication : {} !", this.myService.authenticate("[email protected]", "password") ? "success" : "failure");
this.myService.doSomething();
return new MyBean();
}
}
Output :
MY_SERVICE|sayHelloTo|21|OK|[name='Foo']|['Hello, Foo !']
Hello, Foo !
MY_SERVICE|authenticate|125|OK|[username='[email protected]']|[true]
Authentication : success !
I do something...
MyController|index|163|OK|[]|[action='Please clone me !']