2023-01-31 22:46:47 +00:00
|
|
|
package wakuv2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2023-02-17 13:15:28 +00:00
|
|
|
"github.com/google/uuid"
|
2023-01-31 22:46:47 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/metrics"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BandwidthTelemetryClient struct {
|
|
|
|
serverURL string
|
|
|
|
httpClient *http.Client
|
|
|
|
hostID string
|
|
|
|
logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
2023-02-17 13:15:28 +00:00
|
|
|
func NewBandwidthTelemetryClient(logger *zap.Logger, serverURL string) *BandwidthTelemetryClient {
|
2023-01-31 22:46:47 +00:00
|
|
|
return &BandwidthTelemetryClient{
|
|
|
|
serverURL: serverURL,
|
|
|
|
httpClient: &http.Client{Timeout: time.Minute},
|
2023-02-17 13:15:28 +00:00
|
|
|
hostID: uuid.NewString(),
|
2023-01-31 22:46:47 +00:00
|
|
|
logger: logger.Named("bandwidth-telemetry"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BandwidthTelemetryClient) PushProtocolStats(relayStats metrics.Stats, storeStats metrics.Stats) {
|
|
|
|
url := fmt.Sprintf("%s/protocol-stats", c.serverURL)
|
|
|
|
postBody := map[string]interface{}{
|
|
|
|
"hostID": c.hostID,
|
|
|
|
"relay": map[string]interface{}{
|
|
|
|
"rateIn": relayStats.RateIn,
|
|
|
|
"rateOut": relayStats.RateOut,
|
|
|
|
"totalIn": relayStats.TotalIn,
|
|
|
|
"totalOut": relayStats.TotalOut,
|
|
|
|
},
|
|
|
|
"store": map[string]interface{}{
|
|
|
|
"rateIn": storeStats.RateIn,
|
|
|
|
"rateOut": storeStats.RateOut,
|
|
|
|
"totalIn": storeStats.TotalIn,
|
|
|
|
"totalOut": storeStats.TotalOut,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|