| 2155 | } |
| 2156 | |
| 2157 | func (a *agent) HandleHTTPDebugLogs(w http.ResponseWriter, r *http.Request) { |
| 2158 | logPath := filepath.Join(a.logDir, "coder-agent.log") |
| 2159 | f, err := os.Open(logPath) |
| 2160 | if err != nil { |
| 2161 | a.logger.Error(r.Context(), "open agent log file", slog.Error(err), slog.F("path", logPath)) |
| 2162 | w.WriteHeader(http.StatusInternalServerError) |
| 2163 | _, _ = fmt.Fprintf(w, "could not open log file: %s", err) |
| 2164 | return |
| 2165 | } |
| 2166 | defer f.Close() |
| 2167 | |
| 2168 | // Limit to 10MiB. |
| 2169 | w.WriteHeader(http.StatusOK) |
| 2170 | _, err = io.Copy(w, io.LimitReader(f, 10*1024*1024)) |
| 2171 | if err != nil && !errors.Is(err, io.EOF) { |
| 2172 | a.logger.Error(r.Context(), "read agent log file", slog.Error(err)) |
| 2173 | return |
| 2174 | } |
| 2175 | } |
| 2176 | |
| 2177 | func (a *agent) HTTPDebug() http.Handler { |
| 2178 | r := chi.NewRouter() |