2021-11-03 12:38:37 +00:00
|
|
|
package telemetry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
"github.com/status-im/status-go/protocol/transport"
|
|
|
|
v1protocol "github.com/status-im/status-go/protocol/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
serverURL string
|
|
|
|
httpClient *http.Client
|
|
|
|
logger *zap.Logger
|
|
|
|
keyUID string
|
|
|
|
nodeName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient(logger *zap.Logger, serverURL string, keyUID string, nodeName string) *Client {
|
|
|
|
return &Client{
|
|
|
|
serverURL: serverURL,
|
|
|
|
httpClient: &http.Client{Timeout: time.Minute},
|
|
|
|
logger: logger,
|
|
|
|
keyUID: keyUID,
|
|
|
|
nodeName: nodeName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) PushReceivedMessages(filter transport.Filter, sshMessage *types.Message, messages []*v1protocol.StatusMessage) {
|
|
|
|
c.logger.Debug("Pushing received messages to telemetry server")
|
|
|
|
url := fmt.Sprintf("%s/received-messages", c.serverURL)
|
|
|
|
var postBody []map[string]interface{}
|
|
|
|
for _, message := range messages {
|
|
|
|
postBody = append(postBody, map[string]interface{}{
|
|
|
|
"chatId": filter.ChatID,
|
|
|
|
"messageHash": types.EncodeHex(sshMessage.Hash),
|
|
|
|
"messageId": message.ID,
|
|
|
|
"sentAt": sshMessage.Timestamp,
|
|
|
|
"topic": filter.Topic.String(),
|
2023-01-26 15:07:35 +00:00
|
|
|
"messageType": message.Type.String(),
|
2021-11-03 12:38:37 +00:00
|
|
|
"receiverKeyUID": c.keyUID,
|
|
|
|
"nodeName": c.nodeName,
|
2023-02-01 15:46:01 +00:00
|
|
|
"messageSize": len(sshMessage.Payload),
|
2021-11-03 12:38:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
body, _ := json.Marshal(postBody)
|
|
|
|
_, err := c.httpClient.Post(url, "application/json", bytes.NewBuffer(body))
|
|
|
|
if err != nil {
|
|
|
|
c.logger.Error("Error sending message to telemetry server", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|