【问题标题】:Custom spans using annotations使用注释的自定义跨度
【发布时间】:2019-02-06 17:35:34
【问题描述】:

我正在尝试不同的微服务跟踪方法(我主要使用 RabbitMQ 处理事件驱动服务)。

我在测试什么:

  • spring-cloud-sleuth + zipkin + logstash-reporter
  • elastic-apm-api + elastic-apm-agent + logstash-reporter
  • 通过这两种方法,我正在查看默认输出是什么以及如何添加一些我们认为有趣的跨度/事务。 (“如何”是指成本和对代码的影响)。

给定代码

@Autowired
Tracer tracer;

@NewSpan
@CaptureSpan
private URLConnection getUrlConnection(String url) throws IOException {
    ScopedSpan sp = tracer.startScopedSpanWithParent("getUrlConnection", tracer.currentSpan().context());
    LOGGER.info("COUOSQUDQSUD");
    sp.finish();
    return new URL(url).openConnection();
}

@StreamListener(Sink.INPUT)
@CaptureTransaction
public void transferToS3(FileEntry fileEntry) throws IOException {
    MDC.put("document_id", fileEntry.getId());
    LOGGER.info("Handling Transfer");
    URLConnection fileUrlConnection = getUrlConnection(fileEntry.getUrl());
    PutObjectResponse response = s3Client.putObject(PutObjectRequest.builder()
                    .bucket(fileEntry.getS3().getBucket())
                    .key(fileEntry.getFileName())
                    .build(), RequestBody.fromInputStream(
            new BufferedInputStream(fileUrlConnection.getInputStream()),
            fileUrlConnection.getContentLength()
            )
    );
    fileEntry.getS3().setPushedAt(new Date().getTime());
    fileEntry.getS3().setPath(response.getValueForField("key", String.class).toString());
    LOGGER.info("Transfer done");
}

我的问题/评论/问题

  • @NewSpan 在 Zipkin 中没有添加 span,整个“transferToS3”被认为是一个 span。我尝试了其他几个注释都没有成功。
  • 为了拥有这个新的跨度,我使用ScopedSpan sp = tracer.startScopedSpanWithParent("getUrlConnection", tracer.currentSpan().context());sp.finish()。跨度在 ZipKin 中是可见的,但与仅放置 @NewSpan 相比,它并没有真正吸引人。 我错过了什么吗?

  • Elasticsearch APM 代理 + API 似乎只需添加 @CaptureTransaction@CaptureSpan 即可正确处理此问题。我知道它并不完美,因为它不直接挂接到消费者调用上,也不支持对我的用例进行有效跟踪。 但它也需要添加代理。

谢谢你:)。

【问题讨论】:

    标签: java spring-cloud spring-cloud-sleuth


    【解决方案1】:

    您正在从另一种方法调用一种方法。 Spring 正在围绕您的方法创建一个代理。如果你从同一个类的另一个调用一个方法,那么你就不会通过代理。将使用新 span 注释的方法提取到单独的类中,它会正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-15
      • 1970-01-01
      • 2013-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-10
      • 2013-10-30
      相关资源
      最近更新 更多