hasProperty("count", equalTo(1L))));
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/envs/se/src/test/java/io/astefanutti/metrics/aspectj/se/TimedMethodWithAbsoluteNameTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.astefanutti.metrics.aspectj.se;
17 |
18 | import com.codahale.metrics.MetricRegistry;
19 | import com.codahale.metrics.SharedMetricRegistries;
20 | import com.codahale.metrics.Timer;
21 | import org.junit.After;
22 | import org.junit.Before;
23 | import org.junit.Test;
24 |
25 | import static org.hamcrest.Matchers.*;
26 | import static org.junit.Assert.assertThat;
27 |
28 | public class TimedMethodWithAbsoluteNameTest {
29 |
30 | private final static String REGISTRY_NAME = "timerRegistryForAbsoluteNamedTimer";
31 |
32 | private final static String TIMER_NAME = "absoluteTimedMethod";
33 |
34 | private TimedMethodWithAbsoluteName instance;
35 |
36 | @Before
37 | public void createTimedInstance() {
38 | instance = new TimedMethodWithAbsoluteName();
39 | }
40 |
41 | @After
42 | public void clearSharedMetricRegistries() {
43 | SharedMetricRegistries.clear();
44 | }
45 |
46 | @Test
47 | public void timedMethodNotCalledYet() {
48 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME));
49 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME);
50 | assertThat("Timer is not registered correctly", registry.getTimers(), hasKey(TIMER_NAME));
51 | Timer timer = registry.getTimers().get(TIMER_NAME);
52 |
53 | // Make sure that the timer hasn't been called yet
54 | assertThat("Timer count is incorrect", timer.getCount(), is(equalTo(0L)));
55 | }
56 |
57 | @Test
58 | public void callTimedMethodOnce() {
59 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME));
60 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME);
61 | assertThat("Timer is not registered correctly", registry.getTimers(), hasKey(TIMER_NAME));
62 | Timer timer = registry.getTimers().get(TIMER_NAME);
63 |
64 | // Call the timed method and assert it's been timed
65 | instance.absoluteTimedMethod();
66 | assertThat("Timer count is incorrect", timer.getCount(), is(equalTo(1L)));
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/envs/se/src/test/java/io/astefanutti/metrics/aspectj/se/TimedMethodWithRegistryFromStringTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.astefanutti.metrics.aspectj.se;
17 |
18 | import com.codahale.metrics.MetricRegistry;
19 | import com.codahale.metrics.SharedMetricRegistries;
20 | import com.codahale.metrics.Timer;
21 | import org.junit.After;
22 | import org.junit.Before;
23 | import org.junit.Test;
24 |
25 | import static org.hamcrest.Matchers.*;
26 | import static org.junit.Assert.assertThat;
27 |
28 | public class TimedMethodWithRegistryFromStringTest {
29 |
30 | private final static String REGISTRY_NAME = "singleTimerRegistry";
31 |
32 | private final static String TIMER_NAME = MetricRegistry.name(TimedMethodWithRegistryFromString.class, "singleTimedMethod");
33 |
34 | private TimedMethodWithRegistryFromString instance;
35 |
36 | @Before
37 | public void createTimedInstance() {
38 | instance = new TimedMethodWithRegistryFromString();
39 | }
40 |
41 | @After
42 | public void clearSharedMetricRegistries() {
43 | SharedMetricRegistries.clear();
44 | }
45 |
46 | @Test
47 | public void timedMethodNotCalledYet() {
48 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME));
49 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME);
50 | assertThat("Timer is not registered correctly", registry.getTimers(), hasKey(TIMER_NAME));
51 | Timer timer = registry.getTimers().get(TIMER_NAME);
52 |
53 | // Make sure that the timer hasn't been called yet
54 | assertThat("Timer count is incorrect", timer.getCount(), is(equalTo(0L)));
55 | }
56 |
57 | @Test
58 | public void callTimedMethodOnce() {
59 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME));
60 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME);
61 | assertThat("Timer is not registered correctly", registry.getTimers(), hasKey(TIMER_NAME));
62 | Timer timer = registry.getTimers().get(TIMER_NAME);
63 |
64 | // Call the timed method and assert it's been timed
65 | instance.singleTimedMethod();
66 | assertThat("Timer count is incorrect", timer.getCount(), is(equalTo(1L)));
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/impl/src/main/java/io/astefanutti/metrics/aspectj/Metrics.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.astefanutti.metrics.aspectj;
17 |
18 | import java.lang.annotation.ElementType;
19 | import java.lang.annotation.Inherited;
20 | import java.lang.annotation.Retention;
21 | import java.lang.annotation.RetentionPolicy;
22 | import java.lang.annotation.Target;
23 |
24 | /**
25 | * An annotation that activates Metrics AspectJ weaving.
26 | *
27 | * Metrics AspectJ will scan all the declared methods of the annotated class that are annotated with
28 | * some Metrics annotations, then create and register the corresponding Metric instances and finally weave
29 | * its aspects around these methods, so that at runtime, these Metric instances get called according
30 | * to the Metrics annotations specification.
31 | *
32 | *
33 | * For example, given the following class declaration:
34 | *
35 | * {@literal @}Metrics(registry = "${this.registry}")
36 | * public class TimedMethodWithRegistryFromProperty {
37 | *
38 | * private final MetricRegistry registry;
39 | *
40 | * public TimedMethodWithRegistryFromProperty(MetricRegistry registry) {
41 | * this.registry = registry;
42 | * }
43 | *
44 | * public MetricRegistry getRegistry() {
45 | * return registry;
46 | * }
47 | *
48 | * {@literal @}Timed(name = "timerName")
49 | * public void timedMethod() {
50 | * }
51 | * }
52 | *
53 | * A {@code Timer} instance will be registered in the provided {@code MetricRegistry}.
54 | */
55 | @Inherited
56 | @Target({ElementType.TYPE})
57 | @Retention(RetentionPolicy.RUNTIME)
58 | public @interface Metrics {
59 |
60 | /**
61 | * The expression that is used to resolve the {@code MetricRegistry} to be used to register metrics into.
62 | *
63 | * Its value can either be:
64 | *
65 | * - a string literal that identifies a {@code MetricRegistry} accessible
66 | * from the {@code SharedMetricRegistries} class,
67 | * - or a valid EL expression that evaluates
68 | * to the registry name or the {@code MetricRegistry} instance.
69 | *
70 | *
71 | * The resultant {@code MetricRegistry} is used to register the {@code Metric} instances into.
72 | */
73 | String registry() default "metrics-registry";
74 |
75 | }
--------------------------------------------------------------------------------
/envs/se/src/test/java/io/astefanutti/metrics/aspectj/se/MeteredMethodWithRegistryFromStringTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.astefanutti.metrics.aspectj.se;
17 |
18 | import com.codahale.metrics.Meter;
19 | import com.codahale.metrics.MetricRegistry;
20 | import com.codahale.metrics.SharedMetricRegistries;
21 | import org.junit.After;
22 | import org.junit.Before;
23 | import org.junit.Test;
24 |
25 | import static org.hamcrest.Matchers.*;
26 | import static org.junit.Assert.assertThat;
27 |
28 | public class MeteredMethodWithRegistryFromStringTest {
29 |
30 | private final static String REGISTRY_NAME = "singleMeterRegistry";
31 |
32 | private final static String METER_NAME = MetricRegistry.name(MeteredMethodWithRegistryFromString.class, "singleMeteredMethod");
33 |
34 | private MeteredMethodWithRegistryFromString instance;
35 |
36 | @Before
37 | public void createMeteredInstance() {
38 | instance = new MeteredMethodWithRegistryFromString();
39 | }
40 |
41 | @After
42 | public void clearSharedMetricRegistries() {
43 | SharedMetricRegistries.clear();
44 | }
45 |
46 | @Test
47 | public void meteredMethodNotCalledYet() {
48 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME));
49 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME);
50 | assertThat("Meter is not registered correctly", registry.getMeters(), hasKey(METER_NAME));
51 | Meter meter = registry.getMeters().get(METER_NAME);
52 |
53 | // Make sure that the meter hasn't been called yet
54 | assertThat("Meter count is incorrect", meter.getCount(), is(equalTo(0L)));
55 | }
56 |
57 | @Test
58 | public void callMeteredMethodOnce() {
59 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME));
60 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME);
61 | assertThat("Meter is not registered correctly", registry.getMeters(), hasKey(METER_NAME));
62 | Meter meter = registry.getMeters().get(METER_NAME);
63 |
64 | // Call the metered method and assert it's been marked
65 | instance.singleMeteredMethod();
66 | assertThat("Meter count is incorrect", meter.getCount(), is(equalTo(1L)));
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/impl/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 |
5 |
6 | io.astefanutti.metrics.aspectj
7 | metrics-aspectj-parent
8 | 1.3-SNAPSHOT
9 |
10 |
11 | metrics-aspectj
12 | Metrics AspectJ Library
13 |
14 |
15 |
16 |
17 | org.codehaus.mojo
18 | aspectj-maven-plugin
19 |
20 | 1.6
21 | 1.6
22 | 1.6
23 |
24 |
25 | true
26 |
27 |
28 |
29 |
30 |
31 | compile
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 | org.apache.maven.plugins
40 | maven-source-plugin
41 | 3.0.1
42 |
43 |
44 | attach-sources
45 | package
46 |
47 | jar-no-fork
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 | io.dropwizard.metrics
61 | metrics-core
62 |
63 |
64 |
65 | io.dropwizard.metrics
66 | metrics-annotation
67 |
68 |
69 |
70 | org.aspectj
71 | aspectjrt
72 |
73 |
74 |
75 |
76 |
77 | javax.el
78 | javax.el-api
79 |
80 |
81 |
82 |
83 |
--------------------------------------------------------------------------------
/envs/el/src/test/java/io/astefanutti/metrics/aspectj/el/TimedMethodWithRegistryFromSharedMetricRegistriesTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.astefanutti.metrics.aspectj.el;
17 |
18 | import com.codahale.metrics.MetricRegistry;
19 | import com.codahale.metrics.SharedMetricRegistries;
20 | import com.codahale.metrics.Timer;
21 | import org.junit.After;
22 | import org.junit.Before;
23 | import org.junit.Test;
24 |
25 | import static org.hamcrest.Matchers.*;
26 | import static org.junit.Assert.assertThat;
27 |
28 | public class TimedMethodWithRegistryFromSharedMetricRegistriesTest {
29 |
30 | private final static String REGISTRY_NAME = "staticRegistry";
31 |
32 | private final static String TIMER_NAME = MetricRegistry.name(TimedMethodWithRegistryFromSharedMetricRegistries.class, "singleTimedMethod");
33 |
34 | private TimedMethodWithRegistryFromSharedMetricRegistries instance;
35 |
36 | @Before
37 | public void createTimedInstance() {
38 | instance = new TimedMethodWithRegistryFromSharedMetricRegistries();
39 | }
40 |
41 | @After
42 | public void clearSharedMetricRegistries() {
43 | SharedMetricRegistries.clear();
44 | }
45 |
46 | @Test
47 | public void timedMethodNotCalledYet() {
48 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME));
49 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME);
50 | assertThat("Timer is not registered correctly", registry.getTimers(), hasKey(TIMER_NAME));
51 | Timer timer = registry.getTimers().get(TIMER_NAME);
52 |
53 | // Make sure that the timer hasn't been called yet
54 | assertThat("Timer count is incorrect", timer.getCount(), is(equalTo(0L)));
55 | }
56 |
57 | @Test
58 | public void callTimedMethodOnce() {
59 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME));
60 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME);
61 | assertThat("Timer is not registered correctly", registry.getTimers(), hasKey(TIMER_NAME));
62 | Timer timer = registry.getTimers().get(TIMER_NAME);
63 |
64 | // Call the timed method and assert it's been timed
65 | instance.singleTimedMethod();
66 | assertThat("Timer count is incorrect", timer.getCount(), is(equalTo(1L)));
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/impl/src/main/java/io/astefanutti/metrics/aspectj/JavaxElMetricStrategy.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.astefanutti.metrics.aspectj;
17 |
18 | import com.codahale.metrics.MetricRegistry;
19 | import com.codahale.metrics.SharedMetricRegistries;
20 |
21 | import javax.el.ELProcessor;
22 | import java.util.regex.Matcher;
23 |
24 | /* package-private */ final class JavaxElMetricStrategy implements MetricStrategy {
25 |
26 | private final ELProcessor processor;
27 |
28 | JavaxElMetricStrategy(Object object) {
29 | processor = new ELProcessor();
30 | processor.defineBean("this", object);
31 | }
32 |
33 | JavaxElMetricStrategy(Class> clazz) {
34 | processor = new ELProcessor();
35 | processor.getELManager().importClass(clazz.getName());
36 | }
37 |
38 | @Override
39 | public MetricRegistry resolveMetricRegistry(String registry) {
40 | Matcher matcher = EL_PATTERN.matcher(registry);
41 | if (matcher.matches()) {
42 | Object evaluation = processor.eval(matcher.group(1));
43 | if (evaluation instanceof String)
44 | return SharedMetricRegistries.getOrCreate((String) evaluation);
45 | else if (evaluation instanceof MetricRegistry)
46 | return (MetricRegistry) evaluation;
47 | else
48 | throw new IllegalStateException("Unable to resolve metrics registry from expression [" + registry + "]");
49 | } else if (!matcher.find()) {
50 | return SharedMetricRegistries.getOrCreate(registry);
51 | } else {
52 | return SharedMetricRegistries.getOrCreate(evaluateCompositeExpression(matcher));
53 | }
54 | }
55 |
56 | @Override
57 | public String resolveMetricName(String name) {
58 | Matcher matcher = EL_PATTERN.matcher(name);
59 | if (!matcher.find())
60 | return name;
61 | else
62 | return evaluateCompositeExpression(matcher);
63 | }
64 |
65 | private String evaluateCompositeExpression(Matcher matcher) {
66 | StringBuffer buffer = new StringBuffer();
67 | do {
68 | Object result = processor.eval(matcher.group(1));
69 | matcher.appendReplacement(buffer, result != null ? String.valueOf(result) : "");
70 | } while (matcher.find());
71 |
72 | matcher.appendTail(buffer);
73 | return buffer.toString();
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/envs/se/src/test/java/io/astefanutti/metrics/aspectj/se/MultipleMetricsStaticMethodTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.astefanutti.metrics.aspectj.se;
17 |
18 | import com.codahale.metrics.MetricRegistry;
19 | import com.codahale.metrics.SharedMetricRegistries;
20 | import io.astefanutti.metrics.aspectj.se.util.MetricsUtil;
21 | import org.junit.Test;
22 | import io.astefanutti.metrics.aspectj.se.util.MetricsUtil;
23 |
24 | import java.util.Set;
25 |
26 | import static org.hamcrest.Matchers.equalTo;
27 | import static org.hamcrest.Matchers.hasItem;
28 | import static org.hamcrest.Matchers.hasToString;
29 | import static org.hamcrest.Matchers.is;
30 | import static org.junit.Assert.assertThat;
31 |
32 | public class MultipleMetricsStaticMethodTest {
33 |
34 | private final static String REGISTRY_NAME = "multipleMetricsStaticRegistry";
35 |
36 | private final static String[] METRIC_NAMES = {"exception", "gauge", "meter", "timer"};
37 |
38 | private Set absoluteMetricNames() {
39 | return MetricsUtil.absoluteMetricNames(MultipleMetricsStaticMethod.class, METRIC_NAMES);
40 | }
41 |
42 | private String absoluteMetricName(String name) {
43 | return MetricsUtil.absoluteMetricName(MultipleMetricsStaticMethod.class, name);
44 | }
45 |
46 | @Test
47 | public void callMetricsStaticMethodsOnce() {
48 | // Call the monitored method and assert all the metrics have been created and marked
49 | MultipleMetricsStaticMethod.metricsMethod();
50 |
51 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME));
52 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME);
53 |
54 | assertThat("Metrics are not registered correctly", registry.getMetrics().keySet(), is(equalTo(absoluteMetricNames())));
55 |
56 | // Make sure that the metrics have been called
57 | assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName("exception")).getCount(), is(equalTo(0L)));
58 | assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName("meter")).getCount(), is(equalTo(1L)));
59 | assertThat("Timer count is incorrect", registry.getTimers().get(absoluteMetricName("timer")).getCount(), is(equalTo(1L)));
60 | assertThat("Gauge value is incorrect", registry.getGauges().get(absoluteMetricName("gauge")).getValue(), hasToString((equalTo("value"))));
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/envs/se/src/test/java/io/astefanutti/metrics/aspectj/se/GaugeMethodWithRegistryFromStringTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.astefanutti.metrics.aspectj.se;
17 |
18 | import com.codahale.metrics.Gauge;
19 | import com.codahale.metrics.MetricRegistry;
20 | import com.codahale.metrics.SharedMetricRegistries;
21 | import org.junit.After;
22 | import org.junit.Before;
23 | import org.junit.Test;
24 |
25 | import static org.hamcrest.Matchers.*;
26 | import static org.junit.Assert.assertThat;
27 |
28 | public class GaugeMethodWithRegistryFromStringTest {
29 |
30 | private final static String REGISTRY_NAME = "singleGaugeRegistry";
31 |
32 | private final static String GAUGE_NAME = MetricRegistry.name(GaugeMethodWithRegistryFromString.class, "singleGaugeMethod");
33 |
34 | private GaugeMethodWithRegistryFromString instance;
35 |
36 | @Before
37 | public void createGaugeInstance() {
38 | instance = new GaugeMethodWithRegistryFromString();
39 | }
40 |
41 | @After
42 | public void clearSharedMetricRegistries() {
43 | SharedMetricRegistries.clear();
44 | }
45 |
46 | @Test
47 | public void gaugeCalledWithDefaultValue() {
48 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME));
49 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME);
50 | assertThat("Gauge is not registered correctly", registry.getGauges(), hasKey(GAUGE_NAME));
51 | @SuppressWarnings("unchecked")
52 | Gauge gauge = registry.getGauges().get(GAUGE_NAME);
53 |
54 | // Make sure that the gauge has the expected value
55 | assertThat("Gauge value is incorrect", gauge.getValue(), is(equalTo(0L)));
56 | }
57 |
58 | @Test
59 | public void callGaugeAfterSetterCall() {
60 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME));
61 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME);
62 | assertThat("Gauge is not registered correctly", registry.getGauges(), hasKey(GAUGE_NAME));
63 | @SuppressWarnings("unchecked")
64 | Gauge gauge = registry.getGauges().get(GAUGE_NAME);
65 |
66 | // Call the setter method and assert the gauge is up-to-date
67 | long value = Math.round(Math.random() * Long.MAX_VALUE);
68 | instance.setSingleGauge(value);
69 | assertThat("Gauge value is incorrect", gauge.getValue(), is(equalTo(value)));
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/envs/el/src/test/java/io/astefanutti/metrics/aspectj/el/TimedMethodWithNameFromElExpressionTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.astefanutti.metrics.aspectj.el;
17 |
18 | import com.codahale.metrics.MetricRegistry;
19 | import com.codahale.metrics.SharedMetricRegistries;
20 | import com.codahale.metrics.Timer;
21 | import org.junit.After;
22 | import org.junit.Before;
23 | import org.junit.Test;
24 |
25 |
26 | import static org.hamcrest.Matchers.*;
27 | import static org.hamcrest.Matchers.hasItem;
28 | import static org.junit.Assert.assertThat;
29 |
30 | public class TimedMethodWithNameFromElExpressionTest {
31 |
32 | private final static String REGISTRY_NAME = "timerWithElRegistry";
33 |
34 | private String timerName;
35 |
36 | private TimedMethodWithNameFromElExpression instance;
37 |
38 | @Before
39 | public void createTimedInstance() {
40 | long id = Math.round(Math.random() * Long.MAX_VALUE);
41 | instance = new TimedMethodWithNameFromElExpression(id);
42 | timerName = MetricRegistry.name(TimedMethodWithNameFromElExpression.class, "timer " + id);
43 | }
44 |
45 | @After
46 | public void clearSharedMetricRegistries() {
47 | SharedMetricRegistries.clear();
48 | }
49 |
50 | @Test
51 | public void timedMethodNotCalledYet() {
52 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME));
53 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME);
54 | assertThat("Timer is not registered correctly", registry.getTimers(), hasKey(timerName));
55 | Timer timer = registry.getTimers().get(timerName);
56 |
57 | // Make sure that the timer hasn't been called yet
58 | assertThat("Timer count is incorrect", timer.getCount(), is(equalTo(0L)));
59 | }
60 |
61 | @Test
62 | public void callExpressionTimedMethodOnce() {
63 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME));
64 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME);
65 | assertThat("Timer is not registered correctly", registry.getTimers(), hasKey(timerName));
66 | Timer timer = registry.getTimers().get(timerName);
67 |
68 | // Call the timed methods and assert they've been timed
69 | instance.expressionTimedMethod();
70 | instance.compositeExpressionTimedMethod();
71 | instance.lambdaExpressionTimedMethod();
72 | assertThat("Timer count is incorrect", timer.getCount(), is(equalTo(3L)));
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/envs/se/src/test/java/io/astefanutti/metrics/aspectj/se/TimedMethodWithVisibilityModifiersTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.astefanutti.metrics.aspectj.se;
17 |
18 | import com.codahale.metrics.MetricRegistry;
19 | import com.codahale.metrics.SharedMetricRegistries;
20 | import com.codahale.metrics.Timer;
21 | import io.astefanutti.metrics.aspectj.se.util.MetricsUtil;
22 | import io.astefanutti.metrics.aspectj.se.util.MetricsUtil;
23 | import org.hamcrest.Matchers;
24 | import org.junit.After;
25 | import org.junit.Before;
26 | import org.junit.Test;
27 |
28 | import java.util.Set;
29 |
30 | import static org.fest.reflect.core.Reflection.method;
31 | import static org.hamcrest.Matchers.*;
32 | import static org.junit.Assert.assertThat;
33 |
34 | public class TimedMethodWithVisibilityModifiersTest {
35 |
36 | private final static String REGISTRY_NAME = "visibilityTimerRegistry";
37 |
38 | private final static String[] TIMER_NAMES = {"publicTimedMethod", "packagePrivateTimedMethod", "protectedTimedMethod", "privateTimedMethod"};
39 |
40 | private TimedMethodWithVisibilityModifiers instance;
41 |
42 | private Set absoluteMetricNames() {
43 | return MetricsUtil.absoluteMetricNames(TimedMethodWithVisibilityModifiers.class, TIMER_NAMES);
44 | }
45 |
46 | @Before
47 | public void createTimedInstance() {
48 | instance = new TimedMethodWithVisibilityModifiers();
49 | }
50 |
51 | @After
52 | public void clearSharedMetricRegistries() {
53 | SharedMetricRegistries.clear();
54 | }
55 |
56 | @Test
57 | public void timedMethodsNotCalledYet() {
58 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME));
59 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME);
60 | assertThat("Timers are not registered correctly", registry.getTimers().keySet(), is(equalTo(absoluteMetricNames())));
61 |
62 | // Make sure that all the timers haven't been called yet
63 | assertThat("Timer counts are incorrect", registry.getTimers().values(), everyItem(Matchers.hasProperty("count", equalTo(0L))));
64 | }
65 |
66 | @Test
67 | public void callTimedMethodsOnce() {
68 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME));
69 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME);
70 | assertThat("Timers are not registered correctly", registry.getTimers().keySet(), is(equalTo(absoluteMetricNames())));
71 |
72 | // Call the timed methods and assert they've all been timed once
73 | instance.publicTimedMethod();
74 | instance.protectedTimedMethod();
75 | instance.packagePrivateTimedMethod();
76 | method("privateTimedMethod").in(instance).invoke();
77 | assertThat("Timer counts are incorrect", registry.getTimers().values(), everyItem(Matchers.hasProperty("count", equalTo(1L))));
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/envs/se/src/test/java/io/astefanutti/metrics/aspectj/se/TimedMethodOverloadedTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.astefanutti.metrics.aspectj.se;
17 |
18 | import com.codahale.metrics.MetricRegistry;
19 | import com.codahale.metrics.SharedMetricRegistries;
20 | import com.codahale.metrics.Timer;
21 | import io.astefanutti.metrics.aspectj.se.util.MetricsUtil;
22 | import io.astefanutti.metrics.aspectj.se.util.MetricsUtil;
23 | import org.hamcrest.Matchers;
24 | import org.junit.After;
25 | import org.junit.Before;
26 | import org.junit.Test;
27 |
28 | import java.util.Arrays;
29 | import java.util.Set;
30 |
31 | import static org.hamcrest.Matchers.*;
32 | import static org.junit.Assert.assertThat;
33 |
34 | public class TimedMethodOverloadedTest {
35 |
36 | private final static String REGISTRY_NAME = "overloadedTimerRegistry";
37 |
38 | private final static String[] TIMER_NAMES = {"overloadedTimedMethodWithNoArguments", "overloadedTimedMethodWithStringArgument", "overloadedTimedMethodWithListOfStringArgument", "overloadedTimedMethodWithObjectArgument"};
39 |
40 | private TimedMethodOverloaded instance;
41 |
42 | private Set absoluteMetricNames() {
43 | return MetricsUtil.absoluteMetricNames(TimedMethodOverloaded.class, TIMER_NAMES);
44 | }
45 |
46 | @Before
47 | public void createTimedInstance() {
48 | instance = new TimedMethodOverloaded();
49 | }
50 |
51 | @After
52 | public void clearSharedMetricRegistries() {
53 | SharedMetricRegistries.clear();
54 | }
55 |
56 | @Test
57 | public void overloadedTimedMethodNotCalledYet() {
58 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME));
59 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME);
60 | assertThat("Timers are not registered correctly", registry.getTimers().keySet(), is(equalTo(absoluteMetricNames())));
61 |
62 | // Make sure that all the timers haven't been called yet
63 | assertThat("Timer counts are incorrect", registry.getTimers().values(), everyItem(Matchers.hasProperty("count", equalTo(0L))));
64 | }
65 |
66 | @Test
67 | public void callOverloadedTimedMethodOnce() {
68 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME));
69 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME);
70 | assertThat("Timers are not registered correctly", registry.getTimers().keySet(), is(equalTo(absoluteMetricNames())));
71 |
72 | // Call the timed methods and assert they've all been timed once
73 | instance.overloadedTimedMethod();
74 | instance.overloadedTimedMethod("string");
75 | instance.overloadedTimedMethod(new Object());
76 | instance.overloadedTimedMethod(Arrays.asList("string1", "string2"));
77 | assertThat("Timer counts are incorrect", registry.getTimers().values(), everyItem(Matchers.hasProperty("count", equalTo(1L))));
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/envs/se/src/test/java/io/astefanutti/metrics/aspectj/se/GaugeMethodWithVisibilityModifiersTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.astefanutti.metrics.aspectj.se;
17 |
18 | import com.codahale.metrics.Gauge;
19 | import com.codahale.metrics.MetricRegistry;
20 | import com.codahale.metrics.SharedMetricRegistries;
21 | import io.astefanutti.metrics.aspectj.se.util.MetricsUtil;
22 | import org.hamcrest.Matchers;
23 | import org.junit.After;
24 | import org.junit.Before;
25 | import org.junit.Test;
26 |
27 | import java.util.Set;
28 |
29 | import static org.fest.reflect.core.Reflection.method;
30 | import static org.hamcrest.Matchers.*;
31 | import static org.junit.Assert.assertThat;
32 |
33 | public class GaugeMethodWithVisibilityModifiersTest {
34 |
35 | private final static String REGISTRY_NAME = "visibilityGaugeRegistry";
36 |
37 | private final static String[] GAUGE_NAMES = {"publicGaugeMethod", "packagePrivateGaugeMethod", "protectedGaugeMethod", "privateGaugeMethod"};
38 |
39 | private GaugeMethodWithVisibilityModifiers instance;
40 |
41 | private Set absoluteMetricNames() {
42 | return MetricsUtil.absoluteMetricNames(GaugeMethodWithVisibilityModifiers.class, GAUGE_NAMES);
43 | }
44 |
45 | @Before
46 | public void createGaugeInstance() {
47 | instance = new GaugeMethodWithVisibilityModifiers();
48 | }
49 |
50 | @After
51 | public void clearSharedMetricRegistries() {
52 | SharedMetricRegistries.clear();
53 | }
54 |
55 | @Test
56 | public void gaugesCalledWithDefaultValues() {
57 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME));
58 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME);
59 | assertThat("Gauges are not registered correctly", registry.getGauges().keySet(), is(equalTo(absoluteMetricNames())));
60 |
61 | // Make sure that the gauges have the expected values
62 | assertThat("Gauge values are incorrect", registry.getGauges().values(), everyItem(Matchers.hasProperty("value", equalTo(0L))));
63 | }
64 |
65 | @Test
66 | public void callGaugesAfterSetterCalls() {
67 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME));
68 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME);
69 | assertThat("Gauges are not registered correctly", registry.getGauges().keySet(), is(equalTo(absoluteMetricNames())));
70 |
71 | long value = Math.round(Math.random() * Long.MAX_VALUE);
72 | // Call the setter methods
73 | instance.setPublicGauge(value);
74 | instance.setPackagePrivateGauge(value);
75 | instance.setProtectedGauge(value);
76 | method("setPrivateGauge").withParameterTypes(long.class).in(instance).invoke(value);
77 |
78 | // And assert the gauges are up-to-date
79 | assertThat("Gauge values are incorrect", registry.getGauges().values(), everyItem(Matchers.hasProperty("value", equalTo(value))));
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/envs/se/src/test/java/io/astefanutti/metrics/aspectj/se/InheritedTimedMethodWithVisibilityModifiersTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.astefanutti.metrics.aspectj.se;
17 |
18 | import com.codahale.metrics.MetricRegistry;
19 | import com.codahale.metrics.SharedMetricRegistries;
20 | import com.codahale.metrics.Timer;
21 | import io.astefanutti.metrics.aspectj.se.util.MetricsUtil;
22 | import org.hamcrest.Matchers;
23 | import org.junit.After;
24 | import org.junit.Before;
25 | import org.junit.Test;
26 |
27 | import java.util.Set;
28 |
29 | import static org.fest.reflect.core.Reflection.method;
30 | import static org.hamcrest.Matchers.equalTo;
31 | import static org.hamcrest.Matchers.everyItem;
32 | import static org.hamcrest.Matchers.hasItem;
33 | import static org.hamcrest.Matchers.is;
34 | import static org.junit.Assert.assertThat;
35 |
36 | public class InheritedTimedMethodWithVisibilityModifiersTest {
37 |
38 | private final static String REGISTRY_NAME = "visibilityTimerRegistry";
39 |
40 | private final static String[] TIMER_NAMES = {"publicTimedMethod", "packagePrivateTimedMethod", "protectedTimedMethod", "privateTimedMethod"};
41 |
42 | private InheritedTimedMethodWithVisibilityModifiers instance;
43 |
44 | private Set absoluteMetricNames() {
45 | return MetricsUtil.absoluteMetricNames(TimedMethodWithVisibilityModifiers.class, TIMER_NAMES);
46 | }
47 |
48 | @Before
49 | public void createTimedInstance() {
50 | instance = new InheritedTimedMethodWithVisibilityModifiers();
51 | }
52 |
53 | @After
54 | public void clearSharedMetricRegistries() {
55 | SharedMetricRegistries.clear();
56 | }
57 |
58 | @Test
59 | public void timedMethodsNotCalledYet() {
60 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME));
61 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME);
62 | assertThat("Timers are not registered correctly", registry.getTimers().keySet(), is(equalTo(absoluteMetricNames())));
63 |
64 | // Make sure that all the timers haven't been called yet
65 | assertThat("Timer counts are incorrect", registry.getTimers().values(), everyItem(Matchers.hasProperty("count", equalTo(0L))));
66 | }
67 |
68 | @Test
69 | public void callTimedMethodsOnce() {
70 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME));
71 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME);
72 | assertThat("Timers are not registered correctly", registry.getTimers().keySet(), is(equalTo(absoluteMetricNames())));
73 |
74 | // Call the timed methods and assert they've all been timed once
75 | instance.publicTimedMethod();
76 | instance.protectedTimedMethod();
77 | instance.packagePrivateTimedMethod();
78 | method("privateTimedMethod").in(instance).invoke();
79 | assertThat("Timer counts are incorrect", registry.getTimers().values(), everyItem(Matchers.hasProperty("count", equalTo(1L))));
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/envs/el/src/test/java/io/astefanutti/metrics/aspectj/el/MultipleMetricsMethodWithRegistryFromBeanPropertyTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.astefanutti.metrics.aspectj.el;
17 |
18 | import com.codahale.metrics.MetricRegistry;
19 | import org.junit.Before;
20 | import org.junit.Test;
21 | import io.astefanutti.metrics.aspectj.se.util.MetricsUtil;
22 |
23 | import java.util.Set;
24 |
25 | import static org.hamcrest.Matchers.equalTo;
26 | import static org.hamcrest.Matchers.hasToString;
27 | import static org.hamcrest.Matchers.is;
28 | import static org.junit.Assert.assertThat;
29 |
30 | public class MultipleMetricsMethodWithRegistryFromBeanPropertyTest {
31 |
32 | private final static String[] METRIC_NAMES = {"exception", "gauge", "meter", "timer"};
33 |
34 | private Set absoluteMetricNames() {
35 | return MetricsUtil.absoluteMetricNames(MultipleMetricsMethodWithRegistryFromBeanProperty.class, METRIC_NAMES);
36 | }
37 |
38 | private String absoluteMetricName(String name) {
39 | return MetricsUtil.absoluteMetricName(MultipleMetricsMethodWithRegistryFromBeanProperty.class, name);
40 | }
41 |
42 | private MultipleMetricsMethodWithRegistryFromBeanProperty instance;
43 |
44 | @Before
45 | public void createTimedInstance() {
46 | MetricRegistry registry = new MetricRegistry();
47 | instance = new MultipleMetricsMethodWithRegistryFromBeanProperty(registry);
48 | }
49 |
50 | @Test
51 | public void metricsMethodNotCalledYet() {
52 | MetricRegistry registry = instance.getRegistry();
53 | assertThat("Metrics are not registered correctly", registry.getMetrics().keySet(), is(equalTo(absoluteMetricNames())));
54 |
55 | // Make sure that the metrics haven't been called yet
56 | assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName("exception")).getCount(), is(equalTo(0L)));
57 | assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName("meter")).getCount(), is(equalTo(0L)));
58 | assertThat("Timer count is incorrect", registry.getTimers().get(absoluteMetricName("timer")).getCount(), is(equalTo(0L)));
59 | }
60 |
61 | @Test
62 | public void callMetricsMethodOnce() {
63 | MetricRegistry registry = instance.getRegistry();
64 | assertThat("Metrics are not registered correctly", registry.getMetrics().keySet(), is(equalTo(absoluteMetricNames())));
65 |
66 | // Call the monitored method and assert it's been instrumented
67 | instance.metricsMethod();
68 |
69 | // Make sure that the metrics have been called
70 | assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName("exception")).getCount(), is(equalTo(0L)));
71 | assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName("meter")).getCount(), is(equalTo(1L)));
72 | assertThat("Timer count is incorrect", registry.getTimers().get(absoluteMetricName("timer")).getCount(), is(equalTo(1L)));
73 | assertThat("Gauge value is incorrect", registry.getGauges().get(absoluteMetricName("gauge")).getValue(), hasToString((equalTo("value"))));
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/envs/el/src/test/java/io/astefanutti/metrics/aspectj/el/TimedStaticMethodWithNameFromElExpressionTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.astefanutti.metrics.aspectj.el;
17 |
18 | import com.codahale.metrics.MetricRegistry;
19 | import com.codahale.metrics.SharedMetricRegistries;
20 | import com.codahale.metrics.Timer;
21 | import org.junit.After;
22 | import org.junit.AfterClass;
23 | import org.junit.Test;
24 |
25 | import java.util.concurrent.atomic.AtomicLong;
26 |
27 | import static org.hamcrest.Matchers.*;
28 | import static org.junit.Assert.assertThat;
29 |
30 | public class TimedStaticMethodWithNameFromElExpressionTest {
31 |
32 | private final static String REGISTRY_NAME = "timerStaticWithElRegistry";
33 |
34 | private final static String TIMER_NAME = MetricRegistry.name(TimedStaticMethodWithNameFromElExpression.class, "timer " + TimedStaticMethodWithNameFromElExpression.ID);
35 |
36 | private final static AtomicLong TIMER_COUNT = new AtomicLong();
37 |
38 | @AfterClass
39 | public static void clearSharedMetricRegistries() {
40 | SharedMetricRegistries.clear();
41 | }
42 |
43 | @Test
44 | public void callExpressionTimedStaticMethodOnce() {
45 | // Call the timed static method and assert it's been timed once
46 | TimedStaticMethodWithNameFromElExpression.expressionStaticTimedMethod();
47 |
48 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME));
49 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME);
50 |
51 | assertThat("Timer is not registered correctly", registry.getTimers(), hasKey(TIMER_NAME));
52 | Timer timer = registry.getTimers().get(TIMER_NAME);
53 |
54 | assertThat("Timer count is incorrect", timer.getCount(), is(equalTo(TIMER_COUNT.incrementAndGet())));
55 | }
56 |
57 | @Test
58 | public void callCompositeExpressionTimedStaticMethodOnce() {
59 | // Call the timed static method and assert it's been timed once
60 | TimedStaticMethodWithNameFromElExpression.compositeExpressionStaticTimedMethod();
61 |
62 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME));
63 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME);
64 |
65 | assertThat("Timer is not registered correctly", registry.getTimers(), hasKey(TIMER_NAME));
66 | Timer timer = registry.getTimers().get(TIMER_NAME);
67 |
68 | assertThat("Timer count is incorrect", timer.getCount(), is(equalTo(TIMER_COUNT.incrementAndGet())));
69 | }
70 |
71 | @Test
72 | public void callLambdaExpressionTimedStaticMethodOnce() {
73 | // Call the timed static method and assert it's been timed once
74 | TimedStaticMethodWithNameFromElExpression.lambdaExpressionStaticTimedMethod();
75 |
76 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME));
77 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME);
78 |
79 | assertThat("Timer is not registered correctly", registry.getTimers(), hasKey(TIMER_NAME));
80 | Timer timer = registry.getTimers().get(TIMER_NAME);
81 |
82 | assertThat("Timer count is incorrect", timer.getCount(), is(equalTo(TIMER_COUNT.incrementAndGet())));
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/envs/se/src/test/java/io/astefanutti/metrics/aspectj/se/MultipleMetricsMethodTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.astefanutti.metrics.aspectj.se;
17 |
18 | import com.codahale.metrics.MetricRegistry;
19 | import com.codahale.metrics.SharedMetricRegistries;
20 | import io.astefanutti.metrics.aspectj.se.util.MetricsUtil;
21 | import org.junit.After;
22 | import org.junit.Before;
23 | import org.junit.Test;
24 | import io.astefanutti.metrics.aspectj.se.util.MetricsUtil;
25 |
26 | import java.util.Set;
27 |
28 | import static org.hamcrest.Matchers.equalTo;
29 | import static org.hamcrest.Matchers.hasItem;
30 | import static org.hamcrest.Matchers.hasToString;
31 | import static org.hamcrest.Matchers.is;
32 | import static org.junit.Assert.assertThat;
33 |
34 | public class MultipleMetricsMethodTest {
35 |
36 | private final static String REGISTRY_NAME = "multipleMetricsRegistry";
37 |
38 | private final static String[] METRIC_NAMES = {"exception", "gauge", "meter", "timer"};
39 |
40 | private Set absoluteMetricNames() {
41 | return MetricsUtil.absoluteMetricNames(MultipleMetricsMethod.class, METRIC_NAMES);
42 | }
43 |
44 | private String absoluteMetricName(String name) {
45 | return MetricsUtil.absoluteMetricName(MultipleMetricsMethod.class, name);
46 | }
47 |
48 | private MultipleMetricsMethod instance;
49 |
50 | @Before
51 | public void createInstance() {
52 | instance = new MultipleMetricsMethod();
53 | }
54 |
55 | @After
56 | public void clearSharedMetricRegistries() {
57 | SharedMetricRegistries.clear();
58 | }
59 |
60 | @Test
61 | public void metricsMethodNotCalledYet() {
62 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME));
63 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME);
64 | assertThat("Metrics are not registered correctly", registry.getMetrics().keySet(), is(equalTo(absoluteMetricNames())));
65 |
66 | // Make sure that the metrics haven't been called yet
67 | assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName("exception")).getCount(), is(equalTo(0L)));
68 | assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName("meter")).getCount(), is(equalTo(0L)));
69 | assertThat("Timer count is incorrect", registry.getTimers().get(absoluteMetricName("timer")).getCount(), is(equalTo(0L)));
70 | }
71 |
72 | @Test
73 | public void callMetricsMethodOnce() {
74 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME));
75 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME);
76 | assertThat("Metrics are not registered correctly", registry.getMetrics().keySet(), is(equalTo(absoluteMetricNames())));
77 |
78 | // Call the monitored method and assert it's been instrumented
79 | instance.metricsMethod();
80 |
81 | // Make sure that the metrics have been called
82 | assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName("exception")).getCount(), is(equalTo(0L)));
83 | assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName("meter")).getCount(), is(equalTo(1L)));
84 | assertThat("Timer count is incorrect", registry.getTimers().get(absoluteMetricName("timer")).getCount(), is(equalTo(1L)));
85 | assertThat("Gauge value is incorrect", registry.getGauges().get(absoluteMetricName("gauge")).getValue(), hasToString((equalTo("value"))));
86 | }
87 | }
88 |
--------------------------------------------------------------------------------
/impl/src/main/aspect/io/astefanutti/metrics/aspectj/AbstractMetricAspect.aj:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.astefanutti.metrics.aspectj;
17 |
18 | import com.codahale.metrics.Metric;
19 | import com.codahale.metrics.annotation.ExceptionMetered;
20 | import com.codahale.metrics.annotation.Gauge;
21 | import com.codahale.metrics.annotation.Metered;
22 | import com.codahale.metrics.annotation.Timed;
23 |
24 | import java.lang.annotation.Annotation;
25 | import java.lang.reflect.InvocationTargetException;
26 | import java.lang.reflect.Method;
27 |
28 | abstract aspect AbstractMetricAspect {
29 |
30 | protected interface MetricFactory {
31 | T metric(String name, boolean absolute);
32 | }
33 |
34 | protected AnnotatedMetric metricAnnotation(Method method, Class extends Annotation> clazz, MetricFactory factory) {
35 | if (method.isAnnotationPresent(clazz)) {
36 | Annotation annotation = method.getAnnotation(clazz);
37 | T metric = factory.metric(metricAnnotationName(annotation), metricAnnotationAbsolute(annotation));
38 | return new AnnotatedMetric.IsPresent(metric, annotation);
39 | } else {
40 | return new AnnotatedMetric.IsNotPresent();
41 | }
42 | }
43 |
44 | protected static String metricAnnotationName(Annotation annotation) {
45 | if (Gauge.class.isInstance(annotation))
46 | return ((Gauge) annotation).name();
47 | else if (ExceptionMetered.class.isInstance(annotation))
48 | return ((ExceptionMetered) annotation).name();
49 | else if (Metered.class.isInstance(annotation))
50 | return ((Metered) annotation).name();
51 | else if (Timed.class.isInstance(annotation))
52 | return ((Timed) annotation).name();
53 | else
54 | throw new IllegalArgumentException("Unsupported Metrics annotation [" + annotation.getClass().getName() + "]");
55 | }
56 |
57 | protected static boolean metricAnnotationAbsolute(Annotation annotation) {
58 | if (Gauge.class.isInstance(annotation))
59 | return ((Gauge) annotation).absolute();
60 | else if (ExceptionMetered.class.isInstance(annotation))
61 | return ((ExceptionMetered) annotation).absolute();
62 | else if (Metered.class.isInstance(annotation))
63 | return ((Metered) annotation).absolute();
64 | else if (Timed.class.isInstance(annotation))
65 | return ((Timed) annotation).absolute();
66 | else
67 | throw new IllegalArgumentException("Unsupported Metrics annotation [" + annotation.getClass().getName() + "]");
68 | }
69 |
70 | protected static class ForwardingGauge implements com.codahale.metrics.Gauge