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/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 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------