Skip to content

Commit

Permalink
Switch to Go modules and current nats-server
Browse files Browse the repository at this point in the history
  • Loading branch information
philpennock committed Mar 3, 2021
1 parent 1c2a692 commit 6fe66de
Show file tree
Hide file tree
Showing 142 changed files with 116 additions and 23,403 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ _testmain.go
*.exe
*.test
*.prof

/nats-top
12 changes: 12 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module github.com/nats-io/nats-top

go 1.16

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mattn/go-runewidth v0.0.1 // indirect
github.com/nats-io/nats-server/v2 v2.1.9
github.com/nsf/termbox-go v0.0.0-20160808045038-e8f6d27f72a2 // indirect
github.com/stretchr/testify v1.7.0 // indirect
gopkg.in/gizak/termui.v1 v1.0.0-20151021151108-e62b5929642a
)
54 changes: 54 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/mattn/go-runewidth v0.0.1 h1:+EiaBVXhogb1Klb4tRJ7hYnuGK6PkKOZlK04D/GMOqk=
github.com/mattn/go-runewidth v0.0.1/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/nats-io/jwt v0.3.2/go.mod h1:/euKqTS1ZD+zzjYrY7pseZrTtWQSjujC7xjPc8wL6eU=
github.com/nats-io/jwt v1.1.0 h1:+vOlgtM0ZsF46GbmUoadq0/2rChNS45gtxHEa3H1gqM=
github.com/nats-io/jwt v1.1.0/go.mod h1:n3cvmLfBfnpV4JJRN7lRYCyZnw48ksGsbThGXEk4w9M=
github.com/nats-io/nats-server/v2 v2.1.9 h1:Sxr2zpaapgpBT9ElTxTVe62W+qjnhPcKY/8W5cnA/Qk=
github.com/nats-io/nats-server/v2 v2.1.9/go.mod h1:9qVyoewoYXzG1ME9ox0HwkkzyYvnlBDugfR4Gg/8uHU=
github.com/nats-io/nats.go v1.10.0 h1:L8qnKaofSfNFbXg0C5F71LdjPRnmQwSsA4ukmkt1TvY=
github.com/nats-io/nats.go v1.10.0/go.mod h1:AjGArbfyR50+afOUotNX2Xs5SYHf+CoOa5HH1eEl2HE=
github.com/nats-io/nkeys v0.1.3/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w=
github.com/nats-io/nkeys v0.1.4 h1:aEsHIssIk6ETN5m2/MD8Y4B2X7FfXrBAUdkyRvbVYzA=
github.com/nats-io/nkeys v0.1.4/go.mod h1:XdZpAbhgyyODYqjTawOnIOI7VlbKSarI9Gfy1tqEu/s=
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/nsf/termbox-go v0.0.0-20160808045038-e8f6d27f72a2 h1:uXcqrEm9R4mwFsxD5XErrCXPg1aRODAbK8WX9j1BGLY=
github.com/nsf/termbox-go v0.0.0-20160808045038-e8f6d27f72a2/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59 h1:3zb4D3T4G8jdExgVU/95+vQXfpEPiMdCaZgmGVxjNHM=
golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e h1:D5TXcfTk7xF7hvieo4QErS3qqCB4teTffacDWr7CI+0=
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/gizak/termui.v1 v1.0.0-20151021151108-e62b5929642a h1:3xTF5ZxN6yR6SZlNMCN7z4X633Zi87YC8FkxFsdgdM0=
gopkg.in/gizak/termui.v1 v1.0.0-20151021151108-e62b5929642a/go.mod h1:An8M7oA8GI+74WiR1qJo67ptrdlb7LJLqoXspnSrMUA=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
24 changes: 14 additions & 10 deletions nats-top.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2015 NATS Messaging System
// Copyright (c) 2015-2021 The NATS Authors
package main

