Middleware tags agent logs for requests that originate from chatd. Agent log lines emitted while serving a request with Coder-Chat-Id, or by background work started by such a request, should include chat_id. Install after loggermw.Logger so access-log enrichment can run.
(next http.Handler)
| 54 | // or by background work started by such a request, should include chat_id. |
| 55 | // Install after loggermw.Logger so access-log enrichment can run. |
| 56 | func Middleware(next http.Handler) http.Handler { |
| 57 | return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |
| 58 | chatID, ancestorIDs, ok := extractContext(r) |
| 59 | if !ok { |
| 60 | next.ServeHTTP(rw, r) |
| 61 | return |
| 62 | } |
| 63 | |
| 64 | fields := chatFields(chatID, ancestorIDs) |
| 65 | if requestLogger := loggermw.RequestLoggerFromContext(r.Context()); requestLogger != nil { |
| 66 | requestLogger.WithFields(fields...) |
| 67 | } |
| 68 | |
| 69 | ctx := WithContext(r.Context(), chatID, ancestorIDs) |
| 70 | next.ServeHTTP(rw, r.WithContext(ctx)) |
| 71 | }) |
| 72 | } |
| 73 | |
| 74 | func chatFields(chatID uuid.UUID, ancestorIDs []uuid.UUID) []slog.Field { |
| 75 | fields := []slog.Field{slog.F("chat_id", chatID.String())} |