Generate code to instrument an interface.
Genstrument always needs a go interface to generate an instrumented wrapper around that given interface.
Currently Genstrument supports the following kinds of interfaces for code generation.
Depending on the use case the GeneratorMode
must be passed via the --mode=<mode>
flag to the Genstrument binary.
// Binary mode can generate instrumented code for an interface which methods return an error as the last argument.
// With respect to the value of the error (nil or not nil), the corresponding counter in the counter vector will be increased.
Binary GeneratorMode = "binary"
// Handler can generate instrumented wrappers around a typical http server interface.
// Each method is a http.HandlerFunc `func(w http.ResponseWriter, r *http.Request)`.
// Extensions with more parameters after the `*http.Request` are also possible.
// This way server code generated by https://github.com/deepmap/oapi-codegen can be instrumented.
Handler GeneratorMode = "handler"
// OapiCodeGenClient mode generates instrumented code from the client interface generated by https://github.com/deepmap/oapi-codegen.
// See https://github.com/deepmap/oapi-codegen#generated-client-boilerplate.
OapiCodeGenClient GeneratorMode = "oapi-codegen-client"
Install with
go install github.com/leonnicolas/genstrument@latest
or add github.com/leonnicolas/genstrument
to your tools.go
file.
Run
genstrument --file-path <file-path-to-interface> -p <interface-name> --metric-help <help text> --metric-name <metric-name> -o -
to print the generated code to stdout.
Or add something like
//go:generate go run github.com/leonnicolas/genstrument --file-path <file-name> -p <interface-name> --metric-help <help text> --metric-name <metric-name> -o gen.go
into the go file, that contains the interface you want to instrument.