Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add agent timings #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,10 @@ func (a *agent) reportMetadata(ctx context.Context, conn drpc.Conn) error {
select {
case <-ctx.Done():
return ctx.Err()
case timing := <-*a.scriptRunner.ScriptTimings():
aAPI.ScriptCompleted(ctx, &proto.WorkspaceAgentScriptCompletedRequest{
Timing: timing.ToProto(),
})
case mr := <-metadataResults:
// This can overwrite unsent values, but that's fine because
// we're only interested about up-to-date values.
Expand Down
13 changes: 13 additions & 0 deletions agent/agentscripts/agentscripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func New(opts Options) *Runner {
cronCtxCancel: cronCtxCancel,
cron: cron.New(cron.WithParser(parser)),
closed: make(chan struct{}),
scriptTimings: make(chan timingSpan),
dataDir: filepath.Join(opts.DataDirBase, "coder-script-data"),
scriptsExecuted: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "agent",
Expand All @@ -86,6 +87,7 @@ type Runner struct {
cron *cron.Cron
initialized atomic.Bool
scripts []codersdk.WorkspaceAgentScript
scriptTimings chan timingSpan
dataDir string

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

func (r *Runner) ScriptTimings() *chan timingSpan {
return &r.scriptTimings
}

// DataDir returns the directory where scripts data is stored.
func (r *Runner) DataDir() string {
return r.dataDir
Expand Down Expand Up @@ -314,6 +320,13 @@ func (r *Runner) run(ctx context.Context, script codersdk.WorkspaceAgentScript)
} else {
logger.Info(ctx, fmt.Sprintf("%s script completed", logPath), slog.F("execution_time", execTime), slog.F("exit_code", exitCode))
}

r.scriptTimings <- timingSpan{
displayName: script.DisplayName,
start: start,
end: end,
exitCode: int32(exitCode),
}
}()

err = cmd.Start()
Expand Down
23 changes: 23 additions & 0 deletions agent/agentscripts/timings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package agentscripts

import (
"time"

"github.com/coder/coder/v2/agent/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)

type timingSpan struct {
displayName string
start, end time.Time
exitCode int32
}

func (ts *timingSpan) ToProto() *proto.Timing {
return &proto.Timing{
DisplayName: ts.displayName,
Start: timestamppb.New(ts.start),
End: timestamppb.New(ts.end),
ExitCode: ts.exitCode,
}
}
Loading