Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error logging and debugging #394

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Change Log

- Fix field alignment issue [#393](https://github.com/hypermodeAI/runtime/pull/393)
- Improve error logging and debugging [#394](https://github.com/hypermodeAI/runtime/pull/394)

## 2024-09-26 - Version 0.12.3

Expand Down
22 changes: 15 additions & 7 deletions hostfunctions/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package hostfunctions

import (
"context"
"fmt"
"os"

"hypruntime/logger"
"hypruntime/utils"
Expand All @@ -17,17 +19,23 @@ func init() {

func LogFunctionMessage(ctx context.Context, level, message string) {

// store messages in the context, so we can return them to the caller
messages := ctx.Value(utils.FunctionMessagesContextKey).(*[]utils.LogMessage)
*messages = append(*messages, utils.LogMessage{
Level: level,
Message: message,
})

// If debugging, write debug messages to stderr instead of the logger
if level == "debug" && utils.HypermodeDebugEnabled() {
fmt.Fprintln(os.Stderr, message)
return
}

// write to the logger
logger.Get(ctx).
WithLevel(logger.ParseLevel(level)).
Str("text", message).
Bool("user_visible", true).
Msg("Message logged from function.")

// also store messages in the context, so we can return them to the caller
messages := ctx.Value(utils.FunctionMessagesContextKey).(*[]utils.LogMessage)
*messages = append(*messages, utils.LogMessage{
Level: level,
Message: message,
})
}
8 changes: 8 additions & 0 deletions wasmhost/fncall.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func (host *wasmHost) CallFunction(ctx context.Context, fnInfo functions.Functio
Bool("user_visible", true).
Msg("Function execution was canceled.")
} else {
// While debugging, it helps if we can see the error in the console without escaped newlines and other json formatting.
if utils.HypermodeDebugEnabled() {
fmt.Fprintln(os.Stderr, err)
}
Expand All @@ -161,6 +162,13 @@ func (host *wasmHost) CallFunction(ctx context.Context, fnInfo functions.Functio
Str("function", fnName).
Dur("duration_ms", duration).
Msg("Error while executing function.")

// However, we should still log _something_ that is user visible, so that the user knows something went wrong when they look at the function run logs.
logger.Error(ctx).
Str("function", fnName).
Dur("duration_ms", duration).
Bool("user_visible", true).
Msg("An internal runtime error occurred while executing the function.")
}

// Update metrics
Expand Down
Loading