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

Loki: fix handling of tail requests when using target all or read #4642

Merged
merged 4 commits into from
Nov 4, 2021

Conversation

slim-bean
Copy link
Collaborator

What this PR does / why we need it:

All and Read targets include a frontend which was registering the tail routes, however it currently handles these routes by proxying them to a querier via the tail_proxy_url frontend config. This is difficult and strange behavior when the frontend and querier are in the same process.

This PR changes the registration of the tail routes to always be done by the querier and if the query-fronted and querier are in the same process the query-frontend skips registering of the tail routes and the proxy and defers these to be handled by the querier instead.

Which issue(s) this PR fixes:
Fixes #

Special notes for your reviewer:

Checklist

  • Documentation added
  • Tests updated
  • Add an entry in the CHANGELOG.md about the changes.

@slim-bean slim-bean requested a review from a team as a code owner November 4, 2021 13:51
@@ -480,7 +493,8 @@ func (t *Loki) initQueryFrontend() (_ services.Service, err error) {
).Wrap(frontendHandler)

var defaultHandler http.Handler
if t.Cfg.Frontend.TailProxyURL != "" {
// If this process also acts as a Querier we don't do any proxying of tail requests
if t.Cfg.Frontend.TailProxyURL != "" && !t.ModuleManager.IsModuleRegistered(Querier) {
Copy link
Member

Choose a reason for hiding this comment

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

This isn't actually testing if querier is in the active dependency tree, but only that it's a registered module, which is always the case

Copy link
Collaborator

Choose a reason for hiding this comment

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

I've traditionally used isModuleEnabled which I agree is a bit hacky since it checks for the string in the target. I think if we looked for the Querier, All, and Read targets we should be covered.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Great catch, 😠 that I missed this!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah i went this approach because the isModuleEnabled required supplying multiple targets and I thought this approach would work better...

Instead I've added a method which does exactly what I want

Copy link
Collaborator

Choose a reason for hiding this comment

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

do we still need both versions of that function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think this new function is a better solution to what we did a lot with isModuleEnabled however I'm a bit scared to make that overhaul today.

I created #4644 to track doing this in the future.

Copy link
Collaborator

@trevorwhitney trevorwhitney left a comment

Choose a reason for hiding this comment

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

LGTM. One nit about having 2 isModuleEnabled functions, could your new one replace the one on the Config struct?

pkg/loki/loki.go Outdated
Comment on lines 491 to 505
func (t *Loki) isModuleEnabled(m string) bool {
for _, tg := range t.Cfg.Target {
if tg == m {
return true
}
if k, ok := t.deps[tg]; ok {
for _, dp := range k {
if dp == m {
return true
}
}
}
}
return false
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

ohh, this is a much better check of dependencies, should we replace the function of the same name on the Config struct with this version and use that everywhere?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Worried about introducing bugs as we close in on the 2.4 release so I created #4644 to track us doing this in the future.

pkg/loki/loki.go Outdated
t.ModuleManager = mm

return nil
}

func (t *Loki) isModuleEnabled(m string) bool {
Copy link
Member

Choose a reason for hiding this comment

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

Can we make this isModuleActive? We have another function with the same name on the Config type which behaves differently: https://github.com/grafana/loki/blob/main/pkg/loki/loki.go#L196-L198

Copy link
Member

@owen-d owen-d Nov 4, 2021

Choose a reason for hiding this comment

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

Ideally this would be a recursive check so we could handle IsActive(buzz) == true when target=foo

foo: {bar}
bar: {bazz}
bazz: {buzz}

edit: we should also recontribute that upstream, but I'm fine coding it here for the moment

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Renamed and made it recursive

Copy link
Contributor

Choose a reason for hiding this comment

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

Just double checking: there's no chance of Loki having a cyclic dependency, right? Ex:

foo: {bar}
bar: {bazz}
bazz: {bar}

Because, if there is, I think it could end in an infinite loop (solution would be to track the visited modules and to not visit already visited nodes).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

haha, i think this could totally happen.

@pull-request-size pull-request-size bot added size/L and removed size/M labels Nov 4, 2021
@owen-d owen-d merged commit 89ee022 into main Nov 4, 2021
@owen-d owen-d deleted the handle-tail-requests branch November 4, 2021 16:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants