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

Add basic templating to input #20

Closed
sdboyer-stripe opened this issue Jul 16, 2018 · 3 comments
Closed

Add basic templating to input #20

sdboyer-stripe opened this issue Jul 16, 2018 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@sdboyer-stripe
Copy link

Hi! This tool is super useful - thanks for putting it together!

It would be very handy, for the particular use case that i have, if it were possible to use standard Go templating to swap in variables related to the state of the current run. In particular, what i was hoping for is a unique numeric identifier that can be templated in for each individual request.

Not sure if it's feasible or not, but it'd be nifty!

@bojand
Copy link
Owner

bojand commented Jul 17, 2018

Hello, Thanks for the kind word and the suggestion!

I think I understand the general idea and I agree it would be useful. Can you elaborate perhaps and provide a simple example of an ideal usage or something that demonstrates the desired behaviour, just so I can be sure I understand what's required?

For example request data (and metadata I suppose too) would be JSON text with template actions, and we would evaluate and fill the data with some predefined standard values before actually parsing JSON and serializing into the payload?

ie something like:

message HelloRequest {
  string name = 1;
  int32 request_number = 2;
}

And maybe we have -td and -tm flags for template data and template metadata and usage would be:

$ ghz -proto ./greeter.proto -call helloworld.Greeter.SayHello -td '{"name":"Joe","request_number":"{{.RequestNumber}}"}' -tm '{"requestnumber":"{{.RequestNumber}}"}' 0.0.0.0:50051

And then ghz would fill in the request number into the payloads? We could also provide some additional contextualized values like name of the call, service, proto, etc...

Is this something like what you had in mind or am I overthinking it or going in the wrong direction?

Thanks!

Edit: I guess we would not necessarily need new flags, the standard data and metadata values could always just be parsed and evaluated.

@bojand bojand self-assigned this Jul 17, 2018
@bojand bojand added the enhancement New feature or request label Jul 17, 2018
@sdboyer-stripe
Copy link
Author

sdboyer-stripe commented Jul 17, 2018

Yes to all of this, you've got it exactly! (And i agree, i think you should be able to just unconditionally parse and transform).

The string i'm passing to -d right now is '{"trace_id": 1, "id": 100}', and i'd like to be able to pass '{"trace_id": 1, "id": {{.RequestNumber}}}' instead.

bojand added a commit that referenced this issue Jul 20, 2018
* Initial commit for adding template data and metadata support

* do not error when failing to execute the template

* add timestamps

* add call template data tests and update readme

* improve comment
@bojand
Copy link
Owner

bojand commented Jul 20, 2018

Hello, I believe we should have this now in 0.7.0.

Data and metadata can specify template actions that will be parsed and evaluated at every request. Each request gets a new instance of the data. The available variables / actions are:

// call template data
type callTemplateData struct {
	RequestNumber      int64  // unique incrememnted request number for each request
	FullyQualifiedName string // fully-qualified name of the method call
	MethodName         string // shorter call method name
	ServiceName        string // the service name
	InputName          string // name of the input message type
	OutputName         string // name of the output message type
	IsClientStreaming  bool   // whether this call is client streaming
	IsServerStreaming  bool   // whether this call is server streaming
	Timestamp          string // timestamp of the call in RFC3339 format
	TimestampUnix      int64  // timestamp of the call as unix time
}

This can be useful to inject variable information into the data or metadata payload for each request, such as timestamp or unique request number. Example:

$ ghz -proto ./greeter.proto -call helloworld.Greeter.SayHello -d '{"name":"Joe"}' -m '{"trace_id":"{{.RequestNumber}}","timestamp":"{{.TimestampUnix}}"}' 0.0.0.0:50051

This ends up substituting the template variables with the request number and unix timestamp respectively.

Let me know how this works for you. I'll close this issue and if there are any bugs or issues let me know and feel free to create a new issue.

@bojand bojand closed this as completed Jul 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants