使用@NewSpan 创建跨度时,不会发送时间戳和持续时间

When a span is created using @NewSpan, timestamp and duration are not sent

我正在使用 Spring Cloud Sleuth 和 Zipkin(通过 HTTP),通过将 spring-cloud-starter-zipkin 版本 2.0.0.M6 添加到我的依赖项中(基于 Spring 引导 2.0.0.RC1 和 Spring Cloud Finchley M6).

我正在使用@Newspan 批注来标记围绕某些(昂贵的)操作的子跨度。将跨度信息发送到 Zipkin 时,我注意到缺少子跨度的时间戳和持续时间。这会导致 Zipking 端出现奇怪的渲染。但是,当我通过调用 tracer#newChild 创建子跨度时,它按预期工作。

我错过了什么吗?这会是 Sleuth 2.0.0.M6 的问题吗?

当我 运行 使用 Spring Boot 1.5.9 和 Spring Cloud Edgware SR2 的相同代码时,它的行为符合预期。

这是 Zipkin 端收到的 JSON。名为 "child-span-with-annotation" 的范围是使用 @NewSpan 创建的,而范围 "childspanwithnewchild" 是使用 tracer#newChild 创建的。

[
  {
    "traceId": "b1c2636366c919be",
    "id": "b1c2636366c919be",
    "name": "get",
    "timestamp": 1518495271073166,
    "duration": 862032,
    "annotations": [
      {
        "timestamp": 1518495271073166,
        "value": "sr",
        "endpoint": {
          "serviceName": "sample-sleuth-app",
          "ipv4": "---.---.---.---"
        }
      },
      {
        "timestamp": 1518495271935198,
        "value": "ss",
        "endpoint": {
          "serviceName": "sample-sleuth-app",
          "ipv4": "---.---.---.---"
        }
      }
    ],
    "binaryAnnotations": [
      {
        "key": "ca",
        "value": true,
        "endpoint": {
          "serviceName": "",
          "ipv6": "::1",
          "port": 51982
        }
      },
      {
        "key": "http.path",
        "value": "/hello",
        "endpoint": {
          "serviceName": "sample-sleuth-app",
          "ipv4": "---.---.---.---"
        }
      },
      {
        "key": "mvc.controller.class",
        "value": "MyRestController",
        "endpoint": {
          "serviceName": "sample-sleuth-app",
          "ipv4": "---.---.---.---"
        }
      },
      {
        "key": "mvc.controller.method",
        "value": "sayHello",
        "endpoint": {
          "serviceName": "sample-sleuth-app",
          "ipv4": "---.---.---.---"
        }
      }
    ]
  },
  {
    "traceId": "b1c2636366c919be",
    "id": "14be7ac6eafb0e01",
    "name": "child-span-with-annotation",
    "parentId": "b1c2636366c919be",
    "binaryAnnotations": [
      {
        "key": "class",
        "value": "MyService",
        "endpoint": {
          "serviceName": "sample-sleuth-app",
          "ipv4": "---.---.---.---"
        }
      },
      {
        "key": "method",
        "value": "expensiveOperation1",
        "endpoint": {
          "serviceName": "sample-sleuth-app",
          "ipv4": "---.---.---.---"
        }
      }
    ]
  },
  {
    "traceId": "b1c2636366c919be",
    "id": "b34a4f910f27fdb4",
    "name": "childspanwithnewchild",
    "parentId": "b1c2636366c919be",
    "timestamp": 1518495271479040,
    "duration": 453747,
    "binaryAnnotations": [
      {
        "key": "lc",
        "value": "",
        "endpoint": {
          "serviceName": "sample-sleuth-app",
          "ipv4": "---.---.---.---"
        }
      }
    ]
  }
]

这是一个错误 - https://github.com/spring-cloud/spring-cloud-sleuth/issues/855。我已经在 ATM 上修好了。一种解决方法是在每个使用 @NewSpan 的方法中手动启动它,方法是在当前跨度上调用 start() 方法(不能很好地缩放)

@Autowired SpanCustomizer customizer;

@NewSpan
void foo() {
    this.customizer.start();
}

你也可以创建一个SpanCreator的bean(你可以在这里查看固定版本https://github.com/spring-cloud/spring-cloud-sleuth/blob/master/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/annotation/DefaultSpanCreator.java

class MySpanCreator implements SpanCreator {

    private static final Log log = LogFactory.getLog(MySpanCreator.class);

    private final Tracing tracer;

    MySpanCreator(Tracing tracer) {
        this.tracer = tracer;
    }

    @Override public Span createSpan(MethodInvocation pjp, NewSpan newSpanAnnotation) {
        String name = StringUtils.isEmpty(newSpanAnnotation.name()) ?
                pjp.getMethod().getName() : newSpanAnnotation.name();
        String changedName = SpanNameUtil.toLowerHyphen(name);
        if (log.isDebugEnabled()) {
            log.debug("For the class [" + pjp.getThis().getClass() + "] method "
                    + "[" + pjp.getMethod().getName() + "] will name the span [" + changedName + "]");
        }
        return this.tracer.tracer().nextSpan().name(changedName).start();
    }

}

注意方法末尾的 .start()