I have built a demo REST API in Go with MongoDB as the datastore, with Gin as the HTTP framework. I have followed all of the instructions on the Datadog website, however, am getting traces with little to no information. All that is traced is http.request
. On the Datadog blog there is a screenshot with a Go trace (see below).
Here is what I'm seeing - there is no trace of the MongoDB Query performed in the request.
Is there some other configuration or manual reporting that I have to do?
I am initiating the Datadog tracer with the following in my main function:
rules := []tracer.SamplingRule{tracer.RateRule(1)}
tracer.Start(
tracer.WithSamplingRules(rules),
tracer.WithService("Go Mongo"),
tracer.WithEnv("dev"),
)
defer tracer.Stop()
Full code is available at: https://github.com/NG235/go-mongo
Thanks.
CodePudding user response:
Foremost, in all your handlers you need to use request.Context()
:
ctx := c.Request.Context()
Besides other reasons, that context contains current tracing span ID, and it is the best option to connect spans together.
Using that context you can instrument sections of your code as described in official documentation:
newCtx, span := otel.Tracer(name).Start(ctx, "Run")
defer span.End()
Last piece is instrumentation for used libraries like MongoDB.
import (
"go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo"
)
func Connect() *mongo.Database {
opts := options.Client()
opts.Monitor = otelmongo.NewMonitor()
opts.ApplyURI("mongodb://root:root@localhost:27017")
client, err := mongo.Connect(context.Background(), opts)
Now every MongoDB operation is traced, and it is connected to parent span if you propagate request.Context
into that operation.