snapshot of https://github.com/sqlc-dev/sqlc.git (main, 2026-09-16; blobs > 131072B omitted)
Some checks failed
kotlin / test (push) Has been skipped
python / test (push) Has been skipped
typescript / test (push) Has been skipped
go / build darwin/amd64 (push) Has been cancelled
go / build linux/amd64 (push) Has been cancelled
go / build windows/amd64 (push) Has been cancelled
go / build darwin/arm64 (push) Has been cancelled
go / build linux/arm64 (push) Has been cancelled
go / build windows/arm64 (push) Has been cancelled
go / test (push) Has been cancelled
go / vuln_check (push) Has been cancelled
Some checks failed
kotlin / test (push) Has been skipped
python / test (push) Has been skipped
typescript / test (push) Has been skipped
go / build darwin/amd64 (push) Has been cancelled
go / build linux/amd64 (push) Has been cancelled
go / build windows/amd64 (push) Has been cancelled
go / build darwin/arm64 (push) Has been cancelled
go / build linux/arm64 (push) Has been cancelled
go / build windows/arm64 (push) Has been cancelled
go / test (push) Has been cancelled
go / vuln_check (push) Has been cancelled
This commit is contained in:
44
internal/tracer/trace.go
Normal file
44
internal/tracer/trace.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package tracer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime/trace"
|
||||
|
||||
"github.com/sqlc-dev/sqlc/internal/sqlcdebug"
|
||||
)
|
||||
|
||||
var debugTrace = sqlcdebug.New("trace")
|
||||
|
||||
// Path returns the file to which Go's runtime tracer should write its
|
||||
// output, derived from SQLCDEBUG=trace=...
|
||||
func Path() string {
|
||||
v := debugTrace.Value()
|
||||
if v == "1" {
|
||||
return "trace.out"
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// Start starts Go's runtime tracing facility. Traces are written to the
|
||||
// path returned by [Path]. It also starts a new [*trace.Task] that will
|
||||
// be stopped when the cleanup is called.
|
||||
func Start(base context.Context) (_ context.Context, cleanup func(), _ error) {
|
||||
f, err := os.Create(Path())
|
||||
if err != nil {
|
||||
return base, cleanup, fmt.Errorf("failed to create trace output file: %v", err)
|
||||
}
|
||||
|
||||
if err := trace.Start(f); err != nil {
|
||||
return base, cleanup, fmt.Errorf("failed to start trace: %v", err)
|
||||
}
|
||||
|
||||
ctx, task := trace.NewTask(base, "sqlc")
|
||||
|
||||
return ctx, func() {
|
||||
defer f.Close()
|
||||
defer trace.Stop()
|
||||
defer task.End()
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user