Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
khanayan123 committed Jan 9, 2025
1 parent 59c1a37 commit 3429244
Showing 1 changed file with 27 additions and 48 deletions.
75 changes: 27 additions & 48 deletions tests/test_config_consistency.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,23 +437,11 @@ def test_log_injection_enabled(self):
assert self.r.status_code == 200
pattern = r'"dd":\{[^}]*\}'
stdout.assert_presence(pattern)
for data in stdout.get_data():
json_string = json.dumps(data)
parsed_data = json.loads(json_string)
message = {}
try:
message = json.loads(parsed_data.get("message"))
except json.JSONDecodeError:
continue
if (
message.get("dd")
and message.get(log_injection_fields[context.library.library]["message"]) == self.message
):
dd = message.get("dd")
required_fields = ["trace_id", "span_id", "service", "version", "env"]
for field in required_fields:
assert field in dd, f"Missing field: {field}"
return
dd = parse_log_injection_message(self.message)
required_fields = ["trace_id", "span_id", "service", "version", "env"]
for field in required_fields:
assert field in dd, f"Missing field: {field}"
return


@rfc("https://docs.google.com/document/d/1kI-gTAKghfcwI7YzKhqRv2ExUstcHqADIWA4-TZ387o/edit#heading=h.8v16cioi7qxp")
Expand Down Expand Up @@ -484,22 +472,9 @@ def test_log_injection_128bit_traceid_default(self):
assert self.r.status_code == 200
pattern = r'"dd":\{[^}]*\}'
stdout.assert_presence(pattern)
for data in stdout.get_data():
json_string = json.dumps(data)
parsed_data = json.loads(json_string)
message = {}
try:
message = json.loads(parsed_data.get("message"))
except json.JSONDecodeError:
continue
if (
message.get("dd")
and message.get(log_injection_fields[context.library.library]["message"]) == self.message
):
dd = message.get("dd")
trace_id = dd.get("trace_id")
assert re.match(r"^[0-9a-f]{32}$", trace_id), f"Invalid 128-bit trace_id: {trace_id}"

dd = parse_log_injection_message(self.message)
trace_id = dd.get("trace_id")
assert re.match(r"^[0-9a-f]{32}$", trace_id), f"Invalid 128-bit trace_id: {trace_id}"

@rfc("https://docs.google.com/document/d/1kI-gTAKghfcwI7YzKhqRv2ExUstcHqADIWA4-TZ387o/edit#heading=h.8v16cioi7qxp")
@scenarios.tracing_config_nondefault_3
Expand All @@ -513,21 +488,9 @@ def test_log_injection_128bit_traceid_disabled(self):
assert self.r.status_code == 200
pattern = r'"dd":\{[^}]*\}'
stdout.assert_presence(pattern)
for data in stdout.get_data():
json_string = json.dumps(data)
parsed_data = json.loads(json_string)
message = {}
try:
message = json.loads(parsed_data.get("message"))
except json.JSONDecodeError:
continue
if (
message.get("dd")
and message.get(log_injection_fields[context.library.library]["message"]) == self.message
):
dd = message.get("dd")
trace_id = dd.get("trace_id")
assert re.match(r"\d+", trace_id), f"Invalid 64-bit trace_id: {trace_id}"
dd = parse_log_injection_message(self.message)
trace_id = dd.get("trace_id")
assert re.match(r"\d+", trace_id), f"Invalid 64-bit trace_id: {trace_id}"


@rfc("https://docs.google.com/document/d/1kI-gTAKghfcwI7YzKhqRv2ExUstcHqADIWA4-TZ387o/edit#heading=h.8v16cioi7qxp")
Expand All @@ -554,3 +517,19 @@ class Test_Config_RuntimeMetrics_Default:
def test_config_runtimemetrics_default(self):
data = list(interfaces.library.get_data("/dogstatsd/v2/proxy"))
assert len(data) == 0

def parse_log_injection_message(log_message):
for data in stdout.get_data():
json_string = json.dumps(data)
parsed_data = json.loads(json_string)
message = {}
try:
message = json.loads(parsed_data.get("message"))
except json.JSONDecodeError:
continue
if (
message.get("dd")
and message.get(log_injection_fields[context.library.library]["message"]) == log_message
):
dd = message.get("dd")
return dd

0 comments on commit 3429244

Please sign in to comment.