├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── deps └── pom.xml ├── envs ├── el │ ├── pom.xml │ └── src │ │ ├── main │ │ └── java │ │ │ └── io │ │ │ └── astefanutti │ │ │ └── metrics │ │ │ └── aspectj │ │ │ └── el │ │ │ ├── MultipleMetricsMethodWithRegistryFromBeanProperty.java │ │ │ ├── TimedMethodWithNameFromElExpression.java │ │ │ ├── TimedMethodWithRegistryFromBeanProperty.java │ │ │ ├── TimedMethodWithRegistryFromSharedMetricRegistries.java │ │ │ ├── TimedStaticMethodWithNameFromElExpression.java │ │ │ └── TimedStaticMethodWithRegistryFromStaticProperty.java │ │ └── test │ │ └── java │ │ └── io │ │ └── astefanutti │ │ └── metrics │ │ └── aspectj │ │ └── el │ │ ├── MultipleMetricsMethodWithRegistryFromBeanPropertyTest.java │ │ ├── TimedMethodWithNameFromElExpressionTest.java │ │ ├── TimedMethodWithRegistryFromBeanPropertyTest.java │ │ ├── TimedMethodWithRegistryFromSharedMetricRegistriesTest.java │ │ ├── TimedStaticMethodWithNameFromElExpressionTest.java │ │ └── TimedStaticMethodWithRegistryFromStaticPropertyTest.java ├── pom.xml └── se │ ├── pom.xml │ └── src │ ├── main │ └── java │ │ └── io │ │ └── astefanutti │ │ └── metrics │ │ └── aspectj │ │ └── se │ │ ├── ComplexSignatureMethod.java │ │ ├── GaugeMethodWithRegistryFromString.java │ │ ├── GaugeMethodWithVisibilityModifiers.java │ │ ├── GaugeStaticMethodWithRegistryFromString.java │ │ ├── InheritedTimedMethodWithVisibilityModifiers.java │ │ ├── MeteredMethodWithExceptions.java │ │ ├── MeteredMethodWithRegistryFromString.java │ │ ├── MeteredStaticMethodWithExceptions.java │ │ ├── MeteredStaticMethodWithRegistryFromString.java │ │ ├── MetricMethodWithDefaultNames.java │ │ ├── MultipleMetricsMethod.java │ │ ├── MultipleMetricsStaticMethod.java │ │ ├── TimedMethodOverloaded.java │ │ ├── TimedMethodWithAbsoluteName.java │ │ ├── TimedMethodWithNameFromElExpression.java │ │ ├── TimedMethodWithRegistryFromElExpression.java │ │ ├── TimedMethodWithRegistryFromString.java │ │ ├── TimedMethodWithVisibilityModifiers.java │ │ └── TimedStaticMethodWithVisibilityModifiers.java │ └── test │ └── java │ └── io │ └── astefanutti │ └── metrics │ └── aspectj │ └── se │ ├── ComplexSignatureMethodTest.java │ ├── GaugeMethodWithRegistryFromStringTest.java │ ├── GaugeMethodWithVisibilityModifiersTest.java │ ├── GaugeStaticMethodWithRegistryFromStringTest.java │ ├── InheritedTimedMethodWithVisibilityModifiersTest.java │ ├── MeteredMethodWithExceptionsTest.java │ ├── MeteredMethodWithRegistryFromStringTest.java │ ├── MeteredStaticMethodWithExceptionsTest.java │ ├── MeteredStaticMethodWithRegistryFromStringTest.java │ ├── MetricMethodWithDefaultNamesTest.java │ ├── MultipleMetricsMethodTest.java │ ├── MultipleMetricsStaticMethodTest.java │ ├── TimedMethodOverloadedTest.java │ ├── TimedMethodWithAbsoluteNameTest.java │ ├── TimedMethodWithNameFromElExpressionTest.java │ ├── TimedMethodWithRegistryFromElExpressionTest.java │ ├── TimedMethodWithRegistryFromStringTest.java │ ├── TimedMethodWithVisibilityModifiersTest.java │ ├── TimedStaticMethodWithVisibilityModifiersTest.java │ └── util │ └── MetricsUtil.java ├── impl ├── pom.xml └── src │ └── main │ ├── aspect │ └── io │ │ └── astefanutti │ │ └── metrics │ │ └── aspectj │ │ ├── AbstractMetricAspect.aj │ │ ├── ExceptionMeteredAspect.aj │ │ ├── ExceptionMeteredStaticAspect.aj │ │ ├── MeteredAspect.aj │ │ ├── MeteredStaticAspect.aj │ │ ├── MetricAspect.aj │ │ ├── MetricStaticAspect.aj │ │ ├── TimedAspect.aj │ │ └── TimedStaticAspect.aj │ └── java │ └── io │ └── astefanutti │ └── metrics │ └── aspectj │ ├── AnnotatedMetric.java │ ├── JavaSeMetricStrategy.java │ ├── JavaxElMetricStrategy.java │ ├── MetricStrategy.java │ ├── MetricStrategyFactory.java │ ├── Metrics.java │ └── Profiled.java └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | .classpath 2 | .project 3 | .settings/ 4 | target/ 5 | logs/ 6 | .idea/ 7 | *.iml 8 | dependency-reduced-pom.xml 9 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | dist: trusty 2 | sudo: false 3 | 4 | language: java 5 | 6 | cache: 7 | directories: 8 | - $HOME/.m2 9 | 10 | install: 11 | - mvn clean install -Dmaven.test.skip.exec 12 | 13 | script: 14 | - mvn test -Pwith-lib,jacoco 15 | 16 | jdk: 17 | - oraclejdk8 18 | 19 | notifications: 20 | email: 21 | recipients: 22 | - antonin.stefanutti@gmail.com 23 | 24 | after_success: 25 | - mvn jacoco:report -pl impl 26 | - mvn -N coveralls:report 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, and 10 | distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by the copyright 13 | owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all other entities 16 | that control, are controlled by, or are under common control with that entity. 17 | For the purposes of this definition, "control" means (i) the power, direct or 18 | indirect, to cause the direction or management of such entity, whether by 19 | contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the 20 | outstanding shares, or (iii) beneficial ownership of such entity. 21 | 22 | "You" (or "Your") shall mean an individual or Legal Entity exercising 23 | permissions granted by this License. 24 | 25 | "Source" form shall mean the preferred form for making modifications, including 26 | but not limited to software source code, documentation source, and configuration 27 | files. 28 | 29 | "Object" form shall mean any form resulting from mechanical transformation or 30 | translation of a Source form, including but not limited to compiled object code, 31 | generated documentation, and conversions to other media types. 32 | 33 | "Work" shall mean the work of authorship, whether in Source or Object form, made 34 | available under the License, as indicated by a copyright notice that is included 35 | in or attached to the work (an example is provided in the Appendix below). 36 | 37 | "Derivative Works" shall mean any work, whether in Source or Object form, that 38 | is based on (or derived from) the Work and for which the editorial revisions, 39 | annotations, elaborations, or other modifications represent, as a whole, an 40 | original work of authorship. For the purposes of this License, Derivative Works 41 | shall not include works that remain separable from, or merely link (or bind by 42 | name) to the interfaces of, the Work and Derivative Works thereof. 43 | 44 | "Contribution" shall mean any work of authorship, including the original version 45 | of the Work and any modifications or additions to that Work or Derivative Works 46 | thereof, that is intentionally submitted to Licensor for inclusion in the Work 47 | by the copyright owner or by an individual or Legal Entity authorized to submit 48 | on behalf of the copyright owner. For the purposes of this definition, 49 | "submitted" means any form of electronic, verbal, or written communication sent 50 | to the Licensor or its representatives, including but not limited to 51 | communication on electronic mailing lists, source code control systems, and 52 | issue tracking systems that are managed by, or on behalf of, the Licensor for 53 | the purpose of discussing and improving the Work, but excluding communication 54 | that is conspicuously marked or otherwise designated in writing by the copyright 55 | owner as "Not a Contribution." 56 | 57 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf 58 | of whom a Contribution has been received by Licensor and subsequently 59 | incorporated within the Work. 60 | 61 | 2. Grant of Copyright License. 62 | 63 | Subject to the terms and conditions of this License, each Contributor hereby 64 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, 65 | irrevocable copyright license to reproduce, prepare Derivative Works of, 66 | publicly display, publicly perform, sublicense, and distribute the Work and such 67 | Derivative Works in Source or Object form. 68 | 69 | 3. Grant of Patent License. 70 | 71 | Subject to the terms and conditions of this License, each Contributor hereby 72 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, 73 | irrevocable (except as stated in this section) patent license to make, have 74 | made, use, offer to sell, sell, import, and otherwise transfer the Work, where 75 | such license applies only to those patent claims licensable by such Contributor 76 | that are necessarily infringed by their Contribution(s) alone or by combination 77 | of their Contribution(s) with the Work to which such Contribution(s) was 78 | submitted. If You institute patent litigation against any entity (including a 79 | cross-claim or counterclaim in a lawsuit) alleging that the Work or a 80 | Contribution incorporated within the Work constitutes direct or contributory 81 | patent infringement, then any patent licenses granted to You under this License 82 | for that Work shall terminate as of the date such litigation is filed. 83 | 84 | 4. Redistribution. 85 | 86 | You may reproduce and distribute copies of the Work or Derivative Works thereof 87 | in any medium, with or without modifications, and in Source or Object form, 88 | provided that You meet the following conditions: 89 | 90 | You must give any other recipients of the Work or Derivative Works a copy of 91 | this License; and 92 | You must cause any modified files to carry prominent notices stating that You 93 | changed the files; and 94 | You must retain, in the Source form of any Derivative Works that You distribute, 95 | all copyright, patent, trademark, and attribution notices from the Source form 96 | of the Work, excluding those notices that do not pertain to any part of the 97 | Derivative Works; and 98 | If the Work includes a "NOTICE" text file as part of its distribution, then any 99 | Derivative Works that You distribute must include a readable copy of the 100 | attribution notices contained within such NOTICE file, excluding those notices 101 | that do not pertain to any part of the Derivative Works, in at least one of the 102 | following places: within a NOTICE text file distributed as part of the 103 | Derivative Works; within the Source form or documentation, if provided along 104 | with the Derivative Works; or, within a display generated by the Derivative 105 | Works, if and wherever such third-party notices normally appear. The contents of 106 | the NOTICE file are for informational purposes only and do not modify the 107 | License. You may add Your own attribution notices within Derivative Works that 108 | You distribute, alongside or as an addendum to the NOTICE text from the Work, 109 | provided that such additional attribution notices cannot be construed as 110 | modifying the License. 111 | You may add Your own copyright statement to Your modifications and may provide 112 | additional or different license terms and conditions for use, reproduction, or 113 | distribution of Your modifications, or for any such Derivative Works as a whole, 114 | provided Your use, reproduction, and distribution of the Work otherwise complies 115 | with the conditions stated in this License. 116 | 117 | 5. Submission of Contributions. 118 | 119 | Unless You explicitly state otherwise, any Contribution intentionally submitted 120 | for inclusion in the Work by You to the Licensor shall be under the terms and 121 | conditions of this License, without any additional terms or conditions. 122 | Notwithstanding the above, nothing herein shall supersede or modify the terms of 123 | any separate license agreement you may have executed with Licensor regarding 124 | such Contributions. 125 | 126 | 6. Trademarks. 127 | 128 | This License does not grant permission to use the trade names, trademarks, 129 | service marks, or product names of the Licensor, except as required for 130 | reasonable and customary use in describing the origin of the Work and 131 | reproducing the content of the NOTICE file. 132 | 133 | 7. Disclaimer of Warranty. 134 | 135 | Unless required by applicable law or agreed to in writing, Licensor provides the 136 | Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, 137 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, 138 | including, without limitation, any warranties or conditions of TITLE, 139 | NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are 140 | solely responsible for determining the appropriateness of using or 141 | redistributing the Work and assume any risks associated with Your exercise of 142 | permissions under this License. 143 | 144 | 8. Limitation of Liability. 145 | 146 | In no event and under no legal theory, whether in tort (including negligence), 147 | contract, or otherwise, unless required by applicable law (such as deliberate 148 | and grossly negligent acts) or agreed to in writing, shall any Contributor be 149 | liable to You for damages, including any direct, indirect, special, incidental, 150 | or consequential damages of any character arising as a result of this License or 151 | out of the use or inability to use the Work (including but not limited to 152 | damages for loss of goodwill, work stoppage, computer failure or malfunction, or 153 | any and all other commercial damages or losses), even if such Contributor has 154 | been advised of the possibility of such damages. 155 | 156 | 9. Accepting Warranty or Additional Liability. 157 | 158 | While redistributing the Work or Derivative Works thereof, You may choose to 159 | offer, and charge a fee for, acceptance of support, warranty, indemnity, or 160 | other liability obligations and/or rights consistent with this License. However, 161 | in accepting such obligations, You may act only on Your own behalf and on Your 162 | sole responsibility, not on behalf of any other Contributor, and only if You 163 | agree to indemnify, defend, and hold each Contributor harmless for any liability 164 | incurred by, or claims asserted against, such Contributor by reason of your 165 | accepting any such warranty or additional liability. 166 | 167 | END OF TERMS AND CONDITIONS 168 | 169 | APPENDIX: How to apply the Apache License to your work 170 | 171 | To apply the Apache License to your work, attach the following boilerplate 172 | notice, with the fields enclosed by brackets "[]" replaced with your own 173 | identifying information. (Don't include the brackets!) The text should be 174 | enclosed in the appropriate comment syntax for the file format. We also 175 | recommend that a file or class name and description of purpose be included on 176 | the same "printed page" as the copyright notice for easier identification within 177 | third-party archives. 178 | 179 | Copyright [yyyy] [name of copyright owner] 180 | 181 | Licensed under the Apache License, Version 2.0 (the "License"); 182 | you may not use this file except in compliance with the License. 183 | You may obtain a copy of the License at 184 | 185 | http://www.apache.org/licenses/LICENSE-2.0 186 | 187 | Unless required by applicable law or agreed to in writing, software 188 | distributed under the License is distributed on an "AS IS" BASIS, 189 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 190 | See the License for the specific language governing permissions and 191 | limitations under the License. 192 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AspectJ for Metrics 2 | 3 | [![Build Status][Travis badge]][Travis build] [![Coverage Status][Coveralls badge]][Coveralls build] [![Maven Central][Maven Central badge]][Maven Central build] 4 | 5 | [Travis badge]: https://travis-ci.org/astefanutti/metrics-aspectj.svg?branch=master 6 | [Travis build]: https://travis-ci.org/astefanutti/metrics-aspectj 7 | [Coveralls badge]: https://coveralls.io/repos/astefanutti/metrics-aspectj/badge.svg 8 | [Coveralls build]: https://coveralls.io/github/astefanutti/metrics-aspectj 9 | [Maven Central badge]: http://img.shields.io/maven-central/v/io.astefanutti.metrics.aspectj/metrics-aspectj.svg 10 | [Maven Central build]: http://central.maven.org/maven2/io/astefanutti/metrics/aspectj/metrics-aspectj/1.2.0/ 11 | 12 | [AspectJ][] integration for Dropwizard [Metrics][] with optional [Expression Language 3.0 (JSR-341)][] support. 13 | 14 | [AspectJ]: http://eclipse.org/aspectj/ 15 | [Metrics]: http://metrics.codahale.com/ 16 | [Expression Language 3.0 (JSR-341)]: http://jcp.org/en/jsr/detail?id=341 17 | 18 | ## About 19 | 20 | _Metrics AspectJ_ provides support for the [_Metrics_ annotations][Metrics annotations] in Java SE environments using _AspectJ_ to perform AOP instrumentation. It implements the contract specified by these annotations with the following level of functionality: 21 | + Intercept invocations of instance and class methods annotated with [`@ExceptionMetered`][], [`@Metered`][] and [`@Timed`][], 22 | + Create [`Gauge`][] metrics for instance and class methods annotated with [`@Gauge`][], 23 | + Register / retrieve the [`Metric`][] instances in the resolved [`MetricRegistry`][] instance, 24 | + Resolve the [`MetricRegistry`][] instance by looking up into the [`SharedMetricRegistries`][] class or optionally by dynamically evaluating EL expressions. 25 | 26 | _Metrics AspectJ_ is compatible with _Metrics_ version `3.0.0`+ and requires Java 6 or higher. 27 | 28 | [Metrics annotations]: http://metrics.dropwizard.io/3.1.0/apidocs/com/codahale/metrics/annotation/package-summary.html 29 | [`@ExceptionMetered`]: http://metrics.dropwizard.io/3.1.0/apidocs/com/codahale/metrics/annotation/ExceptionMetered.html 30 | [`@Gauge`]: http://metrics.dropwizard.io/3.1.0/apidocs/com/codahale/metrics/annotation/Gauge.html 31 | [`@Metered`]: http://metrics.dropwizard.io/3.1.0/apidocs/com/codahale/metrics/annotation/Metered.html 32 | [`@Timed`]: http://metrics.dropwizard.io/3.1.0/apidocs/com/codahale/metrics/annotation/Timed.html 33 | [`Gauge`]: http://metrics.dropwizard.io/3.1.0/apidocs/com/codahale/metrics/Gauge.html 34 | [`Metric`]: http://metrics.dropwizard.io/3.1.0/apidocs/com/codahale/metrics/Metric.html 35 | [`MetricRegistry`]: http://metrics.dropwizard.io/3.1.0/apidocs/com/codahale/metrics/MetricRegistry.html 36 | [`SharedMetricRegistries`]: http://metrics.dropwizard.io/3.1.0/apidocs/com/codahale/metrics/SharedMetricRegistries.html 37 | 38 | ## Getting Started 39 | 40 | #### Using Maven 41 | 42 | Add the `metrics-aspectj` library as a dependency: 43 | 44 | ```xml 45 | 46 | io.astefanutti.metrics.aspectj 47 | metrics-aspectj 48 | 1.2.0 49 | 50 | ``` 51 | 52 | And configure the `maven-aspectj-plugin` to compile-time weave (CTW) the `metrics-aspectj` aspects into your project: 53 | 54 | ```xml 55 | 56 | org.codehaus.mojo 57 | aspectj-maven-plugin 58 | 59 | 60 | 61 | io.astefanutti.metrics.aspectj 62 | metrics-aspectj 63 | 64 | 65 | 66 | 67 | 68 | 69 | compile 70 | 71 | 72 | 73 | 74 | ``` 75 | 76 | More information can be found in the [Maven AspectJ plugin][] documentation. 77 | 78 | [Maven AspectJ plugin]: http://mojo.codehaus.org/aspectj-maven-plugin/ 79 | 80 | #### Using Ant 81 | 82 | Use the [AjcTask][] (`iajc`) Ant task: 83 | 84 | ```xml 85 | 86 | 89 | ... 90 | 91 | 92 | 93 | ... 94 | 95 | 96 | ``` 97 | 98 | Other options are detailed in the [AspectJ Ant tasks][] documentation. 99 | 100 | [AjcTask]: http://www.eclipse.org/aspectj/doc/next/devguide/antTasks-iajc.html 101 | [AspectJ Ant tasks]: http://www.eclipse.org/aspectj/doc/next/devguide/antTasks.html 102 | 103 | #### Using Gradle 104 | 105 | [gradle example]: https://github.com/paegun/metrics-aspectj-gradle-example 106 | A working [gradle example][] is available, but each integration point is described here. 107 | 108 | ##### build.gradle snippets 109 | 110 | ```groovy 111 | buildscript { 112 | // ensure the gradle-aspectj integration is w/i the build classpath 113 | dependencies { 114 | classpath 'nl.eveoh:gradle-aspectj:1.6' 115 | } 116 | } 117 | 118 | // specify the aspectjVersion, used by gradle-aspectj 119 | project.ext { 120 | aspectjVersion = '1.8.10' 121 | } 122 | 123 | // specify the Dropwizard Metrics version (metricsVer) 124 | // and the aspect-oriented metrics version (metricsAspectVer, this solution) 125 | ext { 126 | metricsVer = '3.2.2' 127 | metricsAspectVer = '1.2.0' 128 | } 129 | 130 | // via the gradle-aspectj integration, run "aspect weaving" 131 | apply plugin: 'aspectj' 132 | 133 | // ensure Dropwizard Metrics as well as the aspect-oriented metrics (astefanutti.metrics.aspectj) 134 | // runtime dependencies of your solution are satisfied. 135 | dependencies { 136 | compile "io.astefanutti.metrics.aspectj:metrics-aspectj:${metricsAspectVer}" 137 | // add a path for the gradle-aspectj "aspect weaving" (AspectJ Compiler compile) 138 | aspectpath "io.astefanutti.metrics.aspectj:metrics-aspectj:${metricsAspectVer}" 139 | 140 | compile "io.dropwizard.metrics:metrics-core:${metricsVer}" 141 | compile "io.dropwizard.metrics:metrics-annotation:${metricsVer}" 142 | } 143 | ``` 144 | 145 | #### Using the AspectJ Compiler 146 | 147 | The AspectJ compiler can be used directly by executing the following command: 148 | 149 | ``` 150 | ajc -aspectpath metrics-aspectj.jar [Options] [file...] 151 | ``` 152 | 153 | More information can be found in the [AspectJ compiler / weaver][] documentation. 154 | 155 | [AspectJ compiler / weaver]: http://www.eclipse.org/aspectj/doc/next/devguide/ajc-ref.html 156 | 157 | #### Required Dependencies 158 | 159 | Besides depending on _Metrics_ (`metrics-core` and `metrics-annotation` modules), _Metrics AspectJ_ requires the AspectJ `aspectjrt` module: 160 | 161 | ```xml 162 | 163 | org.aspectj 164 | aspectjrt 165 | 166 | ``` 167 | 168 | These three modules are transitive dependencies of the `metrics-aspectj` Maven module. 169 | 170 | Alternatively, the `metrics-aspectj-deps` artifact that re-packages the `metrics-annotation` and the `aspectjrt` modules can be used so that the only required dependency is `metrics-core`: 171 | 172 | ```xml 173 | 174 | io.astefanutti.metrics.aspectj 175 | metrics-aspectj-deps 176 | 177 | ``` 178 | 179 | #### Optional Dependencies 180 | 181 | 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: 182 | 183 | ```xml 184 | 185 | org.glassfish 186 | javax.el 187 | 188 | ``` 189 | 190 | [`metrics-aspectj-el`]: https://github.com/astefanutti/metrics-aspectj/tree/master/envs/el 191 | [GlassFish reference implementation]: https://glassfish.java.net/downloads/ri/ 192 | 193 | ## Usage 194 | 195 | #### _Metrics AspectJ_ Activation 196 | 197 | In order to activate _Metrics AspectJ_ for a particular class, it must be annotated with the `@Metrics` annotation: 198 | 199 | ```java 200 | import com.codahale.metrics.annotation.Timed; 201 | 202 | import io.astefanutti.metrics.aspectj.Metrics; 203 | 204 | @Metrics 205 | class TimedMethod { 206 | 207 | @Timed(name = "timerName") 208 | void timedMethod() {} // Timer name => TimedMethod.timerName 209 | } 210 | ``` 211 | 212 | At weaving time, _Metrics AspectJ_ will detect the `@Metrics` annotation, scan all the declared methods of the target class that are annotated with _Metrics_ annotations, then create and register the corresponding `Metric` instances and weave its aspects around these methods. At runtime, these `Metric` instances will eventually get called according to the _Metrics_ annotations specification. 213 | 214 | Note that _Metrics_ annotations won't be inherited if declared on an interface or a parent class method. More details are available in the [Limitations](#limitations) section. 215 | 216 | #### The _Metrics_ Annotations 217 | 218 | _Metrics_ comes with the [`metrics-annotation`][] module that contains a set of annotations and provides a standard way to integrate _Metrics_ with frameworks supporting Aspect Oriented Programming (AOP). These annotations are supported by _Metrics AspectJ_ that implements their contract as documented in their Javadoc. 219 | 220 | [`metrics-annotation`]: https://github.com/dropwizard/metrics/tree/master/metrics-annotation 221 | 222 | For example, a method can be annotated with the `@Timed` annotation so that its execution can be monitored using _Metrics_: 223 | 224 | ```java 225 | import com.codahale.metrics.annotation.Timed; 226 | 227 | import io.astefanutti.metrics.aspectj.Metrics; 228 | 229 | @Metrics 230 | class TimedMethod { 231 | 232 | @Timed(name = "timerName") 233 | void timedMethod() {} // Timer name => TimedMethod.timerName 234 | } 235 | ``` 236 | 237 | In that example, _Metrics AspectJ_ will instrument all the constructors of the `TimedMethod` class by injecting Java bytecode that will automatically create a `Timer` instance with the provided `name` (or retrieve an existing `Timer` with the same `name` already registered in the `MetricRegistry`) right after the instantiation of the `TimedMethod` class and inline the method invocation around with the needed code to time the method execution using that `Timer` instance. 238 | 239 | A `static` method can also be annotated with the `@Timed` annotation so that its execution can be monitored using _Metrics_: 240 | 241 | ```java 242 | import com.codahale.metrics.annotation.Timed; 243 | 244 | import io.astefanutti.metrics.aspectj.Metrics; 245 | 246 | @Metrics 247 | class TimedMethod { 248 | 249 | @Timed(name = "timerName") 250 | static void timedStaticMethod() {} // Timer name => TimedMethod.timerName 251 | } 252 | ``` 253 | 254 | In that example, _Metrics AspectJ_ will instrument the `TimedMethod` class so that, when it's loaded, a `Timer` instance with the provided `name` will be created (or an existing `Timer` with the same `name` already registered in the `MetricRegistry` will be retrieved) and inline the method invocation around with the needed code to time the method execution using that `Timer` instance. 255 | 256 | Optionally, the `Metric` name can be resolved with an EL expression that evaluates to a `String`: 257 | 258 | ```java 259 | import com.codahale.metrics.annotation.Timed; 260 | 261 | import io.astefanutti.metrics.aspectj.Metrics; 262 | 263 | @Metrics 264 | class TimedMethod { 265 | 266 | private long id; 267 | 268 | public long getId() { 269 | return id; 270 | } 271 | 272 | @Timed(name = "timerName ${this.id}") 273 | void timedMethod() {} // Timer name => TimedMethod.timerName 274 | } 275 | ``` 276 | 277 | In that example, _Metrics AspectJ_ will automatically create a `Timer` instance (respectively retrieve an existing `Timer` instance with the same `name` already registered in the `MetricRegistry`) right after the instantiation of the `TimedMethod` class and evaluate the EL expression based on the value of the `id` attribute of that newly created `TimedMethod` instance to name the `Timer` instance (respectively resolve the `Timer` instance registered in the `MetricRegistry`). If the value of the `id` attribute changes over time, the `name` of the `Timer` instance won't be re-evaluated. 278 | 279 | Note that these annotations won't be inherited if they are placed on interface or parent class methods. Indeed, according to the Java language specification, non-type annotations are not inherited. It is discussed in more details in the [Limitations](#limitations) section. 280 | 281 | #### _Metrics_ Registry Resolution 282 | 283 | 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`. 284 | 285 | The `MetricRegistry` can thus be resolved by name relying on the [`SharedMetricRegistries.getOrCreate(String name)`][] method: 286 | 287 | ```java 288 | import com.codahale.metrics.annotation.Metered; 289 | 290 | import io.astefanutti.metrics.aspectj.Metrics; 291 | 292 | @Metrics(registry = "registryName") 293 | class MeteredMethodWithRegistryByName { 294 | 295 | @Metered(name = "meterName") 296 | void meteredMethod() {} // Registry => SharedMetricRegistries.getOrCreate("registryName") 297 | } 298 | ``` 299 | 300 | Or with an EL expression that evaluates to a bean property of type `MetricRegistry`: 301 | 302 | ```java 303 | import com.codahale.metrics.MetricRegistry; 304 | import com.codahale.metrics.annotation.Metered; 305 | 306 | import io.astefanutti.metrics.aspectj.Metrics; 307 | 308 | @Metrics(registry = "${this.registry}") 309 | class MeteredMethodWithRegistryFromProperty { 310 | 311 | final MetricRegistry registry; 312 | 313 | MeteredMethodWithRegistryFromProperty(MetricRegistry registry) { 314 | this.registry = registry; 315 | } 316 | 317 | MetricRegistry getRegistry() { 318 | return registry; 319 | } 320 | 321 | @Metered(name = "meterName") 322 | void meteredMethod() {} // Registry => this.getRegistry() 323 | } 324 | ``` 325 | 326 | 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. 327 | 328 | [`SharedMetricRegistries.getOrCreate(String name)`]: http://metrics.dropwizard.io/3.1.0/apidocs/com/codahale/metrics/SharedMetricRegistries.html#getOrCreate%28java.lang.String%29 329 | 330 | ## Limitations 331 | 332 | The _Metrics_ annotations are not inherited whether these are declared on a parent class or an implemented interface method. The root causes of that limitation, according to the Java language specification, are: 333 | + Non-type annotations are not inherited, 334 | + Annotations on types are only inherited if they have the `@Inherited` meta-annotation, 335 | + Annotations on interfaces are not inherited irrespective to having the `@Inherited` meta-annotation. 336 | 337 | See the [`@Inherited`][] Javadoc and [Annotation types][] from the Java language specification for more details. 338 | 339 | AspectJ follows the Java language specification and has documented to what extent it's impacted in [Annotation inheritance][] and [Annotation inheritance and pointcut matching][]. There would have been ways of working around that though: 340 | + That would have been working around the Java language specification in the first place, 341 | + Plus that would have required to rely on a combination of [Expression-based pointcuts][], [Runtime type matching][] and [Reflective access][] to define conditional pointcut expressions which: 342 | + Would have widen the scope of matching joint points thus introducing side-effects in addition to being inefficient, 343 | + Would have been evaluated at runtime for each candidate join point relying on the Java Reflection API thus impacting the application performance and incidentally voiding the non-intrusive benefit of AOP in a larger sense. 344 | 345 | [`@Inherited`]: http://docs.oracle.com/javase/7/docs/api/java/lang/annotation/Inherited.html 346 | [Annotation types]: http://docs.oracle.com/javase/specs/jls/se7/html/jls-9.html#jls-9.6 347 | [Annotation inheritance]: http://eclipse.org/aspectj/doc/next/adk15notebook/printable.html#annotation-inheritance 348 | [Annotation inheritance and pointcut matching]: http://eclipse.org/aspectj/doc/released/adk15notebook/annotations-pointcuts-and-advice.html#annotation-inheritance-and-pointcut-matching 349 | [Expression-based pointcuts]: http://eclipse.org/aspectj/doc/released/progguide/semantics-pointcuts.html#d0e5549 350 | [Runtime type matching]: http://eclipse.org/aspectj/doc/released/adk15notebook/annotations-pointcuts-and-advice.html#runtime-type-matching-and-context-exposure 351 | [Reflective access]: http://eclipse.org/aspectj/doc/released/progguide/semantics-advice.html#reflective-access-to-the-join-point 352 | 353 | ## Spring AOP vs. AspectJ 354 | 355 | [Spring AOP][] and [AspectJ][] provides Aspect Oriented Programming (AOP) in two very different ways: 356 | + AspectJ provides a full-fledged aspect definition and support both Compile Time Weaving (CTW) and Load Time Weaving (LTW) (with a Java agent) and implements AOP with class instrumentation (byte code manipulation), 357 | + Spring AOP does not support the whole AspectJ aspect definition and does not support Compile Time Weaving, 358 | + Spring AOP implements AOP either using (see [Spring proxying mechanisms][]): 359 | + JDK dynamic proxies, which add little runtime overhead, clutter stack traces and can be incompatible with other Spring functionality like Spring JMX (for dynamic MBean export for example), 360 | + Or [CGLIB][] (byte code manipulation), that has to be added as a runtime dependency: 361 | + It dynamically extends classes thus it is incompatible with `final` classes or methods, 362 | + CGLIB development isn't active, Hibernate has been deprecating it in favor of [Javassist][] (see [Deprecated CGLIB support][]), 363 | + [AJDT (AspectJ Development Tools)][AJDT] provides deep integration between AspectJ and the Eclipse platform which is not possible with Spring AOP due to the runtime / dynamic nature of its AOP implementation. 364 | 365 | Further details can be found in [Choosing which AOP declaration style to use][] from the Spring framework documentation. The [Spring AOP vs AspectJ][] question on Stack Overflow provides some insights as well. 366 | 367 | [Spring AOP]: http://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/html/aop.html 368 | [AspectJ]: http://eclipse.org/aspectj/ 369 | [CGLIB]: http://cglib.sourceforge.net/ 370 | [Javassist]: http://www.csg.ci.i.u-tokyo.ac.jp/~chiba/javassist/ 371 | [AJDT]: http://www.eclipse.org/ajdt/ 372 | [Spring proxying mechanisms]: http://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/html/aop.html#aop-proxying 373 | [Deprecated CGLIB support]: http://relation.to/16658.lace 374 | [Choosing which AOP declaration style to use]: http://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/html/aop.html#aop-choosing 375 | [Spring AOP vs AspectJ]: http://stackoverflow.com/questions/1606559/spring-aop-vs-aspectj 376 | 377 | ## License 378 | 379 | Copyright © 2013-2016, Antonin Stefanutti 380 | 381 | Published under Apache Software License 2.0, see LICENSE 382 | -------------------------------------------------------------------------------- /deps/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-deps 12 | Metrics AspectJ Dependencies 13 | 14 | 15 | 16 | 17 | org.apache.maven.plugins 18 | maven-shade-plugin 19 | 20 | true 21 | 22 | 23 | io.dropwizard.metrics:metrics-core 24 | org.slf4j:slf4j-api 25 | 26 | 27 | 28 | 29 | org.aspectj:aspectjrt 30 | 31 | org/aspectj/lang/** 32 | org/aspectj/runtime/** 33 | 34 | 35 | 36 | 37 | 38 | 39 | package 40 | 41 | shade 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | io.astefanutti.metrics.aspectj 55 | metrics-aspectj 56 | ${project.version} 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /envs/el/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | 6 | io.astefanutti.metrics.aspectj 7 | metrics-aspectj-envs 8 | 1.3-SNAPSHOT 9 | 10 | 11 | metrics-aspectj-el 12 | Metrics AspectJ Java EL Environment 13 | 14 | 15 | 16 | 17 | 18 | 19 | org.glassfish 20 | javax.el 21 | 22 | 23 | 24 | io.astefanutti.metrics.aspectj 25 | metrics-aspectj-se 26 | ${project.version} 27 | test-jar 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /envs/el/src/main/java/io/astefanutti/metrics/aspectj/el/MultipleMetricsMethodWithRegistryFromBeanProperty.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.annotation.ExceptionMetered; 20 | import com.codahale.metrics.annotation.Gauge; 21 | import com.codahale.metrics.annotation.Metered; 22 | import com.codahale.metrics.annotation.Timed; 23 | import io.astefanutti.metrics.aspectj.Metrics; 24 | 25 | @Metrics(registry = "${this.registry}") 26 | public class MultipleMetricsMethodWithRegistryFromBeanProperty { 27 | 28 | private final MetricRegistry registry; 29 | 30 | public MultipleMetricsMethodWithRegistryFromBeanProperty(MetricRegistry registry) { 31 | this.registry = registry; 32 | } 33 | 34 | public MetricRegistry getRegistry() { 35 | return registry; 36 | } 37 | 38 | @ExceptionMetered(name = "exception") 39 | @Gauge(name = "gauge") 40 | @Metered(name = "meter") 41 | @Timed(name = "timer") 42 | public String metricsMethod() { 43 | return "value"; 44 | } 45 | 46 | // Add a static method that does not declare any Metrics annotation to make sure the static aspect doesn't try to evaluate any expression 47 | public static void staticMethod() { 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /envs/el/src/main/java/io/astefanutti/metrics/aspectj/el/TimedMethodWithNameFromElExpression.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.annotation.Timed; 19 | import io.astefanutti.metrics.aspectj.Metrics; 20 | import io.astefanutti.metrics.aspectj.Metrics; 21 | 22 | @Metrics(registry = "timerWithElRegistry") 23 | public class TimedMethodWithNameFromElExpression { 24 | 25 | private final long id; 26 | 27 | public TimedMethodWithNameFromElExpression(long id) { 28 | this.id = id; 29 | } 30 | 31 | public long getId() { 32 | return id; 33 | } 34 | 35 | @Timed(name = "${'timer ' += this.id}") 36 | public void expressionTimedMethod() { 37 | } 38 | 39 | @Timed(name = "timer ${this.id}") 40 | public void compositeExpressionTimedMethod() { 41 | } 42 | 43 | @Timed(name = "${(id -> 'timer ' += id)(this['id'])}") 44 | public void lambdaExpressionTimedMethod() { 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /envs/el/src/main/java/io/astefanutti/metrics/aspectj/el/TimedMethodWithRegistryFromBeanProperty.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.annotation.Timed; 20 | import io.astefanutti.metrics.aspectj.Metrics; 21 | 22 | @Metrics(registry = "${this.registry}") 23 | public class TimedMethodWithRegistryFromBeanProperty { 24 | 25 | private final MetricRegistry registry; 26 | 27 | public TimedMethodWithRegistryFromBeanProperty(MetricRegistry registry) { 28 | this.registry = registry; 29 | } 30 | 31 | public MetricRegistry getRegistry() { 32 | return registry; 33 | } 34 | 35 | @Timed(name = "singleTimedMethod") 36 | public void singleTimedMethod() { 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /envs/el/src/main/java/io/astefanutti/metrics/aspectj/el/TimedMethodWithRegistryFromSharedMetricRegistries.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.annotation.Timed; 19 | import io.astefanutti.metrics.aspectj.Metrics; 20 | 21 | @Metrics(registry = "${'staticRegistry'}") 22 | public class TimedMethodWithRegistryFromSharedMetricRegistries { 23 | 24 | @Timed(name = "singleTimedMethod") 25 | public void singleTimedMethod() { 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /envs/el/src/main/java/io/astefanutti/metrics/aspectj/el/TimedStaticMethodWithNameFromElExpression.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.annotation.Timed; 19 | import io.astefanutti.metrics.aspectj.Metrics; 20 | 21 | @Metrics(registry = "timerStaticWithElRegistry") 22 | public class TimedStaticMethodWithNameFromElExpression { 23 | 24 | public final static long ID = Math.round(Math.random() * Long.MAX_VALUE); 25 | 26 | @Timed(name = "${'timer ' += TimedStaticMethodWithNameFromElExpression.ID}") 27 | public static void expressionStaticTimedMethod() { 28 | } 29 | 30 | @Timed(name = "timer ${TimedStaticMethodWithNameFromElExpression.ID}") 31 | public static void compositeExpressionStaticTimedMethod() { 32 | } 33 | 34 | @Timed(name = "${(id -> 'timer ' += id)(TimedStaticMethodWithNameFromElExpression['ID'])}") 35 | public static void lambdaExpressionStaticTimedMethod() { 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /envs/el/src/main/java/io/astefanutti/metrics/aspectj/el/TimedStaticMethodWithRegistryFromStaticProperty.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.annotation.Timed; 20 | import io.astefanutti.metrics.aspectj.Metrics; 21 | 22 | @Metrics(registry = "${TimedStaticMethodWithRegistryFromStaticProperty.REGISTRY}") 23 | public class TimedStaticMethodWithRegistryFromStaticProperty { 24 | 25 | public static final MetricRegistry REGISTRY = new MetricRegistry(); 26 | 27 | @Timed(name = "singleTimedStaticMethod") 28 | public static void singleTimedStaticMethod() { 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /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/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/el/src/test/java/io/astefanutti/metrics/aspectj/el/TimedMethodWithRegistryFromBeanPropertyTest.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.Timer; 20 | import org.junit.Before; 21 | import org.junit.Test; 22 | 23 | import static org.hamcrest.Matchers.*; 24 | import static org.junit.Assert.assertThat; 25 | 26 | public class TimedMethodWithRegistryFromBeanPropertyTest { 27 | 28 | private final static String TIMER_NAME = MetricRegistry.name(TimedMethodWithRegistryFromBeanProperty.class, "singleTimedMethod"); 29 | 30 | private TimedMethodWithRegistryFromBeanProperty instance; 31 | 32 | @Before 33 | public void createTimedInstance() { 34 | MetricRegistry registry = new MetricRegistry(); 35 | instance = new TimedMethodWithRegistryFromBeanProperty(registry); 36 | } 37 | 38 | @Test 39 | public void timedMethodNotCalledYet() { 40 | MetricRegistry registry = instance.getRegistry(); 41 | assertThat("Timer is not registered correctly", registry.getTimers(), hasKey(TIMER_NAME)); 42 | Timer timer = registry.getTimers().get(TIMER_NAME); 43 | 44 | // Make sure that the timer hasn't been called yet 45 | assertThat("Timer count is incorrect", timer.getCount(), is(equalTo(0L))); 46 | } 47 | 48 | @Test 49 | public void callTimedMethodOnce() { 50 | MetricRegistry registry = instance.getRegistry(); 51 | assertThat("Timer is not registered correctly", registry.getTimers(), hasKey(TIMER_NAME)); 52 | Timer timer = registry.getTimers().get(TIMER_NAME); 53 | 54 | // Call the timed method and assert it's been timed 55 | instance.singleTimedMethod(); 56 | assertThat("Timer count is incorrect", timer.getCount(), is(equalTo(1L))); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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/el/src/test/java/io/astefanutti/metrics/aspectj/el/TimedStaticMethodWithRegistryFromStaticPropertyTest.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.AfterClass; 22 | import org.junit.Test; 23 | 24 | import static org.hamcrest.Matchers.*; 25 | import static org.junit.Assert.assertThat; 26 | 27 | public class TimedStaticMethodWithRegistryFromStaticPropertyTest { 28 | 29 | private final static String TIMER_NAME = MetricRegistry.name(TimedStaticMethodWithRegistryFromStaticProperty.class, "singleTimedStaticMethod"); 30 | 31 | @AfterClass 32 | public static void clearSharedMetricRegistries() { 33 | SharedMetricRegistries.clear(); 34 | } 35 | 36 | @Test 37 | public void callTimedStaticMethodOnce() { 38 | // Call the timed static method and assert it's been timed once 39 | TimedStaticMethodWithRegistryFromStaticProperty.singleTimedStaticMethod(); 40 | 41 | MetricRegistry registry = TimedStaticMethodWithRegistryFromStaticProperty.REGISTRY; 42 | 43 | assertThat("Timer is not registered correctly", registry.getTimers(), hasKey(TIMER_NAME)); 44 | Timer timer = registry.getTimers().get(TIMER_NAME); 45 | 46 | assertThat("Timer count is incorrect", timer.getCount(), is(equalTo(1L))); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /envs/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-envs 12 | Metrics AspectJ Environments 13 | pom 14 | 15 | 16 | el 17 | se 18 | 19 | 20 | 21 | false 22 | 23 | 24 | 25 | 26 | with-deps 27 | 28 | metrics-aspectj-deps 29 | 30 | 31 | 32 | io.dropwizard.metrics 33 | metrics-core 34 | 35 | 36 | io.astefanutti.metrics.aspectj 37 | metrics-aspectj-deps 38 | ${project.version} 39 | 40 | 41 | 42 | 43 | 44 | with-lib 45 | 46 | true 47 | 48 | 49 | metrics-aspectj 50 | 51 | 52 | 53 | io.astefanutti.metrics.aspectj 54 | metrics-aspectj 55 | ${project.version} 56 | 57 | 58 | 59 | 60 | 61 | jacoco 62 | 63 | true 64 | 65 | 66 | 67 | 68 | org.jacoco 69 | jacoco-maven-plugin 70 | 71 | 72 | prepare-agent 73 | 74 | prepare-agent 75 | 76 | 77 | ${project.basedir}/../../impl/target/jacoco.exec 78 | true 79 | 80 | io.astefanutti.metrics.aspectj.* 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | org.codehaus.mojo 95 | aspectj-maven-plugin 96 | 97 | 1.6 98 | 1.6 99 | 1.6 100 | 101 | 102 | io.astefanutti.metrics.aspectj 103 | ${metrics.aspectj.lib} 104 | 105 | 106 | 107 | ${XnoInline} 108 | 109 | 110 | true 111 | 112 | 113 | 114 | 115 | 116 | compile 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | junit 130 | junit 131 | 132 | 133 | 134 | org.hamcrest 135 | hamcrest-library 136 | 137 | 138 | 139 | org.easytesting 140 | fest-reflect 141 | 142 | 143 | 144 | 145 | -------------------------------------------------------------------------------- /envs/se/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | 6 | io.astefanutti.metrics.aspectj 7 | metrics-aspectj-envs 8 | 1.3-SNAPSHOT 9 | 10 | 11 | metrics-aspectj-se 12 | Metrics AspectJ Java SE Environment 13 | 14 | 15 | 16 | 17 | org.apache.maven.plugins 18 | maven-jar-plugin 19 | 20 | 21 | 22 | test-jar 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /envs/se/src/main/java/io/astefanutti/metrics/aspectj/se/ComplexSignatureMethod.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.annotation.ExceptionMetered; 19 | import com.codahale.metrics.annotation.Metered; 20 | import com.codahale.metrics.annotation.Timed; 21 | import io.astefanutti.metrics.aspectj.Metrics; 22 | 23 | import java.util.List; 24 | 25 | @Metrics(registry = "complexSignatureRegistry") 26 | public class ComplexSignatureMethod { 27 | 28 | @Timed 29 | public static T timedStaticMethod(T first, List second, T... third) throws NullPointerException, IllegalStateException { 30 | return first; 31 | } 32 | 33 | @Metered 34 | public static T meteredStaticMethod(T first, List second, T... third) throws NullPointerException, IllegalStateException { 35 | return first; 36 | } 37 | 38 | @ExceptionMetered 39 | public static T exceptionMeteredStaticMethod(T first, List second, T... third) throws NullPointerException, IllegalStateException { 40 | return first; 41 | } 42 | 43 | @Timed 44 | public T timedMethod(T first, List second, T... third) throws NullPointerException, IllegalStateException { 45 | return first; 46 | } 47 | 48 | @Metered 49 | public T meteredMethod(T first, List second, T... third) throws NullPointerException, IllegalStateException { 50 | return first; 51 | } 52 | 53 | @ExceptionMetered 54 | public T exceptionMeteredMethod(T first, List second, T... third) throws NullPointerException, IllegalStateException { 55 | return first; 56 | } 57 | } -------------------------------------------------------------------------------- /envs/se/src/main/java/io/astefanutti/metrics/aspectj/se/GaugeMethodWithRegistryFromString.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.annotation.Gauge; 19 | import io.astefanutti.metrics.aspectj.Metrics; 20 | 21 | @Metrics(registry = "singleGaugeRegistry") 22 | public class GaugeMethodWithRegistryFromString { 23 | 24 | private long singleGauge; 25 | 26 | @Gauge(name = "singleGaugeMethod") 27 | public long getSingleGauge() { 28 | return singleGauge; 29 | } 30 | 31 | public void setSingleGauge(long gauge) { 32 | this.singleGauge = gauge; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /envs/se/src/main/java/io/astefanutti/metrics/aspectj/se/GaugeMethodWithVisibilityModifiers.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.annotation.Gauge; 19 | import io.astefanutti.metrics.aspectj.Metrics; 20 | 21 | @Metrics(registry = "visibilityGaugeRegistry") 22 | public class GaugeMethodWithVisibilityModifiers { 23 | 24 | private long publicGauge; 25 | 26 | private long packagePrivateGauge; 27 | 28 | private long protectedGauge; 29 | 30 | private long privateGauge; 31 | 32 | @Gauge(name = "publicGaugeMethod") 33 | public long getPublicGaugeMethod() { 34 | return publicGauge; 35 | } 36 | 37 | public void setPublicGauge(long gauge) { 38 | publicGauge = gauge; 39 | } 40 | 41 | @Gauge(name = "packagePrivateGaugeMethod") 42 | long getPackagePrivateGauge() { 43 | return packagePrivateGauge; 44 | } 45 | 46 | void setPackagePrivateGauge(long gauge) { 47 | packagePrivateGauge = gauge; 48 | } 49 | 50 | @Gauge(name = "protectedGaugeMethod") 51 | protected long getProtectedGauge() { 52 | return protectedGauge; 53 | } 54 | 55 | protected void setProtectedGauge(long gauge) { 56 | protectedGauge = gauge; 57 | } 58 | 59 | @Gauge(name = "privateGaugeMethod") 60 | private long getPrivateGauge() { 61 | return privateGauge; 62 | } 63 | 64 | private void setPrivateGauge(long gauge) { 65 | privateGauge = gauge; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /envs/se/src/main/java/io/astefanutti/metrics/aspectj/se/GaugeStaticMethodWithRegistryFromString.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.annotation.Gauge; 19 | import io.astefanutti.metrics.aspectj.Metrics; 20 | import io.astefanutti.metrics.aspectj.Metrics; 21 | 22 | @Metrics(registry = "singleGaugeStaticRegistry") 23 | public class GaugeStaticMethodWithRegistryFromString { 24 | 25 | private static long singleStaticGauge; 26 | 27 | @Gauge(name = "singleGaugeStaticMethod") 28 | public static long getSingleGauge() { 29 | return singleStaticGauge; 30 | } 31 | 32 | public static void setSingleGauge(long gauge) { 33 | singleStaticGauge = gauge; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /envs/se/src/main/java/io/astefanutti/metrics/aspectj/se/InheritedTimedMethodWithVisibilityModifiers.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 | public class InheritedTimedMethodWithVisibilityModifiers extends TimedMethodWithVisibilityModifiers { 19 | } 20 | -------------------------------------------------------------------------------- /envs/se/src/main/java/io/astefanutti/metrics/aspectj/se/MeteredMethodWithExceptions.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.annotation.ExceptionMetered; 19 | import io.astefanutti.metrics.aspectj.Metrics; 20 | 21 | @Metrics(registry = "exceptionMeterRegistry") 22 | public class MeteredMethodWithExceptions { 23 | 24 | @ExceptionMetered(name = "illegalArgumentExceptionMeteredMethod", cause = IllegalArgumentException.class) 25 | public void illegalArgumentExceptionMeteredMethod(Runnable runnable) { 26 | runnable.run(); 27 | } 28 | 29 | @ExceptionMetered(name = "exceptionMeteredMethod", cause = Exception.class) 30 | public void exceptionMeteredMethod(Runnable runnable) { 31 | runnable.run(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /envs/se/src/main/java/io/astefanutti/metrics/aspectj/se/MeteredMethodWithRegistryFromString.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.annotation.Metered; 19 | import io.astefanutti.metrics.aspectj.Metrics; 20 | 21 | @Metrics(registry = "singleMeterRegistry") 22 | public class MeteredMethodWithRegistryFromString { 23 | 24 | @Metered(name = "singleMeteredMethod") 25 | public void singleMeteredMethod() { 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /envs/se/src/main/java/io/astefanutti/metrics/aspectj/se/MeteredStaticMethodWithExceptions.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.annotation.ExceptionMetered; 19 | import io.astefanutti.metrics.aspectj.Metrics; 20 | 21 | @Metrics(registry = "exceptionMeterStaticRegistry") 22 | public class MeteredStaticMethodWithExceptions { 23 | 24 | @ExceptionMetered(name = "illegalArgumentExceptionMeteredStaticMethod", cause = IllegalArgumentException.class) 25 | public static void illegalArgumentExceptionMeteredStaticMethod(Runnable runnable) { 26 | runnable.run(); 27 | } 28 | 29 | @ExceptionMetered(name = "exceptionMeteredStaticMethod", cause = Exception.class) 30 | public static void exceptionMeteredStaticMethod(Runnable runnable) { 31 | runnable.run(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /envs/se/src/main/java/io/astefanutti/metrics/aspectj/se/MeteredStaticMethodWithRegistryFromString.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.annotation.Metered; 19 | import io.astefanutti.metrics.aspectj.Metrics; 20 | 21 | @Metrics(registry = "singleMeterStaticRegistry") 22 | public class MeteredStaticMethodWithRegistryFromString { 23 | 24 | @Metered(name = "singleMeteredStaticMethod") 25 | public static void singleMeteredStaticMethod() { 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /envs/se/src/main/java/io/astefanutti/metrics/aspectj/se/MetricMethodWithDefaultNames.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.annotation.ExceptionMetered; 19 | import com.codahale.metrics.annotation.Metered; 20 | import com.codahale.metrics.annotation.Timed; 21 | import io.astefanutti.metrics.aspectj.Metrics; 22 | 23 | @Metrics(registry = "defaultNameRegistry") 24 | public class MetricMethodWithDefaultNames { 25 | 26 | @Timed 27 | public void defaultNameTimedMethod() { 28 | } 29 | 30 | @Metered 31 | public void defaultNameMeteredMethod() { 32 | } 33 | 34 | @ExceptionMetered 35 | public void defaultNameExceptionMeteredMethod() { 36 | } 37 | 38 | @Timed(absolute = true) 39 | public void absoluteDefaultNameTimedMethod() { 40 | } 41 | 42 | @Metered(absolute = true) 43 | public void absoluteDefaultNameMeteredMethod() { 44 | } 45 | 46 | @ExceptionMetered(absolute = true) 47 | public void absoluteDefaultNameExceptionMeteredMethod() { 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /envs/se/src/main/java/io/astefanutti/metrics/aspectj/se/MultipleMetricsMethod.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.annotation.ExceptionMetered; 19 | import com.codahale.metrics.annotation.Gauge; 20 | import com.codahale.metrics.annotation.Metered; 21 | import com.codahale.metrics.annotation.Timed; 22 | import io.astefanutti.metrics.aspectj.Metrics; 23 | 24 | @Metrics(registry = "multipleMetricsRegistry") 25 | public class MultipleMetricsMethod { 26 | 27 | @ExceptionMetered(name = "exception") 28 | @Gauge(name = "gauge") 29 | @Metered(name = "meter") 30 | @Timed(name = "timer") 31 | public String metricsMethod() { 32 | return "value"; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /envs/se/src/main/java/io/astefanutti/metrics/aspectj/se/MultipleMetricsStaticMethod.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.annotation.ExceptionMetered; 19 | import com.codahale.metrics.annotation.Gauge; 20 | import com.codahale.metrics.annotation.Metered; 21 | import com.codahale.metrics.annotation.Timed; 22 | import io.astefanutti.metrics.aspectj.Metrics; 23 | 24 | @Metrics(registry = "multipleMetricsStaticRegistry") 25 | public class MultipleMetricsStaticMethod { 26 | 27 | @ExceptionMetered(name = "exception") 28 | @Gauge(name = "gauge") 29 | @Metered(name = "meter") 30 | @Timed(name = "timer") 31 | public static String metricsMethod() { 32 | return "value"; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /envs/se/src/main/java/io/astefanutti/metrics/aspectj/se/TimedMethodOverloaded.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.annotation.Timed; 19 | import io.astefanutti.metrics.aspectj.Metrics; 20 | 21 | import java.util.List; 22 | 23 | @Metrics(registry = "overloadedTimerRegistry") 24 | public class TimedMethodOverloaded { 25 | 26 | @Timed(name = "overloadedTimedMethodWithNoArguments") 27 | public void overloadedTimedMethod() { 28 | } 29 | 30 | @Timed(name = "overloadedTimedMethodWithStringArgument") 31 | public void overloadedTimedMethod(String string) { 32 | } 33 | 34 | @Timed(name = "overloadedTimedMethodWithListOfStringArgument") 35 | public void overloadedTimedMethod(List strings) { 36 | } 37 | 38 | @Timed(name = "overloadedTimedMethodWithObjectArgument") 39 | public void overloadedTimedMethod(Object object) { 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /envs/se/src/main/java/io/astefanutti/metrics/aspectj/se/TimedMethodWithAbsoluteName.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.annotation.Timed; 19 | import io.astefanutti.metrics.aspectj.Metrics; 20 | 21 | @Metrics(registry = "timerRegistryForAbsoluteNamedTimer") 22 | public class TimedMethodWithAbsoluteName { 23 | 24 | @Timed(name = "absoluteTimedMethod", absolute = true) 25 | public void absoluteTimedMethod() { 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /envs/se/src/main/java/io/astefanutti/metrics/aspectj/se/TimedMethodWithNameFromElExpression.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.annotation.Timed; 19 | import io.astefanutti.metrics.aspectj.Metrics; 20 | 21 | @Metrics(registry = "timerWithElRegistry") 22 | public class TimedMethodWithNameFromElExpression { 23 | 24 | private final String timerName; 25 | 26 | public TimedMethodWithNameFromElExpression(String timerName) { 27 | this.timerName = timerName; 28 | } 29 | 30 | public String getTimerName() { 31 | return timerName; 32 | } 33 | 34 | @Timed(name = "${this.timerName}") 35 | public void expressionTimedMethod() { 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /envs/se/src/main/java/io/astefanutti/metrics/aspectj/se/TimedMethodWithRegistryFromElExpression.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.annotation.Timed; 20 | import io.astefanutti.metrics.aspectj.Metrics; 21 | import io.astefanutti.metrics.aspectj.Metrics; 22 | 23 | @Metrics(registry = "${this.registry}") 24 | public class TimedMethodWithRegistryFromElExpression { 25 | 26 | private final MetricRegistry registry; 27 | 28 | public TimedMethodWithRegistryFromElExpression(MetricRegistry registry) { 29 | this.registry = registry; 30 | } 31 | 32 | public MetricRegistry getRegistry() { 33 | return registry; 34 | } 35 | 36 | @Timed(name = "singleTimedMethod") 37 | public void singleTimedMethod() { 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /envs/se/src/main/java/io/astefanutti/metrics/aspectj/se/TimedMethodWithRegistryFromString.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.annotation.Timed; 19 | import io.astefanutti.metrics.aspectj.Metrics; 20 | 21 | @Metrics(registry = "singleTimerRegistry") 22 | public class TimedMethodWithRegistryFromString { 23 | 24 | @Timed(name = "singleTimedMethod") 25 | public void singleTimedMethod() { 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /envs/se/src/main/java/io/astefanutti/metrics/aspectj/se/TimedMethodWithVisibilityModifiers.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.annotation.Timed; 19 | import io.astefanutti.metrics.aspectj.Metrics; 20 | 21 | @Metrics(registry = "visibilityTimerRegistry") 22 | public class TimedMethodWithVisibilityModifiers { 23 | 24 | @Timed(name = "publicTimedMethod") 25 | public void publicTimedMethod() { 26 | } 27 | 28 | @Timed(name = "packagePrivateTimedMethod") 29 | void packagePrivateTimedMethod() { 30 | } 31 | 32 | @Timed(name = "protectedTimedMethod") 33 | protected void protectedTimedMethod() { 34 | } 35 | 36 | @Timed(name = "privateTimedMethod") 37 | private void privateTimedMethod() { 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /envs/se/src/main/java/io/astefanutti/metrics/aspectj/se/TimedStaticMethodWithVisibilityModifiers.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.annotation.Timed; 19 | import io.astefanutti.metrics.aspectj.Metrics; 20 | import io.astefanutti.metrics.aspectj.Metrics; 21 | 22 | @Metrics(registry = "visibilityTimerStaticRegistry") 23 | public class TimedStaticMethodWithVisibilityModifiers { 24 | 25 | private TimedStaticMethodWithVisibilityModifiers(){} 26 | 27 | @Timed(name = "publicTimedStaticMethod") 28 | public static void publicTimedStaticMethod() { 29 | } 30 | 31 | @Timed(name = "packagePrivateTimedStaticMethod") 32 | static void packagePrivateTimedStaticMethod() { 33 | } 34 | 35 | @Timed(name = "protectedTimedStaticMethod") 36 | protected static void protectedTimedStaticMethod() { 37 | } 38 | 39 | @Timed(name = "privateTimedStaticMethod") 40 | private static void privateTimedStaticMethod() { 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /envs/se/src/test/java/io/astefanutti/metrics/aspectj/se/ComplexSignatureMethodTest.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 org.junit.Before; 21 | import org.junit.Test; 22 | 23 | import static org.junit.Assert.assertEquals; 24 | 25 | public class ComplexSignatureMethodTest { 26 | 27 | private MetricRegistry registry = SharedMetricRegistries.getOrCreate("complexSignatureRegistry"); 28 | private ComplexSignatureMethod instance = new ComplexSignatureMethod(); 29 | 30 | @Before 31 | public void before() { 32 | assertEquals("Not all metrics were registered", 6, registry.getMetrics().size()); 33 | } 34 | 35 | @Test 36 | public void callMeteredStaticMethodOnce() { 37 | ComplexSignatureMethod.meteredStaticMethod(null, null); 38 | } 39 | 40 | @Test 41 | public void callTimedStaticMethodOnce() { 42 | ComplexSignatureMethod.timedStaticMethod(null, null); 43 | } 44 | 45 | @Test 46 | public void callExceptionMeteredStaticMethodOnce() { 47 | ComplexSignatureMethod.exceptionMeteredStaticMethod(null, null); 48 | } 49 | 50 | @Test 51 | public void callTimedMethodOnce() { 52 | instance.timedMethod(null, null); 53 | } 54 | 55 | @Test 56 | public void callMeteredMethodOnce() { 57 | instance.meteredMethod(null, null); 58 | } 59 | 60 | @Test 61 | public void callExceptionMeteredMethodOnce() { 62 | instance.exceptionMeteredMethod(null, null); 63 | } 64 | } -------------------------------------------------------------------------------- /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/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/GaugeStaticMethodWithRegistryFromStringTest.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.Test; 22 | 23 | import static org.hamcrest.Matchers.*; 24 | import static org.junit.Assert.assertThat; 25 | 26 | public class GaugeStaticMethodWithRegistryFromStringTest { 27 | 28 | private final static String REGISTRY_NAME = "singleGaugeStaticRegistry"; 29 | 30 | private final static String GAUGE_NAME = MetricRegistry.name(GaugeStaticMethodWithRegistryFromString.class, "singleGaugeStaticMethod"); 31 | 32 | @Test 33 | public void callStaticGaugeAfterSetterCall() { 34 | long value = Math.round(Math.random() * Long.MAX_VALUE); 35 | // Call the setter static method and assert the gauge is up-to-date 36 | GaugeStaticMethodWithRegistryFromString.setSingleGauge(value); 37 | 38 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME)); 39 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME); 40 | assertThat("Gauge is not registered correctly", registry.getGauges(), hasKey(GAUGE_NAME)); 41 | @SuppressWarnings("unchecked") 42 | Gauge gauge = registry.getGauges().get(GAUGE_NAME); 43 | assertThat("Gauge value is incorrect", gauge.getValue(), is(equalTo(value))); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /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/se/src/test/java/io/astefanutti/metrics/aspectj/se/MeteredMethodWithExceptionsTest.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 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.hamcrest.Matchers.*; 31 | import static org.junit.Assert.*; 32 | 33 | public class MeteredMethodWithExceptionsTest { 34 | 35 | private final static String REGISTRY_NAME = "exceptionMeterRegistry"; 36 | 37 | private final static String[] METER_NAMES = {"illegalArgumentExceptionMeteredMethod", "exceptionMeteredMethod"}; 38 | 39 | private MeteredMethodWithExceptions instance; 40 | 41 | private Set absoluteMetricNames() { 42 | return MetricsUtil.absoluteMetricNames(MeteredMethodWithExceptions.class, METER_NAMES); 43 | } 44 | 45 | private String absoluteMetricName(int index) { 46 | return MetricsUtil.absoluteMetricName(MeteredMethodWithExceptions.class, METER_NAMES[index]); 47 | } 48 | 49 | @Before 50 | public void createExceptionMeteredInstance() { 51 | instance = new MeteredMethodWithExceptions(); 52 | } 53 | 54 | @After 55 | public void clearSharedMetricRegistries() { 56 | SharedMetricRegistries.clear(); 57 | } 58 | 59 | @Test 60 | public void exceptionMeteredMethodsNotCalledYet() { 61 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME)); 62 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME); 63 | assertThat("Meters are not registered correctly", registry.getMeters().keySet(), is(equalTo(absoluteMetricNames()))); 64 | 65 | // Make sure that all the meters haven't been called yet 66 | assertThat("Meter counts are incorrect", registry.getMeters().values(), everyItem(Matchers.hasProperty("count", equalTo(0L)))); 67 | } 68 | 69 | @Test 70 | public void callExceptionMeteredMethodsOnceWithoutThrowing() { 71 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME)); 72 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME); 73 | assertThat("Meters are not registered correctly", registry.getMeters().keySet(), is(equalTo(absoluteMetricNames()))); 74 | 75 | Runnable runnableThatDoesNoThrowExceptions = new Runnable() { 76 | @Override 77 | public void run() { 78 | } 79 | }; 80 | 81 | // Call the metered methods and assert they haven't been marked 82 | instance.illegalArgumentExceptionMeteredMethod(runnableThatDoesNoThrowExceptions); 83 | instance.exceptionMeteredMethod(runnableThatDoesNoThrowExceptions); 84 | assertThat("Meter counts are incorrect", registry.getMeters().values(), everyItem(Matchers.hasProperty("count", equalTo(0L)))); 85 | } 86 | 87 | @Test 88 | public void callExceptionMeteredMethodOnceWithThrowingExpectedException() { 89 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME)); 90 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME); 91 | assertThat("Meters are not registered correctly", registry.getMeters().keySet(), is(equalTo(absoluteMetricNames()))); 92 | 93 | final RuntimeException exception = new IllegalArgumentException("message"); 94 | Runnable runnableThatThrowsIllegalArgumentException = new Runnable() { 95 | @Override 96 | public void run() { 97 | throw exception; 98 | } 99 | }; 100 | 101 | // Call the metered method and assert it's been marked and that the original exception has been rethrown 102 | try { 103 | instance.illegalArgumentExceptionMeteredMethod(runnableThatThrowsIllegalArgumentException); 104 | } catch (RuntimeException cause) { 105 | assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName(0)).getCount(), is(equalTo(1L))); 106 | assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName(1)).getCount(), is(equalTo(0L))); 107 | assertSame("Exception thrown is incorrect", cause, exception); 108 | return; 109 | } 110 | 111 | fail("No exception has been re-thrown!"); 112 | } 113 | 114 | @Test 115 | public void callExceptionMeteredMethodOnceWithThrowingNonExpectedException() { 116 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME)); 117 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME); 118 | assertThat("Meters are not registered correctly", registry.getMeters().keySet(), is(equalTo(absoluteMetricNames()))); 119 | 120 | final RuntimeException exception = new IllegalStateException("message"); 121 | Runnable runnableThatThrowsIllegalStateException = new Runnable() { 122 | @Override 123 | public void run() { 124 | throw exception; 125 | } 126 | }; 127 | 128 | // Call the metered method and assert it hasn't been marked and that the original exception has been rethrown 129 | try { 130 | instance.illegalArgumentExceptionMeteredMethod(runnableThatThrowsIllegalStateException); 131 | } catch (RuntimeException cause) { 132 | assertThat("Meter counts are incorrect", registry.getMeters().values(), everyItem(Matchers.hasProperty("count", equalTo(0L)))); 133 | assertSame("Exception thrown is incorrect", cause, exception); 134 | return; 135 | } 136 | 137 | fail("No exception has been re-thrown!"); 138 | } 139 | 140 | @Test 141 | public void callExceptionMeteredMethodOnceWithThrowingInstanceOfExpectedException() { 142 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME)); 143 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME); 144 | assertThat("Meters are not registered correctly", registry.getMeters().keySet(), is(equalTo(absoluteMetricNames()))); 145 | 146 | final RuntimeException exception = new IllegalStateException("message"); 147 | Runnable runnableThatThrowsIllegalStateException = new Runnable() { 148 | @Override 149 | public void run() { 150 | throw exception; 151 | } 152 | }; 153 | 154 | // Call the metered method and assert it's been marked and that the original exception has been rethrown 155 | try { 156 | instance.exceptionMeteredMethod(runnableThatThrowsIllegalStateException); 157 | } catch (RuntimeException cause) { 158 | assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName(0)).getCount(), is(equalTo(0L))); 159 | assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName(1)).getCount(), is(equalTo(1L))); 160 | assertSame("Exception thrown is incorrect", cause, exception); 161 | return; 162 | } 163 | 164 | fail("No exception has been re-thrown!"); 165 | } 166 | } 167 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /envs/se/src/test/java/io/astefanutti/metrics/aspectj/se/MeteredStaticMethodWithExceptionsTest.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 io.astefanutti.metrics.aspectj.se.util.MetricsUtil; 22 | import org.junit.Test; 23 | 24 | import java.util.Set; 25 | import java.util.concurrent.atomic.AtomicLong; 26 | 27 | import static org.hamcrest.Matchers.*; 28 | import static org.junit.Assert.*; 29 | 30 | public class MeteredStaticMethodWithExceptionsTest { 31 | 32 | private final static String REGISTRY_NAME = "exceptionMeterStaticRegistry"; 33 | 34 | private final static String[] METER_NAMES = {"illegalArgumentExceptionMeteredStaticMethod", "exceptionMeteredStaticMethod"}; 35 | 36 | private final static AtomicLong[] METER_COUNTS = {new AtomicLong(), new AtomicLong()}; 37 | 38 | private static Set absoluteMetricNames() { 39 | return MetricsUtil.absoluteMetricNames(MeteredStaticMethodWithExceptions.class, METER_NAMES); 40 | } 41 | 42 | private static String absoluteMetricName(int index) { 43 | return MetricsUtil.absoluteMetricName(MeteredStaticMethodWithExceptions.class, METER_NAMES[index]); 44 | } 45 | 46 | @Test 47 | public void callExceptionMeteredStaticMethodsOnceWithoutThrowing() { 48 | Runnable runnableThatDoesNoThrowExceptions = new Runnable() { 49 | @Override 50 | public void run() { 51 | } 52 | }; 53 | 54 | // Call the metered methods and assert they haven't been marked 55 | MeteredStaticMethodWithExceptions.illegalArgumentExceptionMeteredStaticMethod(runnableThatDoesNoThrowExceptions); 56 | MeteredStaticMethodWithExceptions.exceptionMeteredStaticMethod(runnableThatDoesNoThrowExceptions); 57 | 58 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME)); 59 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME); 60 | assertThat("Meters are not registered correctly", registry.getMeters().keySet(), is(equalTo(absoluteMetricNames()))); 61 | assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName(0)).getCount(), is(equalTo(METER_COUNTS[0].get()))); 62 | assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName(1)).getCount(), is(equalTo(METER_COUNTS[1].get()))); 63 | } 64 | 65 | @Test 66 | public void callExceptionMeteredStaticMethodOnceWithThrowingExpectedException() { 67 | final RuntimeException exception = new IllegalArgumentException("message"); 68 | Runnable runnableThatThrowsIllegalArgumentException = new Runnable() { 69 | @Override 70 | public void run() { 71 | throw exception; 72 | } 73 | }; 74 | 75 | // Call the metered method and assert it's been marked and that the original exception has been rethrown 76 | try { 77 | MeteredStaticMethodWithExceptions.illegalArgumentExceptionMeteredStaticMethod(runnableThatThrowsIllegalArgumentException); 78 | } catch (RuntimeException cause) { 79 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME)); 80 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME); 81 | assertThat("Meters are not registered correctly", registry.getMeters().keySet(), is(equalTo(absoluteMetricNames()))); 82 | assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName(0)).getCount(), is(equalTo(METER_COUNTS[0].incrementAndGet()))); 83 | assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName(1)).getCount(), is(equalTo(METER_COUNTS[1].get()))); 84 | assertSame("Exception thrown is incorrect", cause, exception); 85 | return; 86 | } 87 | 88 | fail("No exception has been re-thrown!"); 89 | } 90 | 91 | @Test 92 | public void callExceptionMeteredStaticMethodOnceWithThrowingNonExpectedException() { 93 | final RuntimeException exception = new IllegalStateException("message"); 94 | Runnable runnableThatThrowsIllegalStateException = new Runnable() { 95 | @Override 96 | public void run() { 97 | throw exception; 98 | } 99 | }; 100 | 101 | // Call the metered method and assert it hasn't been marked and that the original exception has been rethrown 102 | try { 103 | MeteredStaticMethodWithExceptions.illegalArgumentExceptionMeteredStaticMethod(runnableThatThrowsIllegalStateException); 104 | } catch (RuntimeException cause) { 105 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME)); 106 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME); 107 | assertThat("Meters are not registered correctly", registry.getMeters().keySet(), is(equalTo(absoluteMetricNames()))); 108 | assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName(0)).getCount(), is(equalTo(METER_COUNTS[0].get()))); 109 | assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName(1)).getCount(), is(equalTo(METER_COUNTS[1].get()))); 110 | assertSame("Exception thrown is incorrect", cause, exception); 111 | return; 112 | } 113 | 114 | fail("No exception has been re-thrown!"); 115 | } 116 | 117 | @Test 118 | public void callExceptionMeteredStaticMethodOnceWithThrowingInstanceOfExpectedException() { 119 | final RuntimeException exception = new IllegalStateException("message"); 120 | Runnable runnableThatThrowsIllegalStateException = new Runnable() { 121 | @Override 122 | public void run() { 123 | throw exception; 124 | } 125 | }; 126 | 127 | // Call the metered method and assert it's been marked and that the original exception has been rethrown 128 | try { 129 | MeteredStaticMethodWithExceptions.exceptionMeteredStaticMethod(runnableThatThrowsIllegalStateException); 130 | } catch (RuntimeException cause) { 131 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME)); 132 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME); 133 | assertThat("Meters are not registered correctly", registry.getMeters().keySet(), is(equalTo(absoluteMetricNames()))); 134 | assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName(0)).getCount(), is(equalTo(METER_COUNTS[0].get()))); 135 | assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName(1)).getCount(), is(equalTo(METER_COUNTS[1].incrementAndGet()))); 136 | assertSame("Exception thrown is incorrect", cause, exception); 137 | return; 138 | } 139 | 140 | fail("No exception has been re-thrown!"); 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /envs/se/src/test/java/io/astefanutti/metrics/aspectj/se/MeteredStaticMethodWithRegistryFromStringTest.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.Test; 22 | 23 | import static org.hamcrest.Matchers.*; 24 | import static org.junit.Assert.assertThat; 25 | 26 | public class MeteredStaticMethodWithRegistryFromStringTest { 27 | 28 | private final static String REGISTRY_NAME = "singleMeterStaticRegistry"; 29 | 30 | private final static String METER_NAME = MetricRegistry.name(MeteredStaticMethodWithRegistryFromString.class, "singleMeteredStaticMethod"); 31 | 32 | @Test 33 | public void callMeteredStaticMethodOnce() { 34 | // Call the metered method and assert it's been marked 35 | MeteredStaticMethodWithRegistryFromString.singleMeteredStaticMethod(); 36 | 37 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME)); 38 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME); 39 | assertThat("Timer is not registered correctly", registry.getMeters(), hasKey(METER_NAME)); 40 | Meter meter = registry.getMeters().get(METER_NAME); 41 | assertThat("Meter count is incorrect", meter.getCount(), is(equalTo(1L))); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /envs/se/src/test/java/io/astefanutti/metrics/aspectj/se/MetricMethodWithDefaultNamesTest.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 | 25 | import java.util.Arrays; 26 | import java.util.Set; 27 | 28 | import static org.hamcrest.Matchers.*; 29 | import static org.junit.Assert.assertThat; 30 | 31 | public class MetricMethodWithDefaultNamesTest { 32 | 33 | private final static String REGISTRY_NAME = "defaultNameRegistry"; 34 | 35 | private final static String[] METRIC_NAMES = {"defaultNameTimedMethod", "defaultNameMeteredMethod", "defaultNameExceptionMeteredMethod.exceptions"}; 36 | 37 | private final static String[] ABSOLUTE_METRIC_NAMES = {"absoluteDefaultNameTimedMethod", "absoluteDefaultNameMeteredMethod", "absoluteDefaultNameExceptionMeteredMethod.exceptions"}; 38 | 39 | private MetricMethodWithDefaultNames instance; 40 | 41 | private Set metricNames() { 42 | Set names = MetricsUtil.absoluteMetricNames(MetricMethodWithDefaultNames.class, METRIC_NAMES); 43 | names.addAll(Arrays.asList(ABSOLUTE_METRIC_NAMES)); 44 | return names; 45 | } 46 | 47 | @Before 48 | public void createMetricInstance() { 49 | instance = new MetricMethodWithDefaultNames(); 50 | } 51 | 52 | @After 53 | public void clearSharedMetricRegistries() { 54 | SharedMetricRegistries.clear(); 55 | } 56 | 57 | @Test 58 | public void metricMethodsWithDefaultNamingConvention() { 59 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME)); 60 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME); 61 | assertThat("Metrics are not registered correctly", registry.getMetrics().keySet(), is(equalTo(metricNames()))); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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/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/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/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.se; 17 | 18 | import com.codahale.metrics.annotation.Timed; 19 | import org.junit.Rule; 20 | import org.junit.Test; 21 | import org.junit.rules.ExpectedException; 22 | 23 | public class TimedMethodWithNameFromElExpressionTest { 24 | 25 | @Rule 26 | public ExpectedException exception = ExpectedException.none(); 27 | 28 | @Test 29 | public void throwExceptionOnInitialization() throws NoSuchMethodException { 30 | exception.expect(UnsupportedOperationException.class); 31 | String name = TimedMethodWithNameFromElExpression.class.getMethod("expressionTimedMethod").getAnnotation(Timed.class).name(); 32 | exception.expectMessage("Unsupported EL expression [" + name + "] evaluation as no EL implementation is available"); 33 | 34 | TimedMethodWithNameFromElExpression instance = new TimedMethodWithNameFromElExpression("timer"); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /envs/se/src/test/java/io/astefanutti/metrics/aspectj/se/TimedMethodWithRegistryFromElExpressionTest.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 io.astefanutti.metrics.aspectj.Metrics; 20 | import org.junit.Rule; 21 | import org.junit.Test; 22 | import org.junit.rules.ExpectedException; 23 | 24 | public class TimedMethodWithRegistryFromElExpressionTest { 25 | 26 | @Rule 27 | public ExpectedException exception = ExpectedException.none(); 28 | 29 | @Test 30 | public void throwExceptionOnInitialization() { 31 | exception.expect(UnsupportedOperationException.class); 32 | String name = TimedMethodWithRegistryFromElExpression.class.getAnnotation(Metrics.class).registry(); 33 | exception.expectMessage("Unsupported EL expression [" + name + "] evaluation as no EL implementation is available"); 34 | 35 | MetricRegistry registry = new MetricRegistry(); 36 | TimedMethodWithRegistryFromElExpression instance = new TimedMethodWithRegistryFromElExpression(registry); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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/TimedStaticMethodWithVisibilityModifiersTest.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.Test; 25 | 26 | import java.util.Set; 27 | 28 | import static org.fest.reflect.core.Reflection.staticMethod; 29 | import static org.hamcrest.Matchers.*; 30 | import static org.junit.Assert.assertThat; 31 | 32 | public class TimedStaticMethodWithVisibilityModifiersTest { 33 | 34 | private final static String REGISTRY_NAME = "visibilityTimerStaticRegistry"; 35 | 36 | private final static String[] TIMER_NAMES = {"publicTimedStaticMethod", "packagePrivateTimedStaticMethod", "protectedTimedStaticMethod", "privateTimedStaticMethod"}; 37 | 38 | private static Set absoluteMetricNames() { 39 | return MetricsUtil.absoluteMetricNames(TimedStaticMethodWithVisibilityModifiers.class, TIMER_NAMES); 40 | } 41 | 42 | @Test 43 | public void callTimedStaticMethodsOnce() { 44 | // Call the timed methods and assert they've all been timed once 45 | TimedStaticMethodWithVisibilityModifiers.publicTimedStaticMethod(); 46 | TimedStaticMethodWithVisibilityModifiers.protectedTimedStaticMethod(); 47 | TimedStaticMethodWithVisibilityModifiers.packagePrivateTimedStaticMethod(); 48 | staticMethod("privateTimedStaticMethod").in(TimedStaticMethodWithVisibilityModifiers.class).invoke(); 49 | 50 | assertThat("Shared metric registry is not created", SharedMetricRegistries.names(), hasItem(REGISTRY_NAME)); 51 | MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME); 52 | 53 | assertThat("Timers are not registered correctly", registry.getTimers().keySet(), is(equalTo(absoluteMetricNames()))); 54 | 55 | assertThat("Timer counts are incorrect", registry.getTimers().values(), everyItem(Matchers.hasProperty("count", equalTo(1L)))); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /envs/se/src/test/java/io/astefanutti/metrics/aspectj/se/util/MetricsUtil.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.util; 17 | 18 | import com.codahale.metrics.MetricRegistry; 19 | 20 | import java.util.HashSet; 21 | import java.util.Set; 22 | 23 | public final class MetricsUtil { 24 | 25 | private MetricsUtil() { 26 | } 27 | 28 | public static Set absoluteMetricNames(Class clazz, String... names) { 29 | Set set = new HashSet(names.length); 30 | for (String name : names) 31 | set.add(absoluteMetricName(clazz, name)); 32 | 33 | return set; 34 | } 35 | 36 | public static String absoluteMetricName(Class clazz, String name) { 37 | return MetricRegistry.name(clazz, name); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 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 { 71 | 72 | final Method method; 73 | final Object object; 74 | 75 | protected ForwardingGauge(Method method, Object object) { 76 | this.method = method; 77 | this.object = object; 78 | method.setAccessible(true); 79 | } 80 | 81 | @Override 82 | public Object getValue() { 83 | // TODO: use more efficient technique than reflection 84 | return invokeMethod(method, object); 85 | } 86 | } 87 | 88 | private static Object invokeMethod(Method method, Object object) { 89 | try { 90 | return method.invoke(object); 91 | } catch (IllegalAccessException cause) { 92 | throw new IllegalStateException("Error while calling method [" + method + "]", cause); 93 | } catch (InvocationTargetException cause) { 94 | throw new IllegalStateException("Error while calling method [" + method + "]", cause); 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /impl/src/main/aspect/io/astefanutti/metrics/aspectj/ExceptionMeteredAspect.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.Meter; 19 | import com.codahale.metrics.annotation.ExceptionMetered; 20 | import org.aspectj.lang.reflect.MethodSignature; 21 | 22 | final aspect ExceptionMeteredAspect { 23 | 24 | pointcut exceptionMetered(Profiled object) : execution(@ExceptionMetered !static * (@Metrics Profiled+).*(..)) && this(object); 25 | 26 | after(Profiled object) throwing (Throwable throwable) : exceptionMetered(object) { 27 | String methodSignature = ((MethodSignature) thisJoinPointStaticPart.getSignature()).getMethod().toString(); 28 | AnnotatedMetric metric = object.meters.get(methodSignature); 29 | if (metric.getAnnotation(ExceptionMetered.class).cause().isInstance(throwable)) 30 | metric.getMetric().mark(); 31 | } 32 | } -------------------------------------------------------------------------------- /impl/src/main/aspect/io/astefanutti/metrics/aspectj/ExceptionMeteredStaticAspect.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.Meter; 19 | import com.codahale.metrics.annotation.ExceptionMetered; 20 | import org.aspectj.lang.reflect.MethodSignature; 21 | 22 | final aspect ExceptionMeteredStaticAspect { 23 | 24 | pointcut exceptionMetered() : execution(@ExceptionMetered static * (@Metrics *).*(..)); 25 | 26 | after() throwing (Throwable throwable) : exceptionMetered() { 27 | String methodSignature = ((MethodSignature) thisJoinPointStaticPart.getSignature()).getMethod().toString(); 28 | AnnotatedMetric metric = MetricStaticAspect.METERS.get(methodSignature); 29 | if (metric.getAnnotation(ExceptionMetered.class).cause().isInstance(throwable)) 30 | metric.getMetric().mark(); 31 | } 32 | } -------------------------------------------------------------------------------- /impl/src/main/aspect/io/astefanutti/metrics/aspectj/MeteredAspect.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.Meter; 19 | import com.codahale.metrics.annotation.Metered; 20 | import org.aspectj.lang.reflect.MethodSignature; 21 | 22 | final aspect MeteredAspect { 23 | 24 | pointcut metered(Profiled object) : execution(@Metered !static * (@Metrics Profiled+).*(..)) && this(object); 25 | 26 | before(Profiled object) : metered(object) { 27 | String methodSignature = ((MethodSignature) thisJoinPointStaticPart.getSignature()).getMethod().toString(); 28 | Meter meter = object.meters.get(methodSignature).getMetric(); 29 | meter.mark(); 30 | } 31 | } -------------------------------------------------------------------------------- /impl/src/main/aspect/io/astefanutti/metrics/aspectj/MeteredStaticAspect.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.Meter; 19 | import com.codahale.metrics.annotation.Metered; 20 | import org.aspectj.lang.reflect.MethodSignature; 21 | 22 | final aspect MeteredStaticAspect { 23 | 24 | pointcut metered() : execution(@Metered static * (@Metrics *).*(..)); 25 | 26 | before() : metered() { 27 | String methodSignature = ((MethodSignature) thisJoinPointStaticPart.getSignature()).getMethod().toString(); 28 | Meter meter = MetricStaticAspect.METERS.get(methodSignature).getMetric(); 29 | meter.mark(); 30 | } 31 | } -------------------------------------------------------------------------------- /impl/src/main/aspect/io/astefanutti/metrics/aspectj/MetricAspect.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.Meter; 19 | import com.codahale.metrics.MetricRegistry; 20 | import com.codahale.metrics.Timer; 21 | import com.codahale.metrics.annotation.ExceptionMetered; 22 | import com.codahale.metrics.annotation.Gauge; 23 | import com.codahale.metrics.annotation.Metered; 24 | import com.codahale.metrics.annotation.Timed; 25 | 26 | import java.lang.reflect.Method; 27 | import java.lang.reflect.Modifier; 28 | import java.util.Map; 29 | import java.util.concurrent.ConcurrentHashMap; 30 | 31 | final aspect MetricAspect extends AbstractMetricAspect { 32 | 33 | declare precedence: MetricStaticAspect, MetricAspect, *; 34 | 35 | declare parents : (@Metrics *) implements Profiled; 36 | 37 | final Map> Profiled.gauges = new ConcurrentHashMap>(); 38 | 39 | final Map> Profiled.meters = new ConcurrentHashMap>(); 40 | 41 | final Map> Profiled.timers = new ConcurrentHashMap>(); 42 | 43 | pointcut profiled(Profiled object) : execution((@Metrics Profiled+).new(..)) && this(object); 44 | 45 | after(final Profiled object) : profiled(object) { 46 | final MetricStrategy strategy = MetricStrategyFactory.newInstance(object); 47 | 48 | Class clazz = object.getClass(); 49 | do { 50 | // TODO: discover annotations declared on implemented interfaces 51 | for (final Method method : clazz.getDeclaredMethods()) { 52 | // Skip advising static methods 53 | if (Modifier.isStatic(method.getModifiers())) 54 | continue; 55 | 56 | final Class type = clazz; 57 | 58 | AnnotatedMetric exception = metricAnnotation(method, ExceptionMetered.class, new MetricFactory() { 59 | @Override 60 | public Meter metric(String name, boolean absolute) { 61 | String finalName = name.isEmpty() ? method.getName() + "." + ExceptionMetered.DEFAULT_NAME_SUFFIX : strategy.resolveMetricName(name); 62 | MetricRegistry registry = strategy.resolveMetricRegistry(type.getAnnotation(Metrics.class).registry()); 63 | return registry.meter(absolute ? finalName : MetricRegistry.name(type, finalName)); 64 | } 65 | }); 66 | if (exception.isPresent()) 67 | object.meters.put(method.toString(), exception); 68 | 69 | AnnotatedMetric gauge = metricAnnotation(method, Gauge.class, new MetricFactory() { 70 | @Override 71 | public com.codahale.metrics.Gauge metric(String name, boolean absolute) { 72 | String finalName = name.isEmpty() ? method.getName() : strategy.resolveMetricName(name); 73 | MetricRegistry registry = strategy.resolveMetricRegistry(type.getAnnotation(Metrics.class).registry()); 74 | return registry.register(absolute ? finalName : MetricRegistry.name(type, finalName), new ForwardingGauge(method, object)); 75 | } 76 | }); 77 | if (gauge.isPresent()) 78 | object.gauges.put(method.toString(), gauge); 79 | 80 | AnnotatedMetric meter = metricAnnotation(method, Metered.class, new MetricFactory() { 81 | @Override 82 | public Meter metric(String name, boolean absolute) { 83 | String finalName = name.isEmpty() ? method.getName() : strategy.resolveMetricName(name); 84 | MetricRegistry registry = strategy.resolveMetricRegistry(type.getAnnotation(Metrics.class).registry()); 85 | return registry.meter(absolute ? finalName : MetricRegistry.name(type, finalName)); 86 | } 87 | }); 88 | if (meter.isPresent()) 89 | object.meters.put(method.toString(), meter); 90 | 91 | AnnotatedMetric timer = metricAnnotation(method, Timed.class, new MetricFactory() { 92 | @Override 93 | public Timer metric(String name, boolean absolute) { 94 | String finalName = name.isEmpty() ? method.getName() : strategy.resolveMetricName(name); 95 | MetricRegistry registry = strategy.resolveMetricRegistry(type.getAnnotation(Metrics.class).registry()); 96 | return registry.timer(absolute ? finalName : MetricRegistry.name(type, finalName)); 97 | } 98 | }); 99 | if (timer.isPresent()) 100 | object.timers.put(method.toString(), timer); 101 | } 102 | clazz = clazz.getSuperclass(); 103 | } while (!Object.class.equals(clazz)); 104 | } 105 | } -------------------------------------------------------------------------------- /impl/src/main/aspect/io/astefanutti/metrics/aspectj/MetricStaticAspect.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.Meter; 19 | import com.codahale.metrics.MetricRegistry; 20 | import com.codahale.metrics.Timer; 21 | import com.codahale.metrics.annotation.ExceptionMetered; 22 | import com.codahale.metrics.annotation.Gauge; 23 | import com.codahale.metrics.annotation.Metered; 24 | import com.codahale.metrics.annotation.Timed; 25 | 26 | import java.lang.reflect.Method; 27 | import java.lang.reflect.Modifier; 28 | import java.util.Map; 29 | import java.util.concurrent.ConcurrentHashMap; 30 | 31 | final aspect MetricStaticAspect extends AbstractMetricAspect { 32 | 33 | static final Map> GAUGES = new ConcurrentHashMap>(); 34 | 35 | static final Map> METERS = new ConcurrentHashMap>(); 36 | 37 | static final Map> TIMERS = new ConcurrentHashMap>(); 38 | 39 | pointcut profiled() : staticinitialization(@Metrics *); 40 | 41 | after() : profiled() { 42 | final Class clazz = thisJoinPointStaticPart.getSignature().getDeclaringType(); 43 | final MetricStrategy strategy = MetricStrategyFactory.newInstance(clazz); 44 | 45 | for (final Method method : clazz.getDeclaredMethods()) { 46 | if (Modifier.isStatic(method.getModifiers()) && !method.isSynthetic()) { 47 | 48 | AnnotatedMetric exception = metricAnnotation(method, ExceptionMetered.class, new MetricFactory() { 49 | @Override 50 | public Meter metric(String name, boolean absolute) { 51 | String finalName = name.isEmpty() ? method.getName() + "." + ExceptionMetered.DEFAULT_NAME_SUFFIX : strategy.resolveMetricName(name); 52 | MetricRegistry registry = strategy.resolveMetricRegistry(clazz.getAnnotation(Metrics.class).registry()); 53 | return registry.meter(absolute ? finalName : MetricRegistry.name(clazz, finalName)); 54 | } 55 | }); 56 | if (exception.isPresent()) 57 | METERS.put(method.toString(), exception); 58 | 59 | AnnotatedMetric gauge = metricAnnotation(method, Gauge.class, new MetricFactory() { 60 | @Override 61 | public com.codahale.metrics.Gauge metric(String name, boolean absolute) { 62 | String finalName = name.isEmpty() ? method.getName() : strategy.resolveMetricName(name); 63 | MetricRegistry registry = strategy.resolveMetricRegistry(clazz.getAnnotation(Metrics.class).registry()); 64 | return registry.register(absolute ? finalName : MetricRegistry.name(clazz, finalName), new ForwardingGauge(method, clazz)); 65 | } 66 | }); 67 | if (gauge.isPresent()) 68 | GAUGES.put(method.toString(), gauge); 69 | 70 | AnnotatedMetric meter = metricAnnotation(method, Metered.class, new MetricFactory() { 71 | @Override 72 | public Meter metric(String name, boolean absolute) { 73 | String finalName = name.isEmpty() ? method.getName() : strategy.resolveMetricName(name); 74 | MetricRegistry registry = strategy.resolveMetricRegistry(clazz.getAnnotation(Metrics.class).registry()); 75 | return registry.meter(absolute ? finalName : MetricRegistry.name(clazz, finalName)); 76 | } 77 | }); 78 | if (meter.isPresent()) 79 | METERS.put(method.toString(), meter); 80 | 81 | AnnotatedMetric timer = metricAnnotation(method, Timed.class, new MetricFactory() { 82 | @Override 83 | public Timer metric(String name, boolean absolute) { 84 | String finalName = name.isEmpty() ? method.getName() : strategy.resolveMetricName(name); 85 | MetricRegistry registry = strategy.resolveMetricRegistry(clazz.getAnnotation(Metrics.class).registry()); 86 | return registry.timer(absolute ? finalName : MetricRegistry.name(clazz, finalName)); 87 | } 88 | }); 89 | if (timer.isPresent()) 90 | TIMERS.put(method.toString(), timer); 91 | } 92 | } 93 | } 94 | } -------------------------------------------------------------------------------- /impl/src/main/aspect/io/astefanutti/metrics/aspectj/TimedAspect.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.Timer; 19 | import com.codahale.metrics.annotation.Timed; 20 | import org.aspectj.lang.reflect.MethodSignature; 21 | 22 | final aspect TimedAspect { 23 | 24 | pointcut timed(Profiled object) : execution(@Timed !static * (@Metrics Profiled+).*(..)) && this(object); 25 | 26 | Object around(Profiled object) : timed(object) { 27 | String methodSignature = ((MethodSignature) thisJoinPointStaticPart.getSignature()).getMethod().toString(); 28 | Timer timer = object.timers.get(methodSignature).getMetric(); 29 | Timer.Context context = timer.time(); 30 | try { 31 | return proceed(object); 32 | } finally { 33 | context.stop(); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /impl/src/main/aspect/io/astefanutti/metrics/aspectj/TimedStaticAspect.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.Timer; 19 | import com.codahale.metrics.annotation.Timed; 20 | import org.aspectj.lang.reflect.MethodSignature; 21 | 22 | final aspect TimedStaticAspect { 23 | 24 | pointcut timed() : execution(@Timed static * (@Metrics *).*(..)); 25 | 26 | Object around() : timed() { 27 | String methodSignature = ((MethodSignature) thisJoinPointStaticPart.getSignature()).getMethod().toString(); 28 | Timer timer = MetricStaticAspect.TIMERS.get(methodSignature).getMetric(); 29 | Timer.Context context = timer.time(); 30 | try { 31 | return proceed(); 32 | } finally { 33 | context.stop(); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /impl/src/main/java/io/astefanutti/metrics/aspectj/AnnotatedMetric.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.Metric; 19 | 20 | import java.lang.annotation.Annotation; 21 | 22 | public interface AnnotatedMetric { 23 | 24 | boolean isPresent(); 25 | 26 | T getMetric(); 27 | 28 | A getAnnotation(Class clazz); 29 | 30 | final class IsPresent implements AnnotatedMetric { 31 | 32 | private final T metric; 33 | 34 | private final Annotation annotation; 35 | 36 | IsPresent(T metric, Annotation annotation) { 37 | this.metric = metric; 38 | this.annotation = annotation; 39 | } 40 | 41 | @Override 42 | public boolean isPresent() { 43 | return true; 44 | } 45 | 46 | @Override 47 | public T getMetric() { 48 | return metric; 49 | } 50 | 51 | @Override 52 | @SuppressWarnings("unchecked") 53 | public A getAnnotation(Class clazz) { 54 | return (A) annotation; 55 | } 56 | } 57 | 58 | final class IsNotPresent implements AnnotatedMetric { 59 | 60 | @Override 61 | public boolean isPresent() { 62 | return false; 63 | } 64 | 65 | @Override 66 | public T getMetric() { 67 | throw new UnsupportedOperationException(); 68 | } 69 | 70 | @Override 71 | @SuppressWarnings("unchecked") 72 | public A getAnnotation(Class clazz) { 73 | throw new UnsupportedOperationException(); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /impl/src/main/java/io/astefanutti/metrics/aspectj/JavaSeMetricStrategy.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 java.util.regex.Matcher; 22 | 23 | /* package-private */ final class JavaSeMetricStrategy implements MetricStrategy { 24 | 25 | @Override 26 | public MetricRegistry resolveMetricRegistry(String registry) { 27 | Matcher matcher = EL_PATTERN.matcher(registry); 28 | if (matcher.find()) 29 | throw new UnsupportedOperationException("Unsupported EL expression [" + registry + "] evaluation as no EL implementation is available"); 30 | else 31 | return SharedMetricRegistries.getOrCreate(registry); 32 | } 33 | 34 | @Override 35 | public String resolveMetricName(String name) { 36 | Matcher matcher = EL_PATTERN.matcher(name); 37 | if (matcher.matches()) 38 | throw new UnsupportedOperationException("Unsupported EL expression [" + name + "] evaluation as no EL implementation is available"); 39 | else 40 | return name; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /impl/src/main/java/io/astefanutti/metrics/aspectj/MetricStrategy.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 | 20 | import java.util.regex.Pattern; 21 | 22 | /* package-private */ interface MetricStrategy { 23 | 24 | Pattern EL_PATTERN = Pattern.compile("[#|$]\\{(.*)\\}"); 25 | 26 | MetricRegistry resolveMetricRegistry(String registry); 27 | 28 | String resolveMetricName(String name); 29 | } 30 | -------------------------------------------------------------------------------- /impl/src/main/java/io/astefanutti/metrics/aspectj/MetricStrategyFactory.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 | /* package-private */ final class MetricStrategyFactory { 19 | 20 | static MetricStrategy newInstance(Object object) { 21 | if (isElAvailable(object.getClass())) 22 | return new JavaxElMetricStrategy(object); 23 | else 24 | // Expression Language 3.0 is not available, fall back to SE implementation 25 | return new JavaSeMetricStrategy(); 26 | } 27 | 28 | static MetricStrategy newInstance(Class clazz) { 29 | if (isElAvailable(clazz)) 30 | return new JavaxElMetricStrategy(clazz); 31 | else 32 | // Expression Language 3.0 is not available, fall back to SE implementation 33 | return new JavaSeMetricStrategy(); 34 | } 35 | 36 | private static ClassLoader getClassLoader(Class clazz) { 37 | if (Thread.currentThread().getContextClassLoader() != null) 38 | return Thread.currentThread().getContextClassLoader(); 39 | else 40 | return clazz.getClassLoader(); 41 | } 42 | 43 | private static boolean isElAvailable(Class clazz) { 44 | try { 45 | getClassLoader(clazz).loadClass("javax.el.ELProcessor"); 46 | return true; 47 | } catch (ClassNotFoundException cause) { 48 | return false; 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /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 | } -------------------------------------------------------------------------------- /impl/src/main/java/io/astefanutti/metrics/aspectj/Profiled.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 | /* package-private */ interface Profiled { 19 | } -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | 6 | org.sonatype.oss 7 | oss-parent 8 | 9 9 | 10 | 11 | io.astefanutti.metrics.aspectj 12 | metrics-aspectj-parent 13 | 1.3-SNAPSHOT 14 | pom 15 | 16 | Metrics AspectJ Parent 17 | 2013 18 | AspectJ for Metrics 19 | https://github.com/astefanutti/metrics-aspectj 20 | 21 | 22 | 23 | Antonin Stefanutti 24 | antonin.stefanutti@gmail.com 25 | +1 26 | 27 | 28 | 29 | 30 | 31 | Apache License 2.0 32 | http://www.apache.org/licenses/LICENSE-2.0.html 33 | 34 | 35 | 36 | 37 | github 38 | https://github.com/astefanutti/metrics-aspectj/issues 39 | 40 | 41 | 42 | scm:git:git@github.com:astefanutti/metrics-aspectj.git 43 | scm:git:git@github.com:astefanutti/metrics-aspectj.git 44 | scm:git:git@github.com:astefanutti/metrics-aspectj.git 45 | HEAD 46 | 47 | 48 | 49 | UTF-8 50 | 3.2.2 51 | 1.8.10 52 | 53 | 54 | 55 | impl 56 | deps 57 | envs 58 | 59 | 60 | 61 | clean package 62 | 63 | 64 | 65 | 66 | com.mycila 67 | license-maven-plugin 68 | 3.0 69 | 70 | 71 | 72 | org.codehaus.mojo 73 | aspectj-maven-plugin 74 | 1.10 75 | 76 | 77 | org.aspectj 78 | aspectjtools 79 | ${aspectj.version} 80 | 81 | 82 | 83 | 84 | 85 | org.apache.maven.plugins 86 | maven-compiler-plugin 87 | 3.6.1 88 | 89 | 90 | 91 | org.apache.maven.plugins 92 | maven-jar-plugin 93 | 3.0.2 94 | 95 | 96 | 97 | org.apache.maven.plugins 98 | maven-shade-plugin 99 | 3.0.0 100 | 101 | 102 | 103 | org.apache.maven.plugins 104 | maven-surefire-plugin 105 | 2.19.1 106 | 107 | 108 | 109 | org.jacoco 110 | jacoco-maven-plugin 111 | 0.7.9 112 | 113 | 114 | 115 | org.eluder.coveralls 116 | coveralls-maven-plugin 117 | 4.3.0 118 | 119 | 120 | ${project.basedir}/impl/target/site/jacoco/jacoco.xml 121 | 122 | ${project.basedir}/impl/target/coveralls.json 123 | 124 | ${project.basedir}/impl/src/main/java 125 | ${project.basedir}/impl/src/main/aspect 126 | 127 | 128 | 129 | 130 | 131 | org.apache.maven.plugins 132 | maven-gpg-plugin 133 | 1.6 134 | 135 | 136 | 137 | org.apache.maven.plugins 138 | maven-release-plugin 139 | 2.5.3 140 | 141 | true 142 | forked-path 143 | false 144 | ${arguments} -Psonatype-oss-release 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | com.mycila 153 | license-maven-plugin 154 | 155 |

com/mycila/maven/plugin/license/templates/APACHE-2.txt
156 | 157 | Antonin Stefanutti 158 | antonin.stefanutti@gmail.com 159 | ${project.inceptionYear} 160 | 161 | 162 | src/**/*.java 163 | src/**/*.aj 164 | 165 | 166 | 167 | 168 | process-sources 169 | 170 | format 171 | 172 | 173 | 174 | 175 | 176 | 177 | org.apache.maven.plugins 178 | maven-compiler-plugin 179 | 180 | 1.6 181 | 1.6 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | io.dropwizard.metrics 194 | metrics-core 195 | ${metrics.version} 196 | 197 | 198 | 199 | io.dropwizard.metrics 200 | metrics-annotation 201 | ${metrics.version} 202 | 203 | 204 | org.slf4j 205 | slf4j-api 206 | 207 | 208 | 209 | 210 | 211 | org.aspectj 212 | aspectjrt 213 | ${aspectj.version} 214 | 215 | 216 | 217 | 218 | 219 | javax.el 220 | javax.el-api 221 | 3.0.0 222 | provided 223 | 224 | 225 | 226 | 227 | 228 | org.glassfish 229 | javax.el 230 | 3.0.0 231 | test 232 | 233 | 234 | 235 | junit 236 | junit 237 | 4.13.1 238 | test 239 | 240 | 241 | 242 | org.hamcrest 243 | hamcrest-library 244 | 1.3 245 | test 246 | 247 | 248 | 249 | org.easytesting 250 | fest-reflect 251 | 1.4.1 252 | test 253 | 254 | 255 | 256 | 257 | 258 | 259 | --------------------------------------------------------------------------------