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

logging: fix the HTTP logger #4041

Merged
merged 3 commits into from
Apr 10, 2021
Merged

logging: fix the HTTP logger #4041

merged 3 commits into from
Apr 10, 2021

Conversation

GiedriusS
Copy link
Member

@GiedriusS GiedriusS commented Apr 9, 2021

Fix the HTTP logger so that it would take a look at the url.URL as
well if r.Host is empty. Without this, the logging functionality doesn't quite work
as Cortex uses url.URL:

https://github.com/cortexproject/cortex/blob/faf92e8f3dbedfa769fbd6a3ff6e0fbd127694f5/pkg/frontend/downstream_roundtripper.go#L16-L23

Add a test to ensure that this works as expected in both ways.

Without this, the user only sees in the logs:

level=error ts=2021-04-09T10:22:05.3319924Z caller=http.go:44 protocol=http http.component=server msg="failed to parse host port for http log decision" err="address foo.bar: missing port in address"

Signed-off-by: Giedrius Statkevičius giedrius.statkevicius@vinted.com

  • I added CHANGELOG entry for this change.
  • Change is not relevant to the end user.

Fix the HTTP logger so that it would take a look at the `url.URL` as
well if `r.Host` is empty. Without this, the logging functionality doesn't quite work
as Cortex uses `url.URL`:

https://github.com/cortexproject/cortex/blob/faf92e8f3dbedfa769fbd6a3ff6e0fbd127694f5/pkg/frontend/downstream_roundtripper.go#L16-L23

Add a test to ensure that this works as expected in both ways.

Signed-off-by: Giedrius Statkevičius <giedriuswork@gmail.com>
Signed-off-by: Giedrius Statkevičius <giedrius.statkevicius@vinted.com>
Signed-off-by: Giedrius Statkevičius <giedrius.statkevicius@vinted.com>
Copy link
Contributor

@yashrsharma44 yashrsharma44 left a comment

Choose a reason for hiding this comment

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

There are few questions, feel free to skip if it doesn't make sense

LGTM 💪

@@ -39,7 +39,11 @@ func (m *HTTPServerMiddleware) HTTPMiddleware(name string, next http.Handler) ht
return func(w http.ResponseWriter, r *http.Request) {
wrapped := httputil.WrapResponseWriterWithStatus(w)
start := time.Now()
_, port, err := net.SplitHostPort(r.Host)
hostPort := r.Host
Copy link
Contributor

Choose a reason for hiding this comment

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

What should we do, if both r.Host and r.URL.Host is set, which one should we display?

Copy link
Member

Choose a reason for hiding this comment

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

If r.Host is there, it will get priority. We are only using r.URL.Host if r.Host is an empty string.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can print anyone of them, as long one of them is non-empty.

Copy link
Member Author

Choose a reason for hiding this comment

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

From the docs:

    // For client requests, the URL's Host specifies the server to
    // connect to, while the Request's Host field optionally
    // specifies the Host header value to send in the HTTP
    // request.

So, we should give priority to r.Host over r.URL.Host because r.Host might be more specific i.e. it's the Host header 😛

Copy link
Contributor

@yashrsharma44 yashrsharma44 Apr 9, 2021

Choose a reason for hiding this comment

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

Looks like the current implementation is comprehensive 😄 !

Copy link
Member

Choose a reason for hiding this comment

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

👍🏽

pkg/logging/http_test.go Outdated Show resolved Hide resolved
onprem
onprem previously approved these changes Apr 9, 2021
Copy link
Member

@onprem onprem left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for the fix, we are seeing these error logs in our deployments as well.

Signed-off-by: Giedrius Statkevičius <giedriuswork@gmail.com>

Co-authored-by: Yash Sharma <yashrsharma44@gmail.com>
Copy link
Member

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

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

Thanks! LGTM, just let's fix CHANGELOG but we can fix it anytime during release

@@ -30,6 +30,7 @@ We use _breaking :warning:_ to mark changes that are not backward compatible (re
- [#3960](https://github.com/thanos-io/thanos/pull/3960) fix deduplication of equal alerts with different labels
- [#3937](https://github.com/thanos-io/thanos/pull/3937) Store: Fix race condition in chunk pool.
- [#4017](https://github.com/thanos-io/thanos/pull/4017) Query Frontend: fix downsampling iterator returning duplicate samples.
- [#4017](https://github.com/thanos-io/thanos/pull/4041) Logging: fix the HTTP logger
Copy link
Member

Choose a reason for hiding this comment

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

Let's mention what is the fix exactly 🤗

@@ -39,7 +39,11 @@ func (m *HTTPServerMiddleware) HTTPMiddleware(name string, next http.Handler) ht
return func(w http.ResponseWriter, r *http.Request) {
wrapped := httputil.WrapResponseWriterWithStatus(w)
start := time.Now()
_, port, err := net.SplitHostPort(r.Host)
hostPort := r.Host
Copy link
Member

Choose a reason for hiding this comment

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

👍🏽

@bwplotka bwplotka merged commit dd97468 into thanos-io:main Apr 10, 2021
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.

4 participants