Skip to content

Commit 0a3c21d

Browse files
authored
switch to no op monitor registry by default (Netflix#466)
Internally Servo is deprecated and delegates to Spectator. This change would reduce the overhead for things that are still using the Servo APIs. The main benefit is that the data would no longer be exported to JMX. Note, this could be quite disruptive to anyone else using Servo. Since it is all set up via system properties, there isn't a convenient way to only change the default we use internally. After making this change, I noticed that loading the JMX monitor registry explicitly using the property no longer worked. That has been fixed so the old behavior can be achieved using a system property.
1 parent 6480bc4 commit 0a3c21d

File tree

4 files changed

+71
-23
lines changed

4 files changed

+71
-23
lines changed

servo-core/src/main/java/com/netflix/servo/DefaultMonitorRegistry.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
* {@code com.netflix.servo.DefaultMonitorRegistry.registryClass} property. The
3030
* specified registry class must have a constructor with no arguments. If the
3131
* property is not specified or the class cannot be loaded an instance of
32-
* {@link com.netflix.servo.jmx.JmxMonitorRegistry} will be used.
32+
* {@link com.netflix.servo.NoopMonitorRegistry} will be used.
3333
* <p/>
34-
* If the default {@link com.netflix.servo.jmx.JmxMonitorRegistry} is used, the property
34+
* If using {@link com.netflix.servo.jmx.JmxMonitorRegistry}, the property
3535
* {@code com.netflix.servo.DefaultMonitorRegistry.jmxMapperClass} can optionally be
3636
* specified to control how monitors are mapped to JMX {@link javax.management.ObjectName}.
3737
* This property specifies the {@link com.netflix.servo.jmx.ObjectNameMapper}
@@ -70,24 +70,27 @@ public static MonitorRegistry getInstance() {
7070
*/
7171
DefaultMonitorRegistry(Properties props) {
7272
final String className = props.getProperty(REGISTRY_CLASS_PROP);
73-
final String registryName = props.getProperty(REGISTRY_NAME_PROP, DEFAULT_REGISTRY_NAME);
7473
if (className != null) {
7574
MonitorRegistry r;
76-
try {
77-
Class<?> c = Class.forName(className);
78-
r = (MonitorRegistry) c.newInstance();
79-
} catch (Throwable t) {
80-
LOG.error(
81-
"failed to create instance of class " + className + ", "
82-
+ "using default class "
83-
+ JmxMonitorRegistry.class.getName(),
84-
t);
85-
r = new JmxMonitorRegistry(registryName);
75+
if (className.equals(JmxMonitorRegistry.class.getName())) {
76+
final String registryName = props.getProperty(REGISTRY_NAME_PROP, DEFAULT_REGISTRY_NAME);
77+
r = new JmxMonitorRegistry(registryName, getObjectNameMapper(props));
78+
} else {
79+
try {
80+
Class<?> c = Class.forName(className);
81+
r = (MonitorRegistry) c.newInstance();
82+
} catch (Throwable t) {
83+
LOG.error(
84+
"failed to create instance of class " + className + ", "
85+
+ "using default class "
86+
+ NoopMonitorRegistry.class.getName(),
87+
t);
88+
r = new NoopMonitorRegistry();
89+
}
8690
}
8791
registry = r;
8892
} else {
89-
registry = new JmxMonitorRegistry(registryName,
90-
getObjectNameMapper(props));
93+
registry = new NoopMonitorRegistry();
9194
}
9295
}
9396

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2020 Netflix, Inc.
3+
* <p/>
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+
* <p/>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p/>
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+
package com.netflix.servo;
17+
18+
import com.netflix.servo.monitor.Monitor;
19+
20+
import java.util.Collection;
21+
import java.util.Collections;
22+
23+
/**
24+
* Monitor registry implementation that does as little as possible.
25+
*/
26+
public class NoopMonitorRegistry implements MonitorRegistry {
27+
28+
/** Create a new instance. */
29+
public NoopMonitorRegistry() {
30+
}
31+
32+
@Override
33+
public Collection<Monitor<?>> getRegisteredMonitors() {
34+
return Collections.emptyList();
35+
}
36+
37+
@Override
38+
public void register(Monitor<?> monitor) {
39+
}
40+
41+
@Override
42+
public void unregister(Monitor<?> monitor) {
43+
}
44+
45+
@Override
46+
public boolean isRegistered(Monitor<?> monitor) {
47+
return false;
48+
}
49+
}

servo-core/src/test/java/com/netflix/servo/DefaultMonitorRegistryTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public class DefaultMonitorRegistryTest {
3232
@Test
3333
public void testCustomJmxObjectMapper() {
3434
Properties props = new Properties();
35+
props.put("com.netflix.servo.DefaultMonitorRegistry.registryClass",
36+
"com.netflix.servo.jmx.JmxMonitorRegistry");
3537
props.put("com.netflix.servo.DefaultMonitorRegistry.jmxMapperClass",
3638
"com.netflix.servo.DefaultMonitorRegistryTest$ChangeDomainMapper");
3739
DefaultMonitorRegistry registry = new DefaultMonitorRegistry(props);
@@ -47,6 +49,8 @@ public void testCustomJmxObjectMapper() {
4749
@Test
4850
public void testInvalidMapperDefaults() {
4951
Properties props = new Properties();
52+
props.put("com.netflix.servo.DefaultMonitorRegistry.registryClass",
53+
"com.netflix.servo.jmx.JmxMonitorRegistry");
5054
props.put("com.netflix.servo.DefaultMonitorRegistry.jmxMapperClass",
5155
"com.my.invalid.class");
5256
DefaultMonitorRegistry registry = new DefaultMonitorRegistry(props);

servo-core/src/test/java/com/netflix/servo/monitor/MonitorsTest.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,6 @@ public void testNewObjectMonitorWithParentClass() throws Exception {
9898
assertEquals(monitors.size(), 10);
9999
}
100100

101-
@Test
102-
public void testIsRegistered() throws Exception {
103-
ClassWithMonitors obj = new ClassWithMonitors();
104-
assertFalse(Monitors.isObjectRegistered("id", obj));
105-
Monitors.registerObject("id", obj);
106-
assertTrue(Monitors.isObjectRegistered("id", obj));
107-
}
108-
109101
@Test
110102
public void testNewObjectConfig() throws Exception {
111103
ClassWithMonitors obj = new ClassWithMonitors() {

0 commit comments

Comments
 (0)