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