/** * Helper function to easily create a composite for all monitor fields and * annotated attributes of a given object. * * @param id a unique id associated with this particular instance of the * object. If multiple objects of the same class are registered * they will have the same config and conflict unless the id * values are distinct. * @param obj object to search for monitors on. All fields of type * {@link Monitor} and fields/methods with a * {@link com.netflix.servo.annotations.Monitor} annotation * will be extracted and returned using * {@link CompositeMonitor#getMonitors()}. * @return composite monitor based on the fields of the class */ public static CompositeMonitor<?> newObjectMonitor(String id, Object obj) { final TagList tags = getMonitorTags(obj); List<Monitor<?>> monitors = new ArrayList<>(); addMonitors(monitors, id, tags, obj); final Class<?> c = obj.getClass(); final String objectId = (id == null) ? DEFAULT_ID : id; return new BasicCompositeMonitor(newObjectConfig(c, objectId, tags), monitors); }
@Test public void testAddMonitorsAnon() throws Exception { List<Monitor<?>> monitors = new ArrayList<>(); ClassWithMonitors obj = new ClassWithMonitors() { final Counter c1 = Monitors.newCounter("publicCounter"); @com.netflix.servo.annotations.Monitor( name = "primitiveGauge", type = DataSourceType.GAUGE) static final long A1 = 0L; }; TagList tags = BasicTagList.of("abc", "def"); Monitors.addMonitors(monitors, null, tags, obj); assertEquals(monitors.size(), 10); for (Monitor m : monitors) { assertEquals(m.getConfig().getTags().getValue("class"), "MonitorsTest", String.format("%s should have class MonitorsTest", m.getConfig().getName())); } }
/** * Helper function to easily create a composite for all monitor fields and * annotated attributes of a given object. * * @param id a unique id associated with this particular instance of the * object. If multiple objects of the same class are registered * they will have the same config and conflict unless the id * values are distinct. * @param obj object to search for monitors on. All fields of type * {@link Monitor} and fields/methods with a * {@link com.netflix.servo.annotations.Monitor} annotation * will be extracted and returned using * {@link CompositeMonitor#getMonitors()}. * @return composite monitor based on the fields of the class */ public static CompositeMonitor<?> newObjectMonitor(String id, Object obj) { final TagList tags = getMonitorTags(obj); List<Monitor<?>> monitors = new ArrayList<>(); addMonitors(monitors, id, tags, obj); final Class<?> c = obj.getClass(); final String objectId = (id == null) ? DEFAULT_ID : id; return new BasicCompositeMonitor(newObjectConfig(c, objectId, tags), monitors); }