@Advice.OnMethodEnter(suppress = Throwable.class) public static void onMethodEnter(@SimpleMethodSignatureOffsetMappingFactory.SimpleMethodSignature String signature, @Advice.Local("span") AbstractSpan<?> span) { if (tracer != null) { final TraceContextHolder<?> parent = tracer.getActive(); if (parent == null) { span = tracer.startTransaction() .withName(signature) .activate(); } else { span = parent.createSpan() .withName(signature) .activate(); } } }
private TransactionPayload createPayloadWithRequiredValues() { final TransactionPayload payload = createPayload(); final Transaction transaction = createTransactionWithRequiredValues(); payload.getTransactions().add(transaction); Span span = new Span(mock(ElasticApmTracer.class)); span.start(TraceContext.fromParent(), transaction) .withType("type") .withSubtype("subtype") .withAction("action") .withName("name"); payload.getSpans().add(span); return payload; }
@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 resetState() { Span span = new Span(mock(ElasticApmTracer.class)) .withName("SELECT FROM product_types") .withType("db") .withSubtype("postgresql") .withAction("query"); span.getContext().getDb() .withInstance("customers") .withStatement("SELECT * FROM product_types WHERE user_id=?") .withType("sql") .withUser("readonly_user"); span.resetState(); assertThat(span.getContext().hasContent()).isFalse(); assertThat((CharSequence) span.getName()).isNullOrEmpty(); assertThat(span.getType()).isNull(); assertThat(span.getSubtype()).isNull(); assertThat(span.getAction()).isNull(); } }
Span span = new Span(mock(ElasticApmTracer.class)) .start(TraceContext.fromParent(), t) .withName("SELECT FROM product_types") .withType("db") .withSubtype("postgresql") .withName("GET /api/types") .withType("request")); spans.add(new Span(mock(ElasticApmTracer.class)) .start(TraceContext.fromParent(), t) .withName("GET /api/types") .withType("request")); spans.add(new Span(mock(ElasticApmTracer.class)) .start(TraceContext.fromParent(), t) .withName("GET /api/types") .withType("request"));