[v2] Add session id to user agent string #9498
Open
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.
This PR generates and appends a session id to the user agent string.
Description
A session id is an identifier that represents an AWS CLI usage session (ie a specific task or logical grouping of tasks). It has the following properties:
Session ids are cached in a SQLite database stored in the
session
table. An entry in the database consists of:key
: Primary key that represents a cache key, which is a hash of the host id + TTY pathsession_id
: The session id associated with a host id + TTY pathtimestamp
: Most recent timestamp of the session id's usageA single host id is generated using uuid4 and persisted in the
host_id
table. The primary key is always hardcoded to0
to ensure there's only ever 1 host id that doesn't change.A cached session id is considered to be expired if it has been at least 30 minutes since the associated timestamp. On every AWS CLI invocation, the cache is cleared by deleting all entries with timestamps < current timestamp - (60*30) seconds. The cleanup job is processed in a daemon thread that terminates as soon as the main CLI process terminates to prevent blocking on cleanup tasks.
When determining the session id, it first checks the cache.
The session id is appended to the user agent string under the
sid
prefix. eg:Caveats
Why SQLite over files?
Caching session data in files can lead to race conditions when multiple processes are writing/reading to/from the same files. We can work around this by either implementing cross-file locks or writing to temporary files and moving them, but both approaches add complexity to implementation.
SQLite offers atomic primitives off the shelf, along with a few useful features:
INSERT OR REPLACEINTO
for simpler writesDELETE .. WHERE
for bulk deletes