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

API Implementation #2372

Merged
merged 28 commits into from
Apr 8, 2020
Merged

API Implementation #2372

merged 28 commits into from
Apr 8, 2020

Conversation

jtlisi
Copy link
Contributor

@jtlisi jtlisi commented Mar 31, 2020

What this PR does:
This is the implementation associated with the design document in #2327.

In this implementation the API struct mainly just handles routing for Cortex. In the future this package will centralize non-component based handlers and create a set of functions and structs that can be reused by Cortex handlers to make the API consistent.

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]

@jtlisi jtlisi marked this pull request as ready for review April 2, 2020 14:04
Copy link
Contributor

@pracucci pracucci left a comment

Choose a reason for hiding this comment

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

Good job @jtlisi. It quite a tricky PR to review to accurately find if breaking changes have been introduced. I've left some comments and questions, but overall the design looks good to me.

I also have few more comments:

  • Could you update docs/apis.md accordingly, please?
  • What's the impact of these changes on Loki?
  • Can you also reflect the changes in the CHANGELOG? Ie. I noticed these changes (but there are probably more):
    • Alertmanager: /status is available only when running in microservices mode and the target is the alertmanager
    • Ruler: /ruler_ring > /ruler/ring

@@ -98,6 +90,22 @@ func (m *ModuleName) UnmarshalYAML(unmarshal func(interface{}) error) error {
return m.Set(s)
}

