Skip to content

Commit 74d0c2d

Browse files
authored
Add set_attributes method for Span (open-telemetry#638)
This adds the convenience method `Span::set_attributes` to set multiple attributes at a time. Co-authored-by: Srikanth Chekuri <[email protected]>
1 parent 999474a commit 74d0c2d

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

opentelemetry-api/src/trace/context.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,21 @@ impl SpanRef<'_> {
129129
self.with_inner_mut(move |inner| inner.set_attribute(attribute))
130130
}
131131

132+
/// Set multiple attributes of this span.
133+
///
134+
/// Setting an attribute with the same key as an existing attribute
135+
/// generally overwrites the existing attribute's value.
136+
///
137+
/// Note that the OpenTelemetry project documents certain "[standard
138+
/// attributes]" that have prescribed semantic meanings and are available via
139+
/// the [opentelemetry_semantic_conventions] crate.
140+
///
141+
/// [standard attributes]: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.9.0/specification/trace/semantic_conventions/README.md
142+
/// [opentelemetry_semantic_conventions]: https://docs.rs/opentelemetry-semantic-conventions
143+
pub fn set_attributes(&mut self, attributes: impl IntoIterator<Item = KeyValue>) {
144+
self.with_inner_mut(move |inner| inner.set_attributes(attributes))
145+
}
146+
132147
/// Sets the status of this `Span`.
133148
///
134149
/// If used, this will override the default span status, which is [`Status::Unset`].

opentelemetry-api/src/trace/span.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,25 @@ pub trait Span {
120120
/// [opentelemetry_semantic_conventions]: https://docs.rs/opentelemetry-semantic-conventions
121121
fn set_attribute(&mut self, attribute: KeyValue);
122122

123+
/// Set multiple attributes of this span.
124+
///
125+
/// Setting an attribute with the same key as an existing attribute
126+
/// generally overwrites the existing attribute's value.
127+
///
128+
/// Note that the OpenTelemetry project documents certain "[standard
129+
/// attributes]" that have prescribed semantic meanings and are available via
130+
/// the [opentelemetry_semantic_conventions] crate.
131+
///
132+
/// [standard attributes]: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.9.0/specification/trace/semantic_conventions/README.md
133+
/// [opentelemetry_semantic_conventions]: https://docs.rs/opentelemetry-semantic-conventions
134+
fn set_attributes(&mut self, attributes: impl IntoIterator<Item = KeyValue>) {
135+
if self.is_recording() {
136+
for attr in attributes.into_iter() {
137+
self.set_attribute(attr);
138+
}
139+
}
140+
}
141+
123142
/// Sets the status of this `Span`.
124143
///
125144
/// If used, this will override the default span status, which is [`Status::Unset`].

opentelemetry-sdk/src/trace/span.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,18 @@ mod tests {
372372
});
373373
}
374374

375+
#[test]
376+
fn set_attributes() {
377+
let mut span = create_span();
378+
let attributes = [KeyValue::new("k1", "v1"), KeyValue::new("k2", "v2")];
379+
span.set_attributes(attributes.clone());
380+
span.with_data(|data| {
381+
for kv in attributes {
382+
assert_eq!(data.attributes.get(&kv.key), Some(&kv.value))
383+
}
384+
});
385+
}
386+
375387
#[test]
376388
fn set_status() {
377389
{

0 commit comments

Comments
 (0)