Skip to content

Commit bcfacf7

Browse files
feat: begin impl of agent script timings
1 parent 7c77a3c commit bcfacf7

27 files changed

+960
-452
lines changed

agent/agent.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,10 @@ func (a *agent) reportMetadata(ctx context.Context, conn drpc.Conn) error {
582582
select {
583583
case <-ctx.Done():
584584
return ctx.Err()
585+
case timing := <-*a.scriptRunner.ScriptTimings():
586+
aAPI.ScriptCompleted(ctx, &proto.WorkspaceAgentScriptCompletedRequest{
587+
Timing: timing.ToProto(),
588+
})
585589
case mr := <-metadataResults:
586590
// This can overwrite unsent values, but that's fine because
587591
// we're only interested about up-to-date values.

agent/agentscripts/agentscripts.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func New(opts Options) *Runner {
6666
cronCtxCancel: cronCtxCancel,
6767
cron: cron.New(cron.WithParser(parser)),
6868
closed: make(chan struct{}),
69+
scriptTimings: make(chan timingSpan),
6970
dataDir: filepath.Join(opts.DataDirBase, "coder-script-data"),
7071
scriptsExecuted: prometheus.NewCounterVec(prometheus.CounterOpts{
7172
Namespace: "agent",
@@ -86,6 +87,7 @@ type Runner struct {
8687
cron *cron.Cron
8788
initialized atomic.Bool
8889
scripts []codersdk.WorkspaceAgentScript
90+
scriptTimings chan timingSpan
8991
dataDir string
9092

9193
// scriptsExecuted includes all scripts executed by the workspace agent. Agents
@@ -94,6 +96,10 @@ type Runner struct {
9496
scriptsExecuted *prometheus.CounterVec
9597
}
9698

99+
func (r *Runner) ScriptTimings() *chan timingSpan {
100+
return &r.scriptTimings
101+
}
102+
97103
// DataDir returns the directory where scripts data is stored.
98104
func (r *Runner) DataDir() string {
99105
return r.dataDir
@@ -314,6 +320,13 @@ func (r *Runner) run(ctx context.Context, script codersdk.WorkspaceAgentScript)
314320
} else {
315321
logger.Info(ctx, fmt.Sprintf("%s script completed", logPath), slog.F("execution_time", execTime), slog.F("exit_code", exitCode))
316322
}
323+
324+
r.scriptTimings <- timingSpan{
325+
displayName: script.DisplayName,
326+
start: start,
327+
end: end,
328+
exitCode: int32(exitCode),
329+
}
317330
}()
318331

319332
err = cmd.Start()

agent/agentscripts/timings.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package agentscripts
2+
3+
import (
4+
"time"
5+
6+
"github.com/coder/coder/v2/agent/proto"
7+
"google.golang.org/protobuf/types/known/timestamppb"
8+
)
9+
10+
type timingSpan struct {
11+
displayName string
12+
start, end time.Time
13+
exitCode int32
14+
}
15+
16+
func (ts *timingSpan) ToProto() *proto.Timing {
17+
return &proto.Timing{
18+
DisplayName: ts.displayName,
19+
Start: timestamppb.New(ts.start),
20+
End: timestamppb.New(ts.end),
21+
ExitCode: ts.exitCode,
22+
}
23+
}

0 commit comments

Comments
 (0)