elasticsearch 弹性APM + Spring + @KafkaListener - @捕获跨度不工作

mrzz3bfm  于 2022-12-03  发布在  ElasticSearch
关注(0)|答案(1)|浏览(175)

我有一个简单的 Spring Boot + Kafka + apm应用程序。我使用ELK+APM服务器7.12.1版本,java代理co.elastic.apm:apm-agent-attach:1.25.0和代理api co.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);
    }
}

我有一个这样的@Kafka听众:

@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的span,但是当我尝试使用@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

有人能告诉我怎么解吗?

rmbxnbpk

rmbxnbpk1#

不确定这是否仍然与您相关,我遇到了同样的问题,即elastic.apm.application_packages Java系统属性没有从我的application.properties文件正确加载。
根据文档(https://www.elastic.co/guide/en/apm/agent/java/1.x/配置堆栈跟踪.html#配置应用程序包):

You can set the application package using Java system properties: -Delastic.apm.application_packages=com.myproject

为了确保已正确加载,请在IntelliJ VM options应用程序环境配置中添加以下行:

-javaagent:elastic-apm-agent-1.34.1.jar -Delastic.apm.application_packages=com.myproject

然后检查您的应用程序启动日志,它应该包含如下的日志行:

2022-11-29 17:24:13,274 [main] INFO  co.elastic.apm.agent.configuration.StartupInfo - application_packages: 'com.myproject' (source: Java System Properties)

干杯!干杯!

相关问题