Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,12 @@ public void removeSensor(String name) {
removeMetric(metric.metricName());
log.debug("Removed sensor with name {}", name);
childSensors = childrenSensors.remove(sensor);
for (final Sensor parent : sensor.parents()) {
final List<Sensor> peers = childrenSensors.get(parent);
if (peers != null) {
peers.remove(sensor);
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
import org.apache.kafka.common.utils.Utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableList;

/**
* A sensor applies a continuous sequence of numerical values to a set of associated metrics. For example a sensor on
* message size would record a sequence of message sizes using the {@link #record(double)} api and would maintain a set
Expand Down Expand Up @@ -132,6 +134,10 @@ public String name() {
return this.name;
}

List<Sensor> parents() {
return unmodifiableList(asList(parents));
}

/**
* Record an occurrence, this is just short-hand for {@link #record(double) record(1.0)}
*/
Expand Down Expand Up @@ -267,6 +273,6 @@ public boolean hasExpired() {
}

synchronized List<KafkaMetric> metrics() {
return Collections.unmodifiableList(this.metrics);
return unmodifiableList(this.metrics);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.kafka.common.metrics;

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -177,6 +179,20 @@ public void testBadSensorHierarchy() {
metrics.sensor("gc", c1, c2); // should fail
}

@Test
public void testRemoveChildSensor() {
final Metrics metrics = new Metrics();

final Sensor parent = metrics.sensor("parent");
final Sensor child = metrics.sensor("child", parent);

assertEquals(singletonList(child), metrics.childrenSensors().get(parent));

metrics.removeSensor("child");

assertEquals(emptyList(), metrics.childrenSensors().get(parent));
}

@Test
public void testRemoveSensor() {
int size = metrics.metrics().size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,9 @@ public void removeSensor(Sensor sensor) {
Objects.requireNonNull(sensor, "Sensor is null");
metrics.removeSensor(sensor.name());

final Sensor parent = parentSensors.get(sensor);
final Sensor parent = parentSensors.remove(sensor);
if (parent != null) {
metrics.removeSensor(parent.name());
parentSensors.remove(sensor);
}

}
Expand Down