Files
lislgosms/gateway/internal/upstream/protocol_log.go
T
hectorzhao 001d5f2cbd
CSS quality / css-quality (push) Has been cancelled
fix: 修复 CMPP 协议字段容量与版本兼容性
2026-09-20 15:49:37 +08:00

78 lines
2.3 KiB
Go

package upstream
import (
"cmpp-platform/gateway/internal/protocollog"
"context"
"fmt"
cmpp "github.com/bigwhite/gocmpp"
"log"
"strings"
)
type protocolLogEvent = protocollog.Event
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.UnpackVersion([]byte(pkt.msgContent), pkt.version); 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) {
event.ConnectionID = c.identity()
event.GatewayInstanceID = c.gatewayInstanceID
if c.protocolLogPublisher != nil {
go func() {
if err := c.protocolLogPublisher.Publish(context.Background(), event); err != nil {
log.Printf("protocol log Redis publish failed channel_id=%s message_id=%s error=%q", event.ChannelID, event.MessageID, err)
}
}()
return
}
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)
}
}()
}