status-go/wakuv2/telemetry.go
frank 38308d48f2
feat_: log on panic (#5849)
* feat_: log error and stacktrace when panic in goroutine

* test_: add test TestSafeGo

* chore_: rename logAndCall to call

* chore_: rename SafeGo to Go

* chore_: make lint-fix

* chore_: use t.Cleanup

* chore_: Revert "chore_: use t.Cleanup"

This reverts commit 4eb420d179cc0e208e84c13cb941e6b3d1ed9819.

* chore_: Revert "chore_: make lint-fix"

This reverts commit fcc995f157e671a4229b47419c3a0e4004b5fdab.

* chore_: Revert "chore_: rename SafeGo to Go"

This reverts commit a6d73d6df583f313032d79aac62f66328039cb55.

* chore_: Revert "chore_: rename logAndCall to call"

This reverts commit 8fbe993bedb9fbba67349a44f151e2dd5e3bc4cc.

* chore_: Revert "test_: add test TestSafeGo"

This reverts commit a1fa91839f3960398980c6bf456e6462ec944819.

* chore_: Revert "feat_: log error and stacktrace when panic in goroutine"

This reverts commit f612dd828fa2ce410d0e806fe773ecbe3e86a68a.

* feat_: log error and stacktrace when panic in goroutine

* chore_: make lint-fix

* chore_: rename logAndCall to call

* chore_: renaming LogOnPanic

* chore_: update rest goroutine function calls

* chore_: make lint-fix
2024-09-27 06:37:32 +08:00

68 lines
2.2 KiB
Go

package wakuv2
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/google/uuid"
"github.com/libp2p/go-libp2p/core/metrics"
"github.com/libp2p/go-libp2p/core/protocol"
"go.uber.org/zap"
gocommon "github.com/status-im/status-go/common"
"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"
)
type BandwidthTelemetryClient struct {
serverURL string
httpClient *http.Client
hostID string
logger *zap.Logger
}
func NewBandwidthTelemetryClient(logger *zap.Logger, serverURL string) *BandwidthTelemetryClient {
return &BandwidthTelemetryClient{
serverURL: serverURL,
httpClient: &http.Client{Timeout: time.Minute},
hostID: uuid.NewString(),
logger: logger.Named("bandwidth-telemetry"),
}
}
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,
}
}
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) {
defer gocommon.LogOnPanic()
url := fmt.Sprintf("%s/protocol-stats", c.serverURL)
body, _ := json.Marshal(c.getTelemetryRequestBody(stats))
_, 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))
}
}