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 CheckRedirect callback #269

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

echlebek
Copy link
Contributor

This commit adds a CheckRedirect callback that opamp-go will call before
following a redirect from the server it's trying to connect to. Like in
net/http, CheckRedirect can be used to observe the request chain that
the client is taking while attempting to make a connection.

The user can optionally terminate redirect following by returning an
error from CheckRedirect.

Unlike in net/http, the via parameter for CheckRedirect is a slice of
responses. Since the user would have no other way to access these in the
context of opamp-go, CheckRedirect makes them available so that users
can know exactly what status codes and headers are set in the response.

Another small improvement is that the error callback is no longer called
when redirecting. This should help to prevent undue error logging by
opamp-go consumers. Since the CheckRedirect callback is now available,
it also doesn't represent any loss in functionality to opamp-go
consumers.

@echlebek echlebek requested a review from a team April 11, 2024 22:34
Copy link

codecov bot commented Apr 11, 2024

Codecov Report

Attention: Patch coverage is 94.64286% with 3 lines in your changes missing coverage. Please review.

Project coverage is 77.71%. Comparing base (b7beae5) to head (22851bb).

Files with missing lines Patch % Lines
client/wsclient.go 94.00% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #269      +/-   ##
==========================================
+ Coverage   77.29%   77.71%   +0.42%     
==========================================
  Files          25       25              
  Lines        2281     2324      +43     
==========================================
+ Hits         1763     1806      +43     
  Misses        410      410              
  Partials      108      108              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

client/wsclient.go Outdated Show resolved Hide resolved
client/types/callbacks.go Outdated Show resolved Hide resolved
@echlebek
Copy link
Contributor Author

@tigrannajaryan Apologies, I had actually forgotten to implement the HTTP client logic and tests, so it took a bit longer. However, I think now the feature is complete for both WS and HTTP clients. Please take another look when you get a chance.

tigrannajaryan added a commit to tigrannajaryan/opamp-go that referenced this pull request Dec 18, 2024
The interface has the following downsides:
- Impossible to define non-trivial default behavior. Here is an example where it was needed: open-telemetry#269 (comment)
- Adding new callbacks requires expanding the interface, which is a breaking change for existing client users.

Getting rid of the interface and keeping just a struct
for callbacks solves both problems:
- Arbitrarily complex default behavior can be now defined on the struct if the user does not provide the particular callback func.
- Adding new callback funcs is not a braking change, existing users won't be affected.
tigrannajaryan added a commit to tigrannajaryan/opamp-go that referenced this pull request Dec 18, 2024
The interface has the following downsides:
- Impossible to define non-trivial default behavior. Here is an example where it was needed: open-telemetry#269 (comment)
- Adding new callbacks requires expanding the interface, which is a breaking change for existing client users.

Getting rid of the interface and keeping just a struct
for callbacks solves both problems:
- Arbitrarily complex default behavior can be now defined on the struct if the user does not provide the particular callback func.
- Adding new callback funcs is not a braking change, existing users won't be affected.
tigrannajaryan added a commit that referenced this pull request Dec 19, 2024
The interface has the following downsides:
- Impossible to define non-trivial default behavior. Here is an example where it was needed: #269 (comment)
- Adding new callbacks requires expanding the interface, which is a breaking change for existing client users.

Getting rid of the interface and keeping just a struct
for callbacks solves both problems:
- Arbitrarily complex default behavior can be now defined on the struct if the user does not provide the particular callback func.
- Adding new callback funcs is not a braking change, existing users won't be affected.
tigrannajaryan added a commit to tigrannajaryan/opamp-go that referenced this pull request Dec 19, 2024
This continues work start in open-telemetry#324 for Client.

The interface has the following downsides:
- Impossible to define non-trivial default behavior. Here is an example where it was needed: open-telemetry#269 (comment)
- Adding new callbacks requires expanding the interface, which is a breaking change for existing client users.

Getting rid of the interface and keeping just a struct
for callbacks solves both problems:
- Arbitrarily complex default behavior can be now defined on the struct if the user does not provide the particular callback func.
- Adding new callback funcs is not a braking change, existing users won't be affected.
Verifies that redirect chains work
This commit adds a CheckRedirect callback that opamp-go will call before
following a redirect from the server it's trying to connect to. Like in
net/http, CheckRedirect can be used to observe the request chain that
the client is taking while attempting to make a connection.

The user can optionally terminate redirect following by returning an
error from CheckRedirect.

Unlike in net/http, the via parameter for CheckRedirect is a slice of
responses. Since the user would have no other way to access these in the
context of opamp-go, CheckRedirect makes them available so that users
can know exactly what status codes and headers are set in the response.

Another small improvement is that the error callback is no longer called
when redirecting. This should help to prevent undue error logging by
opamp-go consumers. Since the CheckRedirect callback is now available,
it also doesn't represent any loss in functionality to opamp-go
consumers.
@echlebek echlebek force-pushed the better-opamp-redirects branch from a461f1f to b711c39 Compare December 19, 2024 21:40
This commit adds support for a CheckRedirect callback to the HTTP opamp
client. It also unifies the API for CheckRedirect between WS and HTTP,
so that the same callback can be used in either circumstance.

Signed-off-by: Eric Chlebek <[email protected]>
@echlebek echlebek force-pushed the better-opamp-redirects branch from b711c39 to 22851bb Compare December 19, 2024 22:00
@echlebek
Copy link
Contributor Author

Please forgive a bit of churn as I fix up some issues found after rebasing.

@echlebek
Copy link
Contributor Author

I think the PR is ready for review again.

Copy link
Member

@tigrannajaryan tigrannajaryan left a comment

Choose a reason for hiding this comment

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

LGTM.
I will keep it open for a bit in case other approvers want to take a look.

{
Name: "check redirect returns error",
Redirector: redirectServer("http://"+redirectee.Endpoint, 302),
MockRedirect: mockRedirect(t, 1, errors.New("hello")),
Copy link
Member

Choose a reason for hiding this comment

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

should this be mockRedirectHTTP?

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.

3 participants