import (
Expand All @@ -10,12 +10,12 @@ import (
"strings"
"time"

gnatsd "github.com/nats-io/gnatsd/server"
"github.com/nats-io/nats-server/v2/server"
top "github.com/nats-io/nats-top/util"
ui "gopkg.in/gizak/termui.v1"
)

const version = "0.3.2"
const version = "0.4.0"

var (
host = flag.String("s", "127.0.0.1", "The nats server host.")
Expand Down Expand Up @@ -102,11 +102,11 @@ func main() {
// Smoke test to abort in case can't connect to server since the beginning.
_, err := engine.Request("/varz")
if err != nil {
log.Printf("nats-top: %s", err)
log.Printf("nats-top: /varz smoke test failed: %s", err)
usage()
}

sortOpt := gnatsd.SortOpt(*sortBy)
sortOpt := server.SortOpt(*sortBy)
if !sortOpt.IsValid() {
log.Fatalf("nats-top: invalid option to sort by: %s\n", sortOpt)
usage()
Expand Down Expand Up @@ -139,6 +139,10 @@ func cleanExit() {

func exitWithError() {
ui.Close()

// Show cursor once again
fmt.Print("\033[?25h")

os.Exit(1)
}

Expand All @@ -161,8 +165,8 @@ func generateParagraph(
slowConsumers := stats.Varz.SlowConsumers

var serverVersion string
if stats.Varz.Info != nil {
serverVersion = stats.Varz.Info.Version
if stats.Varz.Version != "" {
serverVersion = stats.Varz.Version
}

mem := top.Psize(memVal)
Expand Down Expand Up @@ -345,8 +349,8 @@ const (
func StartUI(engine *top.Engine) {

cleanStats := &top.Stats{
Varz: &gnatsd.Varz{},
Connz: &gnatsd.Connz{},
Varz: &server.Varz{},
Connz: &server.Connz{},
Rates: &top.Rates{},
Error: fmt.Errorf(""),
}
Expand Down Expand Up @@ -428,7 +432,7 @@ func StartUI(engine *top.Engine) {

if e.Type == ui.EventKey && e.Key == ui.KeyEnter {

sortOpt := gnatsd.SortOpt(optionBuf)
sortOpt := server.SortOpt(optionBuf)
if sortOpt.IsValid() {
engine.SortOpt = sortOpt
} else {
Expand Down
35 changes: 22 additions & 13 deletions util/toputils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package toputils

import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
Expand All @@ -9,7 +10,7 @@ import (
"net/http"
"time"

gnatsd "github.com/nats-io/gnatsd/server"
"github.com/nats-io/nats-server/v2/server"
)

const DisplaySubscriptions = 1
Expand All @@ -20,7 +21,7 @@ type Engine struct {
HttpClient *http.Client
Uri string
Conns int
SortOpt gnatsd.SortOpt
SortOpt server.SortOpt
Delay int
DisplaySubs bool
StatsCh chan *Stats
Expand All @@ -46,9 +47,9 @@ func (engine *Engine) Request(path string) (interface{}, error) {
uri := engine.Uri + path
switch path {
case "/varz":
statz = &gnatsd.Varz{}
statz = &server.Varz{}
case "/connz":
statz = &gnatsd.Connz{}
statz = &server.Connz{}
uri += fmt.Sprintf("?limit=%d&sort=%s", engine.Conns, engine.SortOpt)
if engine.DisplaySubs {
uri += fmt.Sprintf("&subs=%d", DisplaySubscriptions)
Expand All @@ -62,17 +63,25 @@ func (engine *Engine) Request(path string) (interface{}, error) {
defer resp.Body.Close()
}
if err != nil {
return nil, fmt.Errorf("could not get stats from server: %v\n", err)
return nil, fmt.Errorf("could not get stats from server: %w", err)
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("could not read response body: %v\n", err)
return nil, fmt.Errorf("could not read response body: %w", err)
}

if resp.StatusCode != 200 {
end := bytes.IndexAny(body, "\r\n")
if end > 80 {
end = 80
}
return nil, fmt.Errorf("stats request failed %d: %q", resp.StatusCode, string(body[:end]))
}

err = json.Unmarshal(body, &statz)
if err != nil {
return nil, fmt.Errorf("could not unmarshal json: %v\n", err)
return nil, fmt.Errorf("could not unmarshal statz json: %w", err)
}

return statz, nil
Expand Down Expand Up @@ -105,8 +114,8 @@ func (engine *Engine) MonitorStats() error {

for {
stats := &Stats{
Varz: &gnatsd.Varz{},
Connz: &gnatsd.Connz{},
Varz: &server.Varz{},
Connz: &server.Connz{},
Rates: &Rates{},
Error: fmt.Errorf(""),
}
Expand All @@ -123,7 +132,7 @@ func (engine *Engine) MonitorStats() error {
engine.StatsCh <- stats
continue
}
if varz, ok := result.(*gnatsd.Varz); ok {
if varz, ok := result.(*server.Varz); ok {
stats.Varz = varz
}
}
Expand All @@ -136,7 +145,7 @@ func (engine *Engine) MonitorStats() error {
engine.StatsCh <- stats
continue
}
if connz, ok := result.(*gnatsd.Connz); ok {
if connz, ok := result.(*server.Connz); ok {
stats.Connz = connz
}
}
Expand Down Expand Up @@ -225,8 +234,8 @@ func (engine *Engine) SetupHTTP() {

// Stats represents the monitored data from a NATS server.
type Stats struct {
Varz *gnatsd.Varz
Connz *gnatsd.Connz
Varz *server.Varz
Connz *server.Connz
Rates *Rates
Error error
}
Expand Down
24 changes: 12 additions & 12 deletions util/toputils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ import (
"testing"
"time"

"github.com/nats-io/gnatsd/server"
gnatsd "github.com/nats-io/gnatsd/test"
"github.com/nats-io/nats-server/v2/server"
server_test "github.com/nats-io/nats-server/v2/test"
)

// Borrowed from gnatsd tests
const GNATSD_PORT = 11422
// Borrowed from nats-server tests
const NATS_SERVER_TEST_PORT = 11422

func runMonitorServer(monitorPort int) *server.Server {
resetPreviousHTTPConnections()
opts := gnatsd.DefaultTestOptions
opts := server_test.DefaultTestOptions
opts.Host = "127.0.0.1"
opts.Port = GNATSD_PORT
opts.Port = NATS_SERVER_TEST_PORT
opts.HTTPPort = monitorPort

return gnatsd.RunServer(&opts)
return server_test.RunServer(&opts)
}

func resetPreviousHTTPConnections() {
Expand Down Expand Up @@ -52,9 +52,9 @@ func TestFetchingStatz(t *testing.T) {
t.Fatalf("Could not monitor number of cores. got: %v", got)
}

// Create simple subscription to gnatsd port to show subscriptions
// Create simple subscription to nats-server test port to show subscriptions
go func() {
conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", GNATSD_PORT))
conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", NATS_SERVER_TEST_PORT))
if err != nil {
t.Fatalf("could not create subcription to NATS: %s", err)
}
Expand Down Expand Up @@ -153,7 +153,7 @@ func TestMonitorStats(t *testing.T) {
}

func TestMonitoringTLSConnectionUsingRootCA(t *testing.T) {
srv, _ := gnatsd.RunServerWithConfig("./test/tls.conf")
srv, _ := server_test.RunServerWithConfig("./test/tls.conf")
defer srv.Shutdown()

engine := NewEngine("127.0.0.1", 8223, 10, 1)
Expand Down Expand Up @@ -183,7 +183,7 @@ func TestMonitoringTLSConnectionUsingRootCA(t *testing.T) {
}

func TestMonitoringTLSConnectionUsingRootCAWithCerts(t *testing.T) {
srv, _ := gnatsd.RunServerWithConfig("./test/tls.conf")
srv, _ := server_test.RunServerWithConfig("./test/tls.conf")
defer srv.Shutdown()

engine := NewEngine("127.0.0.1", 8223, 10, 1)
Expand Down Expand Up @@ -213,7 +213,7 @@ func TestMonitoringTLSConnectionUsingRootCAWithCerts(t *testing.T) {
}

func TestMonitoringTLSConnectionUsingCertsAndInsecure(t *testing.T) {
srv, _ := gnatsd.RunServerWithConfig("./test/tls.conf")
srv, _ := server_test.RunServerWithConfig("./test/tls.conf")
defer srv.Shutdown()

engine := NewEngine("127.0.0.1", 8223, 10, 1)
Expand Down
22 changes: 0 additions & 22 deletions vendor/github.com/gizak/termui/LICENSE

This file was deleted.

Loading

0 comments on commit 6fe66de

Please sign in to comment.