generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: timeline events for async calls (#3278)
Adds timeline events for async dequeues and errors. Closes #3282 <img width="1037" alt="Screenshot 2024-10-31 at 3 05 07 PM" src="https://github.com/user-attachments/assets/1291f8fc-16ee-40f0-bcad-2a443ef558db">
- Loading branch information
Showing
23 changed files
with
1,338 additions
and
609 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
backend/controller/sql/schema/20241030224020_timeline_async_execute_event.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
-- migrate:up | ||
|
||
ALTER TYPE event_type ADD VALUE IF NOT EXISTS 'async_execute'; | ||
|
||
-- migrate:down |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package timeline | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/alecthomas/types/optional" | ||
|
||
ftlencryption "github.com/TBD54566975/ftl/backend/controller/encryption/api" | ||
"github.com/TBD54566975/ftl/backend/controller/timeline/internal/sql" | ||
"github.com/TBD54566975/ftl/backend/libdal" | ||
"github.com/TBD54566975/ftl/internal/model" | ||
"github.com/TBD54566975/ftl/internal/schema" | ||
) | ||
|
||
type AsyncExecuteEvent struct { | ||
ID int64 | ||
Duration time.Duration | ||
AsyncExecute | ||
} | ||
|
||
func (e *AsyncExecuteEvent) GetID() int64 { return e.ID } | ||
func (e *AsyncExecuteEvent) event() {} | ||
|
||
type AsyncExecuteEventType string | ||
|
||
const ( | ||
AsyncExecuteEventTypeUnkown AsyncExecuteEventType = "unknown" | ||
AsyncExecuteEventTypeCron AsyncExecuteEventType = "cron" | ||
AsyncExecuteEventTypeFSM AsyncExecuteEventType = "fsm" | ||
AsyncExecuteEventTypePubSub AsyncExecuteEventType = "pubsub" | ||
) | ||
|
||
type AsyncExecute struct { | ||
DeploymentKey model.DeploymentKey | ||
RequestKey optional.Option[string] | ||
EventType AsyncExecuteEventType | ||
Verb schema.Ref | ||
Time time.Time | ||
Error optional.Option[string] | ||
} | ||
|
||
func (e *AsyncExecute) toEvent() (Event, error) { //nolint:unparam | ||
return &AsyncExecuteEvent{ | ||
AsyncExecute: *e, | ||
Duration: time.Since(e.Time), | ||
}, nil | ||
} | ||
|
||
type eventAsyncExecuteJSON struct { | ||
DurationMS int64 `json:"duration_ms"` | ||
EventType AsyncExecuteEventType `json:"event_type"` | ||
Error optional.Option[string] `json:"error,omitempty"` | ||
} | ||
|
||
func (s *Service) insertAsyncExecuteEvent(ctx context.Context, querier sql.Querier, event *AsyncExecuteEvent) error { | ||
asyncJSON := eventAsyncExecuteJSON{ | ||
DurationMS: event.Duration.Milliseconds(), | ||
EventType: event.EventType, | ||
Error: event.Error, | ||
} | ||
|
||
data, err := json.Marshal(asyncJSON) | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal async execute event: %w", err) | ||
} | ||
|
||
var payload ftlencryption.EncryptedTimelineColumn | ||
err = s.encryption.EncryptJSON(json.RawMessage(data), &payload) | ||
if err != nil { | ||
return fmt.Errorf("failed to encrypt cron JSON: %w", err) | ||
} | ||
|
||
err = libdal.TranslatePGError(querier.InsertTimelineAsyncExecuteEvent(ctx, sql.InsertTimelineAsyncExecuteEventParams{ | ||
DeploymentKey: event.DeploymentKey, | ||
RequestKey: event.RequestKey, | ||
TimeStamp: event.Time, | ||
Module: event.Verb.Module, | ||
Verb: event.Verb.Name, | ||
Payload: payload, | ||
})) | ||
if err != nil { | ||
return fmt.Errorf("failed to insert async execute event: %w", err) | ||
} | ||
return err | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.