forked from opensource4you/astraea
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Kafka controller metrics (opensource4you#554)
- Loading branch information
Showing
7 changed files
with
214 additions
and
10 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
app/src/main/java/org/astraea/app/metrics/broker/ControllerMetrics.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,87 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.astraea.app.metrics.broker; | ||
|
||
import java.util.Arrays; | ||
import org.astraea.app.metrics.BeanObject; | ||
import org.astraea.app.metrics.BeanQuery; | ||
import org.astraea.app.metrics.MBeanClient; | ||
|
||
public class ControllerMetrics { | ||
public enum Controller { | ||
ACTIVE_CONTROLLER_COUNT("ActiveControllerCount"), | ||
OFFLINE_PARTITIONS_COUNT("OfflinePartitionsCount"), | ||
PREFERRED_REPLICA_IMBALANCE_COUNT("PreferredReplicaImbalanceCount"), | ||
CONTROLLER_STATE("ControllerState"), | ||
GLOBAL_TOPIC_COUNT("GlobalTopicCount"), | ||
GLOBAL_PARTITION_COUNT("GlobalPartitionCount"), | ||
TOPICS_TO_DELETE_COUNT("TopicsToDeleteCount"), | ||
REPLICAS_TO_DELETE_COUNT("ReplicasToDeleteCount"), | ||
TOPICS_INELIGIBLE_TO_DELETE_COUNT("TopicsIneligibleToDeleteCount"), | ||
REPLICAS_INELIGIBLE_TO_DELETE_COUNT("ReplicasIneligibleToDeleteCount"), | ||
ACTIVE_BROKER_COUNT("ActiveBrokerCount"), | ||
FENCED_BROKER_COUNT("FencedBrokerCount"); | ||
|
||
static Controller of(String metricName) { | ||
return Arrays.stream(Controller.values()) | ||
.filter(metric -> metric.metricName().equalsIgnoreCase(metricName)) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalArgumentException("No such metric: " + metricName)); | ||
} | ||
|
||
private final String metricName; | ||
|
||
Controller(String metricName) { | ||
this.metricName = metricName; | ||
} | ||
|
||
public String metricName() { | ||
return metricName; | ||
} | ||
|
||
public Meter fetch(MBeanClient mBeanClient) { | ||
return new Meter( | ||
mBeanClient.queryBean( | ||
BeanQuery.builder() | ||
.domainName("kafka.controller") | ||
.property("type", "KafkaController") | ||
.property("name", this.metricName()) | ||
.build())); | ||
} | ||
|
||
public static class Meter implements HasValue { | ||
private final BeanObject beanObject; | ||
|
||
public Meter(BeanObject beanObject) { | ||
this.beanObject = beanObject; | ||
} | ||
|
||
public String metricsName() { | ||
return beanObject().properties().get("name"); | ||
} | ||
|
||
public Controller type() { | ||
return Controller.of(metricsName()); | ||
} | ||
|
||
@Override | ||
public BeanObject beanObject() { | ||
return beanObject; | ||
} | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
app/src/test/java/org/astraea/app/metrics/MetricsTestUtil.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,30 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.astraea.app.metrics; | ||
|
||
import java.util.Arrays; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
public class MetricsTestUtil { | ||
|
||
public static <T extends Enum<T>> boolean metricDistinct( | ||
T[] tEnums, Function<T, String> getMetricName) { | ||
var set = Arrays.stream(tEnums).map(getMetricName).collect(Collectors.toSet()); | ||
return set.size() == tEnums.length; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
app/src/test/java/org/astraea/app/metrics/broker/ControllerMetricsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.astraea.app.metrics.broker; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.util.Arrays; | ||
import java.util.Locale; | ||
import org.astraea.app.metrics.MBeanClient; | ||
import org.astraea.app.metrics.MetricsTestUtil; | ||
import org.astraea.app.service.RequireSingleBrokerCluster; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.EnumSource; | ||
|
||
class ControllerMetricsTest extends RequireSingleBrokerCluster { | ||
|
||
@ParameterizedTest | ||
@EnumSource(ControllerMetrics.Controller.class) | ||
void testController(ControllerMetrics.Controller controller) { | ||
var meter = controller.fetch(MBeanClient.local()); | ||
Assertions.assertTrue(meter.value() >= 0); | ||
Assertions.assertEquals(controller, meter.type()); | ||
} | ||
|
||
@Test | ||
void testKafkaMetricsOf() { | ||
Arrays.stream(ControllerMetrics.Controller.values()) | ||
.forEach( | ||
t -> { | ||
Assertions.assertEquals( | ||
t, ControllerMetrics.Controller.of(t.metricName().toLowerCase(Locale.ROOT))); | ||
Assertions.assertEquals( | ||
t, ControllerMetrics.Controller.of(t.metricName().toUpperCase(Locale.ROOT))); | ||
}); | ||
|
||
assertThrows(IllegalArgumentException.class, () -> ControllerMetrics.Controller.of("nothing")); | ||
} | ||
|
||
@Test | ||
void testAllEnumNameUnique() { | ||
Assertions.assertTrue( | ||
MetricsTestUtil.metricDistinct( | ||
ControllerMetrics.Controller.values(), ControllerMetrics.Controller::metricName)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters