Search in sources :

Example 1 with Span

use of co.elastic.apm.agent.impl.transaction.Span in project apm-agent-java by elastic.

the class ElasticApmTracer method createSpan.

private Span createSpan() {
    Span span = spanPool.createInstance();
    while (span.getReferenceCount() != 0) {
        logger.warn("Tried to start a span with a non-zero reference count {} {}", span.getReferenceCount(), span);
        span = spanPool.createInstance();
    }
    return span;
}
Also used : Span(co.elastic.apm.agent.impl.transaction.Span) AbstractSpan(co.elastic.apm.agent.impl.transaction.AbstractSpan)

Example 2 with Span

use of co.elastic.apm.agent.impl.transaction.Span in project apm-agent-java by elastic.

the class DiscardSpanTest method testErrorCapturingMakesSpansNonDiscardable.

@Test
void testErrorCapturingMakesSpansNonDiscardable() {
    Transaction transaction = tracer.startRootTransaction(null);
    assertThat(transaction).isNotNull();
    try {
        Span span = transaction.createSpan();
        try {
            span.requestDiscarding().appendToName("non-discardable").captureException(new Exception());
            assertThat(span.isDiscardable()).isFalse();
        } finally {
            span.end();
        }
    } finally {
        transaction.end();
    }
    assertThat(reporter.getSpans().stream().map(Span::getNameAsString)).containsExactly("non-discardable");
    assertThat(transaction.getSpanCount().getTotal()).hasValue(1);
    assertThat(transaction.getSpanCount().getDropped()).hasValue(0);
    assertThat(transaction.getSpanCount().getReported()).hasValue(1);
}
Also used : Transaction(co.elastic.apm.agent.impl.transaction.Transaction) Span(co.elastic.apm.agent.impl.transaction.Span) Test(org.junit.jupiter.api.Test)

Example 3 with Span

use of co.elastic.apm.agent.impl.transaction.Span in project apm-agent-java by elastic.

the class DiscardSpanTest method testParentsOfContextPropagatingSpansAreNonDiscardable.

@Test
void testParentsOfContextPropagatingSpansAreNonDiscardable() {
    Transaction transaction = tracer.startRootTransaction(null);
    assertThat(transaction).isNotNull();
    try {
        Span parentSpan = transaction.createSpan().requestDiscarding();
        try {
            Span contextPropagatingSpan = parentSpan.createSpan();
            try {
                contextPropagatingSpan.propagateTraceContext(new HashMap<>(), TextHeaderMapAccessor.INSTANCE);
                assertThat(contextPropagatingSpan.isDiscardable()).isFalse();
                assertThat(parentSpan.isDiscardable()).isFalse();
            } finally {
                contextPropagatingSpan.end();
            }
        } finally {
            parentSpan.end();
        }
    } finally {
        transaction.end();
    }
    assertThat(transaction.getSpanCount().getTotal()).hasValue(2);
    assertThat(transaction.getSpanCount().getDropped()).hasValue(0);
    assertThat(transaction.getSpanCount().getReported()).hasValue(2);
}
Also used : Transaction(co.elastic.apm.agent.impl.transaction.Transaction) Span(co.elastic.apm.agent.impl.transaction.Span) Test(org.junit.jupiter.api.Test)

Example 4 with Span

use of co.elastic.apm.agent.impl.transaction.Span in project apm-agent-java by elastic.

the class ElasticApmTracerTest method testNestedSpan.

@Test
void testNestedSpan() {
    Transaction transaction = startTestRootTransaction();
    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);
}
Also used : Transaction(co.elastic.apm.agent.impl.transaction.Transaction) Span(co.elastic.apm.agent.impl.transaction.Span) AbstractSpan(co.elastic.apm.agent.impl.transaction.AbstractSpan) Test(org.junit.jupiter.api.Test)

Example 5 with Span

use of co.elastic.apm.agent.impl.transaction.Span in project apm-agent-java by elastic.

the class ElasticApmTracerTest method testSamplingNone.

@Test
void testSamplingNone() throws IOException {
    config.getConfig(CoreConfiguration.class).getSampleRate().update(0.0, SpyConfiguration.CONFIG_SOURCE_NAME);
    Transaction transaction = startTestRootTransaction().withType("request");
    try (Scope scope = transaction.activateInScope()) {
        transaction.setUser("1", "jon.doe@example.com", "jondoe", "domain");
        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");
}
Also used : Transaction(co.elastic.apm.agent.impl.transaction.Transaction) Span(co.elastic.apm.agent.impl.transaction.Span) AbstractSpan(co.elastic.apm.agent.impl.transaction.AbstractSpan) Test(org.junit.jupiter.api.Test)

Aggregations

Span (co.elastic.apm.agent.impl.transaction.Span)174 Test (org.junit.jupiter.api.Test)79 Transaction (co.elastic.apm.agent.impl.transaction.Transaction)66 AbstractSpan (co.elastic.apm.agent.impl.transaction.AbstractSpan)58 AbstractInstrumentationTest (co.elastic.apm.agent.AbstractInstrumentationTest)36 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)28 Test (org.junit.Test)27 Nullable (javax.annotation.Nullable)21 JsonNode (com.fasterxml.jackson.databind.JsonNode)20 Destination (co.elastic.apm.agent.impl.context.Destination)13 AbstractEsClientInstrumentationTest (co.elastic.apm.agent.esrestclient.AbstractEsClientInstrumentationTest)7 Scope (co.elastic.apm.agent.impl.Scope)7 Id (co.elastic.apm.agent.impl.transaction.Id)7 IOException (java.io.IOException)7 CoreConfiguration (co.elastic.apm.agent.configuration.CoreConfiguration)6 List (java.util.List)6 ValueSource (org.junit.jupiter.params.provider.ValueSource)6 DubboTestApi (co.elastic.apm.agent.dubbo.api.DubboTestApi)5 Collectors (java.util.stream.Collectors)5 ElasticApmTracer (co.elastic.apm.agent.impl.ElasticApmTracer)4