Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 150 additions & 5 deletions lib/saluki-components/src/common/otlp/traces/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1677,6 +1677,13 @@ fn map_attribute_generic(
}

let Some(value) = attribute.value.as_ref().and_then(|wrapper| wrapper.value.as_ref()) else {
conditionally_map_otlp_attribute(
attribute.key.as_str(),
string_attribute_value(None, interner),
attrs,
interner,
string_builder,
);
return;
};

Expand All @@ -1699,10 +1706,10 @@ fn map_attribute_generic(
string_builder,
);
}
OtlpValue::BytesValue(bytes) => {
OtlpValue::BytesValue(_) | OtlpValue::ArrayValue(_) | OtlpValue::KvlistValue(_) => {
Comment thread
lucastemb marked this conversation as resolved.
Resolved
conditionally_map_otlp_attribute(
attribute.key.as_str(),
AttributeValue::Bytes(bytes.clone()),
string_attribute_value(Some(value), interner),
attrs,
interner,
string_builder,
Expand All @@ -1726,12 +1733,16 @@ fn map_attribute_generic(
string_builder,
);
}
_ => {
// Skip complex values for now.
}
}
}

/// Renders an OTLP value that has no numeric or boolean form in a span as a string attribute.
///
/// Bytes become base64 and an absent value becomes the empty string. Arrays and key-value lists become JSON.
fn string_attribute_value(value: Option<&OtlpValue>, interner: &GenericMapInterner) -> AttributeValue {
AttributeValue::String(MetaString::from_interner(&otlp_value_as_string(value), interner))
}

fn instrumentation_scope_attributes_to_attrs(
scope: &OtlpInstrumentationScope, attrs: &mut FastHashMap<MetaString, AttributeValue>,
interner: &GenericMapInterner,
Expand Down Expand Up @@ -2594,6 +2605,140 @@ mod tests {
);
}

/// Maps one span attribute with `map_attribute_generic` and returns the value stored under `conv.value`.
fn mapped_span_attribute(value: Option<OtlpValue>) -> Option<AttributeValue> {
let mut attrs: FastHashMap<MetaString, AttributeValue> = FastHashMap::default();
let interner = test_interner();
let mut string_builder = StringBuilder::new().with_interner(interner.clone());
let attribute = KeyValue {
key: "conv.value".to_string(),
value: Some(AnyValue { value }),
};
map_attribute_generic(&attribute, &mut attrs, &interner, &mut string_builder);
attrs.get("conv.value").cloned()
}

#[test]
fn a_bytes_span_attribute_is_a_base64_string_tag() {
assert_eq!(
mapped_span_attribute(Some(OtlpValue::BytesValue(vec![0, 1, 2, 255]))),
Some(AttributeValue::String(MetaString::from("AAEC/w==")))
);
assert_eq!(
mapped_span_attribute(Some(OtlpValue::BytesValue(Vec::new()))),
Some(AttributeValue::String(MetaString::from("")))
);
}

