@Test public void testToJson() throws Exception { MeasurementSession measurementSession = new MeasurementSession("app", "host", "instance"); final MeasurementSession jsonSession = JsonUtils.getMapper().readValue(JsonUtils.toJson(measurementSession), MeasurementSession.class); assertEquals(measurementSession.getApplicationName(), jsonSession.getApplicationName()); assertEquals(measurementSession.getHostName(), jsonSession.getHostName()); assertEquals(measurementSession.getInstanceName(), jsonSession.getInstanceName()); assertEquals(measurementSession.getInstanceName(), jsonSession.getInstanceName()); assertEquals(measurementSession.getId(), jsonSession.getId()); assertEquals(measurementSession.getStart(), jsonSession.getStart()); } }
@Override public void onStart(SpanWrapper spanWrapper) { info = SpanContextInformation.forSpan(spanWrapper); info.spanWrapper = spanWrapper; info.setParent(SpanContextInformation.getCurrent()); spanContextMap.put(spanWrapper, info); for (Map.Entry<String, String> entry : Stagemonitor.getMeasurementSession().asMap().entrySet()) { spanWrapper.setTag(entry.getKey(), entry.getValue()); } }
private String getGraphitePrefix(MeasurementSession measurementSession) { return name("stagemonitor", sanitizeGraphiteMetricSegment(measurementSession.getApplicationName()), sanitizeGraphiteMetricSegment(measurementSession.getInstanceName()), sanitizeGraphiteMetricSegment(measurementSession.getHostName())); }
@Override public String toString() { StringBuilder sb = new StringBuilder() .append("Incident for check group '").append(checkName).append("':\n") .append("firstFailureAt=").append(firstFailureAt).append("\n"); if (resolvedAt == null) { sb.append("resolvedAt=").append(resolvedAt).append("\n"); } sb.append("oldStatus=").append(oldStatus).append("\n") .append("newStatus=").append(newStatus).append("\n"); if (!checkResultsByMeasurementSessionId.isEmpty()) { sb.append("host|instance|status|description|current value\n") .append("----|--------|------|-----------|-------------\n"); for (CheckResults checkResult : checkResultsByMeasurementSessionId.values()) { MeasurementSession measurement = checkResult.getMeasurementSession(); for (CheckResult result : checkResult.getResults()) { sb.append(measurement.getHostName()).append('|') .append(measurement.getInstanceName()).append('|') .append(result.getStatus()).append('|') .append(result.getFailingExpression()).append('|') .append(result.getCurrentValue()).append("\n"); } } } return sb.toString(); } }
public Incident sendTestAlert(Subscription subscription, CheckResult.Status status) { Check check = new Check(); check.setName("Test Check"); check.setApplication("testApp"); check.setTarget(name("test").build()); check.getWarn().add(new Threshold("mean", Threshold.Operator.GREATER_EQUAL, 1)); Incident testIncident = new Incident(check, new MeasurementSession("testApp", "testHost", "testInstance"), Arrays.asList(new CheckResult("test", 10, status))); tryAlert(testIncident, subscription, alerterByType.get(subscription.getAlerterType())); return testIncident; }
private synchronized void getInstanceNameFromExecution(MonitoredRequest<?> monitoredRequest) { final MeasurementSession measurementSession = Stagemonitor.getMeasurementSession(); if (measurementSession.getInstanceName() == null) { MeasurementSession session = new MeasurementSession(measurementSession.getApplicationName(), measurementSession.getHostName(), monitoredRequest.getInstanceName()); Stagemonitor.setMeasurementSession(session); } }
public RequestTrace(String requestId, MeasurementSession measurementSession, RequestMonitorPlugin requestMonitorPlugin) { this.requestMonitorPlugin = requestMonitorPlugin; this.id = requestId != null ? requestId : UUID.randomUUID().toString(); this.measurementStart = measurementSession.getStartTimestamp(); this.application = measurementSession.getApplicationName(); this.host = measurementSession.getHostName(); this.instance = measurementSession.getInstanceName(); this.timestamp = StringUtils.dateAsIsoString(new Date()); }
private static void addCheck(Check check, MeasurementSession measurementSession) { check.setApplication(measurementSession.getApplicationName()); try { configuration.getConfig(AlertingPlugin.class).addCheck(check); } catch (IOException e) { logger.warn(e.getMessage(), e); } }
when(alertingPlugin.getChecks()).thenReturn(Collections.singletonMap(check.getId(), check)); incidentRepository.createIncident( new Incident(check, new MeasurementSession("testApp", "testHost2", "testInstance"), Arrays.asList(new CheckResult("test", 10, CheckResult.Status.CRITICAL)))); boolean containsTestHost2 = false; for (CheckResults checkResults : storedIncident.getCheckResults()) { if (checkResults.getMeasurementSession().getHostName().equals("testHost2")) { containsTestHost2 = true; } else if (checkResults.getMeasurementSession().getHostName().equals("testHost")) { containsTestHost = true;
@Test public void testStartMonitoringNotActive() throws Exception { when(corePlugin.isStagemonitorActive()).thenReturn(false); final MeasurementSession measurementSession = new MeasurementSession("StagemonitorTest", "testHost", "testInstance"); Stagemonitor.reset(measurementSession); assertTrue(Stagemonitor.isDisabled()); assertFalse(Stagemonitor.isStarted()); assertTrue(Stagemonitor.getMeasurementSession().isInitialized()); assertThat(healthCheckRegistry.getNames()).doesNotContain("TestPlugin", "TestExceptionPlugin"); }
private synchronized void createMeasurementSession() { if (Stagemonitor.getMeasurementSession().isNull()) { MeasurementSession session = new MeasurementSession(corePlugin.getApplicationName(), corePlugin.getHostName(), corePlugin.getInstanceName()); Stagemonitor.setMeasurementSession(session); } }
public Collection<String> getHosts() { Set<String> hosts = new TreeSet<String>(); for (CheckResults checkResult : checkResultsByMeasurementSessionId.values()) { hosts.add(checkResult.getMeasurementSession().getHostName()); } return hosts; }
public Collection<String> getInstances() { Set<String> instances = new TreeSet<String>(); for (CheckResults checkResult : checkResultsByMeasurementSessionId.values()) { instances.add(checkResult.getMeasurementSession().getInstanceName()); } return instances; }
public <T extends RequestTrace> void monitorStart(MonitoredRequest<T> monitoredRequest) { final long start = System.nanoTime(); RequestInformation<T> info = new RequestInformation<T>(); info.monitoredRequest = monitoredRequest; detectForwardedRequest(info); request.set(info); try { if (!corePlugin.isStagemonitorActive()) { return; } if (Stagemonitor.getMeasurementSession().isNull()) { createMeasurementSession(); } info.firstRequest = noOfRequests.get() == 0; if (Stagemonitor.getMeasurementSession().getInstanceName() == null) { getInstanceNameFromExecution(monitoredRequest); } if (info.monitorThisRequest()) { if (!Stagemonitor.isStarted()) { info.startup = Stagemonitor.startMonitoring(); } beforeExecution(monitoredRequest, info); } } finally { info.overhead1 = System.nanoTime() - start; } }
@JsonCreator public Incident(@JsonProperty("checkResults") Collection<CheckResults> checkResults) { for (CheckResults checkResult : checkResults) { checkResultsByMeasurementSessionId.put(checkResult.getMeasurementSession().getId(), checkResult); } }
@Before public void setUp() throws Exception { registry = new Metric2Registry(); final ConfigurationRegistry configuration = Mockito.mock(ConfigurationRegistry.class); final CorePlugin corePlugin = mock(CorePlugin.class); when(corePlugin.isReportToJMX()).thenReturn(true); when(configuration.getConfig(CorePlugin.class)).thenReturn(corePlugin); new CorePlugin(mock(ElasticsearchClient.class)).registerReporters(registry, configuration, new MeasurementSession("JmxReportingTest", "test", "test")); }
@Override public void onStart(SpanWrapper spanWrapper) { if (corePlugin.isStagemonitorActive()) { final MeasurementSession measurementSession = corePlugin.getMeasurementSession(); if (measurementSession != null) { addToMdcIfNotNull("application", measurementSession.getApplicationName()); addToMdcIfNotNull("host", measurementSession.getHostName()); addToMdcIfNotNull("instance", measurementSession.getInstanceName()); } // don't store the context in MDC if stagemonitor is not active // so that thread pools that get created on startup don't inherit the ids if (Stagemonitor.isStarted()) { tracingPlugin.getTracer().inject(spanWrapper.context(), B3HeaderFormat.INSTANCE, new B3HeaderFormat.B3InjectAdapter() { @Override public void setParentId(String value) { } @Override public void setSpanId(String value) { addToMdcIfNotNull("spanId", value); } @Override public void setTraceId(String value) { addToMdcIfNotNull("traceId", value); } }); } } }
@Override public void reportMetrics(Map<MetricName, Gauge> gauges, Map<MetricName, Counter> counters, Map<MetricName, Histogram> histograms, Map<MetricName, Meter> meters, Map<MetricName, Timer> timers) { Map<String, Map<MetricName, Metric>> metricsGroupedByName = new HashMap<String, Map<MetricName, Metric>>(); addMetrics(metricsGroupedByName, gauges); addMetrics(metricsGroupedByName, counters); addMetrics(metricsGroupedByName, histograms); addMetrics(metricsGroupedByName, meters); addMetrics(metricsGroupedByName, timers); for (Check check : alertingPlugin.getChecks().values()) { if (measurementSession.getApplicationName().equals(check.getApplication()) && check.isActive()) { checkMetrics(metricsGroupedByName, check); } } }
@Test public void testStartMonitoring() throws Exception { when(corePlugin.isStagemonitorActive()).thenReturn(true); Stagemonitor.setConfiguration(configuration); Stagemonitor.reset(); final MeasurementSession measurementSession = new MeasurementSession("StagemonitorTest", "testHost", "testInstance"); Stagemonitor.reset(measurementSession); assertTrue(Stagemonitor.isStarted()); assertTrue(Stagemonitor.getMeasurementSession().isInitialized()); assertSame(measurementSession, Stagemonitor.getMeasurementSession()); assertThat(healthCheckRegistry.runHealthCheck("TestPlugin").isHealthy()).isTrue(); assertThat(healthCheckRegistry.runHealthCheck("TestExceptionPlugin").isHealthy()).isFalse(); }
private void setCheckResults(MeasurementSession measurementSession, List<CheckResult> checkResults, int previousConsecutiveFailures) { if (checkResults.isEmpty()) { checkResultsByMeasurementSessionId.remove(measurementSession.getId()); } else if (checkResultsByMeasurementSessionId.containsKey(measurementSession.getId())) { checkResultsByMeasurementSessionId.put(measurementSession.getId(), new CheckResults(checkResultsByMeasurementSessionId.get(measurementSession.getId()), checkResults)); } else { checkResultsByMeasurementSessionId.put(measurementSession.getId(), new CheckResults(measurementSession, checkResults)); } newStatus = getMostSevereStatus(); if (newStatus == CheckResult.Status.OK) { resolvedAt = new Date(); } consecutiveFailures = Math.max(previousConsecutiveFailures, getConsecutiveFailuresFromCheckResults()); }