Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-739

Support OpenTracing

    • Type: Icon: New Feature New Feature
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 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

      Unable to find source-code formatter for language: golang. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      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

      Unable to find source-code formatter for language: golang. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      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()

      Unable to find source-code formatter for language: golang. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      NewClient().WithTracer(tracer)
      

      add tracer option after client init, then driver do the job of start child span for every command.

            Assignee:
            divjot.arora@mongodb.com Divjot Arora (Inactive)
            Reporter:
            wyx.loading wuyuxiang
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: