Skip to content

Commit

Permalink
Make fields optional to match what Relay will forward
Browse files Browse the repository at this point in the history
  • Loading branch information
cmanallen committed Jan 21, 2025
1 parent d098de6 commit da08a51
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 5 deletions.
16 changes: 11 additions & 5 deletions rust_snuba/src/processors/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,14 @@ struct TraceContext {
#[derive(Debug, Default, Deserialize, JsonSchema)]
struct FlagContext {
#[serde(default)]
values: Vec<FlagContextItem>,
values: Option<Vec<Option<FlagContextItem>>>,
}

#[derive(Debug, Default, Deserialize, JsonSchema)]
struct FlagContextItem {
#[serde(default)]
flag: Unicodify,
#[serde(default)]
result: Unicodify,
}

Expand Down Expand Up @@ -586,10 +588,14 @@ impl ErrorRow {
let mut flags_value = Vec::new();

if let Some(ctx) = from_context.flags {
for item in ctx.values {
if let (Some(k), Some(v)) = (item.flag.0, item.result.0) {
flags_key.push(k);
flags_value.push(v);
if let Some(values) = ctx.values {
for value in values {
if let Some(item) = value {
if let (Some(k), Some(v)) = (item.flag.0, item.result.0) {
flags_key.push(k);
flags_value.push(v);
}
}
}
}
};
Expand Down
70 changes: 70 additions & 0 deletions tests/datasets/test_errors_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,3 +886,73 @@ def test_errors_with_flags(self) -> None:
assert self.processor.process_message(payload, meta) == InsertBatch(
[result], ANY
)

def test_errors_with_malformed_flags(self) -> None:
timestamp, recieved = self.__get_timestamps()
message = ErrorEvent(
event_id=str(uuid.UUID("dcb9d002cac548c795d1c9adbfc68040")),
organization_id=1,
project_id=2,
group_id=100,
platform="python",
message="",
trace_id=str(uuid.uuid4()),
trace_sampled=False,
timestamp=timestamp,
received_timestamp=recieved,
release="1.0.0",
dist="dist",
environment="prod",
email="[email protected]",
ip_address="127.0.0.1",
user_id="myself",
username="me",
geo={
"country_code": "XY",
"region": "fake_region",
"city": "fake_city",
"subdivision": "fake_subdivision",
},
replay_id=None,
threads=None,
errors=[{"type": "one"}, {"type": "two"}, {"type": "three"}],
flags=[],
)
payload = message.serialize()
meta = KafkaMessageMetadata(offset=2, partition=2, timestamp=timestamp)

# Assert malformed context type is ignored.
payload[2]["data"]["contexts"]["flags"] = {"key": "value"}
result = self.processor.process_message(payload, meta)
assert result.rows[0]["flags.key"] == []
assert result.rows[0]["flags.value"] == []

# Assert malformed values type is ignored.
payload[2]["data"]["contexts"]["flags"] = {"values": None}
result = self.processor.process_message(payload, meta)
assert result.rows[0]["flags.key"] == []
assert result.rows[0]["flags.value"] == []

# Assert malformed item type is ignored.
payload[2]["data"]["contexts"]["flags"] = {"values": [None]}
result = self.processor.process_message(payload, meta)
assert result.rows[0]["flags.key"] == []
assert result.rows[0]["flags.value"] == []

# Assert incorrect item contents is ignored.
payload[2]["data"]["contexts"]["flags"] = {"values": [{"key": "value"}]}
result = self.processor.process_message(payload, meta)
assert result.rows[0]["flags.key"] == []
assert result.rows[0]["flags.value"] == []

# Assert missing "result" key means the whole item is ignored.
payload[2]["data"]["contexts"]["flags"] = {"values": [{"flag": "value"}]}
result = self.processor.process_message(payload, meta)
assert result.rows[0]["flags.key"] == []
assert result.rows[0]["flags.value"] == []

# Assert missing "flag" key means the whole item is ignored.
payload[2]["data"]["contexts"]["flags"] = {"values": [{"result": "value"}]}
result = self.processor.process_message(payload, meta)
assert result.rows[0]["flags.key"] == []
assert result.rows[0]["flags.value"] == []

0 comments on commit da08a51

Please sign in to comment.