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

feat: measure external latency #779

Merged
merged 7 commits into from
Apr 19, 2024
Merged

Conversation

zepatrik
Copy link
Member

This adds a middleware and utilities to measure external latency.

Copy link
Contributor

@alnr alnr left a comment

Choose a reason for hiding this comment

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

Does this have to be a middleware? This would work much more simply and be less tightly coupled if we defined a public Context key in the reqlog package which gives a *time.Duration, to which external calls can add the time they took.

httpx/ssrf.go Outdated
@@ -64,7 +64,7 @@ func init() {
ssrf.WithAnyPort(),
ssrf.WithNetworks("tcp4", "tcp6"),
).Safe
prohibitInternalAllowIPv6 = t
prohibitInternalAllowIPv6 = &MeasureExternalLatencyTransport{Transport: t}
Copy link
Contributor

Choose a reason for hiding this comment

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

Please let's not couple this functionality to the SSRF protection mechanism. We should allow users to opt into the MeasureExternalLatencyTransport if they wish. It's as easy as wrapping the transport at the site of res, err := client.Do(req).

Copy link
Member Author

Choose a reason for hiding this comment

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

This is still opt-in, because the transport is just a no-op when there is no "container" in the context. However, it does make sense to not do this per default for the httpx client.
I was at first thinking about only measuring the external latency on the "prohibitInternal*" variants of this transport, but as it is right now it does not make sense.

Copy link
Member Author

@zepatrik zepatrik left a comment

Choose a reason for hiding this comment

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

We will always need some kind of middleware to add the data container to the context. As is, the context key is not exposed but there are easy functions to add or get to the data container, I will also add a way to add the data container to a context without the middleware wrapping.

httpx/ssrf.go Outdated
@@ -64,7 +64,7 @@ func init() {
ssrf.WithAnyPort(),
ssrf.WithNetworks("tcp4", "tcp6"),
).Safe
prohibitInternalAllowIPv6 = t
prohibitInternalAllowIPv6 = &MeasureExternalLatencyTransport{Transport: t}
Copy link
Member Author

Choose a reason for hiding this comment

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

This is still opt-in, because the transport is just a no-op when there is no "container" in the context. However, it does make sense to not do this per default for the httpx client.
I was at first thinking about only measuring the external latency on the "prohibitInternal*" variants of this transport, but as it is right now it does not make sense.

@alnr
Copy link
Contributor

alnr commented Apr 12, 2024

We will always need some kind of middleware to add the data container to the context. As is, the context key is not exposed but there are easy functions to add or get to the data container, I will also add a way to add the data container to a context without the middleware wrapping.

Can we add the context key in the existing reqlog package?

@zepatrik
Copy link
Member Author

Can we add the context key in the existing reqlog package?

What's the benefit of exposing the context key? Or do you mean add a context key for the overall latency as well?

@alnr
Copy link
Contributor

alnr commented Apr 15, 2024

This second iteration of the API is a lot better. 👍

IMO, this would be better with a such an API:

func SendWebhook() (...) {
  req, err := http.NewRequestWithContext(ctx, ...)

  defer reqlog.IncrExternalLatency(ctx, time.Now())
  res, err := client.Do(req)
  // maybe retries

  return res, err
}

Alternative usage:

func SendWebhook() (...) {
  req, err := http.NewRequestWithContext(ctx, ...)

  t0 := time.Now()
  res, err := client.Do(req)
  // maybe retries
  reqlog.IncrExternalLatency(ctx, t0)

  // time-consuming post-processing
  return res, err
}

@alnr
Copy link
Contributor

alnr commented Apr 15, 2024

Just re-read the code and this already exists in your API essentially 😅

httpx/external_latency.go Show resolved Hide resolved
reqlog/external_latency.go Show resolved Hide resolved
reqlog/external_latency.go Outdated Show resolved Hide resolved
reqlog/external_latency.go Outdated Show resolved Hide resolved
reqlog/external_latency.go Outdated Show resolved Hide resolved
reqlog/external_latency.go Show resolved Hide resolved
reqlog/external_latency.go Outdated Show resolved Hide resolved
@zepatrik zepatrik merged commit a7a3c8a into master Apr 19, 2024
6 of 7 checks passed
@zepatrik zepatrik deleted the feat/client-measure-external-calls branch April 19, 2024 10:04
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.

2 participants