Log slow API requests with server spans and client timing

This commit is contained in:
Ian Gulliver
2026-10-21 14:45:19 -07:00
parent 671165ec71
commit b4c1dbab37
31 changed files with 458 additions and 185 deletions
+21 -20
View File
@@ -2,6 +2,7 @@ package git
import (
"bufio"
"context"
"fmt"
"io"
"os"
@@ -10,8 +11,8 @@ import (
"strings"
)
func RenameRefs(repo string, rename func(string) string) error {
branches, err := run(repo, "for-each-ref", "--format=%(refname:short)", "refs/heads/byre/")
func RenameRefs(ctx context.Context, repo string, rename func(string) string) error {
branches, err := run(ctx, repo, "for-each-ref", "--format=%(refname:short)", "refs/heads/byre/")
if err != nil {
return err
}
@@ -19,13 +20,13 @@ func RenameRefs(repo string, rename func(string) string) error {
if rename(b) == b {
continue
}
_, err = run(repo, "branch", "-m", b, rename(b))
_, err = run(ctx, repo, "branch", "-m", b, rename(b))
if err != nil {
return err
}
}
snaps, err := run(repo, "for-each-ref", "--format=%(objectname) %(refname)", "refs/byre/tasks/")
snaps, err := run(ctx, repo, "for-each-ref", "--format=%(objectname) %(refname)", "refs/byre/tasks/")
if err != nil {
return err
}
@@ -40,20 +41,20 @@ func RenameRefs(repo string, rename func(string) string) error {
if b.Len() == 0 {
return nil
}
_, err = runInput(repo, nil, b.String(), "update-ref", "--stdin")
_, err = runInput(ctx, repo, nil, b.String(), "update-ref", "--stdin")
return err
}
func RepairWorktrees(repo string, paths []string) error {
func RepairWorktrees(ctx context.Context, repo string, paths []string) error {
if len(paths) == 0 {
return nil
}
_, err := run(repo, append([]string{"worktree", "repair"}, paths...)...)
_, err := run(ctx, repo, append([]string{"worktree", "repair"}, paths...)...)
return err
}
func RewriteNotes(repo string, rewrite func(string) string) error {
parent, stderr, code, err := runCode(repo, nil, "rev-parse", "--verify", "--quiet", NotesRef)
func RewriteNotes(ctx context.Context, repo string, rewrite func(string) string) error {
parent, stderr, code, err := runCode(ctx, repo, nil, "rev-parse", "--verify", "--quiet", NotesRef)
if err != nil {
return err
}
@@ -64,7 +65,7 @@ func RewriteNotes(repo string, rewrite func(string) string) error {
return fmt.Errorf("git rev-parse %s: exit status %d: %s", NotesRef, code, stderr)
}
listing, err := run(repo, "ls-tree", "-r", parent)
listing, err := run(ctx, repo, "ls-tree", "-r", parent)
if err != nil {
return err
}
@@ -80,7 +81,7 @@ func RewriteNotes(repo string, rewrite func(string) string) error {
return nil
}
contents, err := catBlobs(repo, shas)
contents, err := catBlobs(ctx, repo, shas)
if err != nil {
return err
}
@@ -110,12 +111,12 @@ func RewriteNotes(repo string, rewrite func(string) string) error {
return nil
}
blobs, err := runInput(repo, nil, strings.Join(files, "\n")+"\n", "hash-object", "-w", "--stdin-paths")
blobs, err := runInput(ctx, repo, nil, strings.Join(files, "\n")+"\n", "hash-object", "-w", "--stdin-paths")
if err != nil {
return err
}
index := []string{"GIT_INDEX_FILE=" + filepath.Join(dir, "index")}
_, err = runEnv(repo, index, "read-tree", parent)
_, err = runEnv(ctx, repo, index, "read-tree", parent)
if err != nil {
return err
}
@@ -123,30 +124,30 @@ func RewriteNotes(repo string, rewrite func(string) string) error {
for i, blob := range splitLines(blobs) {
fmt.Fprintf(b, "100644 %s\t%s\n", blob, paths[changed[i]])
}
_, err = runInput(repo, index, b.String(), "update-index", "--index-info")
_, err = runInput(ctx, repo, index, b.String(), "update-index", "--index-info")
if err != nil {
return err
}
tree, err := runEnv(repo, index, "write-tree")
tree, err := runEnv(ctx, repo, index, "write-tree")
if err != nil {
return err
}
who, err := run(repo, "log", "-1", "--format=%cn%n%ce", parent)
who, err := run(ctx, repo, "log", "-1", "--format=%cn%n%ce", parent)
if err != nil {
return err
}
name, email, _ := strings.Cut(who, "\n")
env := IdentityEnv(name, email)
commit, err := runEnv(repo, env, "commit-tree", "-p", parent, "-m", "Rewrite byre ids in notes", tree)
commit, err := runEnv(ctx, repo, env, "commit-tree", "-p", parent, "-m", "Rewrite byre ids in notes", tree)
if err != nil {
return err
}
_, err = run(repo, "update-ref", NotesRef, commit, parent)
_, err = run(ctx, repo, "update-ref", NotesRef, commit, parent)
return err
}
func catBlobs(repo string, shas []string) (map[string]string, error) {
out, err := runRaw(repo, nil, strings.Join(shas, "\n")+"\n", "cat-file", "--batch")
func catBlobs(ctx context.Context, repo string, shas []string) (map[string]string, error) {
out, err := runRaw(ctx, repo, nil, strings.Join(shas, "\n")+"\n", "cat-file", "--batch")
if err != nil {
return nil, err
}