public static <T> boolean register(Monitor<T> monitor) { try { DefaultMonitorRegistry.getInstance().register(monitor); } catch (Exception e) { logger.warn("Cannot register monitor {}", monitor.getConfig().getName()); if (logger.isDebugEnabled()) { logger.debug(e.getMessage(), e); } return false; } return true; }
@Override public void toJson(JsonGenerator gen) throws IOException { gen.writeStartObject(); gen.writeObjectFieldStart("tags"); gen.writeStringField("name", config.getName()); for (Tag tag : config.getTags()) { ValidCharacters.tagToJson(gen, tag); } gen.writeEndObject(); gen.writeNumberField("start", start); gen.writeNumberField("value", value); gen.writeEndObject(); gen.flush(); } }
private static void addMetric(List<Metric> metrics, Metric metric) { if (metric.getNumberValue().doubleValue() >= 0.0) { final MonitorConfig c = metric.getConfig(); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Adding " + c.getName() + " " + c.getTags() + " " + metric.getNumberValue()); } metrics.add(metric); } else { LOGGER.debug("Ignoring {}", metric); } }
private void addName(ObjectNameBuilder builder, Monitor<?> monitor) { builder.addProperty("name", monitor.getConfig().getName()); }
/** Convert servo config to spectator id. */ public static Id createId(MonitorConfig config) { // Need to ensure that Servo type tag is removed to avoid incorrectly reprocessing the // data in later transforms Map<String, String> tags = new HashMap<>(config.getTags().asMap()); tags.remove("type"); return registry .createId(config.getName()) .withTags(tags); }
private Map<String, Double> mkMap(List<List<Metric>> updates) { Map<String, Double> map = new HashMap<>(); for (Metric m : updates.get(0)) { map.put(m.getConfig().getName(), m.getNumberValue().doubleValue()); } return map; }
/** * Create a new builder initialized with the specified config. */ public Builder(MonitorConfig config) { this(config.getName()); withTags(config.getTags()); withPublishingPolicy(config.getPublishingPolicy()); }
private Timer getByName(String name) throws Exception { List<Monitor<?>> timers = getTimers(); for (Monitor<?> m : timers) { String monitorName = m.getConfig().getName(); if (name.equals(monitorName)) { return (Timer) m; } } return null; }
private StepCounter getByName(String name) throws Exception { List<Monitor<?>> counters = getCounters(); for (Monitor<?> m : counters) { String monitorName = m.getConfig().getName(); if (name.equals(monitorName)) { return (StepCounter) m; } } return null; }
@Test public void testDefaultNames() throws Exception { Metrics m = new Metrics(); List<Monitor<?>> monitors = new ArrayList<>(); Monitors.addAnnotatedFields(monitors, null, null, m); List<String> expectedNames = UnmodifiableList.of( "annoCounter", "annoGauge", "annoInfo", "primitiveGauge"); List<String> actualNames = monitors.stream().map( monitor -> monitor.getConfig().getName()).collect(Collectors.toList()); Collections.sort(actualNames); assertEquals(actualNames, expectedNames); } }
private Map<String, String> mkTypeMap(List<List<Metric>> updates) { Map<String, String> map = new HashMap<>(); for (Metric m : updates.get(0)) { map.put(m.getConfig().getName(), m.getConfig().getTags().getValue(DataSourceType.KEY)); } return map; }
@Test public void testAccessors() throws Exception { MonitorConfig m1 = new MonitorConfig.Builder("a").withTags(tags1).build(); assertEquals(m1.getName(), "a"); assertEquals(m1.getTags(), tags1); }
/** * {@inheritDoc} */ @Override public MonitorConfig getConfig() { TagList contextTags = context.getTags(); return MonitorConfig.builder(baseConfig.getName()) .withTags(baseConfig.getTags()) .withTags(contextTags) .build(); }
private static MonitorConfig subId(MonitorConfig config, String sub) { String newName = config.getName() + "." + sub; return MonitorConfig.builder(newName).withTags(config.getTags()) .withPublishingPolicy(config.getPublishingPolicy()) .build(); }
@Test public void testNewObjectConfig() throws Exception { ClassWithMonitors obj = new ClassWithMonitors() { }; List<Monitor<?>> monitors = Monitors.newObjectMonitor(obj).getMonitors(); for (Monitor m : monitors) { assertEquals(m.getConfig().getTags().getValue("class"), "MonitorsTest", String.format("%s should have class MonitorsTest", m.getConfig().getName())); } assertEquals(monitors.size(), 8); }
@Test public void testBuilderMonitorConfig() throws Exception { MonitorConfig m1 = new MonitorConfig.Builder("a").build(); MonitorConfig m2 = new MonitorConfig.Builder(m1).build(); assertEquals(m1, m2); MonitorConfig m3 = new MonitorConfig.Builder(m1).withTag("k", "v").build(); assertNotEquals(m1, m3); assertEquals(m1.getName(), m3.getName()); assertNotEquals(m1.getTags(), m3.getTags()); assertEquals(m1.getPublishingPolicy(), m3.getPublishingPolicy()); } }
@Test public void testNewObjectMonitorWithParentClass() throws Exception { ParentHasMonitors obj = new ParentHasMonitors(); List<Monitor<?>> monitors = Monitors.newObjectMonitor(obj).getMonitors(); for (Monitor m : monitors) { assertEquals(m.getConfig().getTags().getValue("class"), "ParentHasMonitors", String.format("%s should have class ParentHasMonitors", m.getConfig().getName())); } assertEquals(monitors.size(), 10); }
@Override public ObjectName createObjectName(String domain, Monitor<?> monitor) { ObjectNameBuilder objNameBuilder = ObjectNameBuilder.forDomain(domain); objNameBuilder.addProperty("name", monitor.getConfig().getName()); objNameBuilder.addProperties(monitor.getConfig().getTags()); return objNameBuilder.build(); }
@Test public void testGetMonitors() throws Exception { List<Monitor<?>> monitors = ((CompositeMonitor<?>) newInstance("test")).getMonitors(); assertEquals(monitors.size(), 2); Monitor<Long> duration = getDuration(monitors); Monitor<Long> activeTasks = getActiveTasks(monitors); assertEquals(duration.getConfig().getName(), "test.duration"); assertEquals(activeTasks.getConfig().getName(), "test.activeTasks"); assertEquals(duration.getValue().longValue(), 0L); assertEquals(activeTasks.getValue().longValue(), 0L); }
MetricDatum createMetricDatum(Metric metric) { MetricDatum metricDatum = new MetricDatum(); return metricDatum.withMetricName(metric.getConfig().getName()) .withDimensions(createDimensions(metric.getConfig().getTags())) .withUnit("None")//DataSourceTypeToAwsUnit.getUnit(metric.)) .withTimestamp(new Date(metric.getTimestamp())) .withValue(truncate(metric.getNumberValue())); //TODO Need to convert into reasonable units based on DataType }