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

Http request middleware function #17

Merged
merged 4 commits into from
Jan 28, 2022
Merged

Conversation

OrenLeung
Copy link

@OrenLeung OrenLeung commented Jan 27, 2022

Why

In order to propagate trace context & headers for distributed tracing, we need some way of "injecting" middleware to our requests

How?

  • an parameter was added to the the function that creates a new webrpc client

Tests

  • re-generated _examples/golang-basic/generate.gen.go

  • webrpc-gen -schema ./_examples/golang-basics/example.ridl -target go -out ./_examples/golang-basics/example.gen.go -client -server -pkg main

  • re-generated in internal repos

Communication

  • post on slack that if anybody needs makes modifications to internal private repo's ridl, they should git pull, re-build, and reinstalling webrpc

Example Generated Client - _examples/golang-basics

image

All Generated Client Helpers function will look like this

image

Type of Function Pointer to Pass as a parameter into Create New Client

image

…dleware injection function to allow us to use telemetry trace context propgation
@OrenLeung OrenLeung self-assigned this Jan 27, 2022
@OrenLeung OrenLeung changed the title Draft: Http request middleware function Http request middleware function Jan 27, 2022
@OrenLeung OrenLeung force-pushed the http-request-middleware-function branch from c2a0063 to 1fb067d Compare January 27, 2022 18:49
@OrenLeung OrenLeung requested a review from kipply January 27, 2022 18:57
gen/golang/templates/client.go.tmpl Outdated Show resolved Hide resolved
Func suffix is the convention
@OrenLeung OrenLeung merged commit 2b3c641 into master Jan 28, 2022
@OrenLeung OrenLeung deleted the http-request-middleware-function branch January 28, 2022 18:22
@VojtechVitek
Copy link

VojtechVitek commented Mar 23, 2023

Hi @OrenLeung @miguelvr @kipply,

I'm walking through this fork's Pull Requests and wondering what we could backport to upstream webrpc repo.

Can you please share how this feature is used in a real world example?
Are you setting some request headers based on the context values?

Example from https://rakyll.medium.com/context-propagation-over-http-in-go-d4540996e9b0:

const requestIDHeader = "request-id"

// Transport serializes the request context into request headers.
type Transport struct {
 // Base is the actual round tripper that makes the request.
 // http.DefaultTransport is used if none is set.
 Base http.RoundTripper
}

// RoundTrip converts request context into headers
// and makes the request.
func (t *Transport) RoundTrip(r *http.Request) (*http.Response, error) {
 r = cloneReq(r) // per RoundTrip interface enforces
 
 rid := request.IDFromContext(r.Context())
 if rid != "" {
  r.Header.Add(requestIDHeader, rid)
 }
 
 base := t.Base
 if base == nil {
  base = http.DefaultTransport
 }
 
 return base.RoundTrip(r)
}

Have you considered using custom http.Transport instead? Would it be sufficient?

Thanks!

Best,
Vojtech | https://golang.cz

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