-
Notifications
You must be signed in to change notification settings - Fork 65
OOM and process telemetry #254
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
859ef3b
add VM-internal failure telemetry (OOM + service crashes)
Sayan- 4ecdc0d
shim: bump eventlistener buffer_size to 100
Sayan- e0d008b
oapi spec
Sayan- a44813d
generated
Sayan- ce8977d
go mod
Sayan- aebf939
vibe code
Sayan- ec172f9
shim conf
Sayan- c7d70f7
vibe code
Sayan- faa2e9c
self review
Sayan- da5fe8d
testing finds
Sayan- b7ff02a
vibe code
Sayan- 3c7d3c1
review and clean up
Sayan- 5cdbd63
fix up
Sayan- 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
17 changes: 17 additions & 0 deletions
17
images/chromium-headful/supervisor/services/supervisord-shim.conf
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 |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| [eventlistener:supervisord-shim] | ||
| command=/usr/local/bin/kernel-images-supervisord-shim | ||
| events=PROCESS_STATE_EXITED,PROCESS_STATE_FATAL | ||
| ; buffer_size defaults to 10 which overflows when several supervised | ||
| ; services flap in quick succession. Bump it so a burst of crashes | ||
| ; doesn't cause supervisord to drop events before the shim drains them. | ||
| buffer_size=100 | ||
| autostart=true | ||
| autorestart=true | ||
| ; Effectively infinite restart attempts. Supervisord does not emit | ||
| ; events about its own eventlisteners, so if the shim ever enters FATAL | ||
| ; state we lose all service_crashed telemetry with no signal. The shim | ||
| ; is tiny and side-effect-free; a transient failure should never reach | ||
| ; the default startretries=3 cap. | ||
| startretries=999999 | ||
| stderr_logfile=/var/log/supervisord/supervisord-shim | ||
| ; stdout is the eventlistener protocol channel; do not redirect. | ||
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
17 changes: 17 additions & 0 deletions
17
images/chromium-headless/image/supervisor/services/supervisord-shim.conf
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 |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| [eventlistener:supervisord-shim] | ||
| command=/usr/local/bin/kernel-images-supervisord-shim | ||
| events=PROCESS_STATE_EXITED,PROCESS_STATE_FATAL | ||
| ; buffer_size defaults to 10 which overflows when several supervised | ||
| ; services flap in quick succession. Bump it so a burst of crashes | ||
| ; doesn't cause supervisord to drop events before the shim drains them. | ||
| buffer_size=100 | ||
| autostart=true | ||
| autorestart=true | ||
| ; Effectively infinite restart attempts. Supervisord does not emit | ||
| ; events about its own eventlisteners, so if the shim ever enters FATAL | ||
| ; state we lose all service_crashed telemetry with no signal. The shim | ||
| ; is tiny and side-effect-free; a transient failure should never reach | ||
| ; the default startretries=3 cap. | ||
| startretries=999999 | ||
| stderr_logfile=/var/log/supervisord/supervisord-shim | ||
| ; stdout is the eventlistener protocol channel; do not redirect. |
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 |
|---|---|---|
| @@ -0,0 +1,256 @@ | ||
| // Command supervisord-shim is a tiny supervisord eventlistener that | ||
| // translates PROCESS_STATE_EXITED (expected=0) and PROCESS_STATE_FATAL | ||
| // events into BrowserServiceCrashedEvent payloads and POSTs them to the | ||
| // local kernel-images-api telemetry endpoint. | ||
| // | ||
| // All schema-mapping and event publishing logic lives here; lib/sysmon | ||
| // does not handle supervisord events. Keeping the shim as the sole owner | ||
| // of the supervisord protocol means lib/sysmon stays single-purpose | ||
| // (kmsg only). | ||
| // | ||
| // Wire protocol per supervisord docs (http://supervisord.org/events.html): | ||
| // | ||
| // stdout: "READY\n" | ||
| // stdin: header line ("ver:3.0 ... eventname:PROCESS_STATE_EXITED len:54\n") | ||
| // stdin: payload of `len` bytes (no trailing newline) | ||
| // stdout: "RESULT 2\nOK" (always; ACK regardless of downstream success) | ||
| // | ||
| // The result frame intentionally has NO trailing newline: supervisord | ||
| // reads exactly the declared number of bytes after the header newline, | ||
| // and a trailing newline would leak into the buffer and corrupt the | ||
| // subsequent READY token, deadlocking the listener after one event. | ||
| // | ||
| // We always ACK with OK so supervisord doesn't quarantine us when the | ||
| // downstream HTTP target is briefly unavailable. The events are | ||
| // best-effort; if the API is down, we drop and log. | ||
| // | ||
| // All logging goes to stderr — stdout is the supervisord protocol channel. | ||
| package main | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "bytes" | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "log" | ||
| "net/http" | ||
| "os" | ||
| "os/signal" | ||
| "strconv" | ||
| "strings" | ||
| "syscall" | ||
| "time" | ||
|
|
||
| oapi "github.com/kernel/kernel-images/server/lib/oapi" | ||
| ) | ||
|
|
||
| const ( | ||
| defaultAPIBaseURL = "http://127.0.0.1:10001" | ||
| httpTimeout = 2 * time.Second | ||
| ) | ||
|
|
||
| func main() { | ||
| log.SetOutput(os.Stderr) | ||
| log.SetFlags(log.LstdFlags | log.Lmicroseconds) | ||
|
|
||
| ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) | ||
| defer stop() | ||
| go func() { | ||
| <-ctx.Done() | ||
| _ = os.Stdin.Close() | ||
| }() | ||
|
|
||
| baseURL := os.Getenv("KERNEL_IMAGES_API_BASE_URL") | ||
| if baseURL == "" { | ||
| baseURL = defaultAPIBaseURL | ||
| } | ||
|
|
||
| client, err := oapi.NewClientWithResponses(baseURL, oapi.WithHTTPClient(&http.Client{Timeout: httpTimeout})) | ||
| if err != nil { | ||
| log.Fatalf("init oapi client: %v", err) | ||
| } | ||
|
|
||
| in := bufio.NewReader(os.Stdin) | ||
| out := bufio.NewWriter(os.Stdout) | ||
|
|
||
| for { | ||
| if _, err := out.WriteString("READY\n"); err != nil { | ||
| log.Fatalf("write READY: %v", err) | ||
| } | ||
| if err := out.Flush(); err != nil { | ||
| log.Fatalf("flush READY: %v", err) | ||
| } | ||
|
|
||
| header, payload, err := readEvent(in) | ||
| if err != nil { | ||
| if err == io.EOF { | ||
| return | ||
| } | ||
| log.Fatalf("read event: %v", err) | ||
| } | ||
|
|
||
| // Try to publish but always ACK supervisord. | ||
| ev, ok := mapEvent(header, payload) | ||
| switch { | ||
| case ok: | ||
| if perr := publish(ctx, client, ev); perr != nil { | ||
| log.Printf("publish telemetry event: %v", perr) | ||
| } | ||
| case isCrashEvent(header["eventname"]): | ||
| // We subscribed to this event type but couldn't map it. | ||
| // Most likely cause: supervisord emitted a from_state we | ||
| // don't have a public phase for. Logging means a future | ||
| // supervisord behavior change shows up in stderr instead | ||
| // of silent telemetry loss. | ||
| log.Printf("skipped crash event: eventname=%q from_state=%q processname=%q expected=%q", | ||
| header["eventname"], payload["from_state"], payload["processname"], payload["expected"]) | ||
| } | ||
|
|
||
| if err := writeResultOK(out); err != nil { | ||
| log.Fatalf("write RESULT: %v", err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // writeResultOK ACKs a single event. See the file header for why the | ||
| // frame body has no trailing newline. | ||
| func writeResultOK(out *bufio.Writer) error { | ||
| if _, err := out.WriteString("RESULT 2\nOK"); err != nil { | ||
| return err | ||
| } | ||
| return out.Flush() | ||
| } | ||
|
|
||
| // readEvent reads one supervisord event: a header line followed by a | ||
| // payload of declared length. | ||
| func readEvent(in *bufio.Reader) (map[string]string, map[string]string, error) { | ||
| headerLine, err := in.ReadString('\n') | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| header := parseFields(strings.TrimRight(headerLine, "\n")) | ||
|
|
||
| lenStr, ok := header["len"] | ||
| if !ok { | ||
| return nil, nil, fmt.Errorf("missing len in header: %q", headerLine) | ||
| } | ||
| n, err := strconv.Atoi(lenStr) | ||
| if err != nil { | ||
| return nil, nil, fmt.Errorf("invalid len %q: %w", lenStr, err) | ||
| } | ||
|
|
||
| buf := make([]byte, n) | ||
| if _, err := io.ReadFull(in, buf); err != nil { | ||
| return nil, nil, fmt.Errorf("read payload: %w", err) | ||
| } | ||
| payload := parseFields(string(buf)) | ||
| return header, payload, nil | ||
| } | ||
|
|
||
| // parseFields parses supervisord's "key:value key:value" tokenization. | ||
| // Values are split on the first colon; supervisord does not escape colons | ||
| // in values, but in practice the values we care about (process names, | ||
| // states, ints) never contain them. | ||
| func parseFields(s string) map[string]string { | ||
| out := make(map[string]string) | ||
| for _, tok := range strings.Fields(s) { | ||
| i := strings.IndexByte(tok, ':') | ||
| if i < 0 { | ||
| continue | ||
| } | ||
| out[tok[:i]] = tok[i+1:] | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| // phaseForExited maps the supervisord state a process exited from to the | ||
| // public lifecycle phase. EXITED in supervisord always originates from | ||
| // RUNNING (post-startsecs); STARTING-during-startsecs-violation routes | ||
| // through BACKOFF→FATAL, not EXITED. We still defend against STARTING | ||
| // here in case a future supervisord version changes the state machine, | ||
| // and we treat anything else as "unknown" so the caller logs and skips | ||
| // rather than inventing a phase. | ||
| func phaseForExited(fromState string) (oapi.BrowserServiceCrashedEventDataPhase, bool) { | ||
| switch fromState { | ||
| case "RUNNING": | ||
| return oapi.BrowserServiceCrashedEventDataPhaseRunning, true | ||
| case "STARTING": | ||
| return oapi.BrowserServiceCrashedEventDataPhaseStartup, true | ||
| default: | ||
| return "", false | ||
| } | ||
| } | ||
|
|
||
| // isCrashEvent reports whether the supervisord eventname is one we | ||
| // subscribed to. Used by the main loop to log when a target event was | ||
| // dropped instead of silently skipping it. | ||
| func isCrashEvent(eventName string) bool { | ||
| return eventName == "PROCESS_STATE_EXITED" || eventName == "PROCESS_STATE_FATAL" | ||
| } | ||
|
|
||
| // mapEvent decides whether to publish and constructs the event payload. | ||
| // Returns ok=false for events we deliberately skip (intentional stops, | ||
| // non-crash event types, or unknown lifecycle transitions). | ||
| func mapEvent(header, payload map[string]string) (oapi.PublishEventRequest, bool) { | ||
| var phase oapi.BrowserServiceCrashedEventDataPhase | ||
| switch header["eventname"] { | ||
| case "PROCESS_STATE_EXITED": | ||
| // expected=0 means the exit was not in `exitcodes` — i.e. a | ||
| // crash. expected=1 means clean shutdown (operator-initiated | ||
| // stop, or a configured exit code). Skip the latter. | ||
| if payload["expected"] != "0" { | ||
| return oapi.PublishEventRequest{}, false | ||
| } | ||
| p, ok := phaseForExited(payload["from_state"]) | ||
| if !ok { | ||
| return oapi.PublishEventRequest{}, false | ||
| } | ||
| phase = p | ||
| case "PROCESS_STATE_FATAL": | ||
| // FATAL is reached exclusively by the BACKOFF→FATAL edge after | ||
| // supervisord exhausts startretries. The from_state is always | ||
| // BACKOFF here, and the semantic is "gave up trying to start". | ||
| phase = oapi.BrowserServiceCrashedEventDataPhaseGaveUp | ||
| default: | ||
| return oapi.PublishEventRequest{}, false | ||
| } | ||
|
|
||
| name := payload["processname"] | ||
| if name == "" { | ||
| return oapi.PublishEventRequest{}, false | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| data := oapi.BrowserServiceCrashedEventData{ | ||
| ServiceName: name, | ||
| Phase: phase, | ||
| } | ||
| if pidStr := payload["pid"]; pidStr != "" { | ||
| if pid, err := strconv.Atoi(pidStr); err == nil { | ||
| data.Pid = &pid | ||
| } | ||
| } | ||
|
|
||
| category := oapi.PublishEventRequestCategory(oapi.TelemetryEventCategorySystem) | ||
| sourceEvent := "service.crashed" | ||
| return oapi.PublishEventRequest{ | ||
| Type: string(oapi.ServiceCrashed), | ||
| Category: &category, | ||
| Source: &oapi.BrowserEventSource{ | ||
| Kind: oapi.LocalProcess, | ||
| Event: &sourceEvent, | ||
| }, | ||
| Data: data, | ||
| }, true | ||
| } | ||
|
|
||
| func publish(ctx context.Context, client *oapi.ClientWithResponses, body oapi.PublishEventRequest) error { | ||
| resp, err := client.PublishTelemetryEventWithResponse(ctx, body) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if resp.StatusCode() >= 300 { | ||
| return fmt.Errorf("status %d: %s", resp.StatusCode(), bytes.TrimSpace(resp.Body)) | ||
| } | ||
| return nil | ||
| } | ||
Oops, something went wrong.
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.