Skip to content

Commit 5d02391

Browse files
Record and log some timings in orchestrator init function (#224)
* Record and log some timings in orchestrator init function * Add changelog entry
1 parent ee9a129 commit 5d02391

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ By following these guidelines, we can easily determine which changes should be i
1010

1111
## Edge
1212

13+
- [#224](https://github.com/circleci/runner-init/pull/224) Record and log timings in orchestrator init function.
1314
- [#212](https://github.com/circleci/runner-init/pull/212) Fix child process cleanup on Windows using job objects. This ensures that child processes are destroyed when the parent process (task-agent) terminates.
1415
- [#197](https://github.com/circleci/runner-init/pull/197) Fix `%PATH%` on Windows by using the OS-specific path list separator.
1516
- [#133](https://github.com/circleci/runner-init/pull/133) Don't re-handle task errors. If GOAT handles a task error (either with an infra-fail or retry), don't exit with a nonzero status code. Doing so causes container agent to overwrite the original error message in the UI.

cmd/orchestrator/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func run(version, date string) (err error) {
8484
c := cli.Init
8585
sys.AddService(func(_ context.Context) error {
8686
defer cancel()
87-
return initialize.Run(c.Source, c.Destination)
87+
return initialize.Run(ctx, c.Source, c.Destination)
8888
})
8989

9090
case "run-task":

init/init.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
11
package init
22

33
import (
4+
"context"
45
"errors"
56
"io"
67
"os"
78
"path/filepath"
89
"runtime"
10+
11+
"github.com/circleci/ex/o11y"
912
)
1013

1114
// Run function performs the copying of the orchestrator and task-agent binaries
12-
func Run(srcDir, destDir string) error {
15+
func Run(ctx context.Context, srcDir, destDir string) (err error) {
16+
ctx, span := o11y.StartSpan(ctx, "orchestrator: init")
17+
defer o11y.End(span, &err)
18+
19+
span.RecordMetric(o11y.Timing("init.duration"))
20+
1321
// Copy the orchestrator binary
1422
orchestratorSrc := filepath.Join(srcDir, binOrchestrator)
1523
orchestratorDest := filepath.Join(destDir, binOrchestrator)
16-
if err := copyFile(orchestratorSrc, orchestratorDest); err != nil {
24+
if err := copyFile(ctx, orchestratorSrc, orchestratorDest); err != nil {
1725
return err
1826
}
1927

2028
// Copy the task agent binary
2129
agentSrc := filepath.Join(srcDir, binCircleciAgent)
2230
agentDest := filepath.Join(destDir, binCircleciAgent)
23-
if err := copyFile(agentSrc, agentDest); err != nil {
31+
if err := copyFile(ctx, agentSrc, agentDest); err != nil {
2432
return err
2533
}
2634

@@ -33,15 +41,21 @@ func Run(srcDir, destDir string) error {
3341
} else {
3442
// We copy the binary instead of creating a symlink to `circleci` as we do on Linux,
3543
// since we do not have the necessary privileges to create symlinks to the shared volume on Windows.
36-
if err := copyFile(agentSrc, circleciDest); err != nil {
44+
if err := copyFile(ctx, agentSrc, circleciDest); err != nil {
3745
return err
3846
}
3947
}
4048

4149
return nil
4250
}
4351

44-
func copyFile(srcPath, destPath string) (err error) {
52+
func copyFile(ctx context.Context, srcPath, destPath string) (err error) {
53+
_, span := o11y.StartSpan(ctx, "orchestrator: init: copy")
54+
defer o11y.End(span, &err)
55+
56+
span.AddField("binary", filepath.Base(srcPath))
57+
span.RecordMetric(o11y.Timing("init.copy.duration"))
58+
4559
closeFile := func(f *os.File) {
4660
err = errors.Join(err, f.Close())
4761
}

init/init_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"runtime"
77
"testing"
88

9+
"github.com/circleci/ex/testing/testcontext"
910
"gotest.tools/v3/assert"
1011
"gotest.tools/v3/assert/cmp"
1112
)
@@ -18,9 +19,10 @@ func TestRun(t *testing.T) {
1819
agentSrc := filepath.Join(srcDir, binCircleciAgent)
1920
agentDest := filepath.Join(destDir, binCircleciAgent)
2021
circleciDest := filepath.Join(destDir, binCircleci)
22+
ctx := testcontext.Background()
2123

2224
t.Run("Copy files and create symlink", func(t *testing.T) {
23-
err := Run(srcDir, destDir)
25+
err := Run(ctx, srcDir, destDir)
2426
assert.NilError(t, err)
2527

2628
assertFileIsCopied(t, orchSrc, orchDest)
@@ -36,7 +38,7 @@ func TestRun(t *testing.T) {
3638
})
3739

3840
t.Run("Fail when source files not present", func(t *testing.T) {
39-
err := Run(srcDir, "non-existent-dir")
41+
err := Run(ctx, srcDir, "non-existent-dir")
4042
if runtime.GOOS == "windows" {
4143
assert.Check(t, cmp.ErrorContains(err, "The system cannot find the path specified"))
4244
} else {

0 commit comments

Comments
 (0)