Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

log field type error: span log displays struct representation than the output of Error method. #199

Open
bsr203 opened this issue Oct 21, 2018 · 1 comment
Labels

Comments

@bsr203
Copy link

bsr203 commented Oct 21, 2018

Hi.

I have a custom error type (which implements Error interface) and use it in log.Error

When I was looking at this error through jaeger-ui, I see it is not the err.Error() I see, but the struct representation.

this may be the culprit of this issue

func (lf Field) String() string {
	return fmt.Sprint(lf.key, ":", lf.Value())
}

Sprint doesn't call the Error() method if the actual interface is an error, but a struct representation. as I see here

I am not sure Marshal formats it differently, but as far as I see, my trace log doesn't have some of the private info I add through Error method.

Please guide me if you think it may be because I use the API wrongly.

        fields := []log.Field {log.Error(myCustomError)}
	fields = append(fields, log.String("message", msg))
	fields = append(fields, log.String("level", "info"))
	span := opentracing.SpanFromContext(ctx) 
         span.LogFields(fields...)
@yurishkuro
Copy link
Member

Minor nit: use the LogFields() method with vararg list instead of allocating an array on the heap each time.

To the actual question, I think this is a combination of Jaeger behavior and OT implementation. You could argue that Jaeger is doing a simplistic (lazy) job of calling toString on the field instead of doing proper dispatching. Having said that, it does make sense to alter Field.String() function to be a bit smarter about the type, e.g. call Error() for errors, maybe check if the value implements Stringer(), etc.

Would you like to add a PR?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants