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

support tracing JS api calls #849

Merged
merged 1 commit into from Oct 14, 2021
Merged

support tracing JS api calls #849

merged 1 commit into from Oct 14, 2021

Conversation

ripienaar
Copy link
Contributor

Signed-off-by: R.I.Pienaar rip@devco.net

@ripienaar
Copy link
Contributor Author

ripienaar commented Oct 14, 2021

This enables CLI to trace into the JetStream context

$ nats kv info TEST --trace
08:03:09 >>> $JS.API.STREAM.INFO.KV_TEST:
08:03:09 <<< $JS.API.STREAM.INFO.KV_TEST: {"type":"io.nats.jetstream.api.v1.stream_info_response","config":{"name":"KV_TEST","description":"KV Bucket TEST","subjects":["$KV.TEST.\u003e"],"retention":"limits","max_consumers":-1,"max_msgs":-1,"max_bytes":-1,"max_age":0,"max_msgs_per_subject":5,"max_msg_size":-1,"discard":"old","storage":"file","num_replicas":1,"duplicate_window":120000000000,"allow_rollup_hdrs":true},"created":"2021-09-27T17:02:17.909428367Z","state":{"messages":0,"bytes":0,"first_seq":6,"first_ts":"1970-01-01T00:00:00Z","last_seq":5,"last_ts":"1970-01-01T00:00:00Z","consumer_count":0},"domain":"hub","cluster":{"name":"lon","leader":"n1-lon"}}
08:03:09 >>> $JS.API.STREAM.INFO.KV_TEST:
08:03:09 <<< $JS.API.STREAM.INFO.KV_TEST: {"type":"io.nats.jetstream.api.v1.stream_info_response","config":{"name":"KV_TEST","description":"KV Bucket TEST","subjects":["$KV.TEST.\u003e"],"retention":"limits","max_consumers":-1,"max_msgs":-1,"max_bytes":-1,"max_age":0,"max_msgs_per_subject":5,"max_msg_size":-1,"discard":"old","storage":"file","num_replicas":1,"duplicate_window":120000000000,"allow_rollup_hdrs":true},"created":"2021-09-27T17:02:17.909428367Z","state":{"messages":0,"bytes":0,"first_seq":6,"first_ts":"1970-01-01T00:00:00Z","last_seq":5,"last_ts":"1970-01-01T00:00:00Z","consumer_count":0},"domain":"hub","cluster":{"name":"lon","leader":"n1-lon"}}
TEST Key-Value Store Status

         Bucket Name: TEST
        History Kept: 5
       Values Stored: 0
  Backing Store Kind: JetStream
         Description: KV Bucket TEST
 Maximum Bucket Size: unlimited
  Maximum Value Size: unlimited
    JetStream Stream: KV_TEST
    Cluster Location: lon
$ nats obj get FILES nats --trace
08:16:35 >>> $JS.API.STREAM.INFO.OBJ_FILES:
08:16:35 <<< $JS.API.STREAM.INFO.OBJ_FILES: {"type":"io.nats.jetstream.api.v1.stream_info_response","config":{"name":"OBJ_FILES","subjects":["$O.FILES.C.\u003e","$O.FILES.M.\u003e"],"retention":"limits","max_consumers":-1,"max_msgs":-1,"max_bytes":-1,"max_age":0,"max_msgs_per_subject":-1,"max_msg_size":-1,"discard":"new","storage":"file","num_replicas":1,"duplicate_window":120000000000,"allow_rollup_hdrs":true},"created":"2021-10-11T14:53:54.167647769Z","state":{"messages":1380,"bytes":179197294,"first_seq":107,"first_ts":"2021-10-11T15:25:12.570732057Z","last_seq":2785,"last_ts":"2021-10-13T16:35:54.440868953Z","num_deleted":789,"consumer_count":0},"domain":"hub","cluster":{"name":"lon","leader":"n3-lon"}}
08:16:35 >>> $JS.API.STREAM.MSG.GET.OBJ_FILES: {"last_by_subj":"$O.FILES.M.nats"}
08:16:35 <<< $JS.API.STREAM.MSG.GET.OBJ_FILES: {"type":"io.nats.jetstream.api.v1.stream_msg_get_response","message":{"subject":"$O.FILES.M.nats","seq":2785,"hdrs":"TkFUUy8xLjANCk5hdHMtUm9sbHVwOiBzdWINCg0K","data":"eyJuYW1lIjoibmF0cyIsImhlYWRlcnMiOnsiSGVsbG8iOlsiV29ybGQiXX0sImJ1Y2tldCI6IkZJTEVTIiwibnVpZCI6ImJXdWxYdm93ZG80cmJYdjVXY0FWM2UiLCJzaXplIjoxMzc4MzA0MCwibXRpbWUiOiIwMDAxLTAxLTAxVDAwOjAwOjAwWiIsImNodW5rcyI6MTA2LCJkaWdlc3QiOiJzaGEtMjU2PWJ2NXJadGhValBOc2JpZndXSjFYRXhoOXNEcGRWVUVfWXQzeWMzajBVbEE9In0=","time":"2021-10-13T16:35:54.440868953Z"}}
08:16:35 >>> $JS.API.CONSUMER.CREATE.OBJ_FILES: {"stream_name":"OBJ_FILES","config":{"deliver_subject":"_INBOX.wb2sPXVfv2vsfCCcVMtUuL","deliver_policy":"all","ack_policy":"none","ack_wait":79200000000000,"max_deliver":1,"filter_subject":"$O.FILES.C.bWulXvowdo4rbXv5WcAV3e","replay_policy":"instant","flow_control":true,"idle_heartbeat":5000000000}}
08:16:35 <<< $JS.API.CONSUMER.CREATE.OBJ_FILES: {"type":"io.nats.jetstream.api.v1.consumer_create_response","stream_name":"OBJ_FILES","name":"pppFDL87","created":"2021-10-14T08:16:35.443756288Z","config":{"deliver_subject":"_INBOX.wb2sPXVfv2vsfCCcVMtUuL","deliver_policy":"all","ack_policy":"none","ack_wait":79200000000000,"max_deliver":1,"filter_subject":"$O.FILES.C.bWulXvowdo4rbXv5WcAV3e","replay_policy":"instant","idle_heartbeat":5000000000,"flow_control":true},"delivered":{"consumer_seq":0,"stream_seq":2678},"ack_floor":{"consumer_seq":0,"stream_seq":106},"num_ack_pending":0,"num_redelivered":0,"num_waiting":0,"num_pending":106,"cluster":{"name":"lon","leader":"n3-lon"},"push_bound":true}
08:16:35 >>> $JS.API.CONSUMER.DELETE.OBJ_FILES.pppFDL87:
wrote: 13 MiB to /home/rip/orders/nats

