File tree Expand file tree Collapse file tree 1 file changed +24
-1
lines changed
opentelemetry-contrib/src/trace Expand file tree Collapse file tree 1 file changed +24
-1
lines changed Original file line number Diff line number Diff line change 1
1
use super :: TracerSource ;
2
2
use opentelemetry:: {
3
3
trace:: { SpanBuilder , TraceContextExt as _, Tracer as _} ,
4
- Context ,
4
+ Context , ContextGuard ,
5
5
} ;
6
6
use std:: {
7
7
fmt:: { Debug , Formatter } ,
@@ -129,6 +129,12 @@ impl<T> Contextualized<T> {
129
129
pub fn into_inner ( self ) -> ( T , Option < Context > ) {
130
130
( self . 0 , self . 1 )
131
131
}
132
+
133
+ /// Attach the contained context if it exists and return both the
134
+ /// associated value and an optional guard for the attached context.
135
+ pub fn attach ( self ) -> ( T , Option < ContextGuard > ) {
136
+ ( self . 0 , self . 1 . map ( |cx| cx. attach ( ) ) )
137
+ }
132
138
}
133
139
134
140
impl < T : Clone > Clone for Contextualized < T > {
@@ -159,3 +165,20 @@ impl<T> DerefMut for Contextualized<T> {
159
165
& mut self . 0
160
166
}
161
167
}
168
+
169
+ #[ cfg( test) ]
170
+ mod tests {
171
+ use super :: * ;
172
+
173
+ #[ test]
174
+ fn cover_contextualized ( ) {
175
+ let cx = Contextualized :: new ( 17 , None ) ;
176
+ let ( i, cx) = cx. into_inner ( ) ;
177
+ assert_eq ! ( i, 17 ) ;
178
+ assert ! ( cx. is_none( ) ) ;
179
+
180
+ let cx = Contextualized :: pass_thru ( 17 ) ;
181
+ let ( i, _guard) = cx. attach ( ) ;
182
+ assert_eq ! ( i, 17 ) ;
183
+ }
184
+ }
You can’t perform that action at this time.
0 commit comments