By Matt Kornfield.may 03, 2023
Because their documents are just so weak now
I recently went to the Datag documents to see how I could get my logs and traces nicely set up. We use a performance, so I thought it would be reality to set up, but the documents are just so scarce.
It is actually “Printing the spanning width manually in your log message.” Yes, no, that’s completely bad.
I thought I would develop Subthing Fancy, but as with most things, Subsone otherwise added to have added Subthing Fancy for me.
First of all: if you simply use log in, you must switch to reach. It makes structured log registration (where the indexation of Datag really seems), and it is super easy to use a decrease in replacement for the regular log library.
Following: Assuming you have reached the installation and data libraries, install, install,
go get github.com/sirupsen/logrus
go get gopkg.in/DataDog/dd-trace-go.v1/ddtrace
And you started your tracer 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 just have to make Southus that is initialized with this chic hook that is not well documented
import ( "github.com/sirupsen/logrus" dd_logrus "gopkg.in/DataDog/dd-trace-go.v1/contrib/sirupsen/logrus" ) ... func Init() { logrus.SetFormatter(&logrus.JSONFormatter{}) // Datadog ingests JSON easily logrus.AddHook(&dd_logrus.DDContextLogHook{}) // This'll do it! And it's included from the install }
That automatically injects the trace -ID into your logbooks, as long as you log in with information using logrus.WithContext
E.g.
log.WithContext(ctx).Info("hello datadog!")
As long as a Span -started (tracer.StartSpanFromContext
) And the context that has been transmitted in your position, the hook you set, gets the Span info out of the context and your logs are nicely correlated within Datidag.
So there you have it … just add the context tag and make sure you spend the spanning width start/ the context.
Thank you for reading! Hopefully this will help you.
The original article published on medium.