Skip to content

Commit 3cc9bc3

Browse files
authored
Merge pull request #872 from ripienaar/micro_user_stats
Fix rendering micro user stats
2 parents 328e52c + 6546f5c commit 3cc9bc3

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

cli/micro_command.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os"
2323
"sort"
2424
"strconv"
25+
"sync"
2526
"time"
2627

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

99100
func (c *microCmd) serveAction(_ *fisk.ParseContext) error {
100101
var err error
102+
var combinedPayload int
103+
var mu sync.Mutex
101104

102105
c.nc, _, err = prepareHelper("", natsOpts()...)
103106
if err != nil {
@@ -112,13 +115,27 @@ func (c *microCmd) serveAction(_ *fisk.ParseContext) error {
112115
"_nats.client.created.library": "natscli",
113116
"_nats.client.created.version": Version,
114117
},
118+
StatsHandler: func(endpoint *micro.Endpoint) any {
119+
mu.Lock()
120+
defer mu.Unlock()
121+
122+
return map[string]any{
123+
"total_payload": combinedPayload,
124+
}
125+
},
115126
})
116127
if err != nil {
117128
return err
118129
}
119130

120131
grp := srv.AddGroup(c.name)
121-
err = grp.AddEndpoint("echo", micro.HandlerFunc(c.echoHandler))
132+
err = grp.AddEndpoint("echo", micro.HandlerFunc(func(request micro.Request) {
133+
mu.Lock()
134+
combinedPayload += len(request.Data())
135+
mu.Unlock()
136+
137+
c.echoHandler(request)
138+
}))
122139
if err != nil {
123140
return err
124141
}
@@ -403,7 +420,7 @@ func (c *microCmd) infoAction(_ *fisk.ParseContext) error {
403420
cols.Indent(2)
404421

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

419436
cols.Indent(0)

columns/columns.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ func (w *Writer) Frender(o io.Writer) error {
9898
}
9999

100100
prev := -1
101+
101102
for i, row := range w.rows {
102103
switch row.kind {
103104
case kindIndent:
@@ -128,10 +129,12 @@ func (w *Writer) Frender(o io.Writer) error {
128129

129130
case kindLine:
130131
// avoid 2 blank lines
131-
if prev != kindTitle {
132-
fmt.Fprintln(o, append([]any{w.indent}, row.values...)...)
133-
prev = row.kind
132+
if prev == kindTitle && len(row.values) == 0 {
133+
continue
134134
}
135+
136+
fmt.Fprintln(o, append([]any{w.indent}, row.values...)...)
137+
prev = row.kind
135138
}
136139
}
137140

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ require (
1818
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
1919
github.com/klauspost/compress v1.17.0
2020
github.com/mattn/go-isatty v0.0.19
21-
github.com/nats-io/jsm.go v0.1.1-0.20230921074448-1bbb5650afc8
21+
github.com/nats-io/jsm.go v0.1.1-0.20230922064108-bb09405bc2c0
2222
github.com/nats-io/nats-server/v2 v2.10.1
2323
github.com/nats-io/nats.go v1.30.0
2424
github.com/nats-io/nuid v1.0.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ github.com/minio/highwayhash v1.0.2 h1:Aak5U0nElisjDCfPSG79Tgzkn2gl66NxOMspRrKnA
8282
github.com/minio/highwayhash v1.0.2/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY=
8383
github.com/nats-io/jsm.go v0.1.1-0.20230921074448-1bbb5650afc8 h1:OKm9e1//rlcl4i9zXQ6QQxj7DJaeL+Oe8WBgAKO4cqI=
8484
github.com/nats-io/jsm.go v0.1.1-0.20230921074448-1bbb5650afc8/go.mod h1:hB4Qd+IKoRvAAWTOI1HkCy4wotjFwOIT+codHCFOZqk=
85+
github.com/nats-io/jsm.go v0.1.1-0.20230922064108-bb09405bc2c0 h1:YrGcddIEq3vsWFO6JKGqHF1NquZCCKXJBJmp61tDRJY=
86+
github.com/nats-io/jsm.go v0.1.1-0.20230922064108-bb09405bc2c0/go.mod h1:hB4Qd+IKoRvAAWTOI1HkCy4wotjFwOIT+codHCFOZqk=
8587
github.com/nats-io/jwt/v2 v2.5.2 h1:DhGH+nKt+wIkDxM6qnVSKjokq5t59AZV5HRcFW0zJwU=
8688
github.com/nats-io/jwt/v2 v2.5.2/go.mod h1:24BeQtRwxRV8ruvC4CojXlx/WQ/VjuwlYiH+vu/+ibI=
8789
github.com/nats-io/nats-server/v2 v2.10.1 h1:MIJ614dhOIdo71iSzY8ln78miXwrYvlvXHUyS+XdKZQ=

0 commit comments

Comments
 (0)