Skip to content

Commit

Permalink
Merge pull request #1147 from ripienaar/render_adr-44-meta
Browse files Browse the repository at this point in the history
Renders ADR-44 metadata as part of state
  • Loading branch information
ripienaar authored Sep 10, 2024
2 parents 593be9e + 57f6d3b commit 9937423
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 47 deletions.
14 changes: 4 additions & 10 deletions cli/consumer_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -939,16 +939,10 @@ func (c *consumerCmd) showInfo(config api.ConsumerConfig, state api.ConsumerInfo
cols.AddRowIf("Paused Until Deadline", fmt.Sprintf("%s (passed)", f(config.PauseUntil)), !config.PauseUntil.IsZero())
}

if len(config.Metadata) > 0 {
meta := iu.RemoveReservedMetadata(config.Metadata)
if len(meta) > 0 {
cols.AddSectionTitle("Metadata")
maxLen := iu.ProgressWidth()
for k, v := range config.Metadata {
if len(v) > maxLen && maxLen > 20 {
w := maxLen/2 - 10
v = fmt.Sprintf("%v ... %v", v[0:w], v[len(v)-w:])
}
cols.AddRow(k, v)
}
cols.AddMapStrings(meta)
}
}

Expand All @@ -972,7 +966,7 @@ func (c *consumerCmd) showInfo(config api.ConsumerConfig, state api.ConsumerInfo
}

cols.AddSectionTitle("State")

