我有以下代码:
func handleMessage(msg Message, out chan<- string, w withdrawalSocketListener) {
tracer := tracing.GetTracer()
ctx, span := tracer.Start(context.Background(), "receive-withdrawal-msg-tcp-socket")
defer func() {
span.End()
}()
wr := &model.WithdrawalRequest{}
_, unmarshallSpan := tracer.Start(ctx, "unmarshalling") // I should also End() this span after the if statement, but this if case with a return messes it up
if err := json.Unmarshal(msg.GetBody(), wr); err != nil {
log.Info().Err(err).Msg("failed to parse tcp inbound body")
out <- "NACK"
return
}
err := w.withdrawalService.Withdraw(wr)
if err != nil {
log.Info().Err(err).Msg("failed to process withdraw request")
out <- "NACK"
return
}
out <- "ACK"
}
正如你在unmarshallSpan
中看到的,我想在if语句之后结束它,但是我不知道如何在它后面的if检查之后正确地停止它。我还想在withdrawalService.Withdraw
函数之前启动另一个子跟踪,因此解组的另一个子跟踪应该在那里停止。我还需要return语句,这使得它有点困难。所以我的问题是,如何正确地关闭子跨度unmarshallSpan
?
1条答案
按热度按时间guz6ccqo1#
这里简单的答案是调用
End
两次。我还建议设置跨度来表示错误:然而,除了最简单的情况之外,创建一个helper函数来使其可重复也是有意义的(因此您也可以对
w.withdrawalService.Withdraw(wr)
执行相同的操作)。例如:然后,您的示例将使用
withSpan
,如下所示: