| 1 | package telemetry |
| 2 | |
| 3 | // Emitting records from a producer, which must never be held up by it. |
| 4 | // |
| 5 | // Telemetry is best effort for the work it describes. Each record is |
| 6 | // stored apart from the caller, which waits at most EmitDeadline for it; |
| 7 | // a record that fails or takes longer is dropped. While one store is |
| 8 | // still stuck, every later record is dropped at once rather than queued |
| 9 | // behind it. The next record stored says how many were dropped before it, |
| 10 | // so a reader sees the loss rather than a quiet stream. |
| 11 | |
| 12 | import ( |
| 13 | "crypto/rand" |
| 14 | "encoding/hex" |
| 15 | "os" |
| 16 | "path/filepath" |
| 17 | "sync" |
| 18 | "time" |
| 19 | ) |
| 20 | |
| 21 | // EmitDeadline is how long a producer waits for one record to be stored. |
| 22 | const EmitDeadline = 500 * time.Millisecond |
| 23 | |
| 24 | // Emitter stores one run's records in a stream. A nil Emitter stores |
| 25 | // nothing, so a producer without telemetry passes none. |
| 26 | type Emitter struct { |
| 27 | stream *Stream |
| 28 | producer *Producer |
| 29 | run string |
| 30 | began time.Time |
| 31 | busy chan struct{} |
| 32 | |
| 33 | mu sync.Mutex |
| 34 | dropped int |
| 35 | base Record |
| 36 | } |
| 37 | |
| 38 | // NewEmitter returns an emitter for one run of a producer into the stream |
| 39 | // at dir, or nil when dir is empty. A stream whose directory, or whose |
| 40 | // parent before it is made, its group may read is exported for the |
| 41 | // collector, and keeps what it makes group-readable (Stream.Shared). |
| 42 | func NewEmitter(dir string, producer *Producer, run string) *Emitter { |
| 43 | if dir == "" { |
| 44 | return nil |
| 45 | } |
| 46 | shared := false |
| 47 | for _, d := range []string{dir, filepath.Dir(dir)} { |
| 48 | if info, err := os.Stat(d); err == nil { |
| 49 | shared = info.Mode().Perm()&0o040 != 0 |
| 50 | break |
| 51 | } |
| 52 | } |
| 53 | if run == "" { |
| 54 | run = NewRunID() |
| 55 | } |
| 56 | return &Emitter{ |
| 57 | stream: &Stream{Dir: dir, Shared: shared, LockWait: EmitDeadline}, |
| 58 | producer: producer, |
| 59 | run: run, |
| 60 | began: time.Now(), |
| 61 | busy: make(chan struct{}, 1), |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // Run is the run the emitter's records name. |
| 66 | func (e *Emitter) Run() string { |
| 67 | if e == nil { |
| 68 | return "" |
| 69 | } |
| 70 | return e.run |
| 71 | } |
| 72 | |
| 73 | // Stream is the stream the emitter stores into. |
| 74 | func (e *Emitter) Stream() *Stream { |
| 75 | if e == nil { |
| 76 | return nil |
| 77 | } |
| 78 | return e.stream |
| 79 | } |
| 80 | |
| 81 | // Correlate sets what every later record is about, where a record does |
| 82 | // not say itself. |
| 83 | func (e *Emitter) Correlate(base Record) { |
| 84 | if e == nil { |
| 85 | return |
| 86 | } |
| 87 | e.mu.Lock() |
| 88 | e.base = base |
| 89 | e.mu.Unlock() |
| 90 | } |
| 91 | |
| 92 | // Emit stores one record of type typ: r says what it is about beyond the |
| 93 | // emitter's correlation, and data says the rest. elapsed_ms, the time |
| 94 | // since the run began on this machine's monotonic clock, is added to |
| 95 | // every record that does not say its own. |
| 96 | func (e *Emitter) Emit(typ string, r Record, data map[string]any) { |
| 97 | if e == nil { |
| 98 | return |
| 99 | } |
| 100 | now := time.Now() |
| 101 | e.mu.Lock() |
| 102 | base := e.base |
| 103 | e.mu.Unlock() |
| 104 | r.Type, r.Time, r.Run, r.Producer = typ, Stamp(now), e.run, e.producer |
| 105 | for _, f := range []struct{ to, from *string }{ |
| 106 | {&r.Project, &base.Project}, {&r.Change, &base.Change}, {&r.Head, &base.Head}, |
| 107 | {&r.Tree, &base.Tree}, {&r.Check, &base.Check}, |
| 108 | } { |
| 109 | if *f.to == "" { |
| 110 | *f.to = *f.from |
| 111 | } |
| 112 | } |
| 113 | if r.Version == 0 { |
| 114 | r.Version = base.Version |
| 115 | } |
| 116 | // The record gets a copy of data: the store may still be writing it |
| 117 | // after Emit has returned, and the caller may go on using its map. |
| 118 | own := make(map[string]any, len(data)+2) |
| 119 | for k, v := range data { |
| 120 | own[k] = v |
| 121 | } |
| 122 | data = own |
| 123 | if _, said := data["elapsed_ms"]; !said { |
| 124 | data["elapsed_ms"] = now.Sub(e.began).Milliseconds() |
| 125 | } |
| 126 | |
| 127 | select { |
| 128 | case e.busy <- struct{}{}: |
| 129 | default: |
| 130 | e.drop() |
| 131 | return |
| 132 | } |
| 133 | e.mu.Lock() |
| 134 | if e.dropped > 0 { |
| 135 | data["dropped_before"] = e.dropped |
| 136 | } |
| 137 | e.mu.Unlock() |
| 138 | r.Data = data |
| 139 | done := make(chan error, 1) |
| 140 | go func() { |
| 141 | defer func() { <-e.busy }() |
| 142 | _, err := e.stream.Append(&r) |
| 143 | done <- err |
| 144 | }() |
| 145 | timer := time.NewTimer(EmitDeadline) |
| 146 | defer timer.Stop() |
| 147 | select { |
| 148 | case err := <-done: |
| 149 | if err != nil { |
| 150 | e.drop() |
| 151 | return |
| 152 | } |
| 153 | e.mu.Lock() |
| 154 | if n, ok := data["dropped_before"].(int); ok { |
| 155 | e.dropped -= n |
| 156 | } |
| 157 | e.mu.Unlock() |
| 158 | case <-timer.C: |
| 159 | e.drop() |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | func (e *Emitter) drop() { |
| 164 | e.mu.Lock() |
| 165 | e.dropped++ |
| 166 | e.mu.Unlock() |
| 167 | } |
| 168 | |
| 169 | // Log opens the build output log of one check in this run. |
| 170 | func (e *Emitter) Log(check string) *LogFile { |
| 171 | if e == nil { |
| 172 | return nil |
| 173 | } |
| 174 | return e.stream.Log(e.run, check, MaxLogBytes) |
| 175 | } |
| 176 | |
| 177 | // MaxLogBytes is how much of one check's output a run keeps. |
| 178 | const MaxLogBytes = 256 << 10 |
| 179 | |
| 180 | // NewRunID names a run: when it began, to the second, and a random part. |
| 181 | func NewRunID() string { |
| 182 | var b [4]byte |
| 183 | rand.Read(b[:]) |
| 184 | return "r" + time.Now().UTC().Format("20060102T150405") + "-" + hex.EncodeToString(b[:]) |
| 185 | } |