fix: reconcile shared-channel receipts and protocol logs
This commit is contained in:
@@ -637,10 +637,46 @@ func (c *connection) submitPart(ctx context.Context, cmd queue.SubmitCommand, pa
|
||||
seq, err := client.SendReqPkt(pkt)
|
||||
c.sendMu.Unlock()
|
||||
if err != nil {
|
||||
c.emitProtocolLog(protocolLogEvent{
|
||||
Protocol: "cmpp",
|
||||
Direction: "platform_to_channel",
|
||||
EventType: "submit",
|
||||
Status: "failed",
|
||||
TenantID: cmd.TenantID,
|
||||
ApplicationID: cmd.ApplicationID,
|
||||
ChannelID: cmd.ChannelID,
|
||||
Account: c.config.Account,
|
||||
MessageID: cmd.MessageID,
|
||||
Phone: cmd.PhoneNumber,
|
||||
ResultCode: "SEND_FAILED",
|
||||
PayloadBytes: len(part.MsgContent),
|
||||
Detail: map[string]any{
|
||||
"segmentTotal": part.PkTotal,
|
||||
"segmentIndex": part.PkNumber,
|
||||
},
|
||||
})
|
||||
c.close()
|
||||
result := submitResult(cmd, 0, "", "timeout", "SEND_FAILED", err.Error())
|
||||
return 0, "", result, err
|
||||
}
|
||||
c.emitProtocolLog(protocolLogEvent{
|
||||
Protocol: "cmpp",
|
||||
Direction: "platform_to_channel",
|
||||
EventType: "submit",
|
||||
Status: "success",
|
||||
TenantID: cmd.TenantID,
|
||||
ApplicationID: cmd.ApplicationID,
|
||||
ChannelID: cmd.ChannelID,
|
||||
Account: c.config.Account,
|
||||
MessageID: cmd.MessageID,
|
||||
Phone: cmd.PhoneNumber,
|
||||
PayloadBytes: len(part.MsgContent),
|
||||
Detail: map[string]any{
|
||||
"sequenceId": seq,
|
||||
"segmentTotal": part.PkTotal,
|
||||
"segmentIndex": part.PkNumber,
|
||||
},
|
||||
})
|
||||
|
||||
c.mu.Lock()
|
||||
c.pending[seq] = rspCh
|
||||
@@ -671,6 +707,25 @@ func (c *connection) submitPart(ctx context.Context, cmd queue.SubmitCommand, pa
|
||||
errorCode = fmt.Sprint(rsp.result)
|
||||
errorMessage = fmt.Sprintf("upstream submit rejected with result %d", rsp.result)
|
||||
}
|
||||
c.emitProtocolLog(protocolLogEvent{
|
||||
Protocol: "cmpp",
|
||||
Direction: "channel_to_platform",
|
||||
EventType: "submit_resp",
|
||||
Status: "success",
|
||||
TenantID: cmd.TenantID,
|
||||
ApplicationID: cmd.ApplicationID,
|
||||
ChannelID: cmd.ChannelID,
|
||||
Account: c.config.Account,
|
||||
MessageID: cmd.MessageID,
|
||||
GatewayMessageID: gatewayMessageID,
|
||||
Phone: cmd.PhoneNumber,
|
||||
ResultCode: fmt.Sprint(rsp.result),
|
||||
Detail: map[string]any{
|
||||
"sequenceId": rsp.seqID,
|
||||
"segmentTotal": part.PkTotal,
|
||||
"segmentIndex": part.PkNumber,
|
||||
},
|
||||
})
|
||||
if rsp.result == 0 {
|
||||
c.mu.Lock()
|
||||
c.tracker[rsp.msgID] = cmd
|
||||
@@ -823,10 +878,12 @@ func (c *connection) readLoop() {
|
||||
ch <- submitPartResponse{seqID: p.SeqId, msgID: p.MsgId, result: p.Result}
|
||||
}
|
||||
case *cmpp.Cmpp2DeliverReqPkt:
|
||||
_ = c.sendResponse(client, &cmpp.Cmpp2DeliverRspPkt{MsgId: p.MsgId, Result: 0}, p.SeqId)
|
||||
responseErr := c.sendResponse(client, &cmpp.Cmpp2DeliverRspPkt{MsgId: p.MsgId, Result: 0}, p.SeqId)
|
||||
c.emitDeliverResponse(deliverPacketFromCMPP2(p), responseErr)
|
||||
c.handleDeliver(deliverPacketFromCMPP2(p))
|
||||
case *cmpp.Cmpp3DeliverReqPkt:
|
||||
_ = c.sendResponse(client, &cmpp.Cmpp3DeliverRspPkt{MsgId: p.MsgId, Result: 0}, p.SeqId)
|
||||
responseErr := c.sendResponse(client, &cmpp.Cmpp3DeliverRspPkt{MsgId: p.MsgId, Result: 0}, p.SeqId)
|
||||
c.emitDeliverResponse(deliverPacketFromCMPP3(p), responseErr)
|
||||
c.handleDeliver(deliverPacketFromCMPP3(p))
|
||||
case *cmpp.CmppActiveTestReqPkt:
|
||||
_ = c.sendResponse(client, &cmpp.CmppActiveTestRspPkt{}, p.SeqId)
|
||||
@@ -914,6 +971,78 @@ type deliverPacket struct {
|
||||
msgContent string
|
||||
}
|
||||
|
||||
type protocolLogEvent struct {
|
||||
Protocol string `json:"protocol"`
|
||||
Direction string `json:"direction"`
|
||||
EventType string `json:"eventType"`
|
||||
Status string `json:"status"`
|
||||
TenantID string `json:"tenantId,omitempty"`
|
||||
ApplicationID string `json:"applicationId,omitempty"`
|
||||
ChannelID string `json:"channelId,omitempty"`
|
||||
Account string `json:"account,omitempty"`
|
||||
MessageID string `json:"messageId,omitempty"`
|
||||
GatewayMessageID string `json:"gatewayMessageId,omitempty"`
|
||||
Phone string `json:"phone,omitempty"`
|
||||
ResultCode string `json:"resultCode,omitempty"`
|
||||
PayloadBytes int `json:"payloadBytes,omitempty"`
|
||||
Detail map[string]any `json:"detail,omitempty"`
|
||||
}
|
||||
|
||||
func (c *connection) emitDeliverResponse(pkt deliverPacket, responseErr error) {
|
||||
status := "success"
|
||||
resultCode := "0"
|
||||
detail := map[string]any{"sequenceId": pkt.seqID}
|
||||
gatewayMessageID := fmt.Sprint(pkt.msgID)
|
||||
messageID := ""
|
||||
phone := ""
|
||||
tenantID := ""
|
||||
applicationID := ""
|
||||
channelID := c.channelID
|
||||
if pkt.registerDelivery == 1 {
|
||||
var receipt cmpp.CmppReceiptPkt
|
||||
if err := receipt.Unpack([]byte(pkt.msgContent)); err == nil {
|
||||
gatewayMessageID = fmt.Sprint(receipt.MsgId)
|
||||
phone = strings.TrimSpace(receipt.DestTerminalId)
|
||||
if cmd, ok := c.commandFor(receipt.MsgId); ok {
|
||||
messageID = cmd.MessageID
|
||||
tenantID = cmd.TenantID
|
||||
applicationID = cmd.ApplicationID
|
||||
channelID = cmd.ChannelID
|
||||
}
|
||||
}
|
||||
}
|
||||
if responseErr != nil {
|
||||
status = "failed"
|
||||
resultCode = "SEND_FAILED"
|
||||
detail["error"] = responseErr.Error()
|
||||
}
|
||||
c.emitProtocolLog(protocolLogEvent{
|
||||
Protocol: "cmpp",
|
||||
Direction: "platform_to_channel",
|
||||
EventType: "deliver_resp",
|
||||
Status: status,
|
||||
TenantID: tenantID,
|
||||
ApplicationID: applicationID,
|
||||
ChannelID: channelID,
|
||||
Account: c.config.Account,
|
||||
MessageID: messageID,
|
||||
GatewayMessageID: gatewayMessageID,
|
||||
Phone: phone,
|
||||
ResultCode: resultCode,
|
||||
Detail: detail,
|
||||
})
|
||||
}
|
||||
|
||||
func (c *connection) emitProtocolLog(event protocolLogEvent) {
|
||||
go func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), defaultHTTPTimeout)
|
||||
defer cancel()
|
||||
if err := postJSON(ctx, c.httpClient, c.apiBaseURL, "/gateway/events/protocol-log", event); err != nil {
|
||||
log.Printf("protocol_event protocol=%s direction=%s event=%s status=telemetry_failed channel_id=%s message_id=%s error=%q", event.Protocol, event.Direction, event.EventType, event.ChannelID, event.MessageID, err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func deliverPacketFromCMPP2(pkt *cmpp.Cmpp2DeliverReqPkt) deliverPacket {
|
||||
return deliverPacket{
|
||||
seqID: pkt.SeqId,
|
||||
|
||||
Reference in New Issue
Block a user