Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rendering micro user stats #872

Merged
merged 1 commit into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions cli/micro_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os"
"sort"
"strconv"
"sync"
"time"

"github.com/choria-io/fisk"
Expand Down Expand Up @@ -98,6 +99,8 @@ func (c *microCmd) echoHandler(req micro.Request) {

func (c *microCmd) serveAction(_ *fisk.ParseContext) error {
var err error
var combinedPayload int
var mu sync.Mutex

c.nc, _, err = prepareHelper("", natsOpts()...)
if err != nil {
Expand All @@ -112,13 +115,27 @@ func (c *microCmd) serveAction(_ *fisk.ParseContext) error {
"_nats.client.created.library": "natscli",
"_nats.client.created.version": Version,
},
StatsHandler: func(endpoint *micro.Endpoint) any {
mu.Lock()
defer mu.Unlock()

return map[string]any{
"total_payload": combinedPayload,
}
},
})
if err != nil {
return err
}

grp := srv.AddGroup(c.name)
err = grp.AddEndpoint("echo", micro.HandlerFunc(c.echoHandler))
err = grp.AddEndpoint("echo", micro.HandlerFunc(func(request micro.Request) {
mu.Lock()
combinedPayload += len(request.Data())
mu.Unlock()

c.echoHandler(request)
}))
if err != nil {
return err
}
Expand Down Expand Up @@ -403,7 +420,7 @@ func (c *microCmd) infoAction(_ *fisk.ParseContext) error {
cols.Indent(2)

cols.AddSectionTitle("%s Endpoint Statistics", e.Name)
cols.AddRowf("Requests", "%s in group %s", f(e.NumRequests), e.QueueGroup)
cols.AddRowf("Requests", "%s in group %q", f(e.NumRequests), e.QueueGroup)
cols.AddRowf("Processing Time", "%s (average %s)", f(e.ProcessingTime), f(e.AverageProcessingTime))
cols.AddRowf("Started:", "%s (%s ago)", f(stats.Started), f(time.Since(stats.Started)))
cols.AddRow("Errors", e.NumErrors)
Expand All @@ -413,7 +430,7 @@ func (c *microCmd) infoAction(_ *fisk.ParseContext) error {
cols.AddSectionTitle("Endpoint Specific Statistics")
out := bytes.NewBuffer([]byte{})
json.Indent(out, e.Data, " ", " ")
cols.Println(" " + out.String())
cols.Println(" " + out.String())
}

cols.Indent(0)
Expand Down
9 changes: 6 additions & 3 deletions columns/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func (w *Writer) Frender(o io.Writer) error {
}

prev := -1

for i, row := range w.rows {
switch row.kind {
case kindIndent:
Expand Down Expand Up @@ -128,10 +129,12 @@ func (w *Writer) Frender(o io.Writer) error {

case kindLine:
// avoid 2 blank lines
if prev != kindTitle {
fmt.Fprintln(o, append([]any{w.indent}, row.values...)...)
prev = row.kind
if prev == kindTitle && len(row.values) == 0 {
continue
}

fmt.Fprintln(o, append([]any{w.indent}, row.values...)...)
prev = row.kind
}
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/klauspost/compress v1.17.0
github.com/mattn/go-isatty v0.0.19
github.com/nats-io/jsm.go v0.1.1-0.20230921074448-1bbb5650afc8
github.com/nats-io/jsm.go v0.1.1-0.20230922064108-bb09405bc2c0
github.com/nats-io/nats-server/v2 v2.10.1
github.com/nats-io/nats.go v1.30.0
github.com/nats-io/nuid v1.0.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ github.com/minio/highwayhash v1.0.2 h1:Aak5U0nElisjDCfPSG79Tgzkn2gl66NxOMspRrKnA
github.com/minio/highwayhash v1.0.2/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY=
github.com/nats-io/jsm.go v0.1.1-0.20230921074448-1bbb5650afc8 h1:OKm9e1//rlcl4i9zXQ6QQxj7DJaeL+Oe8WBgAKO4cqI=
github.com/nats-io/jsm.go v0.1.1-0.20230921074448-1bbb5650afc8/go.mod h1:hB4Qd+IKoRvAAWTOI1HkCy4wotjFwOIT+codHCFOZqk=
github.com/nats-io/jsm.go v0.1.1-0.20230922064108-bb09405bc2c0 h1:YrGcddIEq3vsWFO6JKGqHF1NquZCCKXJBJmp61tDRJY=
github.com/nats-io/jsm.go v0.1.1-0.20230922064108-bb09405bc2c0/go.mod h1:hB4Qd+IKoRvAAWTOI1HkCy4wotjFwOIT+codHCFOZqk=
github.com/nats-io/jwt/v2 v2.5.2 h1:DhGH+nKt+wIkDxM6qnVSKjokq5t59AZV5HRcFW0zJwU=
github.com/nats-io/jwt/v2 v2.5.2/go.mod h1:24BeQtRwxRV8ruvC4CojXlx/WQ/VjuwlYiH+vu/+ibI=
github.com/nats-io/nats-server/v2 v2.10.1 h1:MIJ614dhOIdo71iSzY8ln78miXwrYvlvXHUyS+XdKZQ=
Expand Down
Loading