Skip to content

Commit

Permalink
resources are only dd.internal.resource tags (#829)
Browse files Browse the repository at this point in the history
* resources are only dd.internal.resource tags

* improve test
  • Loading branch information
alexgallotta authored Jan 27, 2025
1 parent 84a92b0 commit 02eb386
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
7 changes: 4 additions & 3 deletions dogstatsd/src/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ pub mod tests {

const PRECISION: f64 = 0.000_000_01;

const SINGLE_METRIC_SIZE: usize = 216; // taken from the test, size of a serialized metric with one tag and 1 digit counter value
const SINGLE_METRIC_SIZE: usize = 193; // taken from the test, size of a serialized metric with one tag and 1 digit counter value
const SINGLE_DISTRIBUTION_SIZE: u64 = 140;
const DEFAULT_TAGS: &str =
"dd_extension_version:63-next,architecture:x86_64,_dd.compute_stats:1";
Expand Down Expand Up @@ -450,7 +450,7 @@ pub mod tests {
fn to_series() {
let mut aggregator = Aggregator::new(EMPTY_TAGS, 2).unwrap();

let metric1 = parse("test:1|c|#k:v").expect("metric parse failed");
let metric1 = parse("test:1|c|#k1:v1,k2:v2").expect("metric parse failed");
let metric2 = parse("foo:1|c|#k:v").expect("metric parse failed");
let metric3 = parse("bar:1|c|#k:v").expect("metric parse failed");

Expand All @@ -459,6 +459,7 @@ pub mod tests {

assert_eq!(aggregator.map.len(), 2);
assert_eq!(aggregator.to_series().len(), 2);
// to_series should not mutate the state
assert_eq!(aggregator.map.len(), 2);
assert_eq!(aggregator.to_series().len(), 2);
assert_eq!(aggregator.map.len(), 2);
Expand Down Expand Up @@ -663,7 +664,7 @@ pub mod tests {
fn consume_metrics_batch_bytes() {
let expected_metrics_per_batch = 2;
let total_number_of_metrics = 5;
let two_metrics_size = 420;
let two_metrics_size = 374;
let max_bytes = SINGLE_METRIC_SIZE * expected_metrics_per_batch + 13;
let mut aggregator = Aggregator {
tags: to_sorted_tags(),
Expand Down
4 changes: 2 additions & 2 deletions dogstatsd/src/datadog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,10 @@ pub(crate) struct Point {
/// A named resource
pub(crate) struct Resource {
/// The name of this resource
pub(crate) name: &'static str,
pub(crate) name: String,
#[serde(rename = "type")]
/// The kind of this resource
pub(crate) kind: &'static str,
pub(crate) kind: String,
}

#[derive(Debug, Clone, Copy)]
Expand Down
29 changes: 23 additions & 6 deletions dogstatsd/src/metric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,18 @@ impl SortedTags {

pub(crate) fn to_resources(&self) -> Vec<datadog::Resource> {
let mut resources = Vec::with_capacity(constants::MAX_TAGS);
for (kind, name) in &self.values {
let resource = datadog::Resource {
name: name.as_str(), // val
kind: kind.as_str(), // key
};
resources.push(resource);
for (key, val) in &self.values {
if key == "dd.internal.resource" {
//anything coming in via dd.internal.resource:<value> has to be a key/value pair
// (e.g., dd.internal.resource:key:value)
if let Some(valid_name_kind) = val.split_once(':') {
let resource = datadog::Resource {
name: valid_name_kind.0.to_string(),
kind: valid_name_kind.1.to_string(),
};
resources.push(resource);
}
}
}
resources
}
Expand Down Expand Up @@ -506,4 +512,15 @@ mod tests {
panic!("Expected UnsupportedType error");
}
}

#[test]
fn sorting_tags() {
let mut tags = SortedTags::parse("z:z0,b:b2,c:c3").unwrap();
tags.extend(&SortedTags::parse("z1:z11,d:d4,e:e5,f:f6").unwrap());
tags.extend(&SortedTags::parse("a:a1").unwrap());
assert_eq!(tags.values.len(), 8);
let first_element = tags.values.first().unwrap();
assert_eq!(first_element.0, Ustr::from("a"));
assert_eq!(first_element.1, Ustr::from("a1"));
}
}

0 comments on commit 02eb386

Please sign in to comment.