-
Type: New Feature
-
Resolution: Works as Designed
-
Priority: Major - P3
-
None
-
Affects Version/s: No Release
-
Component/s: API
Opentracing
Please look at https://opentracing.io first.
We are building distributed services, and we use opentracing stack to help us tracing what our requests really route to, and what our requests really do.
Work with mongodb
Many business has to work with mongodb. But at this time, what we can do if we want to monitor every mongodb command do and time costs. Yes, check the mongodb profiling log may help. But in real industry mode, we don't have this permission to check this log, or mongodb didn't set high profiling level.
That's what opentracing can do. We trace mongodb command at the client side. We trace these three info for every mongodb command: command type, namespace, err. Even more, we can add more infomation if we need.
How does this work
span, ctx := opentracing.StartSpanFromContext(ctx, "mongodb command") defer span.Finish() span.LogFields( log.String("command", "insert"), log.String("namespace", "db.collection"))
That's easy as above.
We start a child span, naming "mongodb command", with two fields, command:insert, namespace: db.collection. Of course, we can add mongo result when mongodb command done.
Proposal
1. Wrapper
func (c *client) WrapCommand(fn func(oldProcess func(context.Context, *command.Command) error) func(context.Context, *command.Command) { return fn(c.mongoRawProcess) } c.WrapCommand(func(oldProcess func(context.Context, *command.Command) error { return func(ctx context.Context, cmd *command.Command) { span, ctx := opentracing.StartSpanFromContext(ctx, "mongodb command") defer span.Finish() span.LogFields( log.String("command", cmd.Type()), log.String("namespace", cmd.Namespace()) err := oldProcess(ctx, cmd) if err != nil { span.LogFields("fail", 1) } return err } })
This may keep mongo-go-driver repo clean. Leave choice to user.
2. WithTracer()
NewClient().WithTracer(tracer)
add tracer option after client init, then driver do the job of start child span for every command.