@Summary Patch workspace agent logs @ID patch-workspace-agent-logs @Security CoderSessionToken @Accept json @Produce json @Tags Agents @Param request body agentsdk.PatchLogs true "logs" @Success 200 {object} codersdk.Response @Router /api/v2/workspaceagents/me/logs [patch]
(rw http.ResponseWriter, r *http.Request)
| 141 | // @Success 200 {object} codersdk.Response |
| 142 | // @Router /api/v2/workspaceagents/me/logs [patch] |
| 143 | func (api *API) patchWorkspaceAgentLogs(rw http.ResponseWriter, r *http.Request) { |
| 144 | ctx := r.Context() |
| 145 | workspaceAgent := httpmw.WorkspaceAgent(r) |
| 146 | var req agentsdk.PatchLogs |
| 147 | if !httpapi.Read(ctx, rw, r, &req) { |
| 148 | return |
| 149 | } |
| 150 | if len(req.Logs) == 0 { |
| 151 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 152 | Message: "No logs provided.", |
| 153 | }) |
| 154 | return |
| 155 | } |
| 156 | // This is to support the legacy API where the log source ID was |
| 157 | // not provided in the request body. We default to the external |
| 158 | // log source in this case. |
| 159 | if req.LogSourceID == uuid.Nil { |
| 160 | // Use the external log source |
| 161 | externalSources, err := api.Database.InsertWorkspaceAgentLogSources(ctx, database.InsertWorkspaceAgentLogSourcesParams{ |
| 162 | WorkspaceAgentID: workspaceAgent.ID, |
| 163 | CreatedAt: dbtime.Now(), |
| 164 | ID: []uuid.UUID{agentsdk.ExternalLogSourceID}, |
| 165 | DisplayName: []string{"External"}, |
| 166 | Icon: []string{"/emojis/1f310.png"}, |
| 167 | }) |
| 168 | if database.IsUniqueViolation(err, database.UniqueWorkspaceAgentLogSourcesPkey) { |
| 169 | err = nil |
| 170 | req.LogSourceID = agentsdk.ExternalLogSourceID |
| 171 | } |
| 172 | if err != nil { |
| 173 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 174 | Message: "Failed to create external log source.", |
| 175 | Detail: err.Error(), |
| 176 | }) |
| 177 | return |
| 178 | } |
| 179 | if len(externalSources) == 1 { |
| 180 | req.LogSourceID = externalSources[0].ID |
| 181 | } |
| 182 | } |
| 183 | output := make([]string, 0) |
| 184 | level := make([]database.LogLevel, 0) |
| 185 | outputLength := 0 |
| 186 | for _, logEntry := range req.Logs { |
| 187 | sanitizedOutput := agentsdk.SanitizeLogOutput(logEntry.Output) |
| 188 | output = append(output, sanitizedOutput) |
| 189 | outputLength += len(sanitizedOutput) |
| 190 | if logEntry.Level == "" { |
| 191 | // Default to "info" to support older agents that didn't have the level field. |
| 192 | logEntry.Level = codersdk.LogLevelInfo |
| 193 | } |
| 194 | parsedLevel := database.LogLevel(logEntry.Level) |
| 195 | if !parsedLevel.Valid() { |
| 196 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 197 | Message: "Invalid log level provided.", |
| 198 | Detail: fmt.Sprintf("invalid log level: %q", logEntry.Level), |
| 199 | }) |
| 200 | return |
nothing calls this directly
no test coverage detected