Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDB Query Analyzer #160

Open
bounoable opened this issue Sep 27, 2023 · 0 comments
Open

MongoDB Query Analyzer #160

bounoable opened this issue Sep 27, 2023 · 0 comments
Labels
draft enhancement New feature or request

Comments

@bounoable
Copy link
Contributor

I want to be able to debug MongoDB queries that are executed by projection jobs.

Something like this:

package example

func example() {
  queries := make(chan mongo.ExecutedQuery)
  go func(){
    for q := range queries {
      log.Printf("Query: %s", q.Query)
      log.Printf("Runtime: %s", q.Runtime)
    }
  }()

  store := mongo.NewEventStore(..., mongo.MonitorQueries(queries))
  events, errs, err := store.Query(context.TODO(), query.New(...))
}

To easily identify which part of an app caused a query, maybe implement "query descriptions":

package example

func example() {
  queries := make(chan mongo.ExecutedQuery)
  go func(){
    for q := range queries {
      if query.Description(q.Query) == "example" {
        log.Printf("Query: %s", q.Query)
        log.Printf("Runtime: %s", q.Runtime)
      }

      // or
      if query.Expand(q.Query).Description() == "example" {
        log.Printf("Query: %s", q.Query)
        log.Printf("Runtime: %s", q.Runtime)
      }
    }
  }()

  q := query.New(...)
  q.Describe("example")

  store := mongo.NewEventStore(..., mongo.MonitorQueries(queries))
  events, errs, err := store.Query(context.TODO(), q)
}

Or maybe event implement full query metadata support:

package example

func example() {
  queries := make(chan mongo.ExecutedQuery)
  go func(){
    for q := range queries {
      if query.MetadataOf[map[string]string](q.Query)["description"] == "example" {
        log.Printf("Query: %s", q.Query)
        log.Printf("Runtime: %s", q.Runtime)
      }

      // or
      if query.Expand(q.Query).Metadata().(map[string]string)["description"] == "example" {
        log.Printf("Query: %s", q.Query)
        log.Printf("Runtime: %s", q.Runtime)
      }
    }
  }()

  q := query.New(query.Metadata(map[string]string{
    "description": "example",
  }))

  store := mongo.NewEventStore(..., mongo.MonitorQueries(queries))
  events, errs, err := store.Query(context.TODO(), q)
}

Metadata could support arbitrary types:

package example

func example() {
  queries := make(chan mongo.ExecutedQuery)
  go func(){
    for q := range queries {
      if query.MetadataOf[string](q.Query) == "example" {
        log.Printf("Query: %s", q.Query)
        log.Printf("Runtime: %s", q.Runtime)
      }

      if query.Expand(q.Query).Metadata().(string) == "example" {
        log.Printf("Query: %s", q.Query)
        log.Printf("Runtime: %s", q.Runtime)
      }
    }
  }()

  q := query.New(query.Metadata("example"))

  store := mongo.NewEventStore(..., mongo.MonitorQueries(queries))
  events, errs, err := store.Query(context.TODO(), q)
}
@bounoable bounoable added enhancement New feature or request draft labels Sep 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
draft enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant