Skip to content

Instantly share code, notes, and snippets.

@rossjones
Created March 22, 2023 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rossjones/2a5955d7f29897c5157c59021ee760cb to your computer and use it in GitHub Desktop.
Save rossjones/2a5955d7f29897c5157c59021ee760cb to your computer and use it in GitHub Desktop.
Streaming wasm logs

Currently only docker logs are streamable to the command line, and we wish to support this for WASM jobs as well.

Docker achieves these streaming logs by allowing the execution process to write to a ring buffer which will write to a log-driver based on configuration. By default, this driver is a JSON file and so docker will write log entries in the following format as jsonl:

{
"log": "\u001b[32mThe Project Gutenberg EBook of The Cosmic Computer, by Henry Beam Piper\n",
"stream": "stdout",
"time": "2023-03-17T11:37:08.764363458Z"
}

When reading the logs which are expected to be a totally ordered set, docker will process the options provided, and read entries from the log file. For options such as since and until this is likely rather inefficient as it would require reading the entire line and parsing the JSON, just to determine whether the message matches the query (time, matching stream etc).

We can follow a similar approach, of pipes being used to write the wasm output to a concurrent buffer, from where another process will write it to the log. Although it would be more efficient to use a binary encoding, it might introduce more complexity when other attributes are added to the structure (e.g. execution id or K8s' pod id). For now we will follow docker's approach with jsonlines.

New requests for the logs will consist of converting all of the JSON lines into messages for transmission to the client. For requests to follow this will also be true, but will then want to be notified of all new messages as the mediating process writes them to disk.

                            ┌───────────────┐
                            │               │
                            │ Wasm Instance │
                            │               │
                            └───┬─────────┬─┘
                          Stdout│         │Stderr
                                │         │
                           ┌────▼─────────▼────┐
                           │Multi Reader Logger│
                           └─────────┬─────────┘
┌──────────────────────┐             │            Read     ┌──────────────────┐
│ Non-following reader │     ┌───────▼────────┐◄───────────┤ Following reader │
│                      ├────►│Logfile mediator│            │                  │
└──────────────────────┘Read └──────┬─────────┴───────────►└──────────────────┘
                                    │            New entries
                                    │
                                ┌───▼───┐
                                │Logfile│
                                └───────┘
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment