Skip to content
Open
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
92 changes: 54 additions & 38 deletions lib/saluki-components/src/sources/dogstatsd/tags.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
use saluki_core::{
constants::datadog::*,
data_model::{
origin::OriginTagCardinality,
tags::{BorrowedTag, RawTags},
},
data_model::{origin::OriginTagCardinality, tags::RawTags},
};
use smallvec::SmallVec;

/// Regular (non-well-known) tags from a DogStatsD payload.
///
/// As an optimization, up to 32 tags are stored inline without allocating. This isn't a limit on the number of tags:
/// any beyond that are stored on the heap.
pub type RegularTags<'a> = SmallVec<[&'a str; 32]>;

/// Returns `true` if the given tag is a well-known tag.
fn is_well_known_tag(tag: &str) -> bool {
tag.starts_with(HOST_TAG_KEY_SUFFIXED)
|| tag.starts_with(ENTITY_ID_TAG_KEY_SUFFIXED)
|| tag.starts_with(JMX_CHECK_NAME_TAG_KEY_SUFFIXED)
|| tag.starts_with(CARDINALITY_TAG_KEY_SUFFIXED)
}
/// Keeps up to 64 tag references inline without allocating. The inline capacity is not a limit on tag count.
/// Larger lists move to heap storage.
pub type RegularTags<'a> = SmallVec<[&'a str; 64]>;

/// Well-known tags shared across DogStatsD payloads.
///
/// Well-known tags are tags which are generally set by the DogStatsD client to convey structured information
/// about the source of the metric, event, or service check.
/// The DogStatsD client uses well-known keys to carry a hostname, entity ID, JMX check name, or cardinality hint.
/// These values describe the payload or its origin instead of appearing as regular tags.
#[derive(Default)]
pub struct WellKnownTags<'a> {
pub hostname: Option<&'a str>,
Expand All @@ -36,38 +25,45 @@ pub struct WellKnownTags<'a> {
impl<'a> WellKnownTags<'a> {
/// Splits the raw tags of a DogStatsD payload into well-known tags and regular tags.
///
/// Any tag with a well-known tag key is excluded from the regular tags, even if its value is ignored. All fields
/// of the well-known tags default to `None`.
/// A tag with a well-known key stays out of the regular list even if the parser ignores its value.
/// Each well-known field starts as `None`.
///
/// The raw tags are walked exactly once, so callers can iterate over the regular tags as many times as needed
/// (such as for both hashing and building a context) without re-parsing the raw tags.
/// The split scans the raw tags once. The caller can reuse the regular tags for hashing and context creation
/// without parsing the payload again.
pub fn split_raw_tags(tags: &'a RawTags<'_>) -> (Self, RegularTags<'a>) {
let mut well_known_tags = Self::default();
let mut regular_tags = RegularTags::new();

for tag in tags.iter() {
if !is_well_known_tag(tag) {
regular_tags.push(tag);
continue;
}

match BorrowedTag::from(tag).name_and_value() {
(HOST_TAG_KEY, Some(hostname)) => {
well_known_tags.hostname = Some(hostname);
}
(ENTITY_ID_TAG_KEY, Some(entity_id)) if entity_id != ENTITY_ID_IGNORE_VALUE => {
well_known_tags.pod_uid = Some(entity_id);
}
(JMX_CHECK_NAME_TAG_KEY, Some(name)) => {
well_known_tags.jmx_check_name = Some(name);
// The first byte selects which well-known prefixes can match. A full prefix match includes the colon.
match tag.as_bytes().first() {
Some(b'h') => {
if let Some(hostname) = tag.strip_prefix(HOST_TAG_KEY_SUFFIXED) {
well_known_tags.hostname = Some(hostname);
continue;
}
}
(CARDINALITY_TAG_KEY, Some(value)) => {
if let Ok(card) = OriginTagCardinality::try_from(value) {
well_known_tags.cardinality = Some(card);
Some(b'd') => {
if let Some(entity_id) = tag.strip_prefix(ENTITY_ID_TAG_KEY_SUFFIXED) {
if entity_id != ENTITY_ID_IGNORE_VALUE {
well_known_tags.pod_uid = Some(entity_id);
}
continue;
}
if let Some(name) = tag.strip_prefix(JMX_CHECK_NAME_TAG_KEY_SUFFIXED) {
well_known_tags.jmx_check_name = Some(name);
continue;
}
if let Some(value) = tag.strip_prefix(CARDINALITY_TAG_KEY_SUFFIXED) {
if let Ok(card) = OriginTagCardinality::try_from(value) {
well_known_tags.cardinality = Some(card);
}
continue;
}
}
_ => {}
}
regular_tags.push(tag);
}

(well_known_tags, regular_tags)
Expand Down Expand Up @@ -145,4 +141,24 @@ mod tests {
assert_eq!(wkt.cardinality, None);
assert_eq!(wkt.jmx_check_name, Some("check"));
}

#[test]
fn well_known_tag_values_keep_suffixes_and_ignore_invalid_overrides() {
let tags = format!(
"host:old,host:new:port,dd.internal.entity_id:pod,dd.internal.entity_id:{ENTITY_ID_IGNORE_VALUE},\
dd.internal.card:low,dd.internal.card:bad,dd.internal.jmx_check_name:,dd.internal.jmx_check_name:x:y,\
,dd.internal.card.extra:keep,jmx_check_name:keep"
);
let tags = RawTags::new(&tags, usize::MAX, usize::MAX);
let (wkt, regular_tags) = WellKnownTags::split_raw_tags(&tags);

assert_eq!(wkt.hostname, Some("new:port"));
assert_eq!(wkt.pod_uid, Some("pod"));
assert_eq!(wkt.cardinality, Some(OriginTagCardinality::Low));
assert_eq!(wkt.jmx_check_name, Some("x:y"));
assert_eq!(
regular_tags.as_slice(),
["", "dd.internal.card.extra:keep", "jmx_check_name:keep"]
);
}
}
Loading