public static Predicate<Stat> isMatch(StatFilter filter) { return stat -> { if ((stat.getValue() != 0 && !filter.reportZeros)) { return false; } if (filter.name != null && !stat.getName().equals(filter.name)) { return false; } if (!stat.getTags().containsAll(filter.tags)) { return false; } return true; }; } }
public MonitoredProcess(Class clazz) { String child = clazz.getCanonicalName(); runtimeStat = new Stat("timing.ns", Stat.MetricType.gauge); runtimeStat.addTag("class", child); errorCountStat = new Stat("error.count", Stat.MetricType.count); errorCountStat.addTag("class", child); successCountStat = new Stat("success.count", Stat.MetricType.count); successCountStat.addTag("class", child); Monitor monitor = Monitor.getInstance(); monitor.addProcess(this); monitor.addInstanceStat(runtimeStat); monitor.addInstanceStat(errorCountStat); monitor.addInstanceStat(successCountStat); }
public String serialize(Object obj) throws SerializationException { this.getRuntimeStat().start(); String serialized; try { serialized = this.serializer.serialize(obj); } catch (Exception e) { this.getErrorCountStat().increment(); throw new SerializationException(e); } finally { this.getRuntimeStat().stop(); } this.getSuccessCountStat().increment(); return serialized; }
@Override public void write(ArrayList<Stat> stats, long invokeTimeMs, Set<Tag> tags) { Set<Tag> allTags = new HashSet<Tag>(); /* * DataDog only tracks to second precision */ long ts = invokeTimeMs / 1000; for (Stat stat : stats) { allTags.addAll(tags); allTags.addAll(stat.getTags()); String tagsString = allTags.stream().map(e -> formatEntry(e.getKey(), e.getValue())) .collect(Collectors.joining(",")); /* * MONITORING|unix_epoch_timestamp|value|metric_type|my.metric.name|#tag1:value,tag2 */ String[] logParts = {"MONITORING", "" + ts, "" + stat.getValue(), stat.getType().name(), prefix + "." + stat.getName(), "#" + tagsString}; allTags.clear(); System.out.println(String.join("|", logParts)); } }
when(runtimeStat.fork()).thenReturn(forkedRuntimeStat); Stat successStat = mock(Stat.class); Stat errorStat = mock(Stat.class); verify(runtimeStat, times(3)).fork(); verify(forkedRuntimeStat, times(3)).start(); verify(forkedRuntimeStat, times(3)).stop(); verify(runtimeStat, times(1)).join(); verify(successStat, times(3)).increment(); verify(errorStat, never()).increment();
/** * Aggregates any sub stats that may have been created. */ public void join() { for (Stat subStat : subStats) { this.value += subStat.getValue(); } } }
@Test public void testFilterZeros() { MonitorFactory mf = new MonitorFactory(); Monitor monitor = mf.getInstance(); StatFilter filter = new StatFilter(); filter.setReportZeros(false); List<StatFilter> filters = new ArrayList<StatFilter>(1); filters.add(filter); DummyReporter reporter = new DummyReporter(filters); monitor.addReporters(Arrays.asList(reporter)); Stat statFail = new Stat("bender.test.fail"); statFail.setValue(0); Stat statPass = new Stat("bender.test.pass"); statPass.setValue(1); monitor.addInvocationStat(statFail); monitor.addInvocationStat(statPass); monitor.writeStats(); assertEquals(1, reporter.buffer.size()); assertEquals(true, reporter.buffer.contains("bender.test.pass 1")); assertEquals(false, reporter.buffer.contains("bender.test.fail 0")); }
public synchronized Stat fork() { Stat stat = new Stat(this.name, this.type); subStats.add(stat); return stat; }
/** * Drains buffer and sends batch asynchronously via {@link Transport}. * * @param buffer the buffer to send. * @param partitions the partitions associated with the buffer. */ private void send(TransportBuffer buffer, LinkedHashMap<String, String> partitions) { if (buffer.isEmpty()) { return; } this.threadCounter.incrementAndGet(); TransportThread tt = new TransportThread(transportFactory, buffer, partitions, this.threadCounter, this.hasUnrecoverableException, getRuntimeStat().fork(), getErrorCountStat(), getSuccessCountStat(), getContext()); tt.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { public void uncaughtException(Thread th, Throwable ex) { logger.error(String.format("transport thread %s failed", th.getName()), ex); } }); this.pool.execute(tt); }
this.getRuntimeStat().join();
@Override public void write(ArrayList<Stat> stats, long invokeTimeMs, Set<Tag> tags) { Set<Tag> allTags = new HashSet<Tag>(); /* * DataDog only tracks to second precision */ long ts = invokeTimeMs / 1000; for (Stat stat : stats) { allTags.addAll(tags); allTags.addAll(stat.getTags()); String tagsString = allTags.stream().map(e -> formatEntry(e.getKey(), e.getValue())) .collect(Collectors.joining(",")); /* * MONITORING|unix_epoch_timestamp|value|metric_type|my.metric.name|#tag1:value,tag2 */ String[] logParts = {"MONITORING", "" + ts, "" + stat.getValue(), stat.getType().name(), prefix + "." + stat.getName(), "#" + tagsString}; allTags.clear(); System.out.println(String.join("|", logParts)); } }
/** * Aggregates any sub stats that may have been created. */ public void join() { for (Stat subStat : subStats) { this.value += subStat.getValue(); } } }
public synchronized Stat fork() { Stat stat = new Stat(this.name, this.type); subStats.add(stat); return stat; }
/** * Drains buffer and sends batch asynchronously via {@link Transport}. * * @param buffer the buffer to send. * @param partitions the partitions associated with the buffer. */ private void send(TransportBuffer buffer, LinkedHashMap<String, String> partitions) { if (buffer.isEmpty()) { return; } this.threadCounter.incrementAndGet(); TransportThread tt = new TransportThread(transportFactory, buffer, partitions, this.threadCounter, this.hasUnrecoverableException, getRuntimeStat().fork(), getErrorCountStat(), getSuccessCountStat(), getContext()); tt.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { public void uncaughtException(Thread th, Throwable ex) { logger.error(String.format("transport thread %s failed", th.getName()), ex); } }); this.pool.execute(tt); }