I have the following code:
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"
}
As you can see with the unmarshallSpan
, I want to end it after the if statement, but I do not know how to stop it correctly after the if check that comes right after it. I also want to start another child trace just before the withdrawalService.Withdraw
function, so the other child of unmarshalling should stop there. I also do need the return statements which makes it a bit harder. So my question is, how to close the child span unmarshallSpan
'correctly'?