Skip to content

Commit

Permalink
Fixes #3 so that a default MetricRegistry is used
Browse files Browse the repository at this point in the history
  • Loading branch information
astefanutti committed May 25, 2014
1 parent 63a402c commit 3ceb889
Show file tree
Hide file tree
Showing 27 changed files with 121 additions and 141 deletions.
93 changes: 49 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ and the `aspectjrt` modules can be used so that the only required dependency is

### Optional Dependencies

In addition to that, _Metrics AspectJ_ optional support of EL 3.0 expression for `MetricRegistry` and `Metric` name
evaluation requires an implementation of [Expression Language 3.0 (JSR-341)][] to be present at runtime.
In addition to that, _Metrics AspectJ_ optional support of EL 3.0 expression for `MetricRegistry` resolution
and `Metric` name evaluation requires an implementation of [Expression Language 3.0 (JSR-341)][] to be present at runtime.
For example, the [`metrics-aspectj-el`][] module is using the [GlassFish reference implementation][]
as `test` dependency for its unit tests execution:
```xml
Expand All @@ -136,6 +136,31 @@ as `test` dependency for its unit tests execution:

## Usage

### _Metrics AspectJ_ Activation

In order to activate _Metrics AspectJ_ for a particular class, it must be annotated with the `@Metrics` annotation:
```java
import com.codahale.metrics.annotation.Timed;

import org.stefanutti.metrics.aspectj.Metrics;

@Metrics
public class TimedMethod {

@Timed(name = "timerName")
public void timedMethod() {
}
}
```

At weaving time, _Metrics Aspects_ will detect the `@Metrics` annotation, scan all the declared methods of the target class
that are annotated with some _Metrics_ annotations, then create and register the corresponding `Metric` instances and finally
weave its aspects around these methods, so that at runtime, these `Metric` instances get called according
to the _Metrics_ annotations specification.

Note that this annotation won't be inherited if it's placed on an interface or a parent class.
More details are available in the [Limitations](#limitations) section.

### The _Metrics_ Annotations

_Metrics_ comes with the [`metrics-annotation`][] module that contains a series of annotations ([`@ExceptionMetered`][],
Expand All @@ -152,6 +177,9 @@ For example, a method can be annotated with the `@Timed` annotation so that its
```java
import com.codahale.metrics.annotation.Timed;

import org.stefanutti.metrics.aspectj.Metrics;

