From 6546f5ce818f0601106d23f6f7dd6776dc2b8bd2 Mon Sep 17 00:00:00 2001 From: "R.I.Pienaar" Date: Fri, 22 Sep 2023 09:01:58 +0200 Subject: [PATCH] Fix rendering micro user stats Signed-off-by: R.I.Pienaar --- cli/micro_command.go | 23 ++++++++++++++++++++--- columns/columns.go | 9 ++++++--- go.mod | 2 +- go.sum | 2 ++ 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/cli/micro_command.go b/cli/micro_command.go index fa1b425d..382ac5e7 100644 --- a/cli/micro_command.go +++ b/cli/micro_command.go @@ -22,6 +22,7 @@ import ( "os" "sort" "strconv" + "sync" "time" "github.com/choria-io/fisk" @@ -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 { @@ -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 } @@ -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) @@ -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) diff --git a/columns/columns.go b/columns/columns.go index 0b805c33..511338fd 100644 --- a/columns/columns.go +++ b/columns/columns.go @@ -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: @@ -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 } } diff --git a/go.mod b/go.mod index 26b252e0..fda68844 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 61d4b896..64f09d5f 100644 --- a/go.sum +++ b/go.sum @@ -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=