| 1 | package main |
| 2 | |
| 3 | // Progress: what a run says about itself while it runs, with --telemetry. |
| 4 | // |
| 5 | // A timing (timing.go) is printed when a check is reported, and checks |
| 6 | // are reported in the order given, so a slow first check holds back the |
| 7 | // lines of every check after it. Progress records are not held back. |
| 8 | // Each is stored in the telemetry stream (../telemetry) when the thing it |
| 9 | // records happens: the shared evaluation, a check queued for a build |
| 10 | // slot, its build starting and ending, and its result. So a reader sees a |
| 11 | // fast second check finish while a slow first one still builds. |
| 12 | // |
| 13 | // With telemetry, a nix build is run with nix's structured log |
| 14 | // (--log-format internal-json -L), which carries the builder's output |
| 15 | // line by line as it is written. That output goes to the check's log in |
| 16 | // the stream, bounded, as it arrives. nix's own messages are kept as the |
| 17 | // check's log the terminal shows on a failure, as without telemetry, and |
| 18 | // go to the stream's log too. The builder's lines are told apart by nix's |
| 19 | // framing, never by what a line says, so a check cannot write a record. |
| 20 | // |
| 21 | // Like a timing, progress is never evidence, and storing it never fails |
| 22 | // or holds up a run (telemetry.Emitter). |
| 23 | |
| 24 | import ( |
| 25 | "bytes" |
| 26 | "encoding/json" |
| 27 | "io" |
| 28 | "os/exec" |
| 29 | "regexp" |
| 30 | "strings" |
| 31 | "sync" |
| 32 | "time" |
| 33 | |
| 34 | "the-valley/telemetry" |
| 35 | ) |
| 36 | |
| 37 | // observer stores a run's progress. A nil observer stores nothing. |
| 38 | type observer struct { |
| 39 | tel *telemetry.Emitter |
| 40 | capacity string // what the run builds on, as given by --capacity |
| 41 | mu sync.Mutex |
| 42 | names map[string]string // a derivation's log is named for the first check that builds it |
| 43 | logs map[string]logged |
| 44 | } |
| 45 | |
| 46 | type logged struct { |
| 47 | bytes int64 |
| 48 | truncated bool |
| 49 | cached bool // nix built and fetched nothing (nixLog.ran) |
| 50 | } |
| 51 | |
| 52 | func newObserver(tel *telemetry.Emitter) *observer { |
| 53 | if tel == nil { |
| 54 | return nil |
| 55 | } |
| 56 | return &observer{tel: tel, names: map[string]string{}, logs: map[string]logged{}} |
| 57 | } |
| 58 | |
| 59 | func (o *observer) emit(typ, check string, data map[string]any) { |
| 60 | if o == nil { |
| 61 | return |
| 62 | } |
| 63 | o.tel.Emit(typ, telemetry.Record{Check: check}, data) |
| 64 | } |
| 65 | |
| 66 | // name says which check's log a derivation's build writes. |
| 67 | func (o *observer) name(drv, check string) { |
| 68 | if o == nil { |
| 69 | return |
| 70 | } |
| 71 | o.mu.Lock() |
| 72 | if _, ok := o.names[drv]; !ok { |
| 73 | o.names[drv] = check |
| 74 | } |
| 75 | o.mu.Unlock() |
| 76 | } |
| 77 | |
| 78 | // logOf is what was logged of a derivation's build. |
| 79 | func (o *observer) logOf(drv string) logged { |
| 80 | if o == nil { |
| 81 | return logged{} |
| 82 | } |
| 83 | o.mu.Lock() |
| 84 | defer o.mu.Unlock() |
| 85 | return o.logs[drv] |
| 86 | } |
| 87 | |
| 88 | // nixBuild builds one derivation as runPure does, and streams its log. |
| 89 | func (o *observer) nixBuild(tree, drv string) (string, string, error) { |
| 90 | o.mu.Lock() |
| 91 | name := o.names[drv] |
| 92 | o.mu.Unlock() |
| 93 | log := o.tel.Log(name) |
| 94 | cmd := exec.Command("nix", "build", "--no-link", "--print-out-paths", "--log-format", "internal-json", "-L", drv+"^*") |
| 95 | cmd.Dir = tree |
| 96 | var out bytes.Buffer |
| 97 | stderr := &nixLog{log: log} |
| 98 | cmd.Stdout = &out |
| 99 | cmd.Stderr = stderr |
| 100 | err := cmd.Run() |
| 101 | stderr.Write([]byte("\n")) // whatever was left without a newline |
| 102 | written, truncated := log.Close() |
| 103 | o.mu.Lock() |
| 104 | o.logs[drv] = logged{written, truncated, err == nil && !stderr.ran} |
| 105 | o.mu.Unlock() |
| 106 | return out.String(), stderr.text.String(), err |
| 107 | } |
| 108 | |
| 109 | // nixLog reads nix's structured log as it is written. The builder's |
| 110 | // lines go to the check's log alone. nix's messages, and its activities |
| 111 | // at the level it prints by default, go to the text a failed check shows |
| 112 | // and to the log. A line that is not nix's framing is kept as text, as |
| 113 | // nix printed it. ran says nix started a build or a substitution: a |
| 114 | // build that started neither found its outputs already in the store. |
| 115 | type nixLog struct { |
| 116 | log io.Writer |
| 117 | pending []byte |
| 118 | text bytes.Buffer |
| 119 | ran bool |
| 120 | } |
| 121 | |
| 122 | // The types of nix's activities that make an output: building a |
| 123 | // derivation and fetching a path from a substituter. |
| 124 | const ( |
| 125 | nixActBuild = 105 |
| 126 | nixActSubstitute = 108 |
| 127 | ) |
| 128 | |
| 129 | // ansi matches the colour sequences nix writes into its messages. |
| 130 | var ansi = regexp.MustCompile("\x1b\\[[0-9;]*[A-Za-z]") |
| 131 | |
| 132 | func (l *nixLog) Write(p []byte) (int, error) { |
| 133 | l.pending = append(l.pending, p...) |
| 134 | for { |
| 135 | i := bytes.IndexByte(l.pending, '\n') |
| 136 | if i < 0 { |
| 137 | break |
| 138 | } |
| 139 | l.line(string(l.pending[:i])) |
| 140 | l.pending = l.pending[i+1:] |
| 141 | } |
| 142 | return len(p), nil |
| 143 | } |
| 144 | |
| 145 | func (l *nixLog) line(line string) { |
| 146 | if line == "" { |
| 147 | return |
| 148 | } |
| 149 | payload, framed := strings.CutPrefix(line, "@nix ") |
| 150 | if !framed { |
| 151 | l.text.WriteString(line + "\n") |
| 152 | l.log.Write([]byte(line + "\n")) |
| 153 | return |
| 154 | } |
| 155 | var m struct { |
| 156 | Action string `json:"action"` |
| 157 | Type int `json:"type"` |
| 158 | Level int `json:"level"` |
| 159 | Msg string `json:"msg"` |
| 160 | Text string `json:"text"` |
| 161 | Fields []any `json:"fields"` |
| 162 | } |
| 163 | if json.Unmarshal([]byte(payload), &m) != nil { |
| 164 | return |
| 165 | } |
| 166 | if m.Action == "start" && (m.Type == nixActBuild || m.Type == nixActSubstitute) { |
| 167 | l.ran = true |
| 168 | } |
| 169 | switch { |
| 170 | case m.Action == "result" && m.Type == 101 && len(m.Fields) > 0: // a line of build output |
| 171 | if s, ok := m.Fields[0].(string); ok { |
| 172 | l.log.Write([]byte(s + "\n")) |
| 173 | } |
| 174 | case m.Action == "msg": |
| 175 | msg := ansi.ReplaceAllString(m.Msg, "") + "\n" |
| 176 | l.text.WriteString(msg) |
| 177 | l.log.Write([]byte(msg)) |
| 178 | case m.Action == "start" && m.Text != "" && m.Level <= 3: |
| 179 | l.text.WriteString(m.Text + "\n") |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // buildData is what a check's build-end and result records say about |
| 184 | // its build. |
| 185 | func buildData(b *drvBuild, wait time.Duration, began, ended time.Time, lg logged) map[string]any { |
| 186 | d := map[string]any{ |
| 187 | "build_ms": b.took.Milliseconds(), |
| 188 | "slot_wait_ms": wait.Milliseconds(), |
| 189 | "build_began": telemetry.Stamp(began), |
| 190 | "build_ended": telemetry.Stamp(ended), |
| 191 | "log_bytes": lg.bytes, |
| 192 | "cached": lg.cached, |
| 193 | } |
| 194 | if lg.truncated { |
| 195 | d["log_truncated"] = true |
| 196 | } |
| 197 | if b.err != nil { |
| 198 | d["build_error"] = clipError(b.err.Error()) |
| 199 | } |
| 200 | return d |
| 201 | } |
| 202 | |
| 203 | func clipError(s string) string { |
| 204 | if len(s) > 200 { |
| 205 | return s[:200] + "…" |
| 206 | } |
| 207 | return s |
| 208 | } |