File tree 3 files changed +46
-0
lines changed
opentelemetry-api/src/trace
opentelemetry-sdk/src/trace
3 files changed +46
-0
lines changed Original file line number Diff line number Diff line change @@ -129,6 +129,21 @@ impl SpanRef<'_> {
129
129
self . with_inner_mut ( move |inner| inner. set_attribute ( attribute) )
130
130
}
131
131
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
+
132
147
/// Sets the status of this `Span`.
133
148
///
134
149
/// If used, this will override the default span status, which is [`Status::Unset`].
Original file line number Diff line number Diff line change @@ -120,6 +120,25 @@ pub trait Span {
120
120
/// [opentelemetry_semantic_conventions]: https://docs.rs/opentelemetry-semantic-conventions
121
121
fn set_attribute ( & mut self , attribute : KeyValue ) ;
122
122
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
+
123
142
/// Sets the status of this `Span`.
124
143
///
125
144
/// If used, this will override the default span status, which is [`Status::Unset`].
Original file line number Diff line number Diff line change @@ -372,6 +372,18 @@ mod tests {
372
372
} ) ;
373
373
}
374
374
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
+
375
387
#[ test]
376
388
fn set_status ( ) {
377
389
{
You can’t perform that action at this time.
0 commit comments