Correlating Logrus Logs and Trace IDs in Datadog

Matt Kornfield
2 min readMay 3, 2023

Because their docs are just so lame right now

Photo by Matthew Henry on Unsplash

Recently I went to the Datadog docs to see how I could get my logs and traces nicely lined up. We use logrus, so I thought it’d be really easy to get set up, but the docs are just so dang sparse.

It’s basically “manually print the span into your log message.” Yeah no that totally sucks.

I thought I’d engineer something fancy, but as with most things, someone else already added something fancy for me.

First off: if you’re just using plain logging in Go, you should switch to Logrus. It does structured logging (which is where Datadog’s indexing really shines), and it’s super easy to use as a drop in replacement for the regular log library.

Next: assuming you have logrus installed and the datadog tracing libraries installed,

go get github.com/sirupsen/logrus
go get gopkg.in/DataDog/dd-trace-go.v1/ddtrace

and you’ve got your tracer started in your main function


import (
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)

func main() {
tracer.Start(tracer.WithAgentAddr("host:port"))
defer tracer.Stop()
...
}

you’ll just need to make sure logrus is initialized with this fancy hook that is not very well documented

--

--

Matt Kornfield
Matt Kornfield

Written by Matt Kornfield

Today's solutions are tomorrow's debugging adventure.

No responses yet