Skip to content

Commit

Permalink
chore: update the tracing events for matching payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed May 27, 2022
1 parent 8dc4c17 commit ad9c37b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
18 changes: 13 additions & 5 deletions src/matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ pub(crate) fn compare(
}

/// Compare the fields of the expected and actual messages
#[tracing::instrument(ret,
skip_all,
fields(%path)
)]
fn compare_message(
path: DocPath,
expected_message_fields: &[ProtobufField],
Expand All @@ -131,8 +135,6 @@ fn compare_message(
message_descriptor: &DescriptorProto,
descriptors: &FileDescriptorSet,
) -> anyhow::Result<BodyMatchResult> {
trace!(">> compare_message({}, {:?}, {:?})", path, expected_message_fields, actual_message_fields);

let mut results = hashmap!{};

let fields = message_descriptor.field.iter()
Expand All @@ -154,21 +156,24 @@ fn compare_message(
field_no.to_string()
});
let field_path = path.join(&field_name);
trace!("Comparing message field {}:{}", field_name, field_no);
trace!(%field_name, field_no, "Comparing message field {:?} => {:?}", expected, actual);

if is_map_field(message_descriptor, field_descriptor) {
trace!(%field_name, field_no, "field is a map field");
let map_comparison = compare_map_field(&field_path, field_descriptor, expected, actual, matching_context, descriptors);
if !map_comparison.is_empty() {
results.insert(field_path.to_string(), map_comparison);
}
} else if is_repeated_field(field_descriptor) {
trace!(%field_name, field_no, "field is a repeated field");
let e = expected.iter().map(|f| (*f).clone()).collect_vec();
let a = actual.iter().map(|f| (*f).clone()).collect_vec();
let repeated_comparison = compare_repeated_field(&field_path, field_descriptor, &e, &a, matching_context, descriptors);
if !repeated_comparison.is_empty() {
results.insert(field_path.to_string(), repeated_comparison);
}
} else if !expected.is_empty() && actual.is_empty() {
trace!(field_name = field_name.as_str(), field_no, "actual field list is empty");
results.insert(field_path.to_string(), vec![
BodyMismatch {
path: field_path.to_string(),
Expand All @@ -183,6 +188,7 @@ fn compare_message(
results.insert(field_path.to_string(), comparison);
}
} else if !actual.is_empty() && matching_context.config() == DiffConfig::NoUnexpectedKeys {
trace!(field_name = field_name.as_str(), field_no, "actual field list is not empty");
results.insert(field_path.to_string(), vec![
BodyMismatch {
path: field_path.to_string(),
Expand All @@ -202,6 +208,10 @@ fn compare_message(
}

/// Compare a simple field (non-map and non-repeated)
#[tracing::instrument(ret,
skip_all,
fields(%path, %field, %actual)
)]
fn compare_field(
path: &DocPath,
field: &ProtobufField,
Expand All @@ -210,8 +220,6 @@ fn compare_field(
matching_context: &dyn MatchingContext,
descriptors: &FileDescriptorSet
) -> Vec<Mismatch> {
trace!("compare_field({}, {:?}, {:?}, {:?})", path, field, descriptor, actual);

match (&field.data, &actual.data) {
(ProtobufFieldData::String(s1), ProtobufFieldData::String(s2)) => {
trace!("Comparing string values");
Expand Down
4 changes: 3 additions & 1 deletion src/message_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ impl Display for ProtobufFieldData {
}

/// Decodes the Protobuf message using the descriptors
#[tracing::instrument(ret, skip_all)]
pub fn decode_message<B>(
buffer: &mut B,
descriptor: &DescriptorProto,
Expand All @@ -142,7 +143,7 @@ pub fn decode_message<B>(

while buffer.has_remaining() {
let (field_num, wire_type) = decode_key(buffer)?;
trace!("field_num={}, wire_type={:?}, bytes remaining = {}", field_num, wire_type, buffer.remaining());
trace!(field_num, ?wire_type, "read field header, bytes remaining = {}", buffer.remaining());

match find_field_descriptor(field_num as i32, descriptor) {
Ok(field_descriptor) => {
Expand Down Expand Up @@ -231,6 +232,7 @@ pub fn decode_message<B>(
_ => return Err(anyhow!("Messages with {:?} wire type fields are not supported", wire_type))
};

trace!(field_num, ?wire_type, ?data, "read field, bytes remaining = {}", buffer.remaining());
fields.push(ProtobufField {
field_num,
wire_type,
Expand Down

0 comments on commit ad9c37b

Please sign in to comment.