Files
lislgosms/gateway/internal/upstream/protocol_log.go
T

82 lines
2.7 KiB
Go

package upstream
import (
"context"
"fmt"
cmpp "github.com/bigwhite/gocmpp"
"log"
"strings"
)
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)
}
}()
}