@Test public void testNotInitialized() throws Exception { when(corePlugin.isStagemonitorActive()).thenReturn(true); final MeasurementSession measurementSession = new MeasurementSession(null, "testHost", "testInstance"); Stagemonitor.reset(measurementSession); assertFalse(Stagemonitor.isStarted()); }
@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(); }
@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"); }
@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 requestInitialized(ServletRequestEvent sre) { if (corePlugin.isStagemonitorActive()) { final MeasurementSession measurementSession = corePlugin.getMeasurementSession(); if (measurementSession.getApplicationName() != null) { MDC.put("application", measurementSession.getApplicationName()); } if (measurementSession.getHostName() != null) { MDC.put("host", measurementSession.getHostName()); } String instanceName = measurementSession.getInstanceName(); if (instanceName == null) { instanceName = sre.getServletRequest().getServerName(); } MDC.put("instance", instanceName); final String requestId = UUID.randomUUID().toString(); sre.getServletRequest().setAttribute(STAGEMONITOR_REQUEST_ID_ATTR, requestId); if (Stagemonitor.isStarted()) { // don't store the requestId in MDC if stagemonitor is not active // so that thread pools that get created on startup don't inherit the requestId MDC.put("requestId", requestId); } } }
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; } }