package inbound import ( "context" "fmt" cmpp "github.com/bigwhite/gocmpp" "log" "strconv" ) 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"` 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"` Detail map[string]any `json:"detail,omitempty"` } 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) { 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, }) }