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"
|
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
|
|
|
|
2024-09-26 22:37:32 +00:00
|
|
|
gocommon "github.com/status-im/status-go/common"
|
|
|
|
|
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
|
|
|
|
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"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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{}{
|
|
|
|
"hostID": c.hostID,
|
|
|
|
"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) {
|
2024-09-26 22:37:32 +00:00
|
|
|
defer gocommon.LogOnPanic()
|
2024-07-24 18:28:38 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|