github.com/status-im/status-go@v1.1.0/wakuv2/telemetry.go (about) 1 package wakuv2 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "net/http" 8 "time" 9 10 "github.com/google/uuid" 11 "github.com/libp2p/go-libp2p/core/metrics" 12 "github.com/libp2p/go-libp2p/core/protocol" 13 "go.uber.org/zap" 14 15 "github.com/waku-org/go-waku/waku/v2/protocol/filter" 16 "github.com/waku-org/go-waku/waku/v2/protocol/legacy_store" 17 "github.com/waku-org/go-waku/waku/v2/protocol/lightpush" 18 "github.com/waku-org/go-waku/waku/v2/protocol/relay" 19 ) 20 21 type BandwidthTelemetryClient struct { 22 serverURL string 23 httpClient *http.Client 24 hostID string 25 logger *zap.Logger 26 } 27 28 func NewBandwidthTelemetryClient(logger *zap.Logger, serverURL string) *BandwidthTelemetryClient { 29 return &BandwidthTelemetryClient{ 30 serverURL: serverURL, 31 httpClient: &http.Client{Timeout: time.Minute}, 32 hostID: uuid.NewString(), 33 logger: logger.Named("bandwidth-telemetry"), 34 } 35 } 36 37 func getStatsPerProtocol(protocolID protocol.ID, stats map[protocol.ID]metrics.Stats) map[string]interface{} { 38 return map[string]interface{}{ 39 "rateIn": stats[protocolID].RateIn, 40 "rateOut": stats[protocolID].RateOut, 41 "totalIn": stats[protocolID].TotalIn, 42 "totalOut": stats[protocolID].TotalOut, 43 } 44 } 45 46 func (c *BandwidthTelemetryClient) getTelemetryRequestBody(stats map[protocol.ID]metrics.Stats) map[string]interface{} { 47 return map[string]interface{}{ 48 "hostID": c.hostID, 49 "relay": getStatsPerProtocol(relay.WakuRelayID_v200, stats), 50 "store": getStatsPerProtocol(legacy_store.StoreID_v20beta4, stats), 51 "filter-push": getStatsPerProtocol(filter.FilterPushID_v20beta1, stats), 52 "filter-subscribe": getStatsPerProtocol(filter.FilterSubscribeID_v20beta1, stats), 53 "lightpush": getStatsPerProtocol(lightpush.LightPushID_v20beta1, stats), 54 } 55 } 56 57 func (c *BandwidthTelemetryClient) PushProtocolStats(stats map[protocol.ID]metrics.Stats) { 58 url := fmt.Sprintf("%s/protocol-stats", c.serverURL) 59 body, _ := json.Marshal(c.getTelemetryRequestBody(stats)) 60 _, err := c.httpClient.Post(url, "application/json", bytes.NewBuffer(body)) 61 if err != nil { 62 c.logger.Error("Error sending message to telemetry server", zap.Error(err)) 63 } 64 }