Skip to content

Annotations examples

Konstantin A. Zolotukhin edited this page Dec 17, 2015 · 3 revisions

Any of annotations can be used repeatably. (since 0.2.0)

Parameter-dependent count metric, with parameter="cat" passed in method registers metric with name "my.neat.metric.cat", while with parameter="dog", metric is registered with name "my.neat.metric.dog"

@Countable(name="my.neat.metric", nameSuffixExpression="args[0]")
public void letsCount(String parameter) {
    //something useful surely happens here 
}

Registers method execution time with name "my.amazing.metric"

@Timeable(name="my.amazing.metric")
public void letsTime(String parameter) {
    //something useful surely happens here 
}

Counts error happen in method and save them with metric name (if parameter="cat") "cat.my.useful.metric". Note, that though name and name suffix are separated by point automatically, in expression it should be expression concern to provide readable and good-looking name.

@ErrorCountable(nameSuffixExpression="args[0] + '.my.useful.metric'")
public void letsError(String parameter) {
    //something useful surely happens here 
}

@Chronable is described more detailed.

With parameter "cat” registers value "3" for metric with name "cat.my.amazing.metric"

@Chronable(valueExpression="args[0].length()", nameSuffixExpression="args[0] + '.my.amazing.metric'")
public String letsChron(String parameter) {
    //something useful surely happens here 
    return "mew mew";
}

Does not register metric when parameter equals “dog", with any other parameter registers value "6" (7 as length of returned sting - 1) for metric with name "my.cats.metric"

@Chronable(valueExpression="result.length() - 1", name="my.cats.metric", condition="args[0]!='dog'")
public String letsChron(String parameter) {
    //something useful surely happens here 
    return "mew mew";
}

With parameter “dog" registers value "3" to metric with name "my.cats.metric.exceptions”. If method ends normally, no metric is registered.

@Chronable(valueExpression="args[0].length()", name="my.cats.metric.exceptions", exceptionClass=DogException.class)
public String letsChron(String parameter) {
    if(parameter.equals("dog")) {
        throw new DogException("Oh noes, its a dog!");
    }
    //something useful surely happens here 
    return "mew mew";
}

Registers method execution time with name "my.amazing.metric" and prefix "with-var-prefix" that resolved from target object attribute MyObject.objectAttribute.

class MyObject {

    public String getObjectAttribute() {
        return "with-var-prefix";
    }    

    @Timeable(name="my.amazing.metric", nameSuffixExpression="target.objectAttribute")
    public void letsTime(String parameter) {
        //something useful surely happens here 
    }
}