@@ -38,12 +38,48 @@ impl<T> Unpin for Streaming<T> {}
38
38
39
39
#[ derive( Debug , Clone ) ]
40
40
enum State {
41
- ReadHeader ,
41
+ ReadHeader {
42
+ span : tracing:: Span ,
43
+ } ,
42
44
ReadBody {
45
+ span : tracing:: Span ,
43
46
compression : Option < CompressionEncoding > ,
44
47
len : usize ,
45
48
} ,
46
- Error ( Status ) ,
49
+ Error ( Box < Status > ) ,
50
+ }
51
+
52
+ impl State {
53
+ fn read_header ( ) -> Self {
54
+ let span = tracing:: debug_span!(
55
+ "read_header" ,
56
+ compression = tracing:: field:: Empty ,
57
+ body. bytes = tracing:: field:: Empty ,
58
+ ) ;
59
+ Self :: ReadHeader { span }
60
+ }
61
+
62
+ fn read_body ( compression : Option < CompressionEncoding > , len : usize ) -> Self {
63
+ let span = tracing:: debug_span!(
64
+ "read_body" ,
65
+ compression = compression. map( |c| c. as_str( ) ) ,
66
+ compressed. bytes = len,
67
+ uncompressed. bytes = compression. is_none( ) . then_some( len) ,
68
+ ) ;
69
+ Self :: ReadBody {
70
+ span,
71
+ compression,
72
+ len,
73
+ }
74
+ }
75
+
76
+ fn span ( & self ) -> Option < & tracing:: Span > {
77
+ match self {
78
+ Self :: ReadHeader { span } => Some ( span) ,
79
+ Self :: ReadBody { span, .. } => Some ( span) ,
80
+ Self :: Error ( _) => None ,
81
+ }
82
+ }
47
83
}
48
84
49
85
#[ derive( Debug , PartialEq , Eq ) ]
@@ -125,7 +161,7 @@ impl<T> Streaming<T> {
125
161
. map_frame ( |frame| frame. map_data ( |mut buf| buf. copy_to_bytes ( buf. remaining ( ) ) ) )
126
162
. map_err ( |err| Status :: map_error ( err. into ( ) ) )
127
163
. boxed_unsync ( ) ,
128
- state : State :: ReadHeader ,
164
+ state : State :: read_header ( ) ,
129
165
direction,
130
166
buf : BytesMut :: with_capacity ( buffer_size) ,
131
167
trailers : None ,
@@ -142,7 +178,8 @@ impl StreamingInner {
142
178
& mut self ,
143
179
buffer_settings : BufferSettings ,
144
180
) -> Result < Option < DecodeBuf < ' _ > > , Status > {
145
- if let State :: ReadHeader = self . state {
181
+ if let State :: ReadHeader { span } = & self . state {
182
+ let _guard = span. enter ( ) ;
146
183
if self . buf . remaining ( ) < HEADER_SIZE {
147
184
return Ok ( None ) ;
148
185
}
@@ -151,7 +188,8 @@ impl StreamingInner {
151
188
0 => None ,
152
189
1 => {
153
190
{
154
- if self . encoding . is_some ( ) {
191
+ if let Some ( ce) = self . encoding {
192
+ span. record ( "compression" , ce. as_str ( ) ) ;
155
193
self . encoding
156
194
} else {
157
195
// https://grpc.github.io/grpc/core/md_doc_compression.html
@@ -177,6 +215,7 @@ impl StreamingInner {
177
215
} ;
178
216
179
217
let len = self . buf . get_u32 ( ) as usize ;
218
+ span. record ( "body.bytes" , len) ;
180
219
let limit = self
181
220
. max_message_size
182
221
. unwrap_or ( DEFAULT_MAX_RECV_MESSAGE_SIZE ) ;
@@ -191,14 +230,19 @@ impl StreamingInner {
191
230
}
192
231
193
232
self . buf . reserve ( len) ;
233
+ drop ( _guard) ;
194
234
195
- self . state = State :: ReadBody {
196
- compression : compression_encoding,
197
- len,
198
- }
235
+ self . state = State :: read_body ( compression_encoding, len)
199
236
}
200
237
201
- if let State :: ReadBody { len, compression } = self . state {
238
+ if let State :: ReadBody {
239
+ len,
240
+ span,
241
+ compression,
242
+ } = & self . state
243
+ {
244
+ let ( len, compression) = ( * len, * compression) ;
245
+ let _guard = span. enter ( ) ;
202
246
// if we haven't read enough of the message then return and keep
203
247
// reading
204
248
if self . buf . remaining ( ) < len || self . buf . len ( ) < len {
@@ -228,6 +272,7 @@ impl StreamingInner {
228
272
return Err ( Status :: new ( Code :: Internal , message) ) ;
229
273
}
230
274
let decompressed_len = self . decompress_buf . len ( ) ;
275
+ span. record ( "uncompressed.bytes" , decompressed_len) ;
231
276
DecodeBuf :: new ( & mut self . decompress_buf , decompressed_len)
232
277
} else {
233
278
DecodeBuf :: new ( & mut self . buf , len)
@@ -241,14 +286,16 @@ impl StreamingInner {
241
286
242
287
// Returns Some(()) if data was found or None if the loop in `poll_next` should break
243
288
fn poll_frame ( & mut self , cx : & mut Context < ' _ > ) -> Poll < Result < Option < ( ) > , Status > > {
289
+ let _guard = self . state . span ( ) . map ( |s| s. enter ( ) ) ;
244
290
let chunk = match ready ! ( Pin :: new( & mut self . body) . poll_frame( cx) ) {
245
291
Some ( Ok ( d) ) => Some ( d) ,
246
292
Some ( Err ( status) ) => {
247
293
if self . direction == Direction :: Request && status. code ( ) == Code :: Cancelled {
248
294
return Poll :: Ready ( Ok ( None ) ) ;
249
295
}
250
296
251
- let _ = std:: mem:: replace ( & mut self . state , State :: Error ( status. clone ( ) ) ) ;
297
+ drop ( _guard) ;
298
+ let _ = std:: mem:: replace ( & mut self . state , State :: Error ( Box :: new ( status. clone ( ) ) ) ) ;
252
299
debug ! ( "decoder inner stream error: {:?}" , status) ;
253
300
return Poll :: Ready ( Err ( status) ) ;
254
301
}
@@ -378,7 +425,7 @@ impl<T> Streaming<T> {
378
425
match self . inner . decode_chunk ( self . decoder . buffer_settings ( ) ) ? {
379
426
Some ( mut decode_buf) => match self . decoder . decode ( & mut decode_buf) ? {
380
427
Some ( msg) => {
381
- self . inner . state = State :: ReadHeader ;
428
+ self . inner . state = State :: read_header ( ) ;
382
429
Ok ( Some ( msg) )
383
430
}
384
431
None => Ok ( None ) ,
@@ -394,7 +441,7 @@ impl<T> Stream for Streaming<T> {
394
441
fn poll_next ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < Self :: Item > > {
395
442
loop {
396
443
if let State :: Error ( status) = & self . inner . state {
397
- return Poll :: Ready ( Some ( Err ( status. clone ( ) ) ) ) ;
444
+ return Poll :: Ready ( Some ( Err ( * status. clone ( ) ) ) ) ;
398
445
}
399
446
400
447
if let Some ( item) = self . decode_chunk ( ) ? {
0 commit comments