# @CaptureSpan not working but ElasticApm.currentSpan().startSpan() works

**URL:** <https://discuss.elastic.co/t/capturespan-not-working-but-elasticapm-currentspan-startspan-works/293127>\
**Category:** APM\
**Tags:** java\
**Created:** [December 29, 2021, 2:01pm UTC](https://discuss.elastic.co/t/capturespan-not-working-but-elasticapm-currentspan-startspan-works/293127 "2021-12-29T14:01:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![solveretur](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/solveretur/32/99586_2.png) [@solveretur](https://discuss.elastic.co/u/solveretur)\
**Post date:** [December 29, 2021, 2:01pm UTC](https://discuss.elastic.co/t/capturespan-not-working-but-elasticapm-currentspan-startspan-works/293127/1 "2021-12-29T14:01:49Z")

</div>

**Kibana version** :  
v 7.12.1  
**Elasticsearch version** :  
7.12.1  
**APM Server version** :  
7.12.1  
**APM Agent language and version** :  
co.elastic.apm:apm-agent-attach:1.28.3  
co.elastic.apm:apm-agent-api:1.28.3

**Original install method (e.g. download page, yum, deb, from source, etc.) and version**:

```auto
@Setter
@Configuration
@ConfigurationProperties(prefix = "app.elastic-apm")
public class ElasticApmConfiguration {

    private static final String SERVER_URL_KEY = "server_url";
    private String serverUrl;

    private static final String SERVICE_NAME_KEY = "service_name";
    private String serviceName;

    private static final String ENVIRONMENT_KEY = "environment";
    private String environment;

    private static final String APPLICATION_PACKAGES_KEY = "application_packages";
    private String applicationPackages;

    @PostConstruct
    public void init() {
        Map<String, String> apmProps = new HashMap<>();
        apmProps.put(SERVER_URL_KEY, serverUrl);
        apmProps.put(SERVICE_NAME_KEY, serviceName);
        apmProps.put(ENVIRONMENT_KEY, environment);
        apmProps.put(APPLICATION_PACKAGES_KEY, applicationPackages);

        ElasticApmAttacher.attach(apmProps);
    }
}

```

**Description of the problem including expected versus actual behavior. Please include screenshots (if relevant)**:

Hi, I have a simple spring boot + kafka + apm app. I have a @KafkaListener like this:

```auto
@Slf4j
@EnableKafka
@Component
@RequiredArgsConstructor
public class MyListener {

    ...deps...

    @KafkaListener(topics = "${app.kafka.topics.mytopic}")
    public void consume(@Payload String input, Acknowledgment acknowledgment) {
        ... my logic ...
        methodIWouldLikeToProfile();
        .....
        acknowledgment.acknowledge();
    }

    private String methodIWouldLikeToProfile(){
        final var span = ElasticApm.currentSpan().startSpan();
        span.setName("calculateTotalQuantity");
        System.out.format("CALCULATING QUANTITY\n");
        ...some more logic....
        span.end();
        return res;
    }
}

```

Everything works smoothly I see in my kibana the span named `calculateTotalQuantity`  
However when I try to use the `@CaptureSpan` annotation it stops working

```auto
@CaptureSpan(value = "calculateTotalQuantity")
private String methodIWouldLikeToProfile(){
        System.out.format("CALCULATING QUANTITY\n");
        ...some more logic....
        return res;
    }

```

and I don't see the span anymore. It can be seen even in the logs:

```auto
2021-12-29 14:49:42,131 [org.springframework.kafka.KafkaListenerEndpointContainer#0-0-C-1] DEBUG co.elastic.apm.agent.impl.transaction.Span - startSpan '' 00-2564fc689dc11bfff90c3501c0c50a13-e508218c002de7c7-01 (2b378884)
2021-12-29 14:49:42,133 [org.springframework.kafka.KafkaListenerEndpointContainer#0-0-C-1] DEBUG co.elastic.apm.agent.impl.transaction.AbstractSpan - increment references to 'Kafka record from mytopic.on.kafka' 00-2564fc689dc11bfff90c3501c0c50a13-e0088731afcf4284-01 (323a0459) (4)
2021-12-29 14:49:42,136 [org.springframework.kafka.KafkaListenerEndpointContainer#0-0-C-1] DEBUG co.elastic.apm.agent.impl.transaction.AbstractSpan - increment references to '' 00-2564fc689dc11bfff90c3501c0c50a13-e508218c002de7c7-01 (2b378884) (1)
2021-12-29 14:49:42,136 [org.springframework.kafka.KafkaListenerEndpointContainer#0-0-C-1] DEBUG co.elastic.apm.agent.impl.transaction.AbstractSpan - increment references to 'Kafka record from mytopic.on.kafka' 00-2564fc689dc11bfff90c3501c0c50a13-e0088731afcf4284-01 (323a0459) (5)
2021-12-29 14:49:42,137 [org.springframework.kafka.KafkaListenerEndpointContainer#0-0-C-1] DEBUG co.elastic.apm.agent.impl.transaction.AbstractSpan - increment references to 'Kafka record from mytopic.on.kafka' 00-2564fc689dc11bfff90c3501c0c50a13-e0088731afcf4284-01 (323a0459) (6)
2021-12-29 14:49:42,137 [org.springframework.kafka.KafkaListenerEndpointContainer#0-0-C-1] DEBUG co.elastic.apm.agent.impl.transaction.AbstractSpan - increment references to '' 00-2564fc689dc11bfff90c3501c0c50a13-e508218c002de7c7-01 (2b378884) (2)
CALCULATING QUANTITY
2021-12-29 14:49:42,150 [org.springframework.kafka.KafkaListenerEndpointContainer#0-0-C-1] DEBUG co.elastic.apm.agent.impl.transaction.Span - endSpan 'calculateTotalQuantity' 00-2564fc689dc11bfff90c3501c0c50a13-e508218c002de7c7-01 (2b378884)
2021-12-29 14:49:42,151 [org.springframework.kafka.KafkaListenerEndpointContainer#0-0-C-1] DEBUG co.elastic.apm.agent.impl.transaction.AbstractSpan - decrement references to 'Kafka record from mytopic.on.kafka' 00-2564fc689dc11bfff90c3501c0c50a13-e0088731afcf4284-01 (323a0459) (5)

```

with the annotation:

```auto
2021-12-29 14:47:22,813 [org.springframework.kafka.KafkaListenerEndpointContainer#0-0-C-1] DEBUG co.elastic.apm.agent.impl.transaction.AbstractSpan - decrement references to 'Kafka record from mytopic.on.kafka' 00-920621868563cf9cd9ad2a5cc89e35a8-cb6aa4ca6a00f907-01 (597b1ec5) (3)
2021-12-29 14:47:22.816 INFO 90644 --- [ntainer#0-0-C-1] e.v.s.s.s.d.service.MyService : Did something
CALCULATING QUANTITY
2021-12-29 14:47:22.821 INFO 90644 --- [ntainer#0-0-C-1] .s.s.MyListener : Did something elese
2021-12-29 14:47:22.822 INFO 90644 --- [ntainer#0-0-C-1] .s.s.MyListener : And else

```

Could someone tell me how to solve it ?

---

<div class="post-metadata">

**Author:** ![Jack\_Shirazi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jack_shirazi/32/91641_2.png) [@Jack\_Shirazi](https://discuss.elastic.co/u/Jack_Shirazi)\
**Post date:** [December 29, 2021, 3:23pm UTC](https://discuss.elastic.co/t/capturespan-not-working-but-elasticapm-currentspan-startspan-works/293127/2 "2021-12-29T15:23:08Z")

</div>

Hiya, might take to next week to get back to you due to current workforce/workload. But please try with the agent on [DEBUG](https://www.elastic.co/guide/en/apm/agent/java/current/config-logging.html#config-log-level) and see if anything pops out

---

<div class="post-metadata">

**Author:** ![tstadler](https://avatars.discourse-cdn.com/v4/letter/t/aca169/32.png) [@tstadler](https://discuss.elastic.co/u/tstadler)\
**Post date:** [December 30, 2021, 3:36pm UTC](https://discuss.elastic.co/t/capturespan-not-working-but-elasticapm-currentspan-startspan-works/293127/3 "2021-12-30T15:36:13Z")

</div>

What is the package of `MyListener` ? What is the value of application\_packages?

---

<div class="post-metadata">

**Author:** ![solveretur](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/solveretur/32/99586_2.png) [@solveretur](https://discuss.elastic.co/u/solveretur)\
**Post date:** [January 3, 2022, 11:30am UTC](https://discuss.elastic.co/t/capturespan-not-working-but-elasticapm-currentspan-startspan-works/293127/4 "2022-01-03T11:30:54Z")

</div>

It was my mistake - issue to be closed

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 24, 2022, 7:31am UTC](https://discuss.elastic.co/t/capturespan-not-working-but-elasticapm-currentspan-startspan-works/293127/5 "2022-01-24T07:31:32Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
