-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add JMX metric for commandRunner status (#4019)
* feat: add metric for commandRunner status * separate metric into separate class and added tests * add clock
- Loading branch information
1 parent
cc7e4ee
commit 55d75f2
Showing
7 changed files
with
341 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...pp/src/main/java/io/confluent/ksql/rest/server/computation/CommandRunnerStatusMetric.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright 2019 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (the "License"); you may not use | ||
* this file except in compliance with the License. You may obtain a copy of the | ||
* License at | ||
* | ||
* http://www.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.rest.server.computation; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import io.confluent.ksql.metrics.MetricCollectors; | ||
import io.confluent.ksql.util.KsqlConstants; | ||
|
||
import java.io.Closeable; | ||
import java.util.Collections; | ||
import java.util.Objects; | ||
|
||
import org.apache.kafka.common.MetricName; | ||
import org.apache.kafka.common.metrics.Gauge; | ||
import org.apache.kafka.common.metrics.Metrics; | ||
|
||
/** | ||
* Emits a JMX metric that indicates the health of the CommandRunner thread. | ||
*/ | ||
public class CommandRunnerStatusMetric implements Closeable { | ||
|
||
private static final String DEFAULT_METRIC_GROUP_PREFIX = "ksql-rest-app"; | ||
private static final String METRIC_GROUP_POST_FIX = "-command-runner"; | ||
|
||
private final Metrics metrics; | ||
private final MetricName metricName; | ||
private final String metricGroupName; | ||
|
||
CommandRunnerStatusMetric( | ||
final String ksqlServiceId, | ||
final CommandRunner commandRunner, | ||
final String metricGroupPrefix | ||
) { | ||
this( | ||
MetricCollectors.getMetrics(), | ||
commandRunner, | ||
ksqlServiceId, | ||
metricGroupPrefix.isEmpty() ? DEFAULT_METRIC_GROUP_PREFIX : metricGroupPrefix | ||
); | ||
} | ||
|
||
@VisibleForTesting | ||
CommandRunnerStatusMetric( | ||
final Metrics metrics, | ||
final CommandRunner commandRunner, | ||
final String ksqlServiceId, | ||
final String metricsGroupPrefix | ||
) { | ||
this.metrics = Objects.requireNonNull(metrics, "metrics"); | ||
this.metricGroupName = metricsGroupPrefix + METRIC_GROUP_POST_FIX; | ||
this.metricName = metrics.metricName( | ||
"status", | ||
KsqlConstants.KSQL_INTERNAL_TOPIC_PREFIX + ksqlServiceId + metricGroupName, | ||
"The status of the commandRunner thread as it processes the command topic.", | ||
Collections.emptyMap() | ||
); | ||
|
||
this.metrics.addMetric(metricName, (Gauge<String>) | ||
(config, now) -> commandRunner.checkCommandRunnerStatus().name()); | ||
} | ||
|
||
/** | ||
* Close the metric | ||
*/ | ||
@Override | ||
public void close() { | ||
metrics.removeMetric(metricName); | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
...rc/test/java/io/confluent/ksql/rest/server/computation/CommandRunnerStatusMetricTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright 2019 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (the "License"); you may not use | ||
* this file except in compliance with the License. You may obtain a copy of the | ||
* License at | ||
* | ||
* http://www.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.rest.server.computation; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyMap; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.ArgumentMatchers.isA; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import org.apache.kafka.common.MetricName; | ||
import org.apache.kafka.common.metrics.Gauge; | ||
import org.apache.kafka.common.metrics.Metrics; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.Captor; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import java.util.Collections; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class CommandRunnerStatusMetricTest { | ||
|
||
private static final MetricName METRIC_NAME = | ||
new MetricName("bob", "g1", "d1", ImmutableMap.of()); | ||
private static final String KSQL_SERVICE_ID = "kcql-1-"; | ||
|
||
@Mock | ||
private Metrics metrics; | ||
@Mock | ||
private CommandRunner commandRunner; | ||
@Captor | ||
private ArgumentCaptor<Gauge<String>> gaugeCaptor; | ||
|
||
private CommandRunnerStatusMetric commandRunnerStatusMetric; | ||
|
||
@Before | ||
public void setUp() { | ||
when(metrics.metricName(any(), any(), any(), anyMap())).thenReturn(METRIC_NAME); | ||
when(commandRunner.checkCommandRunnerStatus()).thenReturn(CommandRunner.CommandRunnerStatus.RUNNING); | ||
|
||
commandRunnerStatusMetric = new CommandRunnerStatusMetric(metrics, commandRunner, KSQL_SERVICE_ID, "rest"); | ||
} | ||
|
||
@Test | ||
public void shouldAddMetricOnCreation() { | ||
// When: | ||
// Listener created in setup | ||
|
||
// Then: | ||
verify(metrics).metricName("status", "_confluent-ksql-kcql-1-rest-command-runner", | ||
"The status of the commandRunner thread as it processes the command topic.", | ||
Collections.emptyMap()); | ||
|
||
verify(metrics).addMetric(eq(METRIC_NAME), isA(Gauge.class)); | ||
} | ||
|
||
@Test | ||
public void shouldInitiallyBeRunningState() { | ||
// When: | ||
// CommandRunnerStatusMetric created in setup | ||
|
||
// Then: | ||
assertThat(currentGaugeValue(), is(CommandRunner.CommandRunnerStatus.RUNNING.name())); | ||
} | ||
|
||
@Test | ||
public void shouldUpdateToErrorState() { | ||
// When: | ||
when(commandRunner.checkCommandRunnerStatus()).thenReturn(CommandRunner.CommandRunnerStatus.ERROR); | ||
|
||
// Then: | ||
assertThat(currentGaugeValue(), is(CommandRunner.CommandRunnerStatus.ERROR.name())); | ||
} | ||
|
||
@Test | ||
public void shouldRemoveMetricOnClose() { | ||
// When: | ||
commandRunnerStatusMetric.close(); | ||
|
||
// Then: | ||
verify(metrics).removeMetric(METRIC_NAME); | ||
} | ||
|
||
private String currentGaugeValue() { | ||
verify(metrics).addMetric(any(), gaugeCaptor.capture()); | ||
return gaugeCaptor.getValue().value(null, 0L); | ||
} | ||
} |
Oops, something went wrong.