Skip to content

Commit

Permalink
Properly fix types for dogstatsd flusher
Browse files Browse the repository at this point in the history
Signed-off-by: Bob Weinand <bob.weinand@datadoghq.com>
  • Loading branch information
bwoebi committed Sep 17, 2024
1 parent 8fecb9a commit ae5c14e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions data-pipeline/src/trace_exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl TraceExporter {
Err(err) => {
self.emit_metric(HealthMetric::Count(STAT_SEND_ERRORS, 1), None);
anyhow::bail!("Failed to send traces: {err}")
},
}
}
})
.or_else(|err| {
Expand All @@ -226,7 +226,7 @@ impl TraceExporter {
HealthMetric::Count(name, c) => flusher.send(vec![DogStatsDAction::Count(
name,
c,
&self.common_stats_tags.iter().chain(&custom_tags.unwrap()),
self.common_stats_tags.iter().chain(&custom_tags.unwrap()),
)]),
}
} else {
Expand Down Expand Up @@ -258,7 +258,10 @@ impl TraceExporter {
return Ok(String::from("{}"));
}

self.emit_metric(HealthMetric::Count(STAT_DESER_TRACES, traces.len() as i64), None);
self.emit_metric(
HealthMetric::Count(STAT_DESER_TRACES, traces.len() as i64),
None,
);

let header_tags: TracerHeaderTags<'_> = (&self.tags).into();

Expand Down
39 changes: 19 additions & 20 deletions dogstatsd-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ const QUEUE_SIZE: usize = 32 * 1024;

/// The `DogStatsDActionRef` enum gathers the metric types that can be sent to the DogStatsD server.
#[derive(Debug, Serialize, Deserialize)]
pub enum DogStatsDAction<T: AsRef<str>, V: std::ops::Deref>
pub enum DogStatsDAction<T: AsRef<str>, V, I>
where
for<'a> &'a <V as std::ops::Deref>::Target: IntoIterator<Item = &'a Tag>,
V: IntoIterator<Item = I>,
I: std::ops::Deref<Target = Tag>,
{
// TODO: instead of AsRef<str> we can accept a marker Trait that users of this crate implement
Count(T, i64, V),
Expand Down Expand Up @@ -70,9 +71,9 @@ impl Flusher {
Ok(())
}

pub fn send<T: AsRef<str>, V: std::ops::Deref>(&self, actions: Vec<DogStatsDAction<T, V>>)
pub fn send<'t, T: AsRef<str>, V>(&self, actions: Vec<DogStatsDAction<T, V, &'t Tag>>)
where
for<'a> &'a <V as std::ops::Deref>::Target: IntoIterator<Item = &'a Tag>,
V: IntoIterator<Item = &'t Tag>,
{
if self.client.is_none() {
return;
Expand All @@ -81,23 +82,21 @@ impl Flusher {

for action in actions {
if let Err(err) = match action {
DogStatsDAction::Count(metric, value, ref tags) => {
DogStatsDAction::Count(metric, value, tags) => {
let metric_builder = client.count_with_tags(metric.as_ref(), value);
do_send(metric_builder, tags.deref())
do_send(metric_builder, tags)
}
DogStatsDAction::Distribution(metric, value, ref tags) => do_send(
client.distribution_with_tags(metric.as_ref(), value),
tags.deref(),
),
DogStatsDAction::Gauge(metric, value, ref tags) => {
do_send(client.gauge_with_tags(metric.as_ref(), value), tags.deref())
DogStatsDAction::Distribution(metric, value, tags) => {
do_send(client.distribution_with_tags(metric.as_ref(), value), tags)
}
DogStatsDAction::Histogram(metric, value, ref tags) => do_send(
client.histogram_with_tags(metric.as_ref(), value),
tags.deref(),
),
DogStatsDAction::Set(metric, value, ref tags) => {
do_send(client.set_with_tags(metric.as_ref(), value), tags.deref())
DogStatsDAction::Gauge(metric, value, tags) => {
do_send(client.gauge_with_tags(metric.as_ref(), value), tags)
}
DogStatsDAction::Histogram(metric, value, tags) => {
do_send(client.histogram_with_tags(metric.as_ref(), value), tags)
}
DogStatsDAction::Set(metric, value, tags) => {
do_send(client.set_with_tags(metric.as_ref(), value), tags)
}
} {
error!("Error while sending metric: {}", err);
Expand Down Expand Up @@ -186,12 +185,12 @@ mod test {
socket.local_addr().unwrap().to_string().as_str(),
));
flusher.send(vec![
Count("test_count", 3, vec![tag!("foo", "bar")]),
Count("test_count", 3, vec![&tag!("foo", "bar")]),
Count("test_neg_count", -2, vec![]),
Distribution("test_distribution", 4.2, vec![]),
Gauge("test_gauge", 7.6, vec![]),
Histogram("test_histogram", 8.0, vec![]),
Set("test_set", 9, vec![tag!("the", "end")]),
Set("test_set", 9, vec![&tag!("the", "end")]),
Set("test_neg_set", -1, vec![]),
]);

Expand Down

0 comments on commit ae5c14e

Please sign in to comment.