func (t *Cortex) initAPI(cfg *Config) (services.Service, error) {
cfg.API.ServerPrefix = cfg.Server.PathPrefix
cfg.API.LegacyHTTPPrefix = cfg.HTTPPrefix
Copy link
Contributor

Choose a reason for hiding this comment

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

This is potentially a breaking change. If an user has customised HTTPPrefix, the behaviour will be different, because for most of the routes before the /api/prom was hard-coded while now it's always based on HTTPPrefix. Should be at least mentioned in the CHANGELOG as CHANGE, but not sure if it breaks the 1.0.0 stability guarantees.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure either. I always felt it was an issue if not a bug that services did not respect the configured prefix. I will add a line in the changelog explaining the change in behaviour. However, I don't think this goes against any of our 1.0 guarantees:

Additional API endpoints for management of Cortex itself, such as the ring. These APIs are not part of the any compatibility guarantees.

@jtlisi jtlisi force-pushed the 20200326_api_spike branch from 0fb552c to 0295435 Compare April 6, 2020 22:53
jtlisi and others added 21 commits April 6, 2020 19:27
Signed-off-by: Jacob Lisi <[email protected]>
Signed-off-by: Jacob Lisi <[email protected]>
Signed-off-by: Jacob Lisi <[email protected]>
Signed-off-by: Jacob Lisi <[email protected]>
Signed-off-by: Jacob Lisi <[email protected]>
Signed-off-by: Jacob Lisi <[email protected]>
Co-Authored-By: Marco Pracucci <[email protected]>
Signed-off-by: Jacob Lisi <[email protected]>
@jtlisi jtlisi force-pushed the 20200326_api_spike branch from eec97a3 to 4dabad2 Compare April 6, 2020 23:35
pkg/api/api.go Outdated
handler = a.authMiddleware.Wrap(handler)
}
if len(methods) == 0 {
a.server.HTTP.Path(path).Handler(handler)
Copy link
Contributor

@pracucci pracucci Apr 7, 2020

Choose a reason for hiding this comment

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

a.server.HTTP.Path() > a.server.HTTP.PathPrefix()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry about that, I was just looking into the failed integration tests and that looked to be the issue. Also the primary reason for not explicitly registering the alertmanager routes is because the Alertmanager UI routing is relatively complex and I didn't want to handle it in this PR. I also think it is fine to serve the Alertmanager directly if it is routed under the /alertmanager/ prefix.

// RegisterDistributor registers the endpoints associated with the distributor.
func (a *API) RegisterDistributor(d *distributor.Distributor, pushConfig distributor.Config) {
a.registerRoute("/api/v1/push", push.Handler(pushConfig, d.Push), true)
a.registerRoute("/distributor/all_user_stats", http.HandlerFunc(d.AllUserStatsHandler), false)
Copy link
Contributor

Choose a reason for hiding this comment

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

Then I think all new routes should use underscores (I also prefer dashes, but my personal preference doesn't matter when it breaks consistency, so agree on underscores).

pkg/api/api.go Outdated
a.server.HTTP.Path(path).Methods(methods...).Handler(handler)
}

func (a *API) registerRoutesWithPrefix(path string, handler http.Handler, auth bool, methods ...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 would rename path to prefix to make it clearer

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

@@ -62,6 +62,15 @@ Where default_value is the value to use if the environment variable is undefined
# CLI flag: -http.prefix
[http_prefix: <string> | default = "/api/prom"]

api:
# Base path for data storage.
Copy link
Contributor

Choose a reason for hiding this comment

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

Remember to address this.

Copy link
Contributor

@pracucci pracucci left a comment

Choose a reason for hiding this comment

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

Thanks @jtlisi for addressing my feedback. I've few more minor comments. May you also look at the failing lint and integration tests, please?

Signed-off-by: Jacob Lisi <[email protected]>
@jtlisi
Copy link
Contributor Author

jtlisi commented Apr 7, 2020

I just wanted to circle back and address:

Alertmanager: /status is available only when running in microservices mode and the target is the alertmanager

I don't know if that is worth addressing since there is no current way to run the alertmanager in the single binary. Even after this PR that functionality has not been enabled.

Signed-off-by: Jacob Lisi <[email protected]>
@pracucci
Copy link
Contributor

pracucci commented Apr 7, 2020

I just wanted to circle back and address:

Alertmanager: /status is available only when running in microservices mode and the target is the alertmanager

I don't know if that is worth addressing since there is no current way to run the alertmanager in the single binary. Even after this PR that functionality has not been enabled.

🤦‍♂ You're definitely right. All good on this 👍

@jtlisi jtlisi linked an issue Apr 7, 2020 that may be closed by this pull request
Copy link
Contributor

@pracucci pracucci left a comment

Choose a reason for hiding this comment

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

Good job @jtlisi! LGTM. I just left few minor nits. Would be great to update the docs/apis.md (not necessarily in this PR, we can do in a subsequent one, but would be great doing it).

CHANGELOG.md Outdated
@@ -2,6 +2,9 @@

## master / unreleased

* [CHANGE] Added v1 API routes documented in #2327. #2372
* Added `-http.alertmanager-http-prefix` and `http.prometheus-http-prefix` flags.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* Added `-http.alertmanager-http-prefix` and `http.prometheus-http-prefix` flags.
* Added `-http.alertmanager-http-prefix` and `-http.prometheus-http-prefix` flags.

@@ -2,6 +2,9 @@

## master / unreleased

* [CHANGE] Added v1 API routes documented in #2327. #2372
* Added `-http.alertmanager-http-prefix` and `http.prometheus-http-prefix` flags.
* Updated the index hosted at the root prefix to point to the updated routes.
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you try to make it more clear? I think it's a bit difficult to understand for the final user.

CHANGELOG.md Outdated
* [CHANGE] Added v1 API routes documented in #2327. #2372
* Added `-http.alertmanager-http-prefix` and `http.prometheus-http-prefix` flags.
* Updated the index hosted at the root prefix to point to the updated routes.
* Legacy routes hardcoded with the `/api/prom` prefix now respect the `http.prefix` flag.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* Legacy routes hardcoded with the `/api/prom` prefix now respect the `http.prefix` flag.
* Legacy routes hardcoded with the `/api/prom` prefix now respect the `-http.prefix` flag.

Signed-off-by: Jacob Lisi <[email protected]>
@jtlisi jtlisi merged commit 1f600cb into cortexproject:master Apr 8, 2020
@jtlisi jtlisi deleted the 20200326_api_spike branch April 8, 2020 18:29
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.

Server path prefix flag is harmful
2 participants