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/v2: add path and method to API v2 logs #2261

Conversation

simonpasquier
Copy link
Member

@simonpasquier simonpasquier commented May 18, 2020

When an API v2 handler logged a message, the log wouldn't include the
path and method. Since different handlers perform the same
validations (e.g. matchers for alerts and silences), it isn't easy to
know which handler was invoked (though the logged filename + line number provides a hint).

@@ -124,8 +124,6 @@ func NewAPI(
openAPI.SilenceGetSilencesHandler = silence_ops.GetSilencesHandlerFunc(api.getSilencesHandler)
openAPI.SilencePostSilencesHandler = silence_ops.PostSilencesHandlerFunc(api.postSilencesHandler)

openAPI.Logger = func(s string, i ...interface{}) { level.Error(api.logger).Log(i...) }
Copy link
Member Author

Choose a reason for hiding this comment

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

After reading through the openapi code, it seems that it isn't used anywhere because we're not using *github.com/prometheus/alertmanager/api/v2/restapi.Server directly.
In any case the code here didn't work as expected, it should have been level.Error(api.logger).Log("msg", fmt.Sprintf(s, i...)).

api/v2/api.go Outdated
@@ -216,18 +214,20 @@ func (api *API) getAlertsHandler(params alert_ops.GetAlertsParams) middleware.Re
// are no alerts present
res = open_api_models.GettableAlerts{}
ctx = params.HTTPRequest.Context()

logger = log.With(api.logger, "handler", "alerts", "methods", "GET")
Copy link
Member

Choose a reason for hiding this comment

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

do we have to hardcode those info?

Copy link
Member

Choose a reason for hiding this comment

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

You could probably extract some of them from params.HTTPRequest.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the feedback! I've updated the code.

When an API v2 handler logged a message, the log wouldn't include the
path and method. Since different handlers perform the same validations
(e.g. matchers for alerts and silences), it isn't easy to know which
handler was invoked (though the logged filename
+ line number provides a hint).

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
@simonpasquier simonpasquier force-pushed the add-handler-and-method-to-api-logs branch from 10e926f to 2b845ab Compare May 25, 2020 12:45
@simonpasquier simonpasquier changed the title api/v2: add handler and method to API v2 logs api/v2: add path and method to API v2 logs May 25, 2020
Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

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

For what my review is worth this looks good to me.

@roidelapluie
Copy link
Member

In prometheus/prometheus log msg fields are capitalized. Consistency would be good

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
@simonpasquier simonpasquier merged commit e0cc523 into prometheus:master Jun 2, 2020
@simonpasquier simonpasquier deleted the add-handler-and-method-to-api-logs branch June 3, 2020 07:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants