0

我有一个简单的 spring boot + kafka + apm app。我使用ELK+APM Server7.12.1版本,java代理co.elastic.apm:apm-agent-attach:1.25.0和代理apico.elastic.apm:apm-agent-api:1.28.3

我的 Elastic apm 配置如下所示:

@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);
    }
}

我有一个像这样的@KafkaListener:

@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;
    }
}

一切正常我在我的kibana中看到名为calculateTotalQuantity 但是当我尝试使用@CaptureSpan注释时它停止工作

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

而且我再也看不到跨度了。甚至可以在日志中看到:

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)

带有注释:

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

有人可以告诉我如何解决吗?

4

0 回答 0