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"
|
2023-10-30 14:51:57 +00:00
|
|
|
|
|
|
|
v2protocol "github.com/waku-org/go-waku/waku/v2/protocol"
|
2021-11-03 12:38:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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),
|
2023-11-08 18:05:33 +00:00
|
|
|
"messageId": message.ApplicationLayer.ID,
|
2021-11-03 12:38:37 +00:00
|
|
|
"sentAt": sshMessage.Timestamp,
|
2023-05-22 21:38:02 +00:00
|
|
|
"pubsubTopic": filter.PubsubTopic,
|
|
|
|
"topic": filter.ContentTopic.String(),
|
2023-11-08 18:05:33 +00:00
|
|
|
"messageType": message.ApplicationLayer.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))
|
|
|
|
}
|
|
|
|
}
|
2023-10-30 14:51:57 +00:00
|
|
|
|
|
|
|
func (c *Client) PushReceivedEnvelope(envelope *v2protocol.Envelope) {
|
|
|
|
url := fmt.Sprintf("%s/received-envelope", c.serverURL)
|
|
|
|
postBody := map[string]interface{}{
|
|
|
|
"messageHash": types.EncodeHex(envelope.Hash()),
|
2023-12-05 04:29:27 +00:00
|
|
|
"sentAt": uint32(envelope.Message().GetTimestamp() / int64(time.Second)),
|
2023-10-30 14:51:57 +00:00
|
|
|
"pubsubTopic": envelope.PubsubTopic(),
|
|
|
|
"topic": envelope.Message().ContentTopic,
|
|
|
|
"receiverKeyUID": c.keyUID,
|
|
|
|
"nodeName": c.nodeName,
|
|
|
|
}
|
|
|
|
body, _ := json.Marshal(postBody)
|
|
|
|
_, err := c.httpClient.Post(url, "application/json", bytes.NewBuffer(body))
|
|
|
|
if err != nil {
|
|
|
|
c.logger.Error("Error sending envelope to telemetry server", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) UpdateEnvelopeProcessingError(shhMessage *types.Message, processingError error) {
|
|
|
|
c.logger.Debug("Pushing envelope update to telemetry server", zap.String("hash", types.EncodeHex(shhMessage.Hash)))
|
|
|
|
url := fmt.Sprintf("%s/update-envelope", c.serverURL)
|
|
|
|
var errorString = ""
|
|
|
|
if processingError != nil {
|
|
|
|
errorString = processingError.Error()
|
|
|
|
}
|
|
|
|
postBody := map[string]interface{}{
|
|
|
|
"messageHash": types.EncodeHex(shhMessage.Hash),
|
|
|
|
"sentAt": shhMessage.Timestamp,
|
|
|
|
"pubsubTopic": shhMessage.PubsubTopic,
|
|
|
|
"topic": shhMessage.Topic,
|
|
|
|
"receiverKeyUID": c.keyUID,
|
|
|
|
"nodeName": c.nodeName,
|
|
|
|
"processingError": errorString,
|
|
|
|
}
|
|
|
|
body, _ := json.Marshal(postBody)
|
|
|
|
_, err := c.httpClient.Post(url, "application/json", bytes.NewBuffer(body))
|
|
|
|
if err != nil {
|
|
|
|
c.logger.Error("Error sending envelope update to telemetry server", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|