Skip to content

Commit 734c2da

Browse files
committed
add inflight and duration to git commands
still kind of buggy as it doesn't properly encode commands
1 parent 206eb3e commit 734c2da

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

modules/git/command.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,39 @@ import (
2222
"code.gitea.io/gitea/modules/log"
2323
"code.gitea.io/gitea/modules/process"
2424
"code.gitea.io/gitea/modules/util"
25+
26+
"github.com/prometheus/client_golang/prometheus"
27+
"github.com/prometheus/client_golang/prometheus/promauto"
2528
)
2629

2730
// TrustedCmdArgs returns the trusted arguments for git command.
2831
// It's mainly for passing user-provided and trusted arguments to git command
2932
// In most cases, it shouldn't be used. Use AddXxx function instead
3033
type TrustedCmdArgs []internal.CmdArg
3134

35+
const gitOperation = "command"
36+
3237
var (
3338
// globalCommandArgs global command args for external package setting
3439
globalCommandArgs TrustedCmdArgs
3540

3641
// defaultCommandExecutionTimeout default command execution timeout duration
3742
defaultCommandExecutionTimeout = 360 * time.Second
43+
44+
reqInflightGauge = promauto.NewGaugeVec(prometheus.GaugeOpts{
45+
Namespace: "gitea",
46+
Subsystem: "git",
47+
Name: "active_commands",
48+
Help: "Number of active git subprocesses.",
49+
}, []string{gitOperation})
50+
// reqDurationHistogram tracks the time taken by http request
51+
reqDurationHistogram = promauto.NewHistogramVec(prometheus.HistogramOpts{
52+
Namespace: "gitea",
53+
Subsystem: "git",
54+
Name: "command_duration_seconds", // diverge from spec to store the unit in metric.
55+
Help: "Measures the time taken by git subprocesses",
56+
Buckets: []float64{0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10, 30, 60, 120, 300}, // based on dotnet buckets https://github.com/open-telemetry/semantic-conventions/issues/336
57+
}, []string{gitOperation})
3858
)
3959

4060
// DefaultLocale is the default LC_ALL to run git commands in.
@@ -315,6 +335,10 @@ func (c *Command) run(ctx context.Context, skip int, opts *RunOpts) error {
315335
desc := fmt.Sprintf("git.Run(by:%s, repo:%s): %s", callerInfo, logArgSanitize(opts.Dir), cmdLogString)
316336
log.Debug("git.Command: %s", desc)
317337

338+
inflight := reqInflightGauge.WithLabelValues(c.args[0]) // add command type
339+
inflight.Inc()
340+
defer inflight.Dec()
341+
318342
_, span := gtprof.GetTracer().Start(ctx, gtprof.TraceSpanGitRun)
319343
defer span.End()
320344
span.SetAttributeString(gtprof.TraceAttrFuncCaller, callerInfo)
@@ -364,6 +388,7 @@ func (c *Command) run(ctx context.Context, skip int, opts *RunOpts) error {
364388
if elapsed > time.Second {
365389
log.Debug("slow git.Command.Run: %s (%s)", c, elapsed)
366390
}
391+
reqDurationHistogram.WithLabelValues(c.args[0]).Observe(elapsed.Seconds())
367392

368393
// We need to check if the context is canceled by the program on Windows.
369394
// This is because Windows does not have signal checking when terminating the process.

0 commit comments

Comments
 (0)