oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

Add OperationID in client request header

Open chrisngyn opened this issue 2 years ago • 2 comments

I utilized this library to create a client from an OpenAPI specification, and now I aim to develop an interceptor for monitoring outgoing HTTP requests using Prometheus. However, the request path varies based on path parameters, making it impractical to use it as a label value for Prometheus metrics. As a solution, I propose adding an additional header (Operation-Id) to each HTTP request. This adjustment simplifies the process of writing the monitoring interceptor.

It just a naive solution. Feel free to suggest me the better solutions.

chrisngyn avatar Dec 18 '23 14:12 chrisngyn

Almost file changes are the result when I ran make generate. I just only a one line at pkg/codegen/templates/client.tmpl

req.Header.Add("Operation-Id", "{{$opid}}")

chrisngyn avatar Dec 18 '23 14:12 chrisngyn

Hey @chrisngyn thanks for raising this.

Unfortunately this isn't something that we'd like to make the default.

You may be able to do this with a RequestEditorFn that's added into the client - it'll still require the OperationID, so maybe adding a field into the ctx that can be retrieved by a RequestEditorFn would work.

If you were to tweak this and make it an opt-in flag (see #1404 for where we've added an opt-in flag) then that'd be great.

jamietanna avatar Jan 25 '24 11:01 jamietanna