send_json/stress_test utils: Prettier tag-sending, pt.2

This commit is contained in:
Klaas van Schelven
2025-11-15 15:44:19 +01:00
parent 8da9ec593e
commit 60bbf8c606
4 changed files with 47 additions and 23 deletions

View File

@@ -15,7 +15,7 @@ from compat.dsn import get_store_url, get_envelope_url, get_header_value
from bugsink.streams import compress_with_zlib, WBITS_PARAM_FOR_GZIP, WBITS_PARAM_FOR_DEFLATE
from bugsink.utils import nc_rnd
from ..utils import random_for_RANDOM
from ..utils import handle_upload_tags
class Command(BaseCommand):
@@ -121,15 +121,13 @@ class Command(BaseCommand):
data["contexts"]["trace"]["trace_id"] = nc_rnd.getrandbits(128).to_bytes(16, byteorder='big').hex()
if options["tag"]:
if "tags" not in data:
data["tags"] = {}
tags = {}
for tag in options["tag"]:
tag = tag[0] # it's a list of lists... how to prevent this is not immediately clear
# it's a list of lists... how to prevent this is not immediately clear
tag = tag[0]
k, v = tag.split(":", 1)
v = random_for_RANDOM(k, v)
data["tags"][k] = v
tags[k] = v
handle_upload_tags(data, tags)
if options["valid_only"] and not self.is_valid(data, identifier):
return False

View File

@@ -15,7 +15,7 @@ from bugsink.streams import compress_with_zlib, WBITS_PARAM_FOR_GZIP, WBITS_PARA
from bugsink.utils import nc_rnd
from issues.utils import get_values
from ..utils import random_postfix
from ..utils import handle_upload_tags, random_postfix
class Command(BaseCommand):
@@ -99,17 +99,13 @@ class Command(BaseCommand):
data["contexts"]["trace"]["trace_id"] = nc_rnd.getrandbits(128).to_bytes(16, byteorder='big').hex()
if options["tag"]:
if "tags" not in data:
data["tags"] = {}
tags = {}
for tag in options["tag"]:
tag = tag[0] # it's a list of lists... how to prevent this is not immediately clear
# it's a list of lists... how to prevent this is not immediately clear
tag = tag[0]
k, v = tag.split(":", 1)
if v == "RANDOM":
v = "value-" + random_postfix()
data["tags"][k] = v
tags[k] = v
handle_upload_tags(data, tags)
if options["random_type"]:
values = get_values(data["exception"])

View File

@@ -1,11 +1,12 @@
from bugsink.utils import nc_rnd
from tags.utils import EVENT_DATA_CONVERSION_TABLE, CONTEXT_CONVERSION_TABLE
PRETTY_RANDOM_TAGS = {
"os": ["Windows", "Linux", "MacOS", "Android", "iOS"],
"os.name": ["Windows", "Linux", "MacOS", "Android", "iOS"],
"os.version": ["10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"],
"cpu": ["x86", "x64", "ARM", "ARM64", "MIPS"],
"browser": ["Chrome", "Firefox", "Safari", "Edge", "Opera"],
"browser.name": ["Chrome", "Firefox", "Safari", "Edge", "Opera"],
"device": ["Desktop", "Laptop", "Tablet", "Smartphone"],
"environment": ["production", "staging", "development", "testing"],
"release": ["1.0.0", "1.1.0", "1.2.0", "2.0.0", "2.1.0"],
@@ -26,6 +27,33 @@ def random_postfix():
return "".join([chr(ord("A") + int(c)) for c in str(unevenly_distributed_number)])
def set_path(data, path, v):
d = data
for part in path[:-1]:
if part not in d:
d[part] = {}
d = d[part]
d[path[-1]] = v
def handle_upload_tags(event_data, tags):
if "tags" not in event_data:
event_data["tags"] = {}
for k, v in tags.items():
v = random_for_RANDOM(k, v)
# push `tag` values into their proper location in event_data, if applicable
if k in EVENT_DATA_CONVERSION_TABLE or k in CONTEXT_CONVERSION_TABLE:
if k in EVENT_DATA_CONVERSION_TABLE:
lookup_path = EVENT_DATA_CONVERSION_TABLE[k]
else:
lookup_path = ("contexts",) + CONTEXT_CONVERSION_TABLE[k]
set_path(event_data, lookup_path, v)
else:
event_data["tags"][k] = v
def random_for_RANDOM(k, v):
if v != "RANDOM":
return v

View File

@@ -94,7 +94,9 @@ def deduce_tags(event_data):
value = get_path(event_data, *lookup_path)
# NOTE: we don't have some kind of "defaulting" mechanism here; if the value is None / non-existent, we simply
# don't add the tag.
# don't add the tag. NOTE also that this code implies that event_data values take precendence over explicitly
# provided tags; this is OK b/c explicitly provided tags are supposed to be for "custom"/"extra" tags; the SDK
# is supposed to just pick the right (non-tag) location for standard tags.
if value not in [None, ""]:
tags[tag_key] = _convert_non_strings(value)
@@ -151,10 +153,10 @@ def is_mostly_unique(key):
if key.startswith("trace"):
return True
if key in ["browser.version", "browser"]:
if key in ["browser.version", "browser"]: # "browser" includes version info, hence "mostly unique"
return True
if key in ["os.version", "os"]:
if key in ["os.version", "os"]: # "os" includes version info, hence "mostly unique"
return True
return False