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

Make logging traceable #1685

Merged
merged 5 commits into from Jan 11, 2020
Merged

Make logging traceable #1685

merged 5 commits into from Jan 11, 2020

Conversation

sawadashota
Copy link
Contributor

@sawadashota sawadashota commented Jan 9, 2020

Related issue

#1680

Proposed changes

Add Tracing logging option.

log:
  tracing:
    public: false
    admin: true

Then, hydra logs like this.

$ curl -k https://localhost:4445/health/alive -H 'Traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01'
{"level":"info","method":"GET","msg":"started handling request","remote":"[::1]:56308","request":"/health/alive","time":"2020-01-11T11:09:46+09:00","trace_id":"00f067aa0ba902b7"}
{"level":"info","measure#hydra/admin: https://localhost:4444/.latency":218780,"method":"GET","msg":"completed handling request","remote":"[::1]:56308","request":"/health/alive","status":200,"text_status":"OK","time":"2020-01-11T11:09:46+09:00","took":218780,"trace_id":"00f067aa0ba902b7"}

If request doesn't have Traceparent header, log doesn't appear trace_id and span_id.

$ curl -k https://localhost:4445/health/alive 
{"level":"info","method":"GET","msg":"started handling request","remote":"[::1]:56396","request":"/health/alive","time":"2020-01-11T11:27:45+09:00"}
{"level":"info","measure#hydra/admin: https://localhost:4444/.latency":99643,"method":"GET","msg":"completed handling request","remote":"[::1]:56396","request":"/health/alive","status":200,"text_status":"OK","time":"2020-01-11T11:27:45+09:00","took":9964

Checklist

  • I have read the contributing guidelines
  • I have read the security policy
  • I confirm that this pull request does not address a security vulnerability. If this pull request addresses a security
    vulnerability, I confirm that I got green light (please contact security@ory.sh) from the maintainers to push the changes.
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation within the code base (if appropriate)
  • I have documented my changes in the developer guide (if appropriate)

logger.Before = func(entry *logrus.Entry, r *http.Request, _ string) *logrus.Entry {
fields := make(map[string]interface{})
for key, headerKey := range headers {
fields[key] = r.Header.Get(headerKey)
Copy link
Member

@aeneasr aeneasr Jan 9, 2020

Choose a reason for hiding this comment

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

This does not seem to be compliant with the open tracing specification: https://github.com/opentracing/specification/blob/584c52365fe0700ba25effa79dce5effb845ced8/rfc/trace_identifiers.md

I would expect that this parses the HTTP Header traceparent. I'm pretty sure that opentracing has a library to do that, so maybe we can just use that here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right. And I found library to extract tracing information from header!
https://github.com/open-telemetry/opentelemetry-go

Signed-off-by: Shota Sawada <xiootas@gmail.com>
Signed-off-by: Shota Sawada <xiootas@gmail.com>
@@ -154,6 +155,21 @@ func RunServeAll(version, build, date string) func(cmd *cobra.Command, args []st
}
}

func setTracingLogger(d driver.Driver, logger *negronilogrus.Middleware) {
logger.Before = func(entry *logrus.Entry, r *http.Request, remoteAddr string) *logrus.Entry {
Copy link
Member

Choose a reason for hiding this comment

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

This overwrites the existing logger.Before, right? I think we have a custom Before somewhere - Maybe you could check if logger.Before is set and if it is, execute it and then execute this logic here, but only adding the trace_id and span_id?

Copy link
Member

Choose a reason for hiding this comment

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

I see, it appears that we're not actually using that here. Here's the actual function I was referring to: https://github.com/ory/x/blob/master/reqlog/middleware.go

I think it would make sense to use that function and add the trace_id in that middleware. We should also add it to both before, and after!

Copy link
Member

Choose a reason for hiding this comment

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

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 fixed it!
It seemed logger.Before was set default(request, method and remote). I realized it's not safety to consider whether logger.Before or not, I fixed to add field.

We should also add it to both before, and after!

It's seems it's enough to add only before. Actually logs appear span_id and trace_id at both stating and completed handling request.

{"level":"info","method":"GET","msg":"started handling request","remote":"[::1]:56308","request":"/health/alive","time":"2020-01-11T11:09:46+09:00","trace_id":"00f067aa0ba902b7"}
{"level":"info","measure#hydra/admin: https://localhost:4444/.latency":218780,"method":"GET","msg":"completed handling request","remote":"[::1]:56308","request":"/health/alive","status":200,"text_status":"OK","time":"2020-01-11T11:09:46+09:00","took":218780,"trace_id":"00f067aa0ba902b7"}

@@ -413,3 +415,11 @@ func (v *ViperProvider) ShareOAuth2Debug() bool {
func (v *ViperProvider) PKCEEnforced() bool {
return viperx.GetBool(v.l, ViperKeyPKCEEnforced, false, "OAUTH2_PKCE_ENFORCED")
}

func (v *ViperProvider) LogTracingPublic() bool {
Copy link
Member

Choose a reason for hiding this comment

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

I would be fine with actually having this enabled always no config required

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 fixed it!

Signed-off-by: sawadashota <xiootas@gmail.com>
Signed-off-by: sawadashota <xiootas@gmail.com>
Signed-off-by: sawadashota <xiootas@gmail.com>
@sawadashota sawadashota changed the title config: Make logging traceable Make logging traceable Jan 11, 2020
@aeneasr
Copy link
Member

aeneasr commented Jan 11, 2020

Thank you! :)

@aeneasr aeneasr merged commit 3cee9b1 into ory:master Jan 11, 2020
@sawadashota sawadashota deleted the feat-1680 branch January 11, 2020 15:11
@sawadashota sawadashota mentioned this pull request Jan 16, 2020
6 tasks
eli-zh pushed a commit to eli-zh/hydra that referenced this pull request Mar 22, 2020
Signed-off-by: Shota Sawada <xiootas@gmail.com>
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.

None yet

2 participants