Skip to content

Commit

Permalink
Add set_attributes method for Span (#638)
Browse files Browse the repository at this point in the history
This adds the convenience method `Span::set_attributes` to set multiple
attributes at a time.

Co-authored-by: Srikanth Chekuri <srikanth.chekuri92@gmail.com>
  • Loading branch information
srikanthccv committed Apr 17, 2022
1 parent 999474a commit 74d0c2d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
15 changes: 15 additions & 0 deletions opentelemetry-api/src/trace/context.rs
Expand Up @@ -129,6 +129,21 @@ impl SpanRef<'_> {
self.with_inner_mut(move |inner| inner.set_attribute(attribute))
}

/// Set multiple attributes of this span.
///
/// Setting an attribute with the same key as an existing attribute
/// generally overwrites the existing attribute's value.
///
/// Note that the OpenTelemetry project documents certain "[standard
/// attributes]" that have prescribed semantic meanings and are available via
/// the [opentelemetry_semantic_conventions] crate.
///
/// [standard attributes]: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.9.0/specification/trace/semantic_conventions/README.md
/// [opentelemetry_semantic_conventions]: https://docs.rs/opentelemetry-semantic-conventions
pub fn set_attributes(&mut self, attributes: impl IntoIterator<Item = KeyValue>) {
self.with_inner_mut(move |inner| inner.set_attributes(attributes))
}

/// Sets the status of this `Span`.
///
/// If used, this will override the default span status, which is [`Status::Unset`].
Expand Down
19 changes: 19 additions & 0 deletions opentelemetry-api/src/trace/span.rs
Expand Up @@ -120,6 +120,25 @@ pub trait Span {
/// [opentelemetry_semantic_conventions]: https://docs.rs/opentelemetry-semantic-conventions
fn set_attribute(&mut self, attribute: KeyValue);

/// Set multiple attributes of this span.
///
/// Setting an attribute with the same key as an existing attribute
/// generally overwrites the existing attribute's value.
///
/// Note that the OpenTelemetry project documents certain "[standard
/// attributes]" that have prescribed semantic meanings and are available via
/// the [opentelemetry_semantic_conventions] crate.
///
/// [standard attributes]: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.9.0/specification/trace/semantic_conventions/README.md
/// [opentelemetry_semantic_conventions]: https://docs.rs/opentelemetry-semantic-conventions
fn set_attributes(&mut self, attributes: impl IntoIterator<Item = KeyValue>) {
if self.is_recording() {
for attr in attributes.into_iter() {
self.set_attribute(attr);
}
}
}

/// Sets the status of this `Span`.
///
/// If used, this will override the default span status, which is [`Status::Unset`].
Expand Down
12 changes: 12 additions & 0 deletions opentelemetry-sdk/src/trace/span.rs
Expand Up @@ -372,6 +372,18 @@ mod tests {
});
}

#[test]
fn set_attributes() {
let mut span = create_span();
let attributes = [KeyValue::new("k1", "v1"), KeyValue::new("k2", "v2")];
span.set_attributes(attributes.clone());
span.with_data(|data| {
for kv in attributes {
assert_eq!(data.attributes.get(&kv.key), Some(&kv.value))
}
});
}

#[test]
fn set_status() {
{
Expand Down

0 comments on commit 74d0c2d

Please sign in to comment.