Log slow API requests with server spans and client timing
This commit is contained in:
+19
-18
@@ -1,6 +1,7 @@
|
||||
package git
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
@@ -17,25 +18,25 @@ var statuses = map[byte]api.FileChangeStatus{
|
||||
'T': api.TypeChanged,
|
||||
}
|
||||
|
||||
func Changes(dir, defaultBranch, since string) (api.TaskChanges, error) {
|
||||
tree, err := Snapshot(dir)
|
||||
func Changes(ctx context.Context, dir, defaultBranch, since string) (api.TaskChanges, error) {
|
||||
tree, err := Snapshot(ctx, dir)
|
||||
if err != nil {
|
||||
return api.TaskChanges{}, err
|
||||
}
|
||||
return Compare(dir, defaultBranch, "HEAD", tree, since)
|
||||
return Compare(ctx, dir, defaultBranch, "HEAD", tree, since)
|
||||
}
|
||||
|
||||
func Compare(dir, defaultBranch, head, tree, since string) (api.TaskChanges, error) {
|
||||
func Compare(ctx context.Context, dir, defaultBranch, head, tree, since string) (api.TaskChanges, error) {
|
||||
base := since
|
||||
if base == "" {
|
||||
mb, err := run(dir, "merge-base", head, defaultBranch)
|
||||
mb, err := run(ctx, dir, "merge-base", head, defaultBranch)
|
||||
if err != nil {
|
||||
return api.TaskChanges{}, err
|
||||
}
|
||||
base = mb
|
||||
}
|
||||
|
||||
files, err := stats(dir, base, tree)
|
||||
files, err := stats(ctx, dir, base, tree)
|
||||
if err != nil {
|
||||
return api.TaskChanges{}, err
|
||||
}
|
||||
@@ -43,20 +44,20 @@ func Compare(dir, defaultBranch, head, tree, since string) (api.TaskChanges, err
|
||||
return api.TaskChanges{Base: base, Tree: tree, Files: files}, nil
|
||||
}
|
||||
|
||||
func ChangedFileCount(dir, defaultBranch, tree string) (int, error) {
|
||||
base, err := run(dir, "merge-base", "HEAD", defaultBranch)
|
||||
func ChangedFileCount(ctx context.Context, dir, defaultBranch, tree string) (int, error) {
|
||||
base, err := run(ctx, dir, "merge-base", "HEAD", defaultBranch)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
paths, err := run(dir, "diff", "--find-renames", "--name-only", "-z", base, tree)
|
||||
paths, err := run(ctx, dir, "diff", "--find-renames", "--name-only", "-z", base, tree)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return strings.Count(paths, "\x00"), nil
|
||||
}
|
||||
|
||||
func Snapshot(dir string) (string, error) {
|
||||
index, err := run(dir, "rev-parse", "--path-format=absolute", "--git-path", "index")
|
||||
func Snapshot(ctx context.Context, dir string) (string, error) {
|
||||
index, err := run(ctx, dir, "rev-parse", "--path-format=absolute", "--git-path", "index")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -90,27 +91,27 @@ func Snapshot(dir string) (string, error) {
|
||||
}
|
||||
|
||||
env := []string{"GIT_INDEX_FILE=" + tmp.Name()}
|
||||
_, err = runEnv(dir, env, "add", "--all")
|
||||
_, err = runEnv(ctx, dir, env, "add", "--all")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return runEnv(dir, env, "write-tree")
|
||||
return runEnv(ctx, dir, env, "write-tree")
|
||||
}
|
||||
|
||||
func FileDiff(dir, base, tree, path, oldPath string) (string, error) {
|
||||
func FileDiff(ctx context.Context, dir, base, tree, path, oldPath string) (string, error) {
|
||||
args := []string{"diff", "--find-renames", "--no-ext-diff", "--no-color", base, tree, "--", path}
|
||||
if oldPath != "" {
|
||||
args = append(args, oldPath)
|
||||
}
|
||||
return run(dir, args...)
|
||||
return run(ctx, dir, args...)
|
||||
}
|
||||
|
||||
func stats(dir, base, tree string) ([]api.FileChange, error) {
|
||||
nameStatus, err := run(dir, "diff", "--find-renames", "--name-status", "-z", base, tree)
|
||||
func stats(ctx context.Context, dir, base, tree string) ([]api.FileChange, error) {
|
||||
nameStatus, err := run(ctx, dir, "diff", "--find-renames", "--name-status", "-z", base, tree)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
numstat, err := run(dir, "diff", "--find-renames", "--numstat", "-z", base, tree)
|
||||
numstat, err := run(ctx, dir, "diff", "--find-renames", "--numstat", "-z", base, tree)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user