| 33 | } |
| 34 | |
| 35 | func (a *LogsAPI) BatchCreateLogs(ctx context.Context, req *agentproto.BatchCreateLogsRequest) (*agentproto.BatchCreateLogsResponse, error) { |
| 36 | workspaceAgent, err := a.AgentFn(ctx) |
| 37 | if err != nil { |
| 38 | return nil, err |
| 39 | } |
| 40 | if workspaceAgent.LogsOverflowed { |
| 41 | return &agentproto.BatchCreateLogsResponse{LogLimitExceeded: true}, nil |
| 42 | } |
| 43 | |
| 44 | if len(req.Logs) == 0 { |
| 45 | return &agentproto.BatchCreateLogsResponse{}, nil |
| 46 | } |
| 47 | logSourceID, err := uuid.FromBytes(req.LogSourceId) |
| 48 | if err != nil { |
| 49 | return nil, xerrors.Errorf("parse log source ID %q: %w", req.LogSourceId, err) |
| 50 | } |
| 51 | |
| 52 | // This is to support the legacy API where the log source ID was |
| 53 | // not provided in the request body. We default to the external |
| 54 | // log source in this case. |
| 55 | if logSourceID == uuid.Nil { |
| 56 | // Use the external log source |
| 57 | externalSources, err := a.Database.InsertWorkspaceAgentLogSources(ctx, database.InsertWorkspaceAgentLogSourcesParams{ |
| 58 | WorkspaceAgentID: workspaceAgent.ID, |
| 59 | CreatedAt: a.now(), |
| 60 | ID: []uuid.UUID{agentsdk.ExternalLogSourceID}, |
| 61 | DisplayName: []string{"External"}, |
| 62 | Icon: []string{"/emojis/1f310.png"}, |
| 63 | }) |
| 64 | if database.IsUniqueViolation(err, database.UniqueWorkspaceAgentLogSourcesPkey) { |
| 65 | err = nil |
| 66 | logSourceID = agentsdk.ExternalLogSourceID |
| 67 | } |
| 68 | if err != nil { |
| 69 | return nil, xerrors.Errorf("insert external workspace agent log source: %w", err) |
| 70 | } |
| 71 | if len(externalSources) == 1 { |
| 72 | logSourceID = externalSources[0].ID |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | output := make([]string, 0) |
| 77 | level := make([]database.LogLevel, 0) |
| 78 | outputLength := 0 |
| 79 | for _, logEntry := range req.Logs { |
| 80 | sanitizedOutput := agentsdk.SanitizeLogOutput(logEntry.Output) |
| 81 | output = append(output, sanitizedOutput) |
| 82 | outputLength += len(sanitizedOutput) |
| 83 | |
| 84 | var dbLevel database.LogLevel |
| 85 | switch logEntry.Level { |
| 86 | case agentproto.Log_TRACE: |
| 87 | dbLevel = database.LogLevelTrace |
| 88 | case agentproto.Log_DEBUG: |
| 89 | dbLevel = database.LogLevelDebug |
| 90 | case agentproto.Log_INFO: |
| 91 | dbLevel = database.LogLevelInfo |
| 92 | case agentproto.Log_WARN: |