@coveralls
Copy link

coveralls commented Oct 14, 2021

Coverage Status

Coverage decreased (-0.3%) to 84.765% when pulling 2554593 on ripienaar:api_trace into 0fdad3d on nats-io:main.

Signed-off-by: R.I.Pienaar <rip@devco.net>
Copy link
Member

@kozlovic kozlovic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

TraceSent TraceOperation = 0
// TraceReceived indicate the payload is being received on subj
TraceReceived TraceOperation = 1
)
Copy link
Member

@wallyqs wallyqs Oct 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative to using these types could be for example to do something similar to what httptrace package does with a struct where multiple callbacks are defined. For example with a ClientTrace struct like the following, it could be made to implement the options for JSOpt to configure just a context:

type ClientTrace struct {
	RequestSent      func(subj string, payload []byte, hdr Header)
	ResponseReceived func(subj string, payload []byte, hdr Header)
	// ProtocolSent...
	// ProtocolReceived...
	// Other misc low level connect options ala net/http/httptrace maybe
}

func (ct ClientTrace) configureJSContext(js *jsOpts) error {
	js.ctrace = ct
	js.shouldTrace = true
	return nil
}

but also same type could be used for other tracing at the nats level with implementing the nats.Option:

func WithClientTrace(ct *ClientTrace) Option {
	return func(o *Options) error {
		// Trace funcs but at the NATS level
		o.ClientTrace = ct
		return nil
	}
}

Then could use either later on:

	// Define at the NATS level to trace protocols or non JS traffic as well:
	nc, err := Connect(s.ClientURL(), WithClientTrace(&ClientTrace{}))
	if err != nil {
		t.Fatalf("Unexpected error: %v", err)
	}
	defer nc.Close()

	// Trace funcs scoped at the JS level.  Implements the interface for
	// JS option so can use the same type.
	nc, _ = Connect(s.ClientURL())
	js, err := nc.JetStream(&ClientTrace{
		RequestSent: func(subj string, payload []byte, hdr Header) {
			fmt.Println(">>> SENT : ", subj, payload)
		},
		ResponseReceived: func(subj string, payload []byte, hdr Header) {
			fmt.Println("<<< RECVD: ", subj, payload)
		},
	})

(Put an example of how this would look here: wallyqs@2a8553d#diff-d3e5f2fc32ab57ad01ff7eff1dc643653af5636e517a4b0677ddcefb94f87f7bR909)

Copy link
Contributor Author

@ripienaar ripienaar Oct 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you have a baked example feel free to put up a PR instead of this one, I dont mind who puts it in :) just need to be able to get tracing on things as debugging stuff atm is really tough :)

I wont have time to work on this past whats here for like week and a half

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, ok maybe let's include as is for now to iterate on the idea already in main branch.

Copy link
Member

@wallyqs wallyqs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ripienaar ripienaar merged commit 4513296 into nats-io:main Oct 14, 2021
@ripienaar ripienaar deleted the api_trace branch October 14, 2021 16:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants