@Test void testDisable() { when(config.getConfig(CoreConfiguration.class).isActive()).thenReturn(false); Transaction transaction = tracerImpl.startTransaction(); try (Scope scope = transaction.activateInScope()) { assertThat(tracerImpl.currentTransaction()).isSameAs(transaction); assertThat(transaction.isSampled()).isFalse(); Span span = tracerImpl.getActive().createSpan(); try (Scope spanScope = span.activateInScope()) { assertThat(tracerImpl.getActive()).isSameAs(span); assertThat(tracerImpl.getActive()).isNotNull(); } assertThat(tracerImpl.getActive()).isSameAs(transaction); transaction.end(); } assertThat(tracerImpl.currentTransaction()).isNull(); assertThat(reporter.getTransactions()).isEmpty(); assertThat(reporter.getSpans()).isEmpty(); }
@Test void testEnableStacktraces() throws InterruptedException { when(tracerImpl.getConfig(StacktraceConfiguration.class).getSpanFramesMinDurationMs()).thenReturn(-1L); Transaction transaction = tracerImpl.startTransaction(); try (Scope scope = transaction.activateInScope()) { Span span = tracerImpl.getActive().createSpan(); try (Scope spanScope = span.activateInScope()) { Thread.sleep(10); span.end(); } transaction.end(); } assertThat(reporter.getFirstSpan().getStacktrace()).isNotNull(); }
@Test void testEnableDropSpans() { when(tracerImpl.getConfig(CoreConfiguration.class).getTransactionMaxSpans()).thenReturn(1); Transaction transaction = tracerImpl.startTransaction(); try (Scope scope = transaction.activateInScope()) { Span span = tracerImpl.getActive().createSpan(); try (Scope spanScope = span.activateInScope()) { assertThat(span.isSampled()).isTrue(); span.end(); } Span span2 = tracerImpl.getActive().createSpan(); try (Scope spanScope = span2.activateInScope()) { assertThat(span2.isSampled()).isFalse(); span2.end(); } transaction.end(); } assertThat(reporter.getFirstTransaction().isSampled()).isTrue(); assertThat(reporter.getFirstTransaction().getSpanCount().getDropped().get()).isEqualTo(1); assertThat(reporter.getFirstTransaction().getSpanCount().getStarted().get()).isEqualTo(1); assertThat(reporter.getSpans()).hasSize(1); }
@Test void testEnableStacktracesForSlowSpans() throws InterruptedException { when(tracerImpl.getConfig(StacktraceConfiguration.class).getSpanFramesMinDurationMs()).thenReturn(1L); Transaction transaction = tracerImpl.startTransaction(); try (Scope scope = transaction.activateInScope()) { Span span = tracerImpl.getActive().createSpan(); try (Scope spanScope = span.activateInScope()) { Thread.sleep(10); span.end(); } transaction.end(); } assertThat(reporter.getFirstSpan().getStacktrace()).isNotNull(); }
@Test void testDisableMidTransaction() { Transaction transaction = tracerImpl.startTransaction(); try (Scope scope = transaction.activateInScope()) { assertThat(tracerImpl.currentTransaction()).isSameAs(transaction); Span span = tracerImpl.getActive().createSpan(); try (Scope spanScope = span.activateInScope()) { when(config.getConfig(CoreConfiguration.class).isActive()).thenReturn(false); span.withName("test"); assertThat(span.getName().toString()).isEqualTo("test"); assertThat(tracerImpl.getActive()).isSameAs(span); assertThat(span.isChildOf(transaction)).isTrue(); span.end(); } Span span2 = tracerImpl.getActive().createSpan(); try (Scope spanScope = span2.activateInScope()) { when(config.getConfig(CoreConfiguration.class).isActive()).thenReturn(false); span2.withName("test2"); assertThat(span2.getName().toString()).isEqualTo("test2"); assertThat(tracerImpl.getActive()).isSameAs(span2); assertThat(span2.isChildOf(transaction)).isTrue(); span2.end(); } assertThat(tracerImpl.getActive()).isEqualTo(transaction); transaction.end(); } assertThat(tracerImpl.currentTransaction()).isNull(); assertThat(reporter.getSpans()).hasSize(2); assertThat(reporter.getFirstTransaction()).isSameAs(transaction); }
@Test void testThreadLocalStorage() { Transaction transaction = tracerImpl.startTransaction(); try (Scope scope = transaction.activateInScope()) { assertThat(tracerImpl.currentTransaction()).isSameAs(transaction); Span span = tracerImpl.getActive().createSpan(); try (Scope spanScope = span.activateInScope()) { assertThat(tracerImpl.currentTransaction()).isSameAs(transaction); assertThat(tracerImpl.getActive()).isSameAs(span); assertThat(span.isChildOf(transaction)).isTrue(); span.end(); } assertThat(tracerImpl.getActive()).isEqualTo(transaction); transaction.end(); } assertThat(tracerImpl.currentTransaction()).isNull(); }
@Test void testDisableStacktracesForFastSpans() { when(tracerImpl.getConfig(StacktraceConfiguration.class).getSpanFramesMinDurationMs()).thenReturn(100L); Transaction transaction = tracerImpl.startTransaction(); try (Scope scope = transaction.activateInScope()) { Span span = tracerImpl.getActive().createSpan(); try (Scope spanScope = span.activateInScope()) { span.end(); } transaction.end(); } assertThat(reporter.getFirstSpan().getStacktrace()).isNull(); }
@Test void testNestedSpan() { Transaction transaction = tracerImpl.startTransaction(); try (Scope scope = transaction.activateInScope()) { assertThat(tracerImpl.currentTransaction()).isSameAs(transaction); Span span = tracerImpl.getActive().createSpan(); try (Scope spanScope = span.activateInScope()) { assertThat(tracerImpl.getActive()).isSameAs(span); assertThat(span.isChildOf(transaction)).isTrue(); Span nestedSpan = tracerImpl.getActive().createSpan(); try (Scope nestedSpanScope = nestedSpan.activateInScope()) { assertThat(tracerImpl.getActive()).isSameAs(nestedSpan); assertThat(nestedSpan.isChildOf(span)).isTrue(); nestedSpan.end(); } span.end(); } assertThat(tracerImpl.getActive()).isEqualTo(transaction); transaction.end(); } assertThat(tracerImpl.currentTransaction()).isNull(); assertThat(reporter.getSpans()).hasSize(2); }
@Test void testDisableStacktraces() { when(tracerImpl.getConfig(StacktraceConfiguration.class).getSpanFramesMinDurationMs()).thenReturn(0L); Transaction transaction = tracerImpl.startTransaction(); try (Scope scope = transaction.activateInScope()) { Span span = tracerImpl.getActive().createSpan(); try (Scope spanScope = span.activateInScope()) { span.end(); } transaction.end(); } assertThat(reporter.getFirstSpan().getStacktrace()).isNull(); }
@Test void testSamplingNone() throws IOException { config.getConfig(CoreConfiguration.class).getSampleRate().update(0.0, SpyConfiguration.CONFIG_SOURCE_NAME); Transaction transaction = tracerImpl.startTransaction().withType("request"); try (Scope scope = transaction.activateInScope()) { transaction.setUser("1", "jon.doe@example.com", "jondoe"); Span span = tracerImpl.getActive().createSpan(); try (Scope spanScope = span.activateInScope()) { span.end(); } transaction.end(); } // we do report non-sampled transactions (without the context) assertThat(reporter.getTransactions()).hasSize(1); assertThat(reporter.getSpans()).hasSize(0); assertThat(reporter.getFirstTransaction().getContext().getUser().getEmail()).isNull(); assertThat(reporter.getFirstTransaction().getType()).isEqualTo("request"); }
private void innerRecordExceptionWithTrace(boolean sampled) { reporter.reset(); Transaction transaction = tracerImpl.startTransaction(TraceContext.asRoot(), null, ConstantSampler.of(sampled), -1); transaction.withType("test-type"); try (Scope scope = transaction.activateInScope()) { transaction.getContext().getRequest() .addHeader("foo", "bar") .withMethod("GET") .getUrl() .withPathname("/foo"); tracerImpl.currentTransaction().captureException(new Exception("from transaction")); ErrorCapture error = validateError(transaction, sampled, transaction); assertThat(error.getContext().getRequest().getHeaders().get("foo")).isEqualTo("bar"); reporter.reset(); Span span = transaction.createSpan().activate(); span.captureException(new Exception("from span")); validateError(span, sampled, transaction); span.deactivate().end(); transaction.end(); } }