@Test public void testMDCStagemonitorDeactivated() throws Exception { when(corePlugin.isStagemonitorActive()).thenReturn(false); when(corePlugin.getMeasurementSession()) .thenReturn(new MeasurementSession("MDCSpanEventListenerTest", "testHost", null)); mdcSpanInterceptor.onStart(spanWrapper); assertNull(MDC.getCopyOfContextMap()); }
@Override protected void onElasticsearchFirstAvailable(ElasticsearchClient elasticsearchClient) { if (corePlugin.isInitializeElasticsearch()) { if (elasticsearchClient.isElasticsearch6Compatible()) { logger.debug("creating KibanaIndexAndMapping for ES 6..."); elasticsearchClient.createIndexAndSendMapping(".kibana", "doc", IOUtils.getResourceAsStream(elasticsearchClient.getElasticsearchResourcePath() + "kibana-index-doc.json")); logger.debug("created KibanaIndexAndMapping for ES 6"); } else { logger.debug("creating KibanaIndexAndMapping for ES 5..."); createKibana5IndexAndMappings(elasticsearchClient); sendConfigurationMapping(elasticsearchClient); logger.debug("created KibanaIndexAndMapping for ES 5"); } manageMetricsIndex(elasticsearchClient, corePlugin); } elasticsearchClient.scheduleIndexManagement(ElasticsearchReporter.STAGEMONITOR_METRICS_INDEX_PREFIX, corePlugin.getMoveToColdNodesAfterDays(), corePlugin.getDeleteElasticsearchMetricsAfterDays()); reportToElasticsearch(corePlugin.getMetricRegistry(), corePlugin.getElasticsearchReportingInterval(), corePlugin.getMeasurementSession()); }
TagRecordingSpanEventListener.asList(tags), samplePriorityDeterminingSpanInterceptor, reportingSpanEventListener); when(corePlugin.getMeasurementSession()).thenReturn(new MeasurementSession(getClass().getSimpleName(), "test", "test")); when(tracingPlugin.getTracer()).then((invocation) -> { if (corePlugin.isStagemonitorActive()) {
@Test public void testMdc() throws Exception { Stagemonitor.reset(new MeasurementSession("MDCSpanEventListenerTest", "testHost", "testInstance")); when(corePlugin.getMeasurementSession()) .thenReturn(new MeasurementSession("MDCSpanEventListenerTest", "testHost", "testInstance")); mdcSpanInterceptor.onStart(spanWrapper); assertNotNull(MDC.get("spanId")); assertNotNull(MDC.get("traceId")); assertNull(MDC.get("parentId")); mdcSpanInterceptor.onFinish(spanWrapper, null, 0); assertThat(MDC.getCopyOfContextMap()).isEmpty(); }
@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); } }); } } }
@Test public void testMdcStagemonitorNotStarted() throws Exception { final MeasurementSession measurementSession = new MeasurementSession("MDCSpanEventListenerTest", "testHost", null); Stagemonitor.reset(measurementSession); when(corePlugin.getMeasurementSession()).thenReturn(measurementSession); mdcSpanInterceptor.onStart(spanWrapper); assertEquals("testHost", MDC.get("host")); assertEquals("MDCSpanEventListenerTest", MDC.get("application")); assertNull(MDC.get("instance")); assertNull(MDC.get("spanId")); assertNull(MDC.get("traceId")); assertNull(MDC.get("parentId")); mdcSpanInterceptor.onFinish(spanWrapper, null, 0); }
@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); } } }