package inbound import ( "cmpp-platform/gateway/internal/protocollog" "context" "fmt" cmpp "github.com/bigwhite/gocmpp" "log" "strconv" ) type protocolLogEvent = protocollog.Event func (s Server) submitResponseProtocolLogger( account string, protocol string, sequenceID uint32, phone string, messageID string, gatewayMessageID uint64, result uint32, ) func(error) { return func(sendErr error) { s.emitProtocolLog(protocolLogEvent{ Protocol: "cmpp", Direction: "platform_to_client", EventType: "submit_resp", Status: protocolSendStatus(sendErr), Account: account, MessageID: messageID, GatewayMessageID: fmt.Sprint(gatewayMessageID), Phone: phone, ResultCode: protocolSendResultCode(sendErr, result), Detail: protocolSubmitResponseDetail(sequenceID, sendErr), }) } } func protocolSendStatus(sendErr error) string { if sendErr != nil { return "failed" } return "success" } func protocolSendResultCode(sendErr error, result uint32) string { if sendErr != nil { return "SEND_FAILED" } return fmt.Sprint(result) } func protocolSubmitResponseDetail(sequenceID uint32, sendErr error) map[string]any { detail := map[string]any{"sequenceId": sequenceID} if sendErr != nil { detail["error"] = sendErr.Error() } return detail } func (s Server) emitProtocolLog(event protocolLogEvent) { event.GatewayInstanceID = s.GatewayInstanceID if s.ProtocolLogPublisher != nil { go func() { if err := s.ProtocolLogPublisher.Publish(context.Background(), event); err != nil { log.Printf("cmpp inbound protocol log Redis publish failed account=%s message_id=%s error=%q", event.Account, event.MessageID, err) } }() return } go func() { ctx, cancel := context.WithTimeout(context.Background(), defaultHTTPTimeout) defer cancel() if err := s.post(ctx, "/gateway/events/protocol-log", event, nil); err != nil { log.Printf("cmpp inbound protocol_event direction=%s event=%s status=telemetry_failed account=%s message_id=%s error=%q", event.Direction, event.EventType, event.Account, event.MessageID, err) } }() } func (session *downstreamSession) recordDownstreamProtocol( deliver cmpp.Packer, deliveryID string, sequenceID uint32, messageID uint64, status string, resultCode string, sendErr error, ) { if session == nil || session.protocolLog == nil { return } eventType, phone := downstreamDeliverMetadata(deliver) detail := map[string]any{"sequenceId": sequenceID, "deliveryId": deliveryID} if sendErr != nil { detail["error"] = sendErr.Error() } session.protocolLog(protocolLogEvent{ Protocol: "cmpp", Direction: "platform_to_client", EventType: eventType, Status: status, TenantID: session.tenantID, ApplicationID: session.applicationID, Account: session.account, MessageID: session.messageID, GatewayMessageID: strconv.FormatUint(messageID, 10), Phone: defaultString(phone, session.phoneNumber), ResultCode: resultCode, Detail: detail, }) }