Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support multiple Metrics annotations per method
- Loading branch information
1 parent
a87be36
commit 507235c
Showing
24 changed files
with
357 additions
and
85 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
envs/se/src/main/java/org/stefanutti/metrics/aspectj/se/MultipleMetricsMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Copyright (C) 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.stefanutti.metrics.aspectj.se; | ||
|
||
import com.codahale.metrics.annotation.ExceptionMetered; | ||
import com.codahale.metrics.annotation.Gauge; | ||
import com.codahale.metrics.annotation.Metered; | ||
import com.codahale.metrics.annotation.Timed; | ||
import org.stefanutti.metrics.aspectj.Metrics; | ||
|
||
@Metrics(registry = "multipleMetricsRegistry") | ||
public class MultipleMetricsMethod { | ||
|
||
@ExceptionMetered(name = "exception") | ||
@Gauge(name = "gauge") | ||
@Metered(name = "meter") | ||
@Timed(name = "timer") | ||
public String metricsMethod() { | ||
return "value"; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
envs/se/src/main/java/org/stefanutti/metrics/aspectj/se/MultipleMetricsStaticMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Copyright (C) 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.stefanutti.metrics.aspectj.se; | ||
|
||
import com.codahale.metrics.annotation.ExceptionMetered; | ||
import com.codahale.metrics.annotation.Gauge; | ||
import com.codahale.metrics.annotation.Metered; | ||
import com.codahale.metrics.annotation.Timed; | ||
import org.stefanutti.metrics.aspectj.Metrics; | ||
|
||
@Metrics(registry = "multipleMetricsStaticRegistry") | ||
public class MultipleMetricsStaticMethod { | ||
|
||
@ExceptionMetered(name = "exception") | ||
@Gauge(name = "gauge") | ||
@Metered(name = "meter") | ||
@Timed(name = "timer") | ||
public static String metricsMethod() { | ||
return "value"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
envs/se/src/test/java/org/stefanutti/metrics/aspectj/se/MultipleMetricsMethodTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/** | ||
* Copyright (C) 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.stefanutti.metrics.aspectj.se; | ||
|
||
import com.codahale.metrics.MetricRegistry; | ||
import com.codahale.metrics.SharedMetricRegistries; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.stefanutti.metrics.aspectj.se.util.MetricsUtil; | ||
|
||
import java.util.Set; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.hasItem; | ||
import static org.hamcrest.Matchers.hasToString; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class MultipleMetricsMethodTest { | ||
|
||
private final static String REGISTRY_NAME = "multipleMetricsRegistry"; | ||
|
||
private final static String[] METRIC_NAMES = {"exception", "gauge", "meter", "timer"}; | ||
|
||
private Set<String> absoluteMetricNames() { | ||
return MetricsUtil.absoluteMetricNames(MultipleMetricsMethod.class, METRIC_NAMES); | ||
} | ||
|
||
private String absoluteMetricName(String name) { | ||
return MetricsUtil.absoluteMetricName(MultipleMetricsMethod.class, name); | ||
} | ||
|
||
private MultipleMetricsMethod instance; | ||
|
||
@Before | ||
public void createInstance() { | ||
instance = new MultipleMetricsMethod(); | ||
} | ||
|
||
@After | ||
public void clearSharedMetricRegistries() { | ||
SharedMetricRegistries.clear(); | ||
} | ||
|
||
@Test | ||
public void metricsMethodNotCalledYet() { | ||
assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME)); | ||
MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME); | ||
assertThat("Metrics are not registered correctly", registry.getMetrics().keySet(), is(equalTo(absoluteMetricNames()))); | ||
|
||
// Make sure that the metrics haven't been called yet | ||
assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName("exception")).getCount(), is(equalTo(0L))); | ||
assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName("meter")).getCount(), is(equalTo(0L))); | ||
assertThat("Timer count is incorrect", registry.getTimers().get(absoluteMetricName("timer")).getCount(), is(equalTo(0L))); | ||
} | ||
|
||
@Test | ||
public void callMetricsMethodOnce() { | ||
assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME)); | ||
MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME); | ||
assertThat("Metrics are not registered correctly", registry.getMetrics().keySet(), is(equalTo(absoluteMetricNames()))); | ||
|
||
// Call the monitored method and assert it's been instrumented | ||
instance.metricsMethod(); | ||
|
||
// Make sure that the metrics have been called | ||
assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName("exception")).getCount(), is(equalTo(0L))); | ||
assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName("meter")).getCount(), is(equalTo(1L))); | ||
assertThat("Timer count is incorrect", registry.getTimers().get(absoluteMetricName("timer")).getCount(), is(equalTo(1L))); | ||
assertThat("Gauge value is incorrect", registry.getGauges().get(absoluteMetricName("gauge")).getValue(), hasToString((equalTo("value")))); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
envs/se/src/test/java/org/stefanutti/metrics/aspectj/se/MultipleMetricsStaticMethodTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Copyright (C) 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.stefanutti.metrics.aspectj.se; | ||
|
||
import com.codahale.metrics.MetricRegistry; | ||
import com.codahale.metrics.SharedMetricRegistries; | ||
import org.junit.Test; | ||
import org.stefanutti.metrics.aspectj.se.util.MetricsUtil; | ||
|
||
import java.util.Set; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.hasItem; | ||
import static org.hamcrest.Matchers.hasToString; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class MultipleMetricsStaticMethodTest { | ||
|
||
private final static String REGISTRY_NAME = "multipleMetricsStaticRegistry"; | ||
|
||
private final static String[] METRIC_NAMES = {"exception", "gauge", "meter", "timer"}; | ||
|
||
private Set<String> absoluteMetricNames() { | ||
return MetricsUtil.absoluteMetricNames(MultipleMetricsStaticMethod.class, METRIC_NAMES); | ||
} | ||
|
||
private String absoluteMetricName(String name) { | ||
return MetricsUtil.absoluteMetricName(MultipleMetricsStaticMethod.class, name); | ||
} | ||
|
||
@Test | ||
public void callMetricsStaticMethodsOnce() { | ||
// Call the monitored method and assert all the metrics have been created and marked | ||
MultipleMetricsStaticMethod.metricsMethod(); | ||
|
||
assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME)); | ||
MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME); | ||
|
||
assertThat("Metrics are not registered correctly", registry.getMetrics().keySet(), is(equalTo(absoluteMetricNames()))); | ||
|
||
// Make sure that the metrics have been called | ||
assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName("exception")).getCount(), is(equalTo(0L))); | ||
assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName("meter")).getCount(), is(equalTo(1L))); | ||
assertThat("Timer count is incorrect", registry.getTimers().get(absoluteMetricName("timer")).getCount(), is(equalTo(1L))); | ||
assertThat("Gauge value is incorrect", registry.getGauges().get(absoluteMetricName("gauge")).getValue(), hasToString((equalTo("value")))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.