2023-01-31 22:46:47 +00:00
|
|
|
package wakuv2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p/core/metrics"
|
2024-07-24 18:28:38 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/protocol"
|
2023-01-31 22:46:47 +00:00
|
|
|
"go.uber.org/zap"
|
2024-07-24 18:28:38 +00:00
|
|
|
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/filter"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/legacy_store"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/lightpush"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/relay"
|
2023-01-31 22:46:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type BandwidthTelemetryClient struct {
|
|
|
|
serverURL string
|
|
|
|
httpClient *http.Client
|
2024-10-03 13:29:45 +00:00
|
|
|
peerId string
|
2023-01-31 22:46:47 +00:00
|
|
|
logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
2024-10-03 13:29:45 +00:00
|
|
|
func NewBandwidthTelemetryClient(logger *zap.Logger, serverURL string, peerId string) *BandwidthTelemetryClient {
|
2023-01-31 22:46:47 +00:00
|
|
|
return &BandwidthTelemetryClient{
|
|
|
|
serverURL: serverURL,
|
|
|
|
httpClient: &http.Client{Timeout: time.Minute},
|
2024-10-03 13:29:45 +00:00
|
|
|
peerId: peerId,
|
2023-01-31 22:46:47 +00:00
|
|
|
logger: logger.Named("bandwidth-telemetry"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-24 18:28:38 +00:00
|
|
|
func getStatsPerProtocol(protocolID protocol.ID, stats map[protocol.ID]metrics.Stats) map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"rateIn": stats[protocolID].RateIn,
|
|
|
|
"rateOut": stats[protocolID].RateOut,
|
|
|
|
"totalIn": stats[protocolID].TotalIn,
|
|
|
|
"totalOut": stats[protocolID].TotalOut,
|
2023-01-31 22:46:47 +00:00
|
|
|
}
|
2024-07-24 18:28:38 +00:00
|
|
|
}
|
2023-01-31 22:46:47 +00:00
|
|
|
|
2024-07-24 18:28:38 +00:00
|
|
|
func (c *BandwidthTelemetryClient) getTelemetryRequestBody(stats map[protocol.ID]metrics.Stats) map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
2024-10-03 13:29:45 +00:00
|
|
|
"hostID": c.peerId,
|
2024-07-24 18:28:38 +00:00
|
|
|
"relay": getStatsPerProtocol(relay.WakuRelayID_v200, stats),
|
|
|
|
"store": getStatsPerProtocol(legacy_store.StoreID_v20beta4, stats),
|
|
|
|
"filter-push": getStatsPerProtocol(filter.FilterPushID_v20beta1, stats),
|
|
|
|
"filter-subscribe": getStatsPerProtocol(filter.FilterSubscribeID_v20beta1, stats),
|
|
|
|
"lightpush": getStatsPerProtocol(lightpush.LightPushID_v20beta1, stats),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BandwidthTelemetryClient) PushProtocolStats(stats map[protocol.ID]metrics.Stats) {
|
|
|
|
url := fmt.Sprintf("%s/protocol-stats", c.serverURL)
|
|
|
|
body, _ := json.Marshal(c.getTelemetryRequestBody(stats))
|
2023-01-31 22:46:47 +00:00
|
|
|
_, 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))
|
|
|
|
}
|
|
|
|
}
|