Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add set_attributes method for Span #638

Merged
merged 1 commit into from Apr 17, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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