iu.RenderMetaApi(cols, config.Metadata)
if state.Delivered.Last == nil {
cols.AddRowf("Last Delivered Message", "Consumer sequence: %s Stream sequence: %s", f(state.Delivered.Consumer), f(state.Delivered.Stream))
} else {
Expand Down
60 changes: 31 additions & 29 deletions cli/stream_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2039,9 +2039,10 @@ func (c *streamCmd) showStreamConfig(cols *columns.Writer, cfg api.StreamConfig)
cols.AddRowIf("Consumer Inactive Threshold", cfg.ConsumerLimits.InactiveThreshold, cfg.ConsumerLimits.InactiveThreshold > 0)
cols.AddRowIf("Consumer Max Ack Pending", cfg.ConsumerLimits.MaxAckPending, cfg.ConsumerLimits.MaxAckPending > 0)

if len(cfg.Metadata) > 0 {
meta := iu.RemoveReservedMetadata(cfg.Metadata)
if len(meta) > 0 {
cols.AddSectionTitle("Metadata")
cols.AddMapStrings(cfg.Metadata)
cols.AddMapStrings(meta)
}

if cfg.Mirror != nil || len(cfg.Sources) > 0 {
Expand Down Expand Up @@ -2125,38 +2126,38 @@ func (c *streamCmd) showStreamInfo(info *api.StreamInfo) {

if info.Cluster != nil && info.Cluster.Name != "" {
cols.AddSectionTitle("Cluster Information")
if info.Cluster != nil && info.Cluster.Name != "" {
cols.AddRow("Name", info.Cluster.Name)
cols.AddRowIfNotEmpty("Cluster Group", info.Cluster.RaftGroup)
cols.AddRow("Leader", info.Cluster.Leader)
for _, r := range info.Cluster.Replicas {
state := []string{r.Name}

if r.Current {
state = append(state, "current")
} else {
state = append(state, "outdated")
}

cols.AddRow("Name", info.Cluster.Name)
cols.AddRowIfNotEmpty("Cluster Group", info.Cluster.RaftGroup)
cols.AddRow("Leader", info.Cluster.Leader)
for _, r := range info.Cluster.Replicas {
state := []string{r.Name}

if r.Current {
state = append(state, "current")
} else {
state = append(state, "outdated")
}
if r.Offline {
state = append(state, "OFFLINE")
}

if r.Offline {
state = append(state, "OFFLINE")
}
if r.Active > 0 && r.Active < math.MaxInt64 {
state = append(state, fmt.Sprintf("seen %s ago", f(r.Active)))
} else {
state = append(state, "not seen")
}

if r.Active > 0 && r.Active < math.MaxInt64 {
state = append(state, fmt.Sprintf("seen %s ago", f(r.Active)))
} else {
state = append(state, "not seen")
}
switch {
case r.Lag > 1:
state = append(state, fmt.Sprintf("%s operations behind", f(r.Lag)))
case r.Lag == 1:
state = append(state, fmt.Sprintf("%s operation behind", f(r.Lag)))
}

switch {
case r.Lag > 1:
state = append(state, fmt.Sprintf("%s operations behind", f(r.Lag)))
case r.Lag == 1:
state = append(state, fmt.Sprintf("%s operation behind", f(r.Lag)))
cols.AddRow("Replica", state)
}

cols.AddRow("Replica", state)

}
cols.Println()
}
Expand Down Expand Up @@ -2226,6 +2227,7 @@ func (c *streamCmd) showStreamInfo(info *api.StreamInfo) {
}

cols.AddSectionTitle("State")
iu.RenderMetaApi(cols, info.Config.Metadata)
cols.AddRow("Messages", info.State.Msgs)
cols.AddRow("Bytes", humanize.IBytes(info.State.Bytes))

Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/klauspost/compress v1.17.9
github.com/mattn/go-isatty v0.0.20
github.com/nats-io/jsm.go v0.1.1-0.20240909124020-543abd828bd1
github.com/nats-io/jsm.go v0.1.1-0.20240910110459-a94b3842a419
github.com/nats-io/jwt/v2 v2.5.8
github.com/nats-io/nats-server/v2 v2.11.0-dev.0.20240909173510-a07bde9fa7d4
github.com/nats-io/nats.go v1.37.0
Expand Down Expand Up @@ -63,7 +63,6 @@ require (
github.com/rivo/uniseg v0.4.7 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
go.uber.org/automaxprocs v1.5.3 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,10 @@ github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zx
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/nats-io/jsm.go v0.1.1-0.20240909124020-543abd828bd1 h1:2V6TS5DnWPjmlkdMeGlA9zGlOUNtCEKo3UakdUkMgRY=
github.com/nats-io/jsm.go v0.1.1-0.20240909124020-543abd828bd1/go.mod h1:qarKt1X8221zgCOg+JcjkH1/i7+p3HQFRWNYv1lk3dI=
github.com/nats-io/jsm.go v0.1.1-0.20240910110459-a94b3842a419 h1:ahH+acQcvfgk8sqFzVlZcVPdK2jHMxqVnZSsLYBshOU=
github.com/nats-io/jsm.go v0.1.1-0.20240910110459-a94b3842a419/go.mod h1:qarKt1X8221zgCOg+JcjkH1/i7+p3HQFRWNYv1lk3dI=
github.com/nats-io/jwt/v2 v2.5.8 h1:uvdSzwWiEGWGXf+0Q+70qv6AQdvcvxrv9hPM0RiPamE=
github.com/nats-io/jwt/v2 v2.5.8/go.mod h1:ZdWS1nZa6WMZfFwwgpEaqBV8EPGVgOTDHN/wTbz0Y5A=
github.com/nats-io/nats-server/v2 v2.11.0-dev.0.20240907185554-7f92c34c1415 h1:gGXxOszQols/MkhpSc3Ec2RfP4T+aW0EBZkOinsHJ9M=
github.com/nats-io/nats-server/v2 v2.11.0-dev.0.20240907185554-7f92c34c1415/go.mod h1:odNGaRFETFJYsviPk2grICKGgoObmMjgIBDwny/uVrc=
github.com/nats-io/nats-server/v2 v2.11.0-dev.0.20240909173510-a07bde9fa7d4 h1:4ZtCJK+tZMXPcyzFGNWNkJSY1/twBs15DHwMFp7BHWw=
github.com/nats-io/nats-server/v2 v2.11.0-dev.0.20240909173510-a07bde9fa7d4/go.mod h1:o07K/z9ovs02pREUUNWwGGuIYqL110VKnjbjHp+/TjE=
github.com/nats-io/nats.go v1.37.0 h1:07rauXbVnnJvv1gfIyghFEo6lUcYRY0WXc3x7x0vUxE=
Expand Down Expand Up @@ -160,8 +158,6 @@ github.com/synadia-io/jwt-auth-builder.go v0.0.0-20240628155003-21e8d1e9d490/go.
github.com/tylertreat/hdrhistogram-writer v0.0.0-20210816161836-2e440612a39f h1:SGznmvCovewbaSgBsHgdThtWsLj5aCLX/3ZXMLd1UD0=
github.com/tylertreat/hdrhistogram-writer v0.0.0-20210816161836-2e440612a39f/go.mod h1:IY84XkhrEJTdHYLNy/zObs8mXuUAp9I65VyarbPSCCY=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.uber.org/automaxprocs v1.5.3 h1:kWazyxZUrS3Gs4qUpbwo5kEIMGe/DAvi5Z4tl2NW4j8=
go.uber.org/automaxprocs v1.5.3/go.mod h1:eRbA25aqJrxAbsLO0xy5jVwPt7FQnRgjW+efnwa1WM0=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
Expand Down
31 changes: 31 additions & 0 deletions internal/util/jetstream.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package util

import (
"github.com/nats-io/jsm.go/api"
"github.com/nats-io/natscli/columns"
)

// RenderMetaApi draws the _nats.* metadata on streams and consumers
func RenderMetaApi(cols *columns.Writer, metadata map[string]string) {
versionMeta := metadata[api.JSMetaCurrentServerVersion]
levelMeta := metadata[api.JSMetaCurrentServerLevel]
requiredMeta := metadata[api.JsMetaRequiredServerLevel]
createdVersion := metadata[api.JSMetaCreatedServerVersion]

if versionMeta != "" || levelMeta != "" || requiredMeta != "" {
if versionMeta != "" {
if createdVersion == "" || createdVersion == versionMeta {
cols.AddRow("Host Version", versionMeta)
} else {
cols.AddRowf("Host Version", "%s created on %s", versionMeta, createdVersion)
}
}

if levelMeta != "" || requiredMeta != "" {
if requiredMeta == "" {
requiredMeta = "0"
}
cols.AddRowf("Required API Level", "%s hosted at level %s", requiredMeta, levelMeta)
}
}
}

0 comments on commit 9937423

Please sign in to comment.