├── .gitignore ├── .mvn └── wrapper │ ├── maven-wrapper.jar │ └── maven-wrapper.properties ├── .travis.yml ├── LICENSE ├── README.md ├── mvnw ├── mvnw.cmd ├── pom.xml └── src ├── checkstyle ├── checkstyle-header.txt ├── checkstyle-suppressions.xml └── checkstyle.xml ├── main └── java │ └── org │ └── springframework │ └── boot │ └── actuate │ └── metrics │ └── kafka │ ├── KafkaConfigUtils.java │ ├── KafkaMetricContainer.java │ └── KafkaStatisticsProvider.java └── test └── java └── org └── springframework └── boot └── actuate └── metrics └── kafka ├── KafkaConfigUtilsTests.java ├── KafkaMetricContainerTests.java └── KafkaStatisticsProviderTests.java /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | *.sw? 3 | .#* 4 | *# 5 | *~ 6 | /build 7 | /code 8 | .classpath 9 | .project 10 | .settings 11 | .metadata 12 | .factorypath 13 | .recommenders 14 | bin 15 | build 16 | lib/ 17 | target 18 | .factorypath 19 | .springBeans 20 | interpolated*.xml 21 | dependency-reduced-pom.xml 22 | build.log 23 | _site/ 24 | .*.md.html 25 | manifest.yml 26 | MANIFEST.MF 27 | settings.xml 28 | activemq-data 29 | overridedb.* 30 | *.iml 31 | *.ipr 32 | *.iws 33 | .idea 34 | *.jar 35 | !.mvn/wrapper/maven-wrapper.jar 36 | .DS_Store 37 | .factorypath 38 | /pom.xml.versionsBackup 39 | -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stepio/spring-actuator-kafka/a325e511146ff4ef9ade15a523e053c55d9b6d9f/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: openjdk8 3 | install: true 4 | script: mvn -P owasp,sonar install -Dmaven.javadoc.skip=true -B -Dsonar.projectKey=$SONAR_PROJECT_KEY -Dsonar.organization=$SONAR_ORGANIZATION -Dsonar.host.url=$SONAR_HOST_URL -Dsonar.login=$SONAR_LOGIN 5 | -------------------------------------------------------------------------------- /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, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # spring-actuator-kafka 2 | 3 | [![Build Status](https://travis-ci.org/stepio/spring-actuator-kafka.svg?branch=master)](https://travis-ci.org/stepio/spring-actuator-kafka) 4 | [![Sonarcloud Status](https://sonarcloud.io/api/project_badges/measure?project=stepio_spring-actuator-kafka&metric=alert_status)](https://sonarcloud.io/dashboard?id=stepio_spring-actuator-kafka) 5 | [![Codacy Badge](https://api.codacy.com/project/badge/Grade/100dd036555647f689505fee873af770)](https://app.codacy.com/app/stepio/spring-actuator-kafka?utm_source=github.com&utm_medium=referral&utm_content=stepio/spring-actuator-kafka&utm_campaign=Badge_Grade_Dashboard) 6 | [![DepShield Badge](https://depshield.sonatype.org/badges/stepio/spring-actuator-kafka/depshield.svg)](https://depshield.github.io) 7 | [![Apache 2.0 License](https://img.shields.io/badge/license-Apache%202-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0.txt) 8 | 9 | **Update:** Project is outdated and not supported, use [Micrometer](https://github.com/micrometer-metrics) for exporting metrics (including Kafka metrics). 10 | 11 | This tiny project provides reference implementation for `MetricsReporter` interface, backed with Spring Actuator's `GaugeService`. 12 | 13 | Tested with [spring-projects/spring-kafka](https://github.com/spring-projects/spring-kafka). Example for consumer creation: 14 | ```java 15 | private Class deserializerClass; 16 | private String kafkaBroker; 17 | private String kafkaGroup; 18 | private Integer kafkaConcurrency; 19 | // Spring Actuator's GaugeService should be initialized before passing it to Kafka 20 | // Use @Autowired, for example 21 | private GaugeService gaugeService; 22 | 23 | public ConcurrentKafkaListenerContainerFactory listenerContainerFactory() { 24 | ConcurrentKafkaListenerContainerFactory factory = new ConcurrentKafkaListenerContainerFactory<>(); 25 | factory.setConsumerFactory(new DefaultKafkaConsumerFactory<>(consumerConfigs())); 26 | factory.setConcurrency(kafkaConcurrency); 27 | return factory; 28 | } 29 | 30 | private Map consumerConfigs() { 31 | Map props = new HashMap<>(); 32 | props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaBroker); 33 | props.put(ConsumerConfig.GROUP_ID_CONFIG, kafkaGroup); 34 | props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); 35 | props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, deserializerClass); 36 | // implemented a "helper" method which sets the required properties: 37 | KafkaConfigUtils.configureKafkaMetrics(props, gaugeService); 38 | // alternalive option: public static void configureKafkaMetrics(Map configs, GaugeService gaugeService, String prefix, ScheduledExecutorService executorService, Long updateInterval) 39 | return props; 40 | } 41 | ``` 42 | 43 | Similar approach is relevant for producer creation: 44 | ```java 45 | private String kafkaBroker; 46 | // Spring Actuator's GaugeService should be initialized before passing it to Kafka 47 | // Use @Autowired, for example 48 | private GaugeService gaugeService; 49 | 50 | public KafkaTemplate kafkaTemplate() { 51 | new KafkaTemplate<>(new DefaultKafkaProducerFactory<>(createProducerConfigs())); 52 | } 53 | 54 | public Map createProducerConfigs() { 55 | Map props = new HashMap<>(); 56 | props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, broker); 57 | props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); 58 | props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class); 59 | // implemented a "helper" method which sets the required properties: 60 | KafkaConfigUtils.configureKafkaMetrics(props, gaugeService); 61 | // alternalive option: public static void configureKafkaMetrics(Map configs, GaugeService gaugeService, String prefix, ScheduledExecutorService executorService, Long updateInterval) 62 | return props; 63 | } 64 | ``` 65 | 66 | As the result, your `/metrics` endoints gets lots of additional data, e.g.: 67 | ```properties 68 | gauge.kafka.consumer-14.node-2.consumer-node-metrics.request-size-avg: 91, 69 | gauge.kafka.consumer-7.consumer-fetch-manager-metrics.fetch-throttle-time-avg: 0, 70 | gauge.kafka.consumer-15.consumer-metrics.request-size-avg: 95.13245033112582, 71 | gauge.kafka.consumer-1.node-2.consumer-node-metrics.outgoing-byte-rate: 168.6803914951063, 72 | gauge.kafka.consumer-11.consumer-metrics.io-ratio: 0.0002161823165518282, 73 | gauge.kafka.consumer-14.consumer-fetch-manager-metrics.fetch-size-avg: 0, 74 | gauge.kafka.consumer-11.node-2147483645.consumer-node-metrics.request-latency-max: "-Infinity", 75 | gauge.kafka.consumer-16.node-2147483645.consumer-node-metrics.response-rate: 0.5638636920813925, 76 | gauge.kafka.consumer-14.consumer-coordinator-metrics.commit-rate: 0.22379500377654069, 77 | gauge.kafka.consumer-14.node-3.consumer-node-metrics.request-size-avg: 0, 78 | gauge.kafka.consumer-8.consumer-coordinator-metrics.sync-time-max: 0, 79 | gauge.kafka.consumer-11.consumer-fetch-manager-metrics.fetch-latency-avg: 500.9, 80 | gauge.kafka.consumer-11.node-1.consumer-node-metrics.outgoing-byte-rate: 174.0321740153642, 81 | gauge.kafka.consumer-9.consumer-fetch-manager-metrics.fetch-throttle-time-avg: 0, 82 | gauge.kafka.consumer-14.node--2.consumer-node-metrics.request-size-max: "-Infinity", 83 | gauge.kafka.consumer-15.consumer-coordinator-metrics.join-time-avg: 0, 84 | gauge.kafka.consumer-4.node-2.consumer-node-metrics.request-latency-max: "-Infinity", 85 | gauge.kafka.consumer-1.consumer-metrics.request-rate: 4.555501189491977, 86 | gauge.kafka.consumer-2.node--2.consumer-node-metrics.request-size-avg: 0, 87 | 88 | ``` 89 | 90 | This implementation is just a POC - appreciate your feedback/recommendations. As a side effect, I do not intend to publish it to maven central as of now, so to get the jar you may do the following: 91 | 92 | 1. Clone the project locally and navigate to its folder via console/terminal. 93 | 2. Make sure that java 8 is installed and `JAVA_HOME` is set properly. 94 | 3. Execute `./mvnw clean install` 95 | 4. Enrich your project's pom.xml with next block: 96 | 97 | ```xml 98 | 99 | org.stepio.kafka 100 | spring-actuator-kafka 101 | 0.0.1-SNAPSHOT 102 | 103 | ``` 104 | 105 | Related issues: 106 | - [spring-kafka/issues/127](https://github.com/spring-projects/spring-kafka/issues/127) 107 | - [spring-boot/issues/6227](https://github.com/spring-projects/spring-boot/issues/6227) 108 | - [KAFKA-3923](https://issues.apache.org/jira/browse/KAFKA-3923) 109 | -------------------------------------------------------------------------------- /mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Maven2 Start Up Batch script 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # M2_HOME - location of maven2's installed home dir 31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 32 | # e.g. to debug Maven itself, use 33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 35 | # ---------------------------------------------------------------------------- 36 | 37 | if [ -z "$MAVEN_SKIP_RC" ] ; then 38 | 39 | if [ -f /etc/mavenrc ] ; then 40 | . /etc/mavenrc 41 | fi 42 | 43 | if [ -f "$HOME/.mavenrc" ] ; then 44 | . "$HOME/.mavenrc" 45 | fi 46 | 47 | fi 48 | 49 | # OS specific support. $var _must_ be set to either true or false. 50 | cygwin=false; 51 | darwin=false; 52 | mingw=false 53 | case "`uname`" in 54 | CYGWIN*) cygwin=true ;; 55 | MINGW*) mingw=true;; 56 | Darwin*) darwin=true 57 | # 58 | # Look for the Apple JDKs first to preserve the existing behaviour, and then look 59 | # for the new JDKs provided by Oracle. 60 | # 61 | if [ -z "$JAVA_HOME" ] && [ -L /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK ] ; then 62 | # 63 | # Apple JDKs 64 | # 65 | export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home 66 | fi 67 | 68 | if [ -z "$JAVA_HOME" ] && [ -L /System/Library/Java/JavaVirtualMachines/CurrentJDK ] ; then 69 | # 70 | # Apple JDKs 71 | # 72 | export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home 73 | fi 74 | 75 | if [ -z "$JAVA_HOME" ] && [ -L "/Library/Java/JavaVirtualMachines/CurrentJDK" ] ; then 76 | # 77 | # Oracle JDKs 78 | # 79 | export JAVA_HOME=/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home 80 | fi 81 | 82 | if [ -z "$JAVA_HOME" ] && [ -x "/usr/libexec/java_home" ]; then 83 | # 84 | # Apple JDKs 85 | # 86 | export JAVA_HOME=`/usr/libexec/java_home` 87 | fi 88 | ;; 89 | esac 90 | 91 | if [ -z "$JAVA_HOME" ] ; then 92 | if [ -r /etc/gentoo-release ] ; then 93 | JAVA_HOME=`java-config --jre-home` 94 | fi 95 | fi 96 | 97 | if [ -z "$M2_HOME" ] ; then 98 | ## resolve links - $0 may be a link to maven's home 99 | PRG="$0" 100 | 101 | # need this for relative symlinks 102 | while [ -h "$PRG" ] ; do 103 | ls=`ls -ld "$PRG"` 104 | link=`expr "$ls" : '.*-> \(.*\)$'` 105 | if expr "$link" : '/.*' > /dev/null; then 106 | PRG="$link" 107 | else 108 | PRG="`dirname "$PRG"`/$link" 109 | fi 110 | done 111 | 112 | saveddir=`pwd` 113 | 114 | M2_HOME=`dirname "$PRG"`/.. 115 | 116 | # make it fully qualified 117 | M2_HOME=`cd "$M2_HOME" && pwd` 118 | 119 | cd "$saveddir" 120 | # echo Using m2 at $M2_HOME 121 | fi 122 | 123 | # For Cygwin, ensure paths are in UNIX format before anything is touched 124 | if $cygwin ; then 125 | [ -n "$M2_HOME" ] && 126 | M2_HOME=`cygpath --unix "$M2_HOME"` 127 | [ -n "$JAVA_HOME" ] && 128 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 129 | [ -n "$CLASSPATH" ] && 130 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 131 | fi 132 | 133 | # For Migwn, ensure paths are in UNIX format before anything is touched 134 | if $mingw ; then 135 | [ -n "$M2_HOME" ] && 136 | M2_HOME="`(cd "$M2_HOME"; pwd)`" 137 | [ -n "$JAVA_HOME" ] && 138 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 139 | # TODO classpath? 140 | fi 141 | 142 | if [ -z "$JAVA_HOME" ]; then 143 | javaExecutable="`which javac`" 144 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 145 | # readlink(1) is not available as standard on Solaris 10. 146 | readLink=`which readlink` 147 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 148 | if $darwin ; then 149 | javaHome="`dirname \"$javaExecutable\"`" 150 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 151 | else 152 | javaExecutable="`readlink -f \"$javaExecutable\"`" 153 | fi 154 | javaHome="`dirname \"$javaExecutable\"`" 155 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 156 | JAVA_HOME="$javaHome" 157 | export JAVA_HOME 158 | fi 159 | fi 160 | fi 161 | 162 | if [ -z "$JAVACMD" ] ; then 163 | if [ -n "$JAVA_HOME" ] ; then 164 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 165 | # IBM's JDK on AIX uses strange locations for the executables 166 | JAVACMD="$JAVA_HOME/jre/sh/java" 167 | else 168 | JAVACMD="$JAVA_HOME/bin/java" 169 | fi 170 | else 171 | JAVACMD="`which java`" 172 | fi 173 | fi 174 | 175 | if [ ! -x "$JAVACMD" ] ; then 176 | echo "Error: JAVA_HOME is not defined correctly." >&2 177 | echo " We cannot execute $JAVACMD" >&2 178 | exit 1 179 | fi 180 | 181 | if [ -z "$JAVA_HOME" ] ; then 182 | echo "Warning: JAVA_HOME environment variable is not set." 183 | fi 184 | 185 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 186 | 187 | # traverses directory structure from process work directory to filesystem root 188 | # first directory with .mvn subdirectory is considered project base directory 189 | find_maven_basedir() { 190 | local basedir=$(pwd) 191 | local wdir=$(pwd) 192 | while [ "$wdir" != '/' ] ; do 193 | if [ -d "$wdir"/.mvn ] ; then 194 | basedir=$wdir 195 | break 196 | fi 197 | wdir=$(cd "$wdir/.."; pwd) 198 | done 199 | echo "${basedir}" 200 | } 201 | 202 | # concatenates all lines of a file 203 | concat_lines() { 204 | if [ -f "$1" ]; then 205 | echo "$(tr -s '\n' ' ' < "$1")" 206 | fi 207 | } 208 | 209 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-$(find_maven_basedir)} 210 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 211 | 212 | # For Cygwin, switch paths to Windows format before running java 213 | if $cygwin; then 214 | [ -n "$M2_HOME" ] && 215 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 216 | [ -n "$JAVA_HOME" ] && 217 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 218 | [ -n "$CLASSPATH" ] && 219 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 220 | [ -n "$MAVEN_PROJECTBASEDIR" ] && 221 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` 222 | fi 223 | 224 | # Provide a "standardized" way to retrieve the CLI args that will 225 | # work with both Windows and non-Windows executions. 226 | MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" 227 | export MAVEN_CMD_LINE_ARGS 228 | 229 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 230 | 231 | # avoid using MAVEN_CMD_LINE_ARGS below since that would loose parameter escaping in $@ 232 | exec "$JAVACMD" \ 233 | $MAVEN_OPTS \ 234 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 235 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 236 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" 237 | -------------------------------------------------------------------------------- /mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM http://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Maven2 Start Up Batch script 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM M2_HOME - location of maven2's installed home dir 28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending 30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | @REM e.g. to debug Maven itself, use 32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | @REM ---------------------------------------------------------------------------- 35 | 36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 37 | @echo off 38 | @REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' 39 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 40 | 41 | @REM set %HOME% to equivalent of $HOME 42 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 43 | 44 | @REM Execute a user defined script before this one 45 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 46 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 47 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" 48 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" 49 | :skipRcPre 50 | 51 | @setlocal 52 | 53 | set ERROR_CODE=0 54 | 55 | @REM To isolate internal variables from possible post scripts, we use another setlocal 56 | @setlocal 57 | 58 | @REM ==== START VALIDATION ==== 59 | if not "%JAVA_HOME%" == "" goto OkJHome 60 | 61 | echo. 62 | echo Error: JAVA_HOME not found in your environment. >&2 63 | echo Please set the JAVA_HOME variable in your environment to match the >&2 64 | echo location of your Java installation. >&2 65 | echo. 66 | goto error 67 | 68 | :OkJHome 69 | if exist "%JAVA_HOME%\bin\java.exe" goto init 70 | 71 | echo. 72 | echo Error: JAVA_HOME is set to an invalid directory. >&2 73 | echo JAVA_HOME = "%JAVA_HOME%" >&2 74 | echo Please set the JAVA_HOME variable in your environment to match the >&2 75 | echo location of your Java installation. >&2 76 | echo. 77 | goto error 78 | 79 | @REM ==== END VALIDATION ==== 80 | 81 | :init 82 | 83 | set MAVEN_CMD_LINE_ARGS=%MAVEN_CONFIG% %* 84 | 85 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 86 | @REM Fallback to current working directory if not found. 87 | 88 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 89 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 90 | 91 | set EXEC_DIR=%CD% 92 | set WDIR=%EXEC_DIR% 93 | :findBaseDir 94 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 95 | cd .. 96 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 97 | set WDIR=%CD% 98 | goto findBaseDir 99 | 100 | :baseDirFound 101 | set MAVEN_PROJECTBASEDIR=%WDIR% 102 | cd "%EXEC_DIR%" 103 | goto endDetectBaseDir 104 | 105 | :baseDirNotFound 106 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 107 | cd "%EXEC_DIR%" 108 | 109 | :endDetectBaseDir 110 | 111 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 112 | 113 | @setlocal EnableExtensions EnableDelayedExpansion 114 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 115 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 116 | 117 | :endReadAdditionalConfig 118 | 119 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 120 | 121 | set WRAPPER_JAR=""%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar"" 122 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 123 | 124 | # avoid using MAVEN_CMD_LINE_ARGS below since that would loose parameter escaping in %* 125 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* 126 | if ERRORLEVEL 1 goto error 127 | goto end 128 | 129 | :error 130 | set ERROR_CODE=1 131 | 132 | :end 133 | @endlocal & set ERROR_CODE=%ERROR_CODE% 134 | 135 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost 136 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 137 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" 138 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" 139 | :skipRcPost 140 | 141 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 142 | if "%MAVEN_BATCH_PAUSE%" == "on" pause 143 | 144 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% 145 | 146 | exit /B %ERROR_CODE% 147 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 4.0.0 6 | 7 | 8 | org.springframework.boot 9 | spring-boot-starter-parent 10 | 1.5.21.RELEASE 11 | 12 | 13 | org.stepio.kafka 14 | spring-actuator-kafka 15 | 0.2.0-SNAPSHOT 16 | jar 17 | 18 | 19 | 1.7 20 | 3.13.2 21 | 0.9.0.1 22 | 1.7.4 23 | 5.2.1 24 | 0.8.4 25 | 3.6.0.1398 26 | 27 | 8 28 | 29 | 30 | 31 | 32 | 33 | org.apache.kafka 34 | kafka-clients 35 | ${kafka.version} 36 | 37 | 38 | org.assertj 39 | assertj-core 40 | ${assertj.version} 41 | 42 | 43 | org.powermock 44 | powermock-api-mockito 45 | ${powermock.version} 46 | 47 | 48 | org.powermock 49 | powermock-module-junit4 50 | ${powermock.version} 51 | 52 | 53 | 54 | 55 | 56 | 57 | org.apache.kafka 58 | kafka-clients 59 | 60 | 61 | org.springframework.boot 62 | spring-boot-starter-actuator 63 | 64 | 65 | 66 | org.assertj 67 | assertj-core 68 | test 69 | 70 | 71 | org.powermock 72 | powermock-api-mockito 73 | test 74 | 75 | 76 | org.powermock 77 | powermock-module-junit4 78 | test 79 | 80 | 81 | org.springframework.boot 82 | spring-boot-starter-test 83 | test 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | org.jacoco 92 | jacoco-maven-plugin 93 | ${jacoco-maven-plugin.version} 94 | 95 | 96 | org.owasp 97 | dependency-check-maven 98 | ${dependency-check-plugin.version} 99 | 100 | 101 | org.sonarsource.scanner.maven 102 | sonar-maven-plugin 103 | ${sonar-maven-plugin.version} 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | owasp 112 | 113 | 114 | 115 | org.owasp 116 | dependency-check-maven 117 | 118 | ${dependency-check-plugin.score-limit} 119 | 120 | 121 | 122 | 123 | check 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | sonar 133 | 134 | 135 | 136 | org.jacoco 137 | jacoco-maven-plugin 138 | 139 | 140 | 141 | prepare-agent 142 | 143 | 144 | 145 | 146 | 147 | org.sonarsource.scanner.maven 148 | sonar-maven-plugin 149 | 150 | 151 | 152 | sonar 153 | 154 | verify 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | repo1.maven.apache.org 166 | http://repo1.maven.apache.org/maven2 167 | 168 | 169 | repo.exist.com 170 | http://repo.exist.com/maven2 171 | 172 | 173 | ibiblio.org 174 | http://mirrors.ibiblio.org/pub/mirrors/maven2 175 | 176 | 177 | uk.maven.org 178 | http://uk.maven.org/maven2 179 | 180 | 181 | 182 | 183 | -------------------------------------------------------------------------------- /src/checkstyle/checkstyle-header.txt: -------------------------------------------------------------------------------- 1 | ^\Q/*\E$ 2 | ^\Q * Copyright \E20\d\d(\-20\d\d)?\Q the original author or authors.\E$ 3 | ^\Q *\E$ 4 | ^\Q * Licensed under the Apache License, Version 2.0 (the "License");\E$ 5 | ^\Q * you may not use this file except in compliance with the License.\E$ 6 | ^\Q * You may obtain a copy of the License at\E$ 7 | ^\Q *\E$ 8 | ^\Q * http://www.apache.org/licenses/LICENSE-2.0\E$ 9 | ^\Q *\E$ 10 | ^\Q * Unless required by applicable law or agreed to in writing, software\E$ 11 | ^\Q * distributed under the License is distributed on an "AS IS" BASIS,\E$ 12 | ^\Q * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\E$ 13 | ^\Q * See the License for the specific language governing permissions and\E$ 14 | ^\Q * limitations under the License.\E$ 15 | ^\Q */\E$ 16 | ^$ 17 | ^.*$ 18 | -------------------------------------------------------------------------------- /src/checkstyle/checkstyle-suppressions.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /src/checkstyle/checkstyle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 146 | 147 | 148 | 149 | 150 | 151 | 153 | 154 | 155 | 156 | 157 | 158 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/boot/actuate/metrics/kafka/KafkaConfigUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 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 | 17 | package org.springframework.boot.actuate.metrics.kafka; 18 | 19 | import java.util.Collections; 20 | import java.util.Map; 21 | import java.util.concurrent.ScheduledExecutorService; 22 | 23 | import org.apache.kafka.clients.CommonClientConfigs; 24 | import org.slf4j.Logger; 25 | import org.slf4j.LoggerFactory; 26 | 27 | import org.springframework.boot.actuate.metrics.GaugeService; 28 | 29 | /** 30 | * Utility methods to simplify the initialization of properties, required for metrics' 31 | * gathering. 32 | * 33 | * @author Igor Stepanov 34 | */ 35 | public final class KafkaConfigUtils { 36 | 37 | private static final Logger LOGGER = LoggerFactory.getLogger(KafkaConfigUtils.class); 38 | 39 | private KafkaConfigUtils() { 40 | } 41 | 42 | /** 43 | * Method for setting Kafka-related properties, required for proper updating of 44 | * metrics. 45 | * @param configs {@link Map} with Kafka-specific properties, required to initialize 46 | * the appropriate consumer/producer. 47 | * @param gaugeService reference to an instance of Springs {@link GaugeService}, used 48 | * to set the collected metrics 49 | * @param prefix initial part of the metric's label 50 | * @param executorService reference to an instance of 51 | * {@link ScheduledExecutorService}, used to schedule periodic values' recalculation 52 | * for metrics 53 | * @param updateInterval interval for iterating the whole set of tracked metrics to 54 | * recalculate and resubmit their values 55 | */ 56 | public static void configureKafkaMetrics(Map configs, 57 | GaugeService gaugeService, String prefix, 58 | ScheduledExecutorService executorService, Long updateInterval) { 59 | if (gaugeService == null) { 60 | throw new IllegalArgumentException( 61 | "Initializing GaugeService as null is meaningless!"); 62 | } 63 | configs.put(CommonClientConfigs.METRIC_REPORTER_CLASSES_CONFIG, 64 | Collections.singletonList(KafkaStatisticsProvider.class.getName())); 65 | configs.put(KafkaStatisticsProvider.METRICS_GAUGE_SERVICE_IMPL, gaugeService); 66 | LOGGER.debug("Set property {} with provided GaugeService instance reference", 67 | KafkaStatisticsProvider.METRICS_GAUGE_SERVICE_IMPL); 68 | if (executorService != null) { 69 | configs.put(KafkaStatisticsProvider.METRICS_UPDATE_EXECUTOR_IMPL, 70 | executorService); 71 | LOGGER.debug( 72 | "Set property {} with provided ScheduledExecutorService instance reference", 73 | KafkaStatisticsProvider.METRICS_UPDATE_EXECUTOR_IMPL); 74 | } 75 | if (updateInterval != null) { 76 | configs.put(KafkaStatisticsProvider.METRICS_UPDATE_INTERVAL_PARAM, 77 | updateInterval); 78 | LOGGER.debug("Set property {} with value {}", 79 | KafkaStatisticsProvider.METRICS_UPDATE_INTERVAL_PARAM, 80 | updateInterval); 81 | } 82 | if (prefix != null) { 83 | configs.put(KafkaStatisticsProvider.METRICS_PREFIX_PARAM, prefix); 84 | LOGGER.debug("Set property {} with value {}", 85 | KafkaStatisticsProvider.METRICS_PREFIX_PARAM, prefix); 86 | } 87 | } 88 | 89 | /** 90 | * Overloaded method, which sets updateInterval with default value. 91 | * @param configs {@link Map} with Kafka-specific properties, required to initialize 92 | * the appropriate consumer/producer. 93 | * @param gaugeService reference to an instance of Spring's {@link GaugeService}, used 94 | * to set the collected metrics 95 | */ 96 | public static void configureKafkaMetrics(Map configs, 97 | GaugeService gaugeService) { 98 | configureKafkaMetrics(configs, gaugeService, null, null, null); 99 | } 100 | 101 | } 102 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/boot/actuate/metrics/kafka/KafkaMetricContainer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 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 | 17 | package org.springframework.boot.actuate.metrics.kafka; 18 | 19 | import java.util.Map; 20 | import java.util.TreeMap; 21 | 22 | import org.apache.kafka.common.Metric; 23 | import org.apache.kafka.common.MetricName; 24 | import org.apache.kafka.common.metrics.KafkaMetric; 25 | 26 | /** 27 | * Container for {@link KafkaMetric}, defining the name of the appropriate metric, which 28 | * should be exposed to Spring Boot Actuator. 29 | * 30 | * @author Igor Stepanov 31 | */ 32 | public class KafkaMetricContainer { 33 | 34 | private String metricName; 35 | 36 | private String prefix; 37 | 38 | private Metric value; 39 | 40 | public KafkaMetricContainer(Metric value, String prefix) { 41 | this.value = value; 42 | this.prefix = prefix; 43 | this.metricName = metricName(value); 44 | } 45 | 46 | public String getMetricName() { 47 | return this.metricName; 48 | } 49 | 50 | public Metric getValue() { 51 | return this.value; 52 | } 53 | 54 | protected String metricName(Metric metric) { 55 | MetricName name = metric.metricName(); 56 | StringBuilder builder = new StringBuilder(); 57 | builder.append(this.prefix); 58 | builder.append('.'); 59 | for (Map.Entry entry : new TreeMap<>(name.tags()).entrySet()) { 60 | if (!entry.getKey().isEmpty() && !entry.getValue().isEmpty()) { 61 | builder.append(entry.getValue()); 62 | builder.append('.'); 63 | } 64 | } 65 | builder.append(name.group()); 66 | builder.append('.'); 67 | builder.append(name.name()); 68 | return builder.toString(); 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/boot/actuate/metrics/kafka/KafkaStatisticsProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 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 | 17 | package org.springframework.boot.actuate.metrics.kafka; 18 | 19 | import java.util.List; 20 | import java.util.Map; 21 | import java.util.concurrent.ConcurrentHashMap; 22 | import java.util.concurrent.ConcurrentMap; 23 | import java.util.concurrent.Executors; 24 | import java.util.concurrent.ScheduledExecutorService; 25 | import java.util.concurrent.TimeUnit; 26 | 27 | import org.apache.kafka.common.MetricName; 28 | import org.apache.kafka.common.metrics.KafkaMetric; 29 | import org.apache.kafka.common.metrics.MetricsReporter; 30 | import org.slf4j.Logger; 31 | import org.slf4j.LoggerFactory; 32 | 33 | import org.springframework.boot.actuate.metrics.GaugeService; 34 | 35 | /** 36 | * Implementation of Apache Kafka's {@link MetricsReporter}, backed with Spring Boot 37 | * Actuator. Unfortunately, Kafka's approach makes it unclear how to use Spring's context 38 | * (if it's somehow possible), so all the job is done with pure Java - cannot use Spring's 39 | * annotations effectively. 40 | * 41 | * @author Igor Stepanov 42 | */ 43 | public class KafkaStatisticsProvider implements MetricsReporter { 44 | 45 | private static final Logger LOGGER = LoggerFactory 46 | .getLogger(KafkaStatisticsProvider.class); 47 | 48 | /** 49 | * Custom property for Kafka's {@link org.apache.kafka.common.Configurable} to specify 50 | * Spring's {@link GaugeService} implementation for metrics' reporting. Required. 51 | */ 52 | public static final String METRICS_GAUGE_SERVICE_IMPL = "kafka.metrics.gauge.service.impl"; 53 | 54 | /** 55 | * Custom property for Kafka's {@link org.apache.kafka.common.Configurable} to specify 56 | * {@link ScheduledExecutorService}, for handling the recalculation of metrics' 57 | * values. Optional, default value: {@link Executors#newSingleThreadExecutor()}. 58 | */ 59 | public static final String METRICS_UPDATE_EXECUTOR_IMPL = "kafka.metrics.update.executor"; 60 | 61 | /** 62 | * Custom property for Kafka's {@link org.apache.kafka.common.Configurable} to specify 63 | * interval for metrics' recalculating (in milliseconds). Optional, default value: 64 | * 30000. 65 | */ 66 | public static final String METRICS_UPDATE_INTERVAL_PARAM = "kafka.metrics.update.interval"; 67 | 68 | /** 69 | * Custom property for Kafka's {@link org.apache.kafka.common.Configurable} to specify 70 | * naming prefix for metrics. Optional, default value: "kafka". 71 | *

