Skip to content

Commit

Permalink
Merge pull request #51 from anyproto/GO-2420-debug-stat
Browse files Browse the repository at this point in the history
GO-2420: Add debugstat
  • Loading branch information
mcrakhman authored Nov 21, 2023
2 parents 6aa0889 + 619901e commit aeadb0f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 26 deletions.
43 changes: 24 additions & 19 deletions cmd/any-sync-node.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"context"
"flag"
"fmt"
"github.com/anyproto/any-sync-node/nodehead"
"github.com/anyproto/any-sync-node/nodespace/peermanager"
"github.com/anyproto/any-sync-node/nodespace/spacedeleter"
"github.com/anyproto/any-sync-node/nodespace/statusprovider"
"github.com/anyproto/any-sync-node/nodesync"
"github.com/anyproto/any-sync-node/nodesync/coldsync"
"github.com/anyproto/any-sync-node/nodesync/hotsync"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"syscall"
"time"

"github.com/anyproto/any-sync/app/debugstat"
"github.com/anyproto/any-sync/commonspace/credentialprovider"
"github.com/anyproto/any-sync/consensus/consensusclient"
"github.com/anyproto/any-sync/coordinator/coordinatorclient"
Expand All @@ -24,26 +25,29 @@ import (
"github.com/anyproto/any-sync/net/transport/yamux"
"github.com/anyproto/any-sync/nodeconf"
"github.com/anyproto/any-sync/nodeconf/nodeconfstore"

"github.com/anyproto/any-sync-node/nodehead"
"github.com/anyproto/any-sync-node/nodespace/peermanager"
"github.com/anyproto/any-sync-node/nodespace/spacedeleter"
"github.com/anyproto/any-sync-node/nodespace/statusprovider"
"github.com/anyproto/any-sync-node/nodesync"
"github.com/anyproto/any-sync-node/nodesync/coldsync"
"github.com/anyproto/any-sync-node/nodesync/hotsync"
// import this to keep govvv in go.mod on mod tidy
_ "github.com/ahmetb/govvv/integration-test/app-different-package/mypkg"
"github.com/anyproto/any-sync-node/account"
"github.com/anyproto/any-sync-node/config"
"github.com/anyproto/any-sync-node/debug/nodedebugrpc"
"github.com/anyproto/any-sync-node/nodespace"
"github.com/anyproto/any-sync-node/nodespace/nodecache"
"github.com/anyproto/any-sync-node/nodestorage"
"github.com/anyproto/any-sync/app"
"github.com/anyproto/any-sync/app/logger"
"github.com/anyproto/any-sync/commonspace"
"github.com/anyproto/any-sync/metric"
"github.com/anyproto/any-sync/net/secureservice"
"go.uber.org/zap"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"syscall"
"time"

"github.com/anyproto/any-sync-node/account"
"github.com/anyproto/any-sync-node/config"
"github.com/anyproto/any-sync-node/debug/nodedebugrpc"
"github.com/anyproto/any-sync-node/nodespace"
"github.com/anyproto/any-sync-node/nodespace/nodecache"
"github.com/anyproto/any-sync-node/nodestorage"
)

