Re: JetStream get messages count

365 views
Skip to first unread message

Phil Pennock

unread,
Sep 1, 2022, 7:54:26 PM9/1/22
to nat...@googlegroups.com
On 2022-08-30 at 23:36 -0700, Dana Keydar wrote:
> Is there an option to get a count of all existing messages in a stream ? I
> didn't find any documentation of it.

Running `nats stream info $stream_name` includes a "Messages:" count, so
it's there. The Go API involves a bit more work to find than the NATS
message API, since we can just use `nats --trace` to find the latter.

The code line is:
fmt.Printf(" Messages: %s\n", humanize.Comma(int64(info.State.Msgs)))

where the `info` is `info *api.StreamInfo`; the "api" identifier is the
`github.com/nats-io/jsm.go/api` package. See `type StreamState struct`
in <https://github.com/nats-io/jsm.go/blob/main/api/streams.go>.

Back in the natscli, we have:

where we have:
info, err := stream.LatestInformation()

which gives you the Go API call.

To see the actual NATS message, we can use the --trace flag to the
`nats` command:

% nats --trace str info crackers
19:46:52 >>> $JS.API.STREAM.INFO.crackers
null

19:46:52 <<< $JS.API.STREAM.INFO.crackers
{"type":"io.nats.jetstream.api.v1.stream_info_response","config":{"name":"crackers","subjects":["crackers.\u003e"],"retention":"limits","max_consumers":-1,"max_msgs":-1,"max_bytes":-1,"max_age":63072000000000000,"max_msgs_per_subject":-1,"max_msg_size":-1,"discard":"old","storage":"file","num_replicas":1,"duplicate_window":3600000000000,"sealed":false,"deny_delete":false,"deny_purge":false,"allow_rollup_hdrs":false},"created":"2021-02-19T22:09:55.310496002Z","state":{"messages":108,"bytes":5270,"first_seq":1,"first_ts":"2021-02-19T22:11:30.962169041Z","last_seq":108,"last_ts":"2021-02-19T22:16:20.37121518Z","num_subjects":2,"consumer_count":1}}

and there we have it. Send an empty request to
"$JS.API.STREAM.INFO."+stream_name and get back a JSON payload. The
`type` field there, "io.nats.jetstream.api.v1.stream_info_response",
refers to an autogenerated Go type, and gives us enough information to
browse the schemas/ area of the jsm.go repo to find:

<https://github.com/nats-io/jsm.go/blob/main/schemas/jetstream/api/v1/stream_info_response.json>

I can verify this with:

% nats --context home-git req --raw '$JS.API.STREAM.INFO.SVEN-GIT-REFS' '' | jq -C .

which indeed includes a section of the response:

"state": {
"messages": 5516,
"bytes": 1668397,
"first_seq": 1,
"first_ts": "2020-11-10T02:44:37.830720468Z",
"last_seq": 5516,
"last_ts": "2022-09-01T23:02:13.720786257Z",
"num_subjects": 173,
"consumer_count": 0
},

Regards,
-Phil
Reply all
Reply to author
Forward
0 new messages