@Override public long getTime() { final long time = delegate.getTime(); return time - (time % periodInMS); } }
@Override public MonitorCallback onMessageIngested(Message<?> message) { final long start = clock.getTime(); return new MonitorCallback() { @Override public void reportSuccess() { processedDurationHistogram.update(clock.getTime() - start); } @Override public void reportFailure(Throwable cause) { processedDurationHistogram.update(clock.getTime() - start); } @Override public void reportIgnored() { processedDurationHistogram.update(clock.getTime() - start); } }; }
@Override public void reportIgnored() { processedDurationHistogram.update(clock.getTime() - start); } };
@Override public void reportFailure(Throwable cause) { processedDurationHistogram.update(clock.getTime() - start); }
@Override public void reportSuccess() { processedDurationHistogram.update(clock.getTime() - start); }
private long currentTimeInSeconds() { return TimeUnit.MILLISECONDS.toSeconds(clock.getTime()); }
private GregorianCalendar getNowUTC() { final GregorianCalendar now = new GregorianCalendar(); now.setTimeZone(TimeZone.getTimeZone("UTC")); now.setTime(new Date(clock.getTime())); return now; }
/** * Starts the reporter polling at the given period. * * @param period the amount of time between polls * @param unit the unit for {@code period} */ public void start(long period, TimeUnit unit) { synchronized (this) { if (started) { throw new IllegalStateException("This reporter has already been started"); } final long periodInMS = unit.toMillis(period); executor.scheduleAtFixedRate(new Runnable() { @Override public void run() { try { report(); } catch (RuntimeException ex) { logger.error("RuntimeException thrown from {}#report. Exception was suppressed.", ScheduledMetrics2Reporter.this.getClass().getSimpleName(), ex); } } }, getOffsetUntilTimestampIsDivisableByPeriod(clock.getTime(), periodInMS), periodInMS, TimeUnit.MILLISECONDS); this.clock = new QuantizedClock(clock, periodInMS); this.started = true; } }
@Override @SuppressWarnings("rawtypes") public void report(SortedMap<String, Gauge> gauges, SortedMap<String, Counter> counters, SortedMap<String, Histogram> histograms, SortedMap<String, Meter> meters, SortedMap<String, Timer> timers) { final long timestamp = TimeUnit.MILLISECONDS.toSeconds(clock.getTime()); for (Map.Entry<String, Gauge> entry : gauges.entrySet()) { reportGauge(timestamp, entry.getKey(), entry.getValue()); } for (Map.Entry<String, Counter> entry : counters.entrySet()) { reportCounter(timestamp, entry.getKey(), entry.getValue()); } for (Map.Entry<String, Histogram> entry : histograms.entrySet()) { reportHistogram(timestamp, entry.getKey(), entry.getValue()); } for (Map.Entry<String, Meter> entry : meters.entrySet()) { reportMeter(timestamp, entry.getKey(), entry.getValue()); } for (Map.Entry<String, Timer> entry : timers.entrySet()) { reportTimer(timestamp, entry.getKey(), entry.getValue()); } }
private void setTime(long time) { Clock clock = Mockito.mock(Clock.class); when(clock.getTime()).thenReturn(time); indexSelector = new IndexSelector(clock); }
@Before public void setUp() throws Exception { clock = Mockito.mock(Clock.class); when(clock.getTime()).thenReturn(0L); indexSelector = new IndexSelector(clock); }
@Override public void reportMetrics(final Map<MetricName, Gauge> gauges, final Map<MetricName, Counter> counters, final Map<MetricName, Histogram> histograms, final Map<MetricName, Meter> meters, final Map<MetricName, Timer> timers) { if (!corePlugin.isStagemonitorActive()) { return; } long timestamp = clock.getTime(); final Timer.Context time = registry.timer(reportingTimeMetricName).time(); final MetricsOutputStreamHandler metricsOutputStreamHandler = new MetricsOutputStreamHandler(gauges, counters, histograms, meters, timers, timestamp); if (!corePlugin.isOnlyLogElasticsearchMetricReports()) { if (!elasticsearchClient.isElasticsearchAvailable()) { return; } final String url = corePlugin.getElasticsearchUrl() + "/" + getTodaysIndexName() + "/" + METRICS_TYPE + "/_bulk"; httpClient.send("POST", url, CONTENT_TYPE_JSON, metricsOutputStreamHandler, NoopResponseHandler.INSTANCE); } else { try { final ByteArrayOutputStream os = new ByteArrayOutputStream(); metricsOutputStreamHandler.withHttpURLConnection(os); elasticsearchMetricsLogger.info(os.toString("UTF-8")); } catch (IOException e) { logger.warn(e.getMessage(), e); } } time.stop(); }
@Override public void reportMetrics(Map<MetricName, Gauge> gauges, Map<MetricName, Counter> counters, Map<MetricName, Histogram> histograms, Map<MetricName, Meter> meters, Map<MetricName, Timer> timers) { final Timer.Context time = registry.timer(reportingTimeMetricName).time(); long timestamp = clock.getTime(); reportGauges(gauges, timestamp); reportCounter(counters, timestamp); reportHistograms(histograms, timestamp); reportMeters(meters, timestamp); reportTimers(timers, timestamp); flush(); time.stop(); }
@Test public void getTime1() throws Exception { when(delegate.getTime()).thenReturn(1001L); assertEquals(1000, new QuantizedClock(delegate, 100).getTime()); }
@Test public void getTime3() throws Exception { when(delegate.getTime()).thenReturn(1000L); assertEquals(1000, new QuantizedClock(delegate, 100).getTime()); } }
@Test public void testSelectIndexIntraMonth() throws Exception { when(clock.getTime()).thenReturn(DateUtils.getDayInMillis() * 7); final String indexPattern = indexSelector.getIndexPatternOlderThanDays("metrics-", 2); assertEquals("metrics-*,-metrics-1970.01.08,-metrics-1970.01.07,-metrics-1970.01.06", indexPattern); } }
@Test public void getTime2() throws Exception { when(delegate.getTime()).thenReturn(1999L); assertEquals(1900, new QuantizedClock(delegate, 100).getTime()); }
@Before public void setUp() throws Exception { httpClient = mock(HttpClient.class); Clock clock = mock(Clock.class); timestamp = System.currentTimeMillis(); when(clock.getTime()).thenReturn(timestamp); final CorePlugin corePlugin = mock(CorePlugin.class); when(corePlugin.getInfluxDbUrl()).thenReturn(new URL("http://localhost:8086")); when(corePlugin.getInfluxDbDb()).thenReturn("stm"); influxDbReporter = InfluxDbReporter.forRegistry(new Metric2Registry(), corePlugin) .convertRatesTo(TimeUnit.SECONDS) .convertDurationsTo(DURATION_UNIT) .globalTags(singletonMap("app", "test")) .httpClient(httpClient) .clock(clock) .build(); }
@Test public void testSchedule() throws Exception { when(corePlugin.isOnlyLogElasticsearchMetricReports()).thenReturn(true); // this clock starts at 0 and then progresses normally when(clock.getTime()).then(new Answer<Long>() { long firstTimestamp; @Override public Long answer(InvocationOnMock invocation) throws Throwable { final long time = System.currentTimeMillis(); if (firstTimestamp == 0) { firstTimestamp = time; } return (time - firstTimestamp); } }); registry.register(name("test").build(), new Gauge<Integer>() { @Override public Integer getValue() { return 1; } }); elasticsearchReporter.start(100, TimeUnit.MILLISECONDS); Thread.sleep(300); verify(metricsLogger).info(eq(String.format("{\"index\":{\"_index\":\"stagemonitor-metrics-%s\",\"_type\":\"metrics\"}}\n" + "{\"@timestamp\":100,\"name\":\"test\",\"app\":\"test\",\"value\":1.0}\n", StringUtils.getLogstashStyleDate()))); verify(metricsLogger).info(eq(String.format("{\"index\":{\"_index\":\"stagemonitor-metrics-%s\",\"_type\":\"metrics\"}}\n" + "{\"@timestamp\":200,\"name\":\"test\",\"app\":\"test\",\"value\":1.0}\n", StringUtils.getLogstashStyleDate()))); }
this.clock = mock(Clock.class); timestamp = System.currentTimeMillis(); when(clock.getTime()).thenReturn(timestamp); final HttpClient httpClient = mock(HttpClient.class); when(httpClient.send(any(), any(), any(), any(), any())).thenAnswer(new Answer<Integer>() {