sendLogsV2 uses the v2 agent API to send logs. Only compatibile with coder versions >= 2.9.
(ctx context.Context, dest agentsdk.LogDest, ls coderLogSender, l slog.Logger)
| 144 | |
| 145 | // sendLogsV2 uses the v2 agent API to send logs. Only compatibile with coder versions >= 2.9. |
| 146 | func sendLogsV2(ctx context.Context, dest agentsdk.LogDest, ls coderLogSender, l slog.Logger) (logger Func, closer func()) { |
| 147 | sendCtx, sendCancel := context.WithCancel(ctx) |
| 148 | done := make(chan struct{}) |
| 149 | uid := uuid.New() |
| 150 | go func() { |
| 151 | defer close(done) |
| 152 | if err := ls.SendLoop(sendCtx, dest); err != nil { |
| 153 | if !errors.Is(err, context.Canceled) { |
| 154 | l.Warn(ctx, "failed to send logs to Coder", slog.Error(err)) |
| 155 | } |
| 156 | } |
| 157 | }() |
| 158 | |
| 159 | var closeOnce sync.Once |
| 160 | return func(l Level, msg string, args ...any) { |
| 161 | ls.Enqueue(uid, agentsdk.Log{ |
| 162 | CreatedAt: time.Now(), |
| 163 | Output: fmt.Sprintf(msg, args...), |
| 164 | Level: codersdk.LogLevel(l), |
| 165 | }) |
| 166 | }, func() { |
| 167 | closeOnce.Do(func() { |
| 168 | // Trigger a flush and wait for logs to be sent. |
| 169 | ls.Flush(uid) |
| 170 | ctx, cancel := context.WithTimeout(ctx, logSendGracePeriod) |
| 171 | defer cancel() |
| 172 | err := ls.WaitUntilEmpty(ctx) |
| 173 | if err != nil { |
| 174 | l.Warn(ctx, "log sender did not empty", slog.Error(err)) |
| 175 | } |
| 176 | |
| 177 | // Stop the send loop. |
| 178 | sendCancel() |
| 179 | }) |
| 180 | |
| 181 | // Wait for the send loop to finish. |
| 182 | <-done |
| 183 | } |
| 184 | } |