72 | * Note: Spring Actuator's default prefix for {@link GaugeService} is "gauge", so 73 | * resulting default prefix is "gauge.kafka" Same rule is applicable for custom 74 | * metrics as well. 75 | */ 76 | public static final String METRICS_PREFIX_PARAM = "kafka.metrics.prefix"; 77 | 78 | /** 79 | * Default value for "kafka.metrics.update.interval" property. 80 | */ 81 | public static final long METRICS_UPDATE_INTERVAL_DEFAULT = 30000; 82 | 83 | /** 84 | * Default value for "kafka.metrics.prefix" property. 85 | */ 86 | public static final String METRICS_PREFIX_DEFAULT = "kafka"; 87 | 88 | protected ConcurrentMap configuredMetrics; 89 | 90 | protected ScheduledExecutorService executorService; 91 | 92 | protected boolean closeExecutorService = false; 93 | 94 | protected GaugeService gaugeService; // effectively final, no setter - should not be 95 | // updated once it's initialized 96 | 97 | protected Long updateInterval; // effectively final, no setter - should not be updated 98 | // once it's initialized 99 | 100 | protected String prefix; 101 | 102 | public KafkaStatisticsProvider() { 103 | LOGGER.debug("Constructed an empty object"); 104 | } 105 | 106 | @Override 107 | public void init(List metrics) { 108 | for (KafkaMetric metric : metrics) { 109 | metricChange(metric); 110 | } 111 | LOGGER.debug("Initialized {} metrics", metrics.size()); 112 | } 113 | 114 | @Override 115 | public void metricChange(KafkaMetric metric) { 116 | KafkaMetricContainer container = new KafkaMetricContainer(metric, this.prefix); 117 | this.configuredMetrics.put(metric.metricName(), container); 118 | LOGGER.trace("Metric {} is added/modified", container.getMetricName()); 119 | } 120 | 121 | @Override 122 | public void metricRemoval(KafkaMetric metric) { 123 | KafkaMetricContainer container = this.configuredMetrics 124 | .remove(metric.metricName()); 125 | if (container != null) { 126 | LOGGER.trace("Metric {} is removed", container.getMetricName()); 127 | } 128 | } 129 | 130 | /** 131 | * Closing the {@link ScheduledExecutorService} if it's initialized internally. 132 | */ 133 | @Override 134 | public void close() { 135 | if (this.executorService != null && !this.executorService.isShutdown() 136 | && this.closeExecutorService) { 137 | this.executorService.shutdown(); 138 | LOGGER.debug("Object cleared, executor stopped"); 139 | } 140 | } 141 | 142 | @Override 143 | public void configure(Map configs) { 144 | this.gaugeService = (GaugeService) configs.get(METRICS_GAUGE_SERVICE_IMPL); 145 | this.executorService = (ScheduledExecutorService) configs 146 | .get(METRICS_UPDATE_EXECUTOR_IMPL); 147 | this.updateInterval = (Long) configs.get(METRICS_UPDATE_INTERVAL_PARAM); 148 | this.prefix = (String) configs.get(METRICS_PREFIX_PARAM); 149 | postConstruct(); 150 | } 151 | 152 | /** 153 | * Actually does the configuration of {@link KafkaStatisticsProvider} instance if the 154 | * appropriate {@link GaugeService} is set. 155 | */ 156 | protected void postConstruct() { 157 | LOGGER.debug("Performing initialization to schedule the metrics gathering..."); 158 | this.configuredMetrics = new ConcurrentHashMap<>(); 159 | if (this.executorService == null) { 160 | this.executorService = Executors.newSingleThreadScheduledExecutor(); 161 | this.closeExecutorService = true; 162 | } 163 | if (this.updateInterval == null) { 164 | this.updateInterval = METRICS_UPDATE_INTERVAL_DEFAULT; 165 | } 166 | if (this.prefix == null) { 167 | this.prefix = METRICS_PREFIX_DEFAULT; 168 | } 169 | this.executorService.scheduleAtFixedRate(new Runnable() { 170 | public void run() { 171 | try { 172 | String metricName; 173 | double metricValue; 174 | for (ConcurrentMap.Entry entry : KafkaStatisticsProvider.this.configuredMetrics 175 | .entrySet()) { 176 | metricName = entry.getValue().getMetricName(); 177 | metricValue = entry.getValue().getValue().value(); 178 | LOGGER.trace("Set metric {} with value {}", metricName, 179 | metricValue); 180 | KafkaStatisticsProvider.this.gaugeService.submit(metricName, 181 | metricValue); 182 | } 183 | } 184 | catch (Exception ex) { 185 | // Javadoc: If any execution of the task encounters an exception, 186 | // subsequent executions are suppressed 187 | LOGGER.error("Exception occurred in the scheduled task", ex); 188 | } 189 | } 190 | }, 0, this.updateInterval, TimeUnit.MILLISECONDS); 191 | LOGGER.debug( 192 | "Initialization complete, metrics updating scheduled with {} ms interval between the updates", 193 | this.updateInterval); 194 | } 195 | 196 | } 197 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/boot/actuate/metrics/kafka/KafkaConfigUtilsTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 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 | 17 | package org.springframework.boot.actuate.metrics.kafka; 18 | 19 | import static org.assertj.core.api.Assertions.assertThat; 20 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType; 21 | import static org.mockito.BDDMockito.mock; 22 | 23 | import java.util.HashMap; 24 | import java.util.Map; 25 | import java.util.concurrent.Executors; 26 | import java.util.concurrent.ScheduledExecutorService; 27 | 28 | import org.apache.kafka.clients.CommonClientConfigs; 29 | import org.assertj.core.api.ThrowableAssert; 30 | import org.junit.Test; 31 | 32 | import org.springframework.boot.actuate.metrics.GaugeService; 33 | 34 | /** 35 | * Tests for {@link KafkaConfigUtils}. 36 | * 37 | * @author Igor Stepanov 38 | */ 39 | public class KafkaConfigUtilsTests { 40 | 41 | @Test 42 | public void configureKafkaMetrics_withNullMap_NullPointerException() { 43 | assertThatExceptionOfType(NullPointerException.class) 44 | .isThrownBy(new ThrowableAssert.ThrowingCallable() { 45 | @Override 46 | public void call() throws Throwable { 47 | KafkaConfigUtils.configureKafkaMetrics(null, mockGaugeService()); 48 | } 49 | }); 50 | final ScheduledExecutorService executors = Executors 51 | .newSingleThreadScheduledExecutor(); 52 | try { 53 | assertThatExceptionOfType(NullPointerException.class) 54 | .isThrownBy(new ThrowableAssert.ThrowingCallable() { 55 | @Override 56 | public void call() throws Throwable { 57 | KafkaConfigUtils.configureKafkaMetrics(null, 58 | mockGaugeService(), "test", executors, 10L); 59 | } 60 | }); 61 | } 62 | finally { 63 | executors.shutdown(); 64 | } 65 | } 66 | 67 | @Test 68 | public void configureKafkaMetrics_withNullGaugeService_NullPointerException() { 69 | assertThatExceptionOfType(IllegalArgumentException.class) 70 | .isThrownBy(new ThrowableAssert.ThrowingCallable() { 71 | @Override 72 | public void call() throws Throwable { 73 | KafkaConfigUtils.configureKafkaMetrics( 74 | new HashMap(), null); 75 | } 76 | }).withMessageContaining( 77 | "Initializing GaugeService as null is meaningless!"); 78 | final ScheduledExecutorService executors = Executors 79 | .newSingleThreadScheduledExecutor(); 80 | try { 81 | assertThatExceptionOfType(IllegalArgumentException.class) 82 | .isThrownBy(new ThrowableAssert.ThrowingCallable() { 83 | @Override 84 | public void call() throws Throwable { 85 | KafkaConfigUtils.configureKafkaMetrics( 86 | new HashMap(), null, "test", 87 | executors, 10L); 88 | } 89 | }).withMessageContaining( 90 | "Initializing GaugeService as null is meaningless!"); 91 | } 92 | finally { 93 | executors.shutdown(); 94 | } 95 | } 96 | 97 | @Test 98 | public void configureKafkaMetrics_withGaugeService() { 99 | Map config = new HashMap<>(); 100 | assertThat(config).isEmpty(); 101 | GaugeService gaugeService = mockGaugeService(); 102 | KafkaConfigUtils.configureKafkaMetrics(config, gaugeService); 103 | assertThat(config).hasSize(2); 104 | assertThat(config.get(CommonClientConfigs.METRIC_REPORTER_CLASSES_CONFIG)) 105 | .asList().contains(KafkaStatisticsProvider.class.getCanonicalName()); 106 | assertThat(config.get(KafkaStatisticsProvider.METRICS_GAUGE_SERVICE_IMPL)) 107 | .isSameAs(gaugeService); 108 | assertThat(config.get(KafkaStatisticsProvider.METRICS_PREFIX_PARAM)).isNull(); 109 | assertThat(config.get(KafkaStatisticsProvider.METRICS_UPDATE_EXECUTOR_IMPL)) 110 | .isNull(); 111 | assertThat(config.get(KafkaStatisticsProvider.METRICS_UPDATE_INTERVAL_PARAM)) 112 | .isNull(); 113 | } 114 | 115 | @Test 116 | public void configureKafkaMetrics_withGaugeServiceAndPrefix() { 117 | Map config = new HashMap<>(); 118 | assertThat(config).isEmpty(); 119 | GaugeService gaugeService = mockGaugeService(); 120 | String prefix = "test.prefix"; 121 | KafkaConfigUtils.configureKafkaMetrics(config, gaugeService, prefix, null, null); 122 | assertThat(config).hasSize(3); 123 | assertThat(config.get(CommonClientConfigs.METRIC_REPORTER_CLASSES_CONFIG)) 124 | .asList().contains(KafkaStatisticsProvider.class.getCanonicalName()); 125 | assertThat(config.get(KafkaStatisticsProvider.METRICS_GAUGE_SERVICE_IMPL)) 126 | .isSameAs(gaugeService); 127 | assertThat(config.get(KafkaStatisticsProvider.METRICS_PREFIX_PARAM)) 128 | .isEqualTo(prefix); 129 | assertThat(config.get(KafkaStatisticsProvider.METRICS_UPDATE_EXECUTOR_IMPL)) 130 | .isNull(); 131 | assertThat(config.get(KafkaStatisticsProvider.METRICS_UPDATE_INTERVAL_PARAM)) 132 | .isNull(); 133 | } 134 | 135 | @Test 136 | public void configureKafkaMetrics_withGaugeServiceAndInterval() { 137 | Map config = new HashMap<>(); 138 | assertThat(config).isEmpty(); 139 | GaugeService gaugeService = mockGaugeService(); 140 | Long universalAnswer = 42L; 141 | KafkaConfigUtils.configureKafkaMetrics(config, gaugeService, null, null, 142 | universalAnswer); 143 | assertThat(config).hasSize(3); 144 | assertThat(config.get(CommonClientConfigs.METRIC_REPORTER_CLASSES_CONFIG)) 145 | .asList().contains(KafkaStatisticsProvider.class.getCanonicalName()); 146 | assertThat(config.get(KafkaStatisticsProvider.METRICS_GAUGE_SERVICE_IMPL)) 147 | .isSameAs(gaugeService); 148 | assertThat(config.get(KafkaStatisticsProvider.METRICS_PREFIX_PARAM)).isNull(); 149 | assertThat(config.get(KafkaStatisticsProvider.METRICS_UPDATE_EXECUTOR_IMPL)) 150 | .isNull(); 151 | assertThat(config.get(KafkaStatisticsProvider.METRICS_UPDATE_INTERVAL_PARAM)) 152 | .isEqualTo(universalAnswer); 153 | } 154 | 155 | @Test 156 | public void configureKafkaMetrics_withGaugeServiceAndExecutors() { 157 | ScheduledExecutorService executors = Executors.newSingleThreadScheduledExecutor(); 158 | try { 159 | Map config = new HashMap<>(); 160 | assertThat(config).isEmpty(); 161 | GaugeService gaugeService = mockGaugeService(); 162 | KafkaConfigUtils.configureKafkaMetrics(config, gaugeService, null, executors, 163 | null); 164 | assertThat(config).hasSize(3); 165 | assertThat(config.get(CommonClientConfigs.METRIC_REPORTER_CLASSES_CONFIG)) 166 | .asList().contains(KafkaStatisticsProvider.class.getCanonicalName()); 167 | assertThat(config.get(KafkaStatisticsProvider.METRICS_GAUGE_SERVICE_IMPL)) 168 | .isSameAs(gaugeService); 169 | assertThat(config.get(KafkaStatisticsProvider.METRICS_PREFIX_PARAM)).isNull(); 170 | assertThat(config.get(KafkaStatisticsProvider.METRICS_UPDATE_EXECUTOR_IMPL)) 171 | .isSameAs(executors); 172 | assertThat(config.get(KafkaStatisticsProvider.METRICS_UPDATE_INTERVAL_PARAM)) 173 | .isNull(); 174 | } 175 | finally { 176 | executors.shutdown(); 177 | } 178 | } 179 | 180 | private GaugeService mockGaugeService() { 181 | return mock(GaugeService.class); 182 | } 183 | 184 | } 185 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/boot/actuate/metrics/kafka/KafkaMetricContainerTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 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 | 17 | package org.springframework.boot.actuate.metrics.kafka; 18 | 19 | import static org.assertj.core.api.Assertions.assertThat; 20 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType; 21 | import static org.mockito.BDDMockito.given; 22 | import static org.mockito.BDDMockito.mock; 23 | 24 | import java.util.Random; 25 | 26 | import org.apache.kafka.common.Metric; 27 | import org.apache.kafka.common.MetricName; 28 | import org.assertj.core.api.ThrowableAssert; 29 | import org.junit.Test; 30 | 31 | /** 32 | * Tests for {@link KafkaMetricContainer}. 33 | * 34 | * @author Igor Stepanov 35 | */ 36 | public class KafkaMetricContainerTests { 37 | 38 | private Random random; 39 | 40 | private String metricPrefix; 41 | 42 | private String metricGroup; 43 | 44 | private String metricName; 45 | 46 | public KafkaMetricContainerTests() { 47 | this.random = new Random(); 48 | this.metricPrefix = "first_dummy_value"; 49 | this.metricGroup = "second_dummy_value"; 50 | this.metricName = "third_dummy_value"; 51 | } 52 | 53 | /** 54 | * Getter returns the same instance as was set to constructor. 55 | */ 56 | @Test 57 | public void getValue_withKafkaMetricContainer_same() { 58 | Metric metric = randomMetric(); 59 | KafkaMetricContainer metricContainer = new KafkaMetricContainer(metric, "test"); 60 | assertThat(metricContainer.getValue()).isSameAs(metric); 61 | } 62 | 63 | /** 64 | * Name is calculated once (upon constructing the object) and then the same instance 65 | * is returned each time. 66 | */ 67 | @Test 68 | public void getMetricName_withKafkaMetricContainer_same() { 69 | KafkaMetricContainer metricContainer = randomKafkaMetricContainer(); 70 | String name = metricContainer.getMetricName(); 71 | assertThat(metricContainer.getMetricName()).isSameAs(name); 72 | } 73 | 74 | /** 75 | * Name is calculated once (upon constructing the object) and then the same instance 76 | * is returned each time. 77 | */ 78 | @Test 79 | public void metricName_withConstantValues() { 80 | assertThat(randomKafkaMetricContainer().getMetricName()).isEqualTo( 81 | this.metricPrefix + '.' + this.metricGroup + '.' + this.metricName); 82 | } 83 | 84 | /** 85 | * Tags' values are used in metric's name, ordered by the appropriate keys. 86 | */ 87 | @Test 88 | public void metricName_withConstantValuesAndTags() { 89 | final String tag1 = "some_dummy_key"; 90 | final String tag2 = "another_dummy_key"; 91 | final String value1 = "some_dummy_val"; 92 | final String value2 = "another_dummy_val"; 93 | assertThat(randomKafkaMetricContainer(new MetricName(this.metricName, 94 | this.metricGroup, "Test metric", tag1, value1, tag2, value2)) 95 | .getMetricName()) 96 | .isEqualTo(this.metricPrefix + '.' + value2 + '.' + value1 97 | + '.' + this.metricGroup + '.' + this.metricName); 98 | } 99 | 100 | /** 101 | * Instance of {@link MetricName} cannot be initialized without name. 102 | */ 103 | @Test 104 | public void metricName_withNullName_NullPointerException() { 105 | assertThatExceptionOfType(NullPointerException.class) 106 | .isThrownBy(new ThrowableAssert.ThrowingCallable() { 107 | @Override 108 | public void call() throws Throwable { 109 | new KafkaMetricContainer( 110 | randomMetric(new MetricName(null, 111 | KafkaMetricContainerTests.this.metricGroup)), 112 | KafkaMetricContainerTests.this.metricPrefix); 113 | } 114 | }); 115 | } 116 | 117 | /** 118 | * Instance of {@link MetricName} cannot be initialized without group. 119 | */ 120 | @Test 121 | public void metricName_withNullGroup_NullPointerException() { 122 | assertThatExceptionOfType(NullPointerException.class) 123 | .isThrownBy(new ThrowableAssert.ThrowingCallable() { 124 | @Override 125 | public void call() throws Throwable { 126 | new KafkaMetricContainer( 127 | randomMetric(new MetricName( 128 | KafkaMetricContainerTests.this.metricName, null)), 129 | KafkaMetricContainerTests.this.metricPrefix); 130 | } 131 | }); 132 | } 133 | 134 | /** 135 | * Name's calculation fails for null value of {@link Metric}. 136 | */ 137 | @Test 138 | public void metricName_withNull_NullPointerException() { 139 | assertThatExceptionOfType(NullPointerException.class) 140 | .isThrownBy(new ThrowableAssert.ThrowingCallable() { 141 | @Override 142 | public void call() throws Throwable { 143 | randomKafkaMetricContainer().metricName(null); 144 | } 145 | }); 146 | } 147 | 148 | protected MetricName constMetricName() { 149 | return new MetricName(this.metricName, this.metricGroup); 150 | } 151 | 152 | protected Metric randomMetric() { 153 | return randomMetric(constMetricName()); 154 | } 155 | 156 | protected Metric randomMetric(MetricName name) { 157 | Metric metric = mock(Metric.class); 158 | given(metric.value()).willReturn(this.random.nextDouble()); 159 | given(metric.metricName()).willReturn(name); 160 | return metric; 161 | } 162 | 163 | protected KafkaMetricContainer randomKafkaMetricContainer(MetricName name) { 164 | return new KafkaMetricContainer(randomMetric(name), this.metricPrefix); 165 | } 166 | 167 | protected KafkaMetricContainer randomKafkaMetricContainer() { 168 | return randomKafkaMetricContainer(constMetricName()); 169 | } 170 | 171 | } 172 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/boot/actuate/metrics/kafka/KafkaStatisticsProviderTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 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 | 17 | package org.springframework.boot.actuate.metrics.kafka; 18 | 19 | import static org.assertj.core.api.Assertions.assertThat; 20 | import static org.mockito.BDDMockito.mock; 21 | import static org.mockito.BDDMockito.willAnswer; 22 | import static org.mockito.Matchers.anyDouble; 23 | import static org.mockito.Matchers.anyString; 24 | 25 | import java.util.Arrays; 26 | import java.util.HashMap; 27 | import java.util.Map; 28 | import java.util.Random; 29 | import java.util.UUID; 30 | import java.util.concurrent.CountDownLatch; 31 | import java.util.concurrent.Executors; 32 | import java.util.concurrent.ScheduledExecutorService; 33 | import java.util.concurrent.TimeUnit; 34 | 35 | import org.apache.kafka.common.MetricName; 36 | import org.apache.kafka.common.metrics.KafkaMetric; 37 | import org.junit.After; 38 | import org.junit.Before; 39 | import org.junit.Test; 40 | import org.junit.runner.RunWith; 41 | import org.mockito.invocation.InvocationOnMock; 42 | import org.mockito.stubbing.Answer; 43 | import org.powermock.api.mockito.PowerMockito; 44 | import org.powermock.core.classloader.annotations.PrepareForTest; 45 | import org.powermock.modules.junit4.PowerMockRunner; 46 | import org.slf4j.Logger; 47 | import org.slf4j.LoggerFactory; 48 | 49 | import org.springframework.boot.actuate.metrics.GaugeService; 50 | 51 | /** 52 | * Tests for {@link KafkaStatisticsProvider}. It's a single point of using PowerMock, as 53 | * it was required to mock final {@link KafkaMetric} for testing of my implementation of 54 | * {@link org.apache.kafka.common.metrics.MetricsReporter}, which surprisingly depends on 55 | * {@link KafkaMetric} instead of {@link org.apache.kafka.common.Metric} interface. The 56 | * same is reported in KAFKA-3923. 57 | * 58 | * @author Igor Stepanov 59 | * @see MetricReporter 60 | * interface depends on final class KafkaMetric instead of Metric interface 61 | */ 62 | @RunWith(PowerMockRunner.class) 63 | @PrepareForTest(KafkaMetric.class) 64 | public class KafkaStatisticsProviderTests { 65 | 66 | private static final Logger LOGGER = LoggerFactory 67 | .getLogger(KafkaStatisticsProvider.class); 68 | 69 | private Random random; 70 | 71 | private String metricGroup; 72 | 73 | private long updateInterval; 74 | 75 | private CountDownLatch latch; 76 | 77 | private GaugeService gaugeService; 78 | 79 | private KafkaStatisticsProvider simpleProvider; 80 | 81 | public KafkaStatisticsProviderTests() { 82 | this.random = new Random(); 83 | this.metricGroup = "some_dummy_value"; 84 | this.updateInterval = 500; 85 | } 86 | 87 | @Before 88 | public void setUp() { 89 | this.simpleProvider = new KafkaStatisticsProvider(); 90 | Map config = new HashMap<>(); 91 | this.gaugeService = mockGaugeService(); 92 | config.put(KafkaStatisticsProvider.METRICS_GAUGE_SERVICE_IMPL, this.gaugeService); 93 | config.put(KafkaStatisticsProvider.METRICS_UPDATE_INTERVAL_PARAM, 94 | this.updateInterval); 95 | this.simpleProvider.configure(config); 96 | } 97 | 98 | @After 99 | public void tearDown() { 100 | this.simpleProvider.close(); 101 | } 102 | 103 | /** 104 | * Initial set of {@link KafkaMetric} instances starts being tracked with 105 | * {@link GaugeService} periodically. Each metric updates {@link GaugeService} 106 | * separately. 107 | */ 108 | @Test 109 | public void init_mockMetrics() throws InterruptedException { 110 | this.latch = new CountDownLatch(2); 111 | this.simpleProvider.init(Arrays.asList(randomMetric(), randomMetric())); 112 | assertThat(this.latch.getCount()).isEqualTo(2); 113 | assertThat(this.latch.await(2 * this.updateInterval, TimeUnit.MILLISECONDS)) 114 | .isTrue(); 115 | } 116 | 117 | /** 118 | * After adding new {@link KafkaMetric}, it's value is pushed to {@link GaugeService} 119 | * periodically. 120 | */ 121 | @Test 122 | public void metricChange_mockMetric() throws InterruptedException { 123 | this.latch = new CountDownLatch(1); 124 | this.simpleProvider.metricChange(randomMetric()); 125 | assertThat(this.latch.getCount()).isEqualTo(1); 126 | assertThat(this.latch.await(2 * this.updateInterval, TimeUnit.MILLISECONDS)) 127 | .isTrue(); 128 | } 129 | 130 | /** 131 | * If {@link KafkaMetric} is removed, {@link GaugeService} is not triggered anymore. 132 | */ 133 | @Test 134 | public void metricRemoval_mockMetric() throws InterruptedException { 135 | KafkaMetric metric = randomMetric(); 136 | this.simpleProvider.metricChange(metric); 137 | this.latch = new CountDownLatch(1); 138 | this.simpleProvider.metricRemoval(metric); 139 | assertThat(this.latch.getCount()).isEqualTo(1); 140 | assertThat(this.latch.await(2 * this.updateInterval, TimeUnit.MILLISECONDS)) 141 | .isFalse(); 142 | } 143 | 144 | @Test 145 | public void close_externalExecutors() { 146 | KafkaStatisticsProvider customProvider = new KafkaStatisticsProvider(); 147 | Map config = new HashMap<>(); 148 | config.put(KafkaStatisticsProvider.METRICS_GAUGE_SERVICE_IMPL, 149 | mockGaugeService()); 150 | try { 151 | config.put(KafkaStatisticsProvider.METRICS_UPDATE_EXECUTOR_IMPL, 152 | Executors.newSingleThreadScheduledExecutor()); 153 | config.put(KafkaStatisticsProvider.METRICS_UPDATE_INTERVAL_PARAM, 154 | this.updateInterval); 155 | customProvider.configure(config); 156 | assertThat(customProvider.executorService.isShutdown()).isFalse(); 157 | customProvider.close(); 158 | assertThat(customProvider.executorService.isShutdown()).isFalse(); 159 | } 160 | finally { 161 | try { 162 | customProvider.executorService.shutdown(); 163 | assertThat(customProvider.executorService.isShutdown()).isTrue(); 164 | } 165 | catch (Exception ex) { 166 | LOGGER.error("Failed to shutdown executor", ex); 167 | } 168 | } 169 | } 170 | 171 | /** 172 | * If {@link ScheduledExecutorService} is initialized specifically for current 173 | * {@link KafkaStatisticsProvider}, it should be stopped upon closing. 174 | */ 175 | @Test 176 | public void close_internalExecutors() { 177 | assertThat(this.simpleProvider.executorService.isShutdown()).isFalse(); 178 | this.simpleProvider.close(); 179 | assertThat(this.simpleProvider.executorService.isShutdown()).isTrue(); 180 | } 181 | 182 | /** 183 | * Reference to {@link GaugeService} is set properly with provided value. 184 | */ 185 | @Test 186 | public void configure_gaugeService() { 187 | assertThat(this.simpleProvider.gaugeService).isSameAs(gaugeService); 188 | } 189 | 190 | /** 191 | * Value of updateInterval is set properly with provided value. 192 | */ 193 | @Test 194 | public void configure_customUpdateInterval() { 195 | assertThat(this.simpleProvider.updateInterval).isEqualTo(updateInterval); 196 | } 197 | 198 | /** 199 | * Value of updateInterval is set properly with default value. 200 | */ 201 | @Test 202 | public void configure_defaultUpdateInterval() { 203 | KafkaStatisticsProvider customProvider = new KafkaStatisticsProvider(); 204 | Map config = new HashMap<>(); 205 | config.put(KafkaStatisticsProvider.METRICS_GAUGE_SERVICE_IMPL, 206 | mockGaugeService()); 207 | customProvider.configure(config); 208 | assertThat(customProvider.updateInterval) 209 | .isEqualTo(KafkaStatisticsProvider.METRICS_UPDATE_INTERVAL_DEFAULT); 210 | } 211 | 212 | /** 213 | * Reference to {@link ScheduledExecutorService} is set properly with provided value. 214 | */ 215 | @Test 216 | public void configure_customExecutorService() { 217 | ScheduledExecutorService scheduledExecutorService = Executors 218 | .newSingleThreadScheduledExecutor(); 219 | KafkaStatisticsProvider customProvider = new KafkaStatisticsProvider(); 220 | Map config = new HashMap<>(); 221 | config.put(KafkaStatisticsProvider.METRICS_UPDATE_EXECUTOR_IMPL, 222 | scheduledExecutorService); 223 | customProvider.configure(config); 224 | assertThat(customProvider.executorService).isSameAs(scheduledExecutorService); 225 | } 226 | 227 | /** 228 | * Value of prefix is set properly with provided value. 229 | */ 230 | @Test 231 | public void configure_customPrefix() { 232 | String customPrefix = "test_value_for_prefix"; 233 | KafkaStatisticsProvider customProvider = new KafkaStatisticsProvider(); 234 | Map config = new HashMap<>(); 235 | config.put(KafkaStatisticsProvider.METRICS_PREFIX_PARAM, customPrefix); 236 | customProvider.configure(config); 237 | assertThat(customProvider.prefix).isEqualTo(customPrefix); 238 | } 239 | 240 | private GaugeService mockGaugeService() { 241 | GaugeService gauge = mock(GaugeService.class); 242 | willAnswer(new Answer() { 243 | public Void answer(InvocationOnMock invocation) { 244 | Object[] args = invocation.getArguments(); 245 | LOGGER.info("Called GaugeService.submit with arguments: {}", 246 | Arrays.toString(args)); 247 | KafkaStatisticsProviderTests.this.latch.countDown(); 248 | return null; 249 | } 250 | }).given(gauge).submit(anyString(), anyDouble()); 251 | return gauge; 252 | } 253 | 254 | protected MetricName randomMetricName() { 255 | return new MetricName(UUID.randomUUID().toString(), this.metricGroup); 256 | } 257 | 258 | protected KafkaMetric randomMetric() { 259 | return randomMetric(randomMetricName()); 260 | } 261 | 262 | protected KafkaMetric randomMetric(MetricName name) { 263 | KafkaMetric metric = PowerMockito.mock(KafkaMetric.class); 264 | PowerMockito.when(metric.value()).thenReturn(this.random.nextDouble()); 265 | PowerMockito.when(metric.metricName()).thenReturn(name); 266 | return metric; 267 | } 268 | 269 | } 270 | --------------------------------------------------------------------------------