Skip to content

Commit 92c620a

Browse files
committed
add NoopSensor
1 parent 38d329e commit 92c620a

4 files changed

Lines changed: 142 additions & 1 deletion

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
group=io.tehuti
22
archivesBaseName=tehuti
3-
version=0.12.4
3+
version=0.12.5
44

55
signing.enabled=false
66
signing.keyId=

src/main/java/io/tehuti/metrics/MetricsRepository.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ public synchronized Sensor sensor(String name, MetricConfig config, Sensor... pa
142142
return s;
143143
}
144144

145+
/**
146+
* Create a {@link NoopSensor} with the given name.
147+
*/
148+
public Sensor getNoopSensor(String name) {
149+
return new NoopSensor(this, name, null, this.config, time);
150+
}
151+
145152
/**
146153
* Remove a sensor with the given unique name. Unregister all metrics with this sensor too.
147154
* @param name The name of the sensor
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
3+
* file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
4+
* to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
5+
* License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package io.tehuti.metrics;
14+
15+
import io.tehuti.Metric;
16+
import io.tehuti.utils.Time;
17+
import java.util.Collections;
18+
import java.util.Map;
19+
20+
21+
/**
22+
* A sensor that does nothing.
23+
*/
24+
public class NoopSensor extends Sensor {
25+
NoopSensor(MetricsRepository registry, String name, Sensor[] parents, MetricConfig config, Time time) {
26+
super(registry, name, parents, config, time);
27+
}
28+
29+
@Override
30+
public void record() {
31+
// NOOP
32+
}
33+
34+
@Override
35+
public void record(double value) {
36+
// NOOP
37+
}
38+
39+
@Override
40+
public void record(double value, long timeMs) {
41+
//NOOP
42+
}
43+
44+
@Override
45+
public Map<String, Metric> add(CompoundStat stat, MetricConfig config) {
46+
return Collections.emptyMap();
47+
}
48+
49+
@Override
50+
public Metric add(String name, String description, MeasurableStat stat, MetricConfig config) {
51+
return null;
52+
}
53+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package io.tehuti.metrics.stats;
2+
3+
import io.tehuti.Metric;
4+
import io.tehuti.metrics.JmxReporter;
5+
import io.tehuti.metrics.MetricConfig;
6+
import io.tehuti.metrics.MetricsReporter;
7+
import io.tehuti.metrics.MetricsRepository;
8+
import io.tehuti.metrics.Sensor;
9+
import io.tehuti.utils.MockTime;
10+
import java.util.Arrays;
11+
import org.junit.Test;
12+
13+
import static org.junit.Assert.assertEquals;
14+
import static org.junit.Assert.assertNull;
15+
import static org.junit.Assert.assertTrue;
16+
17+
/**
18+
* Unit tests for {@link io.tehuti.metrics.NoopSensor}.
19+
*/
20+
public class NoopSensorTest {
21+
22+
@Test
23+
public void testNoopSensorIsNotRegisteredAndHasName() {
24+
MockTime time = new MockTime();
25+
MetricConfig config = new MetricConfig();
26+
MetricsRepository repo = new MetricsRepository(config, Arrays.asList((MetricsReporter) new JmxReporter()), time);
27+
28+
Sensor noop = repo.getNoopSensor("noop");
29+
30+
// Name should be set
31+
assertEquals("noop", noop.name());
32+
// NoopSensor is not tracked/registered in the repository
33+
assertNull(repo.getSensor("noop"));
34+
// Repository should have no metrics
35+
assertTrue(repo.metrics().isEmpty());
36+
}
37+
38+
@Test
39+
public void testRecordDoesNothing() {
40+
MockTime time = new MockTime();
41+
MetricConfig config = new MetricConfig();
42+
MetricsRepository repo = new MetricsRepository(config, Arrays.asList((MetricsReporter) new JmxReporter()), time);
43+
44+
Sensor noop = repo.getNoopSensor("noop");
45+
46+
// Invoke all record variants; should be no-ops and not throw
47+
noop.record();
48+
noop.record(1.23);
49+
noop.record(4.56, time.milliseconds());
50+
51+
// Still no metrics registered/affected
52+
assertTrue(repo.metrics().isEmpty());
53+
}
54+
55+
@Test
56+
public void testAddMethodsReturnNullOrEmptyAndDoNotRegisterMetrics() {
57+
MockTime time = new MockTime();
58+
MetricConfig config = new MetricConfig();
59+
MetricsRepository repo = new MetricsRepository(config, Arrays.asList((MetricsReporter) new JmxReporter()), time);
60+
61+
Sensor noop = repo.getNoopSensor("noop");
62+
63+
// MeasurableStat adds should return null and not register metrics
64+
Metric m1 = noop.add("test.avg", new Avg());
65+
Metric m2 = noop.add("test.avg2", "desc", new Avg());
66+
Metric m3 = noop.add("test.avg3", new Avg(), new MetricConfig());
67+
assertNull(m1);
68+
assertNull(m2);
69+
assertNull(m3);
70+
71+
// CompoundStat adds should return empty map and not register metrics
72+
Percentiles percs = new Percentiles(10, 0.0, 100.0, Percentiles.BucketSizing.CONSTANT,
73+
new Percentile("noop.p50", 50.0));
74+
assertTrue(noop.add(percs, null).isEmpty());
75+
76+
// Repository should still have no metrics registered
77+
assertTrue(repo.metrics().isEmpty());
78+
assertNull(repo.getMetric("test.avg"));
79+
assertNull(repo.getMetric("noop.p50"));
80+
}
81+
}

0 commit comments

Comments
 (0)