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

[Question] How to get hold of service, env, trace id, span id from span #1492

Closed
NicklasWallgren opened this issue Sep 30, 2022 · 3 comments
Closed
Assignees

Comments

@NicklasWallgren
Copy link
Contributor

How do one get hold of service, env, trace id and span id from the current span?

tracer.span implements fmt.Formatter which builds a string of the mentioned fields, but I need to get hold of a map, or struct containing the fields.

The end goal is to pass the fields to our (zap or logrus) logger, i.e

import (
  log "github.com/Sirupsen/logrus"
)
...
`log.WithFields(log.Fields{"dd.service": "<service>", "dd.env": "<env>", "dd.trace_id": <trace id>, "dd.span_id": "<span id>"})`
@NicklasWallgren
Copy link
Contributor Author

NicklasWallgren commented Sep 30, 2022

So the following should work for TraceId and SpanId.

span, _ := tracer.SpanFromContext(c.Request.Context())
span.Context().TraceID()
span.Context().SpanID()

Do we need to pass dd.env, dd.service and dd.version as well, as mentioned in https://docs.datadoghq.com/tracing/other_telemetry/connect_logs_and_traces/go/#:~:text=dd.trace_id%2C%20dd.span_id%2C%20dd.service%2C%20dd.env?

@ajgajg1134
Copy link
Contributor

I see your overall intent seems to be to connect spans to logs, we do have an integration for logrus already that may simplify that case here: https://github.com/DataDog/dd-trace-go/tree/main/contrib/sirupsen/logrus (or you can look at the source code for some inspiration on how to do something similar for zap. (And if you do and are interested in contributing it to this repository we're open to contributions! 😄 )

As for connecting the log entry to a span you should only need the trace_id and span_id.

@NicklasWallgren
Copy link
Contributor Author

NicklasWallgren commented Oct 1, 2022

@ajgajg1134 Thanks for the reply.

Zap dosen't support logging contextual metadata yet uber-go/zap#654, but I guess I can add my own implementation.

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

No branches or pull requests

2 participants