Skip to content

Commit

Permalink
Replace format!() by heapless fmt for i64, f64 and bool
Browse files Browse the repository at this point in the history
  • Loading branch information
Hartigan committed Mar 16, 2024
1 parent d5b9bcb commit 8d8e71b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
2 changes: 2 additions & 0 deletions opentelemetry-datadog/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ thiserror = "1.0"
itertools = "0.11"
http = "0.2"
futures-core = "0.3"
ryu = "1"
itoa = "1"

[dev-dependencies]
async-trait = "0.1"
Expand Down
23 changes: 17 additions & 6 deletions opentelemetry-datadog/src/exporter/intern.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use indexmap::set::IndexSet;
use opentelemetry::Value;
use std::{borrow::Cow, hash::Hash};
use rmp::encode::{RmpWrite, ValueWriteError};
use std::hash::Hash;

#[derive(PartialEq)]
pub(crate) enum InternValue<'a> {
Expand All @@ -16,8 +17,7 @@ impl<'a> Hash for InternValue<'a> {
Value::Bool(x) => x.hash(state),
Value::I64(x) => x.hash(state),

Check warning on line 18 in opentelemetry-datadog/src/exporter/intern.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-datadog/src/exporter/intern.rs#L17-L18

Added lines #L17 - L18 were not covered by tests
Value::String(x) => x.hash(state),

Value::F64(x) => std::ptr::hash(std::ptr::addr_of!(x), state),
Value::F64(x) => x.to_bits().hash(state),
Value::Array(x) => std::ptr::hash(std::ptr::addr_of!(x), state),

Check warning on line 21 in opentelemetry-datadog/src/exporter/intern.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-datadog/src/exporter/intern.rs#L20-L21

Added lines #L20 - L21 were not covered by tests
},
}
Expand All @@ -27,10 +27,21 @@ impl<'a> Hash for InternValue<'a> {
impl<'a> Eq for InternValue<'a> {}

impl<'a> InternValue<'a> {
pub(crate) fn as_str(&self) -> Cow<'a, str> {
pub(crate) fn write_as_str<W: RmpWrite>(
&self,
payload: &mut W,
) -> Result<(), ValueWriteError<W::Error>> {
match self {
InternValue::RegularString(x) => Cow::Borrowed(x),
InternValue::OpenTelemetryValue(x) => x.as_str(),
InternValue::RegularString(x) => rmp::encode::write_str(payload, x),
InternValue::OpenTelemetryValue(v) => match v {
Value::Bool(x) => {
rmp::encode::write_str(payload, if *x { "true" } else { "false" })

Check warning on line 38 in opentelemetry-datadog/src/exporter/intern.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-datadog/src/exporter/intern.rs#L37-L38

Added lines #L37 - L38 were not covered by tests
}
Value::I64(x) => rmp::encode::write_str(payload, itoa::Buffer::new().format(*x)),
Value::F64(x) => rmp::encode::write_str(payload, ryu::Buffer::new().format(*x)),

Check warning on line 41 in opentelemetry-datadog/src/exporter/intern.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-datadog/src/exporter/intern.rs#L40-L41

Added lines #L40 - L41 were not covered by tests
Value::String(x) => rmp::encode::write_str(payload, x.as_ref()),
x => rmp::encode::write_str(payload, x.as_str().as_ref()),

Check warning on line 43 in opentelemetry-datadog/src/exporter/intern.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-datadog/src/exporter/intern.rs#L43

Added line #L43 was not covered by tests
},
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions opentelemetry-datadog/src/exporter/model/v05.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ where

rmp::encode::write_array_len(&mut payload, interner.len())?;
for data in interner.iter() {
rmp::encode::write_str(&mut payload, data.as_str().as_ref())?;
data.write_as_str(&mut payload)?;
}

payload.append(&mut encoded_traces);
Expand Down Expand Up @@ -171,12 +171,12 @@ where
rmp::encode::write_array_len(&mut encoded, SPAN_NUM_ELEMENTS)?;
rmp::encode::write_u32(
&mut encoded,
interner.intern(get_service_name(&span, model_config)),
interner.intern(get_service_name(span, model_config)),
)?;
rmp::encode::write_u32(&mut encoded, interner.intern(get_name(&span, model_config)))?;
rmp::encode::write_u32(&mut encoded, interner.intern(get_name(span, model_config)))?;
rmp::encode::write_u32(
&mut encoded,
interner.intern(get_resource(&span, model_config)),
interner.intern(get_resource(span, model_config)),
)?;
rmp::encode::write_u64(
&mut encoded,
Expand Down Expand Up @@ -208,7 +208,7 @@ where
)?;
for (key, value) in span.resource.iter() {
rmp::encode::write_u32(&mut encoded, interner.intern(key.as_str()))?;
rmp::encode::write_u32(&mut encoded, interner.intern_value(&value))?;
rmp::encode::write_u32(&mut encoded, interner.intern_value(value))?;
}

write_unified_tags(&mut encoded, interner, unified_tags)?;
Expand Down

0 comments on commit 8d8e71b

Please sign in to comment.