Switch to using ULIDs for context_ids (#70246)

This commit is contained in:
J. Nick Koston 2022-04-18 19:45:41 -10:00 committed by GitHub
parent 1da7927fbb
commit d03760d0bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 2 deletions

View file

@ -0,0 +1,25 @@
"""Helpers to generate ulids."""
from random import getrandbits
import time
# In the future once we require python 3.10 and above, we can
# create a new function that uses base64.b32encodehex to shorten
# these to 26 characters.
def ulid_hex() -> str:
"""Generate a ULID in hex that will work for a UUID.
This ulid should not be used for cryptographically secure
operations.
01AN4Z07BY 79KA1307SR9X4MV3
|----------| |----------------|
Timestamp Randomness
48bits 80bits
This string can be converted with https://github.com/ahawker/ulid
ulid.from_uuid(uuid.UUID(value))
"""
return f"{int(time.time()*1000):012x}{getrandbits(80):020x}"