-
Notifications
You must be signed in to change notification settings - Fork 275
Handle error events in HCS notifications. #2526
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
Open
helsaawy
wants to merge
1
commit into
microsoft:main
Choose a base branch
from
helsaawy:notif-err-events
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+149
−41
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -8,9 +8,13 @@ import ( | |
| "errors" | ||
| "fmt" | ||
| "net" | ||
| "strings" | ||
| "syscall" | ||
|
|
||
| "github.com/sirupsen/logrus" | ||
|
|
||
| "github.com/Microsoft/hcsshim/internal/log" | ||
| "github.com/Microsoft/hcsshim/internal/logfields" | ||
| ) | ||
|
|
||
| var ( | ||
|
|
@@ -99,48 +103,85 @@ type ErrorEvent struct { | |
| EventID uint16 `json:"EventId,omitempty"` | ||
| Flags uint32 `json:"Flags,omitempty"` | ||
| Source string `json:"Source,omitempty"` | ||
| //Data []EventData `json:"Data,omitempty"` // Omit this as HCS doesn't encode this well. It's more confusing to include. It is however logged in debug mode (see processHcsResult function) | ||
| } | ||
|
|
||
| type hcsResult struct { | ||
| Error int32 | ||
| ErrorMessage string | ||
| ErrorEvents []ErrorEvent `json:"ErrorEvents,omitempty"` | ||
| // Omit this as HCS doesn't encode this well. It's more confusing to include. | ||
| // It is however logged in during [processHcsResult] errors. | ||
| //Data []EventData `json:"Data,omitempty"` | ||
| } | ||
|
|
||
| func (ev *ErrorEvent) String() string { | ||
| evs := "[Event Detail: " + ev.Message | ||
| sb := strings.Builder{} | ||
| ev.writeTo(&sb) | ||
| return sb.String() | ||
| } | ||
|
|
||
| func (ev *ErrorEvent) writeTo(b *strings.Builder) { | ||
| // rough wag at needed length | ||
| b.Grow(64 + len(ev.Message) + len(ev.StackTrace) + len(ev.Provider) + len(ev.Source)) | ||
|
|
||
| // [strings.Builder] Write* functions always return nil errors | ||
| _, _ = b.WriteString("[Event Detail: " + ev.Message) | ||
| if ev.StackTrace != "" { | ||
| evs += " Stack Trace: " + ev.StackTrace | ||
| _, _ = b.WriteString(" Stack Trace: " + ev.StackTrace) | ||
| } | ||
| if ev.Provider != "" { | ||
| evs += " Provider: " + ev.Provider | ||
| _, _ = b.WriteString(" Provider: " + ev.Provider) | ||
| } | ||
| if ev.EventID != 0 { | ||
| evs = fmt.Sprintf("%s EventID: %d", evs, ev.EventID) | ||
| fmt.Fprintf(b, " EventID: %d", ev.EventID) | ||
helsaawy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| if ev.Flags != 0 { | ||
| evs = fmt.Sprintf("%s flags: %d", evs, ev.Flags) | ||
| fmt.Fprintf(b, " flags: %d", ev.Flags) | ||
| } | ||
| if ev.Source != "" { | ||
| evs += " Source: " + ev.Source | ||
| _, _ = b.WriteString(" Source: " + ev.Source) | ||
| } | ||
| evs += "]" | ||
| return evs | ||
| _ = b.WriteByte(']') | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we using |
||
| } | ||
|
|
||
| type hcsResult struct { | ||
| Error int32 | ||
| ErrorMessage string | ||
| ErrorEvents []ErrorEvent `json:"ErrorEvents,omitempty"` | ||
| // TODO: AttributionRecords | ||
| } | ||
|
|
||
| func processHcsResult(ctx context.Context, resultJSON string) []ErrorEvent { | ||
| if resultJSON != "" { | ||
| result := &hcsResult{} | ||
| if err := json.Unmarshal([]byte(resultJSON), result); err != nil { | ||
| log.G(ctx).WithError(err).Warning("Could not unmarshal HCS result") | ||
| log.G(ctx).WithFields(logrus.Fields{ | ||
| logfields.JSON: resultJSON, | ||
| logrus.ErrorKey: err, | ||
| }).Warn("Could not unmarshal HCS result") | ||
| return nil | ||
| } | ||
| return result.ErrorEvents | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // resultError allows passing the events along with the error from [notificationWatcher] | ||
| // so they can both be including in the resulting [makeSystemError]/[makeProcessError] call. | ||
| // | ||
| // errorMessage **should** be the same as err.Error(), so it can safely be ignored. | ||
| type resultError struct { | ||
| Err error | ||
| Events []ErrorEvent | ||
| } | ||
|
|
||
| func (e *resultError) Error() string { | ||
| return appendErrorEvents(e.Err.Error(), e.Events) | ||
| } | ||
|
|
||
| func (e *resultError) Is(target error) bool { | ||
| return errors.Is(e.Err, target) | ||
| } | ||
|
|
||
| func (e *resultError) Unwrap() error { | ||
| return e.Err | ||
| } | ||
|
|
||
| type HcsError struct { | ||
| Op string | ||
| Err error | ||
|
|
@@ -150,11 +191,7 @@ type HcsError struct { | |
| var _ net.Error = &HcsError{} | ||
|
|
||
| func (e *HcsError) Error() string { | ||
| s := e.Op + ": " + e.Err.Error() | ||
| for _, ev := range e.Events { | ||
| s += "\n" + ev.String() | ||
| } | ||
| return s | ||
| return appendErrorEvents(e.Op+": "+e.Err.Error(), e.Events) | ||
| } | ||
|
|
||
| func (e *HcsError) Is(target error) bool { | ||
|
|
@@ -194,11 +231,7 @@ type SystemError struct { | |
| var _ net.Error = &SystemError{} | ||
|
|
||
| func (e *SystemError) Error() string { | ||
| s := e.Op + " " + e.ID + ": " + e.Err.Error() | ||
| for _, ev := range e.Events { | ||
| s += "\n" + ev.String() | ||
| } | ||
| return s | ||
| return appendErrorEvents(e.Op+" "+e.ID+": "+e.Err.Error(), e.Events) | ||
| } | ||
|
|
||
| func makeSystemError(system *System, op string, err error, events []ErrorEvent) error { | ||
|
|
@@ -208,6 +241,7 @@ func makeSystemError(system *System, op string, err error, events []ErrorEvent) | |
| return err | ||
| } | ||
|
|
||
| events, err = getEvents(events, err) | ||
| return &SystemError{ | ||
| ID: system.ID(), | ||
| HcsError: HcsError{ | ||
|
|
@@ -228,11 +262,7 @@ type ProcessError struct { | |
| var _ net.Error = &ProcessError{} | ||
|
|
||
| func (e *ProcessError) Error() string { | ||
| s := fmt.Sprintf("%s %s:%d: %s", e.Op, e.SystemID, e.Pid, e.Err.Error()) | ||
| for _, ev := range e.Events { | ||
| s += "\n" + ev.String() | ||
| } | ||
| return s | ||
| return appendErrorEvents(fmt.Sprintf("%s %s:%d: %s", e.Op, e.SystemID, e.Pid, e.Err.Error()), e.Events) | ||
| } | ||
|
|
||
| func makeProcessError(process *Process, op string, err error, events []ErrorEvent) error { | ||
|
|
@@ -241,6 +271,8 @@ func makeProcessError(process *Process, op string, err error, events []ErrorEven | |
| if errors.As(err, &e) { | ||
| return err | ||
| } | ||
|
|
||
| events, err = getEvents(events, err) | ||
| return &ProcessError{ | ||
| Pid: process.Pid(), | ||
| SystemID: process.SystemID(), | ||
|
|
@@ -252,6 +284,32 @@ func makeProcessError(process *Process, op string, err error, events []ErrorEven | |
| } | ||
| } | ||
|
|
||
| // getEvents checks to see if err has events associated with it, and if so, returns those. | ||
| // This is used to flatten an [HceError], [SystemError], or [ProcessError] that wraps a [resultError] | ||
| func getEvents(events []ErrorEvent, err error) ([]ErrorEvent, error) { | ||
| // only return nested events if the original events is empty. | ||
| // don't use [errors.As], since that will unwrap the error chain and drop the parents. | ||
| if resErr, ok := err.(*resultError); err != nil && ok && len(events) == 0 { //nolint:errorlint | ||
| return resErr.Events, resErr.Err | ||
| } | ||
| return events, err | ||
| } | ||
|
|
||
| func appendErrorEvents(s string, events []ErrorEvent) string { | ||
| if len(events) == 0 { | ||
| return s | ||
| } | ||
|
|
||
| sb := &strings.Builder{} | ||
| _, _ = sb.WriteString(s) | ||
| for _, ev := range events { | ||
| // don't join with newlines since those are ... awkward within error strings | ||
| _, _ = sb.WriteString(": ") | ||
| ev.writeTo(sb) | ||
| } | ||
| return sb.String() | ||
| } | ||
|
|
||
| // IsNotExist checks if an error is caused by the Container or Process not existing. | ||
| // Note: Currently, ErrElementNotFound can mean that a Process has either | ||
| // already exited, or does not exist. Both IsAlreadyStopped and IsNotExist | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.