var log = logger.NewNamed("main")
Expand Down Expand Up @@ -113,6 +117,7 @@ func main() {

func Bootstrap(a *app.App) {
a.Register(account.New()).
Register(debugstat.New()).
Register(statusprovider.New()).
Register(credentialprovider.NewNoOp()).
Register(coordinatorclient.New()).
Expand Down
31 changes: 27 additions & 4 deletions debug/nodedebugrpc/nodedebugrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@ package nodedebugrpc

import (
"context"
"github.com/anyproto/any-sync-node/debug/nodedebugrpc/nodedebugrpcproto"
"github.com/anyproto/any-sync-node/nodespace"
nodestorage "github.com/anyproto/any-sync-node/nodestorage"
"github.com/anyproto/any-sync-node/nodesync"
"encoding/json"
"net/http"

"github.com/anyproto/any-sync/app"
"github.com/anyproto/any-sync/app/debugstat"
"github.com/anyproto/any-sync/app/logger"
"github.com/anyproto/any-sync/commonspace/object/treemanager"
"github.com/anyproto/any-sync/commonspace/spacestorage"
"github.com/anyproto/any-sync/net/rpc/debugserver"
"github.com/anyproto/any-sync/net/secureservice"
"github.com/anyproto/any-sync/nodeconf"
"go.uber.org/zap"

"github.com/anyproto/any-sync-node/debug/nodedebugrpc/nodedebugrpcproto"
"github.com/anyproto/any-sync-node/nodespace"
nodestorage "github.com/anyproto/any-sync-node/nodestorage"
"github.com/anyproto/any-sync-node/nodesync"
)

const CName = "node.debug.nodedebugrpc"
Expand All @@ -35,6 +41,7 @@ type nodeDebugRpc struct {
nodeSync nodesync.NodeSync
nodeConf nodeconf.Service
server debugserver.DebugServer
statService debugstat.StatService
}

func (s *nodeDebugRpc) Init(a *app.App) (err error) {
Expand All @@ -45,6 +52,8 @@ func (s *nodeDebugRpc) Init(a *app.App) (err error) {
s.nodeSync = a.MustComponent(nodesync.CName).(nodesync.NodeSync)
s.nodeConf = a.MustComponent(nodeconf.CName).(nodeconf.Service)
s.server = a.MustComponent(debugserver.CName).(debugserver.DebugServer)
s.statService = a.MustComponent(debugstat.CName).(debugstat.StatService)
http.HandleFunc("/stats", s.handleStats)
return nil
}

Expand All @@ -61,3 +70,17 @@ func (s *nodeDebugRpc) Run(ctx context.Context) (err error) {
func (s *nodeDebugRpc) Close(ctx context.Context) (err error) {
return nil
}

func (s *nodeDebugRpc) handleStats(rw http.ResponseWriter, req *http.Request) {
stats := s.statService.GetStat()
rw.Header().Set("Content-Type", "application/json")
marshalled, err := json.MarshalIndent(stats, "", " ")
if err != nil {
log.Error("failed to marshal stat", zap.Error(err))
rw.WriteHeader(http.StatusInternalServerError)
rw.Write([]byte("{\"error\": \"failed to marshal stat\"}"))
return
}
rw.WriteHeader(http.StatusOK)
_, _ = rw.Write(marshalled)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21
require (
github.com/ahmetb/govvv v0.3.0
github.com/akrylysov/pogreb v0.10.1
github.com/anyproto/any-sync v0.3.8
github.com/anyproto/any-sync v0.3.9
github.com/anyproto/go-chash v0.1.0
github.com/gogo/protobuf v1.3.2
github.com/prometheus/client_golang v1.17.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ github.com/ahmetb/govvv v0.3.0 h1:YGLGwEyiUwHFy5eh/RUhdupbuaCGBYn5T5GWXp+WJB0=
github.com/ahmetb/govvv v0.3.0/go.mod h1:4WRFpdWtc/YtKgPFwa1dr5+9hiRY5uKAL08bOlxOR6s=
github.com/akrylysov/pogreb v0.10.1 h1:FqlR8VR7uCbJdfUob916tPM+idpKgeESDXOA1K0DK4w=
github.com/akrylysov/pogreb v0.10.1/go.mod h1:pNs6QmpQ1UlTJKDezuRWmaqkgUE2TuU0YTWyqJZ7+lI=
github.com/anyproto/any-sync v0.3.8 h1:MusYy5HmkWQWeLGPetrnl9N+sVHOqkk+e4EXHfd6aeA=
github.com/anyproto/any-sync v0.3.8/go.mod h1:ZM/hfNni7fdzSYzDqSg8QyO1iIxWskWjYiykT1qaceo=
github.com/anyproto/any-sync v0.3.9 h1:VkjehKQM2P/v58/vCNLM4qbznEIiSrMy/BG9sidDyIc=
github.com/anyproto/any-sync v0.3.9/go.mod h1:ZM/hfNni7fdzSYzDqSg8QyO1iIxWskWjYiykT1qaceo=
github.com/anyproto/go-chash v0.1.0 h1:I9meTPjXFRfXZHRJzjOHC/XF7Q5vzysKkiT/grsogXY=
github.com/anyproto/go-chash v0.1.0/go.mod h1:0UjNQi3PDazP0fINpFYu6VKhuna+W/V+1vpXHAfNgLY=
github.com/anyproto/go-slip10 v1.0.0 h1:uAEtSuudR3jJBOfkOXf3bErxVoxbuKwdoJN55M1i6IA=
Expand Down

0 comments on commit aeadb0f

Please sign in to comment.