Skip to content
Merged
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
45 changes: 31 additions & 14 deletions libdd-library-config/src/tracer_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,20 @@ pub struct TracerMetadata {
#[serde(skip_serializing_if = "Option::is_none")]
pub container_id: Option<String>,
/// Ordered list of attribute key names for thread-level context records. Key indices from
/// thread context records index into this table. Keep empty if thread-level context is not
/// used.
/// thread context records index into this table. Set to `None` to disable thread-level related
/// attributes to the process-level context.
///
/// If set to `Some`, the first key will be automatically set to `datadog.local_root_span_id`
/// in the OTel process context, because the thread context handling elsewhere in libdatadog
/// relies on this key's index to be zero. Only set additional keys in
/// `threadlocal_attribute_keys`; the root span id is considered to always be here implicitly.
///
/// This field is specific to OTel process context. It is ignored for (de)serialization, and is
/// only used when converting to an OTel process context in
/// [TracerMetadata::to_otel_process_ctx].
#[cfg(feature = "otel-thread-ctx")]
#[serde(skip)]
pub threadlocal_attribute_keys: Vec<String>,
pub threadlocal_attribute_keys: Option<Vec<String>>,
}

impl Default for TracerMetadata {
Expand All @@ -59,7 +64,7 @@ impl Default for TracerMetadata {
process_tags: None,
container_id: None,
#[cfg(feature = "otel-thread-ctx")]
threadlocal_attribute_keys: vec![],
threadlocal_attribute_keys: None,
}
}
}
Expand Down Expand Up @@ -124,21 +129,25 @@ impl TracerMetadata {
];

#[cfg(feature = "otel-thread-ctx")]
if !threadlocal_attribute_keys.is_empty() {
if let Some(threadlocal_attribute_keys) = threadlocal_attribute_keys.as_ref() {
attributes.push(key_value(
"threadlocal.schema_version",
"tlsdesc_v1_dev".to_owned(),
));

attributes.push(KeyValue {
key: "threadlocal.attribute_key_map".to_owned(),
value: Some(AnyValue {
value: Some(any_value::Value::ArrayValue(ArrayValue {
values: threadlocal_attribute_keys
.iter()
.map(|k| AnyValue {
value: Some(any_value::Value::StringValue(k.clone())),
})
.collect(),
values: std::iter::once(AnyValue {
value: Some(any_value::Value::StringValue(
"datadog.local_root_span_id".to_owned(),
)),
})
.chain(threadlocal_attribute_keys.iter().map(|k| AnyValue {
value: Some(any_value::Value::StringValue(k.clone())),
}))
.collect(),
})),
}),
key_ref: 0,
Expand Down Expand Up @@ -250,11 +259,11 @@ mod tests {
#[test]
fn threadlocal_attrs_present_with_correct_values() {
let ctx = TracerMetadata {
threadlocal_attribute_keys: vec![
threadlocal_attribute_keys: Some(vec![
"span.id".to_owned(),
"trace.id".to_owned(),
"custom.key".to_owned(),
],
]),
..Default::default()
}
.to_otel_process_ctx();
Expand Down Expand Up @@ -282,6 +291,14 @@ mod tests {
other => panic!("expected StringValue, got {:?}", other),
})
.collect();
assert_eq!(keys, ["span.id", "trace.id", "custom.key"]);
assert_eq!(
keys,
[
"datadog.local_root_span_id",
"span.id",
"trace.id",
"custom.key"
]
);
}
}
Loading
Loading