#[test]
fn an_array_span_attribute_is_a_json_string_tag() {
let array = |values: Vec<Option<OtlpValue>>| {
OtlpValue::ArrayValue(ArrayValue {
values: values.into_iter().map(|value| AnyValue { value }).collect(),
})
};
let json = |text: &str| Some(AttributeValue::String(MetaString::from(text)));

let strings = array(vec![
Some(OtlpValue::StringValue("a".to_string())),
Some(OtlpValue::StringValue("b".to_string())),
]);
assert_eq!(mapped_span_attribute(Some(strings)), json(r#"["a","b"]"#));

let mixed = array(vec![
Some(OtlpValue::IntValue(1)),
Some(OtlpValue::StringValue("x".to_string())),
Some(OtlpValue::BoolValue(true)),
]);
assert_eq!(mapped_span_attribute(Some(mixed)), json(r#"[1,"x",true]"#));

assert_eq!(mapped_span_attribute(Some(array(Vec::new()))), json("[]"));
}

#[test]
fn a_key_value_list_span_attribute_is_a_json_string_tag() {
let entry = |key: &str, value: OtlpValue| KeyValue {
key: key.to_string(),
value: Some(AnyValue { value: Some(value) }),
};
let list = |values: Vec<KeyValue>| OtlpValue::KvlistValue(KeyValueList { values });
let json = |text: &str| Some(AttributeValue::String(MetaString::from(text)));

let nested = list(vec![
entry("a", OtlpValue::IntValue(1)),
entry("b", list(vec![entry("c", OtlpValue::StringValue("d".to_string()))])),
]);
assert_eq!(mapped_span_attribute(Some(nested)), json(r#"{"a":1,"b":{"c":"d"}}"#));

assert_eq!(mapped_span_attribute(Some(list(Vec::new()))), json("{}"));
}

#[test]
fn a_span_attribute_with_no_value_is_an_empty_string_tag() {
let empty = Some(AttributeValue::String(MetaString::from("")));
assert_eq!(mapped_span_attribute(None), empty);

let mut attrs: FastHashMap<MetaString, AttributeValue> = FastHashMap::default();
let interner = test_interner();
let mut string_builder = StringBuilder::new().with_interner(interner.clone());
let no_wrapper = KeyValue {
key: "conv.value".to_string(),
value: None,
};
map_attribute_generic(&no_wrapper, &mut attrs, &interner, &mut string_builder);
assert_eq!(attrs.get("conv.value").cloned(), empty);

let no_key = KeyValue {
key: String::new(),
value: None,
};
map_attribute_generic(&no_key, &mut attrs, &interner, &mut string_builder);
assert_eq!(attrs.len(), 1);
}

#[test]
fn both_receive_paths_tag_the_attributes_that_are_not_scalars_alike() {
let attribute = |key: &str, value: Option<OtlpValue>| KeyValue {
key: key.to_string(),
value: Some(AnyValue { value }),
};
let span = OtlpSpan {
name: "typed".to_string(),
attributes: vec![
attribute("conv.bytes", Some(OtlpValue::BytesValue(vec![0, 1, 2, 255]))),
attribute(
"conv.strings",
Some(OtlpValue::ArrayValue(ArrayValue {
values: vec![AnyValue {
value: Some(OtlpValue::StringValue("a".to_string())),
}],
})),
),
attribute(
"conv.map",
Some(OtlpValue::KvlistValue(KeyValueList {
values: vec![attribute("a", Some(OtlpValue::IntValue(1)))],
})),
),
attribute("conv.no_value", None),
],
..Default::default()
};

let resource = Resource::default();
let v1 = convert_v1(&span, &resource, None, &conversion());
let v2 = convert_v2(&span, &resource, None, &conversion());
for (key, expected) in [
("conv.bytes", "AAEC/w=="),
("conv.strings", r#"["a"]"#),
("conv.map", r#"{"a":1}"#),
("conv.no_value", ""),
] {
assert_eq!(meta_str(&v1, key), Some(expected), "V1 {key}");
assert_eq!(meta_str(&v2, key), Some(expected), "V2 {key}");
}
}

/// TestGetOTelVersion - Tests GetOTelVersion function
#[test]
fn test_get_otel_version() {
Expand Down
31 changes: 0 additions & 31 deletions test/correctness/cases/otlp-traces-conversion/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -145,37 +145,6 @@ procedure:
# The baseline is the Agent, and the comparison is ADP. One entry for each kind of difference of a span, so
# that each side's part of a difference goes stale on its own.
known_differences:
# Attributes that are not scalars.
- gap: gap/otlp-typed-attributes
issue: 3443
paths: [span.meta.conv.bytes]
kinds: [missing]
when: {service: [probe-conv], resource: [otlpprobe.conv.typed.bytes]}
reason: The Agent keeps a bytes attribute as a base64 string in `meta`. ADP does not.
- gap: gap/otlp-typed-attributes
issue: 3443
paths: [span.meta_struct.conv.bytes]
kinds: [extra]
when: {service: [probe-conv], resource: [otlpprobe.conv.typed.bytes]}
reason: ADP keeps a bytes attribute as raw bytes in `meta_struct`. The Agent does not.
- gap: gap/otlp-typed-attributes
issue: 3443
paths: [span.meta.conv.strings, span.meta.conv.mixed, span.meta.conv.empty_array]
kinds: [missing]
when: {service: [probe-conv], resource: [otlpprobe.conv.typed.arrays]}
reason: The Agent keeps an array attribute as a JSON string in `meta`. ADP drops it.
- gap: gap/otlp-typed-attributes
issue: 3443
paths: [span.meta.conv.map]
kinds: [missing]
when: {service: [probe-conv], resource: [otlpprobe.conv.typed.kvlist]}
reason: The Agent keeps a key-value list attribute as a JSON string in `meta`. ADP drops it.
- gap: gap/otlp-typed-attributes
issue: 3443
paths: [span.meta.conv.no_value]
kinds: [missing]
when: {service: [probe-conv], resource: [otlpprobe.conv.typed.empty]}
reason: The Agent keeps an attribute with no value as an empty string in `meta`. ADP drops it.
# HTTP status codes and errors.
- gap: gap/otlp-http-status-code-string
issue: 3433
Expand Down
Loading