Skip to content

Commit

Permalink
Rename SpanValue
Browse files Browse the repository at this point in the history
  • Loading branch information
VianneyRuhlmann committed Feb 21, 2025
1 parent f27b214 commit 4be708f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion trace-utils/src/span/v04/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ mod span;

pub mod trace_utils;

pub use span::{Span, SpanBytes, SpanKey, SpanKeyParseError, SpanLink, SpanLinkBytes, SpanValue};
pub use span::{Span, SpanBytes, SpanKey, SpanKeyParseError, SpanLink, SpanLinkBytes, SpanText};
18 changes: 9 additions & 9 deletions trace-utils/src/span/v04/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ impl FromStr for SpanKey {

/// Trait representing the requirements for a type to be used as a Span "string" type.
/// Note: Borrow<str> is not required by the derived traits, but allows to access HashMap elements
/// from a static str.
pub trait SpanValue: Eq + Hash + Borrow<str> {}
/// Implement the SpanValue trait for any type which satisfies the sub trait.
impl<T: Eq + Hash + Borrow<str>> SpanValue for T {}
/// from a static str and check if the string is empty.
pub trait SpanText: Eq + Hash + Borrow<str> {}
/// Implement the SpanText trait for any type which satisfies the sub traits.
impl<T: Eq + Hash + Borrow<str>> SpanText for T {}

/// Checks if the `value` represents an empty string. Used to skip serializing empty strings in
/// Checks if the `value` represents an empty string. Used to skip serializing empty strings
/// with serde.
fn is_empty_str<T: Borrow<str>>(value: &T) -> bool {
value.borrow().is_empty()
Expand All @@ -70,15 +70,15 @@ fn is_empty_str<T: Borrow<str>>(value: &T) -> bool {
/// or borrowed (e.g. &str). To define a generic function taking any `Span<T>` you can use the
/// [`SpanValue`] trait:
/// ```
/// use datadog_trace_utils::span::v04::{Span, SpanValue};
/// fn foo<T: SpanValue>(span: Span<T>) {
/// use datadog_trace_utils::span::v04::{Span, SpanText};
/// fn foo<T: SpanText>(span: Span<T>) {
/// let _ = span.meta.get("foo");
/// }
/// ```
#[derive(Clone, Debug, Default, PartialEq, Serialize)]
pub struct Span<T>
where
T: SpanValue,
T: SpanText,
{
pub service: T,
pub name: T,
Expand Down Expand Up @@ -108,7 +108,7 @@ where
#[derive(Clone, Debug, Default, PartialEq, Serialize)]
pub struct SpanLink<T>
where
T: SpanValue,
T: SpanText,
{
pub trace_id: u64,
pub trace_id_high: u64,
Expand Down
12 changes: 6 additions & 6 deletions trace-utils/src/span/v04/trace_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

//! Trace-utils functionalities implementation for tinybytes based spans
use super::{Span, SpanValue};
use super::{Span, SpanText};
use std::collections::HashMap;

/// Span metric the mini agent must set for the backend to recognize top level span
Expand All @@ -15,7 +15,7 @@ const PARTIAL_VERSION_KEY: &str = "_dd.partial_version";

fn set_top_level_span<'a, T>(span: &mut Span<T>, is_top_level: bool)
where
T: SpanValue + From<&'a str>,
T: SpanText + From<&'a str>,
{
if is_top_level {
span.metrics.insert(TOP_LEVEL_KEY.into(), 1.0);
Expand All @@ -32,7 +32,7 @@ where
/// ancestor of other spans belonging to this service and attached to it).
pub fn compute_top_level_span<'a, T>(trace: &mut [Span<T>])
where
T: SpanValue + Clone + From<&'a str>,
T: SpanText + Clone + From<&'a str>,
{
let mut span_id_to_service: HashMap<u64, T> = HashMap::new();
for span in trace.iter() {
Expand All @@ -59,15 +59,15 @@ where
}

/// Return true if the span has a top level key set
pub fn has_top_level<T: SpanValue>(span: &Span<T>) -> bool {
pub fn has_top_level<T: SpanText>(span: &Span<T>) -> bool {
span.metrics
.get(TRACER_TOP_LEVEL_KEY)
.is_some_and(|v| *v == 1.0)
|| span.metrics.get(TOP_LEVEL_KEY).is_some_and(|v| *v == 1.0)
}

/// Returns true if a span should be measured (i.e., it should get trace metrics calculated).
pub fn is_measured<T: SpanValue>(span: &Span<T>) -> bool {
pub fn is_measured<T: SpanText>(span: &Span<T>) -> bool {
span.metrics.get(MEASURED_KEY).is_some_and(|v| *v == 1.0)
}

Expand All @@ -76,7 +76,7 @@ pub fn is_measured<T: SpanValue>(span: &Span<T>) -> bool {
/// When incomplete, a partial snapshot has a metric _dd.partial_version which is a positive
/// integer. The metric usually increases each time a new version of the same span is sent by
/// the tracer
pub fn is_partial_snapshot<T: SpanValue>(span: &Span<T>) -> bool {
pub fn is_partial_snapshot<T: SpanText>(span: &Span<T>) -> bool {
span.metrics
.get(PARTIAL_VERSION_KEY)
.is_some_and(|v| *v >= 0.0)
Expand Down

0 comments on commit 4be708f

Please sign in to comment.