@Metrics
public class TimedMethod {

@Timed(name = "timerName")
Expand All @@ -169,6 +197,9 @@ A `static` method can also be annotated with the `@Timed` annotation so that its
```java
import com.codahale.metrics.annotation.Timed;

import org.stefanutti.metrics.aspectj.Metrics;

@Metrics
public class TimedMethod {

@Timed(name = "timerName")
Expand All @@ -186,6 +217,9 @@ Optionally, the `Metric` name can be resolved with an EL expression that evaluat
```java
import com.codahale.metrics.annotation.Timed;

import org.stefanutti.metrics.aspectj.Metrics;

@Metrics
public class TimedMethod {

private long id;
Expand All @@ -210,49 +244,21 @@ Note that these annotations won't be inherited if they are placed on interfaces
Indeed, according to the Java language specification, non-type annotations are not inherited. It's discussed
in more details in the [Limitations](#limitations) section.

### _Metrics AspectJ_ Activation and the `@Metrics` Annotation

In order to activate _Metrics AspectJ_ for a particular class, it must be annotated with the `@Metrics` annotation:
```java
import com.codahale.metrics.annotation.Timed;

import org.stefanutti.metrics.aspectj.Metrics;

@Metrics
public class TimedMethod {

@Timed(name = "timerName")
public void timedMethod() {
}
}
```

At weaving time, _Metrics Aspects_ will detect the `@Metrics` annotation, scan all the declared methods of the target class
that are annotated with some _Metrics_ annotations, then create and register the corresponding `Metric` instances and finally
weave its aspects around these methods, so that at runtime, these `Metric` instances get called according
to the _Metrics_ annotations specification.
### _Metrics_ Registry Resolution

Note that this annotation won't be inherited if it's placed on an interface or a parent class.
More details are available in the [Limitations](#limitations) section.

### _Metrics_ Registry Resolution and the `@Registry` Annotation

The `@Registry` annotation provides the way to declare the `MetricRegistry` to register the generated `Metric` instances into.
It targets classes and is ultimately used to create the `Metric` instances and weave the _Metrics AspectJ_ aspects into the annotated class.
The `Metrics.registry` annotation attribute provides the way to declare the `MetricRegistry` to register the generated `Metric` instances into.
Its value can either be a string literal that identifies a `MetricRegistry` accessible by name from the [`SharedMetricRegistries`][] class
or a valid EL expression that evaluates to the registry name or the registry instance. The resultant `MetricRegistry` is used
to register the `Metric` instantiated into each time a _Metrics_ annotation is present on that class methods.
It defaults to the string literal `metrics-registry`.

The `@Registry.value` mandatory `String` attribute can either be the registry name or a valid EL expression that evaluates to
the registry name or the registry instance. The resultant `MetricRegistry` is used to register the `Metric` instantiated into
each time a _Metrics_ annotation is present on that class methods.

The `MetricRegistry` can be resolved based on the registry name using the [`SharedMetricRegistries.getOrCreate(String name)`][] method:
The `MetricRegistry` can thus be resolved by name relying on the [`SharedMetricRegistries.getOrCreate(String name)`][] method:
```java
import com.codahale.metrics.annotation.Timed;

import org.stefanutti.metrics.aspectj.Metrics;
import org.stefanutti.metrics.aspectj.Registry;

@Metrics
@Registry("registryName")
@Metrics(registry = "registryName")
public class TimedMethodWithRegistryByName {

@Timed(name = "timerName")
Expand All @@ -261,16 +267,14 @@ public class TimedMethodWithRegistryByName {
}
```

The `MetricRegistry` can be resolved with an EL expression that evaluates to a bean property of type `MetricRegistry`:
Or with an EL expression that evaluates to a bean property of type `MetricRegistry`:
```java
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.annotation.Timed;

import org.stefanutti.metrics.aspectj.Metrics;
import org.stefanutti.metrics.aspectj.Registry;

@Metrics
@Registry("${this.registry}")
@Metrics(registry = "${this.registry}")
public class TimedMethodWithRegistryFromProperty {

private final MetricRegistry registry;
Expand All @@ -289,9 +293,10 @@ public class TimedMethodWithRegistryFromProperty {
}
```

The `MetricRegistry` can be resolved with an EL expression that evaluates to a `String`.
In that case the registry is resolved using the [`SharedMetricRegistries.getOrCreate(String name)`][] method.
Or with an EL expression that evaluates to a `String`. In that case the registry is resolved by name
using the [`SharedMetricRegistries.getOrCreate(String name)`][] method.

[`SharedMetricRegistries`]: http://maginatics.github.io/metrics/apidocs/com/codahale/metrics/SharedMetricRegistries.html
[`SharedMetricRegistries.getOrCreate(String name)`]: http://maginatics.github.io/metrics/apidocs/com/codahale/metrics/SharedMetricRegistries.html#getOrCreate%28java.lang.String%29

## Limitations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@

import com.codahale.metrics.annotation.Timed;
import org.stefanutti.metrics.aspectj.Metrics;
import org.stefanutti.metrics.aspectj.Registry;

@Metrics
@Registry("timerWithElRegistry")
@Metrics(registry = "timerWithElRegistry")
public class TimedMethodWithNameFromElExpression {

private final long id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.annotation.Timed;
import org.stefanutti.metrics.aspectj.Metrics;
import org.stefanutti.metrics.aspectj.Registry;

@Metrics
@Registry("${this.registry}")
@Metrics(registry = "${this.registry}")
public class TimedMethodWithRegistryFromBeanProperty {

private final MetricRegistry registry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@

import com.codahale.metrics.annotation.Timed;
import org.stefanutti.metrics.aspectj.Metrics;
import org.stefanutti.metrics.aspectj.Registry;

@Metrics
@Registry("${'staticRegistry'}")
@Metrics(registry = "${'staticRegistry'}")
public class TimedMethodWithRegistryFromSharedMetricRegistries {

@Timed(name = "singleTimedMethod")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@

import com.codahale.metrics.annotation.Timed;
import org.stefanutti.metrics.aspectj.Metrics;
import org.stefanutti.metrics.aspectj.Registry;

@Metrics
@Registry("timerStaticWithElRegistry")
@Metrics(registry = "timerStaticWithElRegistry")
public class TimedStaticMethodWithNameFromElExpression {

public final static long ID = Math.round(Math.random() * Long.MAX_VALUE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.annotation.Timed;
import org.stefanutti.metrics.aspectj.Metrics;
import org.stefanutti.metrics.aspectj.Registry;

@Metrics
@Registry("${TimedStaticMethodWithRegistryFromStaticProperty.REGISTRY}")
@Metrics(registry = "${TimedStaticMethodWithRegistryFromStaticProperty.REGISTRY}")
public class TimedStaticMethodWithRegistryFromStaticProperty {

public static final MetricRegistry REGISTRY = new MetricRegistry();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@
import com.codahale.metrics.annotation.Metered;
import com.codahale.metrics.annotation.Timed;
import org.stefanutti.metrics.aspectj.Metrics;
import org.stefanutti.metrics.aspectj.Registry;

import java.util.List;

@Metrics
@Registry("complexSignatureRegistry")
@Metrics(registry = "complexSignatureRegistry")
public class ComplexSignatureMethod {

@Timed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@

import com.codahale.metrics.annotation.Gauge;
import org.stefanutti.metrics.aspectj.Metrics;
import org.stefanutti.metrics.aspectj.Registry;

@Metrics
@Registry("singleGaugeRegistry")
@Metrics(registry = "singleGaugeRegistry")
public class GaugeMethodWithRegistryFromString {

private long singleGauge;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@

import com.codahale.metrics.annotation.Gauge;
import org.stefanutti.metrics.aspectj.Metrics;
import org.stefanutti.metrics.aspectj.Registry;

@Metrics
@Registry("visibilityGaugeRegistry")
@Metrics(registry = "visibilityGaugeRegistry")
public class GaugeMethodWithVisibilityModifiers {

private long publicGauge;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@

import com.codahale.metrics.annotation.Gauge;
import org.stefanutti.metrics.aspectj.Metrics;
import org.stefanutti.metrics.aspectj.Registry;

@Metrics
@Registry("singleGaugeStaticRegistry")
@Metrics(registry = "singleGaugeStaticRegistry")
public class GaugeStaticMethodWithRegistryFromString {

private static long singleStaticGauge;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@

import com.codahale.metrics.annotation.ExceptionMetered;
import org.stefanutti.metrics.aspectj.Metrics;
import org.stefanutti.metrics.aspectj.Registry;

@Metrics
@Registry("exceptionMeterRegistry")
@Metrics(registry = "exceptionMeterRegistry")
public class MeteredMethodWithExceptions {

@ExceptionMetered(name = "illegalArgumentExceptionMeteredMethod", cause = IllegalArgumentException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@

import com.codahale.metrics.annotation.Metered;
import org.stefanutti.metrics.aspectj.Metrics;
import org.stefanutti.metrics.aspectj.Registry;

@Metrics
@Registry("singleMeterRegistry")
@Metrics(registry = "singleMeterRegistry")
public class MeteredMethodWithRegistryFromString {

@Metered(name = "singleMeteredMethod")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@

import com.codahale.metrics.annotation.ExceptionMetered;
import org.stefanutti.metrics.aspectj.Metrics;
import org.stefanutti.metrics.aspectj.Registry;

@Metrics
@Registry("exceptionMeterStaticRegistry")
@Metrics(registry = "exceptionMeterStaticRegistry")
public class MeteredStaticMethodWithExceptions {

@ExceptionMetered(name = "illegalArgumentExceptionMeteredStaticMethod", cause = IllegalArgumentException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@

import com.codahale.metrics.annotation.Metered;
import org.stefanutti.metrics.aspectj.Metrics;
import org.stefanutti.metrics.aspectj.Registry;

@Metrics
@Registry("singleMeterStaticRegistry")
@Metrics(registry = "singleMeterStaticRegistry")
public class MeteredStaticMethodWithRegistryFromString {

@Metered(name = "singleMeteredStaticMethod")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
import com.codahale.metrics.annotation.Metered;
import com.codahale.metrics.annotation.Timed;
import org.stefanutti.metrics.aspectj.Metrics;
import org.stefanutti.metrics.aspectj.Registry;

@Metrics
@Registry("defaultNameRegistry")
@Metrics(registry = "defaultNameRegistry")
public class MetricMethodWithDefaultNames {

@Timed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@

import com.codahale.metrics.annotation.Timed;
import org.stefanutti.metrics.aspectj.Metrics;
import org.stefanutti.metrics.aspectj.Registry;

import java.util.List;

@Metrics
@Registry("overloadedTimerRegistry")
@Metrics(registry = "overloadedTimerRegistry")
public class TimedMethodOverloaded {

@Timed(name = "overloadedTimedMethodWithNoArguments")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@

import com.codahale.metrics.annotation.Timed;
import org.stefanutti.metrics.aspectj.Metrics;
import org.stefanutti.metrics.aspectj.Registry;

@Metrics
@Registry("timerRegistryForAbsoluteNamedTimer")
@Metrics(registry = "timerRegistryForAbsoluteNamedTimer")
public class TimedMethodWithAbsoluteName {

@Timed(name = "absolutelyTimedMethod", absolute = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@

import com.codahale.metrics.annotation.Timed;
import org.stefanutti.metrics.aspectj.Metrics;
import org.stefanutti.metrics.aspectj.Registry;

@Metrics
@Registry("timerWithElRegistry")
@Metrics(registry = "timerWithElRegistry")
public class TimedMethodWithNameFromElExpression {

private final String timerName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.annotation.Timed;
import org.stefanutti.metrics.aspectj.Metrics;
import org.stefanutti.metrics.aspectj.Registry;

@Metrics
@Registry("${this.registry}")
@Metrics(registry = "${this.registry}")
public class TimedMethodWithRegistryFromElExpression {

private final MetricRegistry registry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@

import com.codahale.metrics.annotation.Timed;
import org.stefanutti.metrics.aspectj.Metrics;
import org.stefanutti.metrics.aspectj.Registry;

@Metrics
@Registry("singleTimerRegistry")
@Metrics(registry = "singleTimerRegistry")
public class TimedMethodWithRegistryFromString {

@Timed(name = "singleTimedMethod")
Expand Down
Loading

0 comments on commit 3ceb889

Please sign in to comment.