trace.go 630 B

123456789101112131415161718192021222324252627282930313233
  1. package tracer
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "runtime/trace"
  7. "github.com/kyleconroy/sqlc/internal/debug"
  8. )
  9. func Start(base context.Context) (context.Context, func(), error) {
  10. if !debug.Traced {
  11. return base, func() {}, nil
  12. }
  13. f, err := os.Create(debug.Debug.Trace)
  14. if err != nil {
  15. return base, func() {}, fmt.Errorf("failed to create trace output file: %v", err)
  16. }
  17. if err := trace.Start(f); err != nil {
  18. return base, func() {}, fmt.Errorf("failed to start trace: %v", err)
  19. }
  20. ctx, task := trace.NewTask(base, "sqlc")
  21. return ctx, func() {
  22. defer f.Close()
  23. defer trace.Stop()
  24. defer task.End()
  25. }, nil
  26. }