22// Licensed under the MIT License.
33
44use futures:: StreamExt ;
5+ use tracing:: level_filters:: LevelFilter ;
6+ use tracing_subscriber:: fmt:: format:: FmtSpan ;
57
68#[ tokio:: main]
79async fn main ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
10+ // Log traces to stdout.
11+ let _log = tracing_subscriber:: fmt ( )
12+ . with_span_events ( FmtSpan :: ENTER | FmtSpan :: EXIT )
13+ . with_max_level ( LevelFilter :: DEBUG )
14+ . init ( ) ;
15+
816 // Get a response from a service client.
9- let response = client:: get_binary_data_response ( ) ?;
17+ let response = client:: get_binary_data ( ) ?;
1018
1119 // Normally you'd deserialize into a type or `collect()` the body,
1220 // but this better simulates fetching multiple chunks from a slow response.
@@ -18,7 +26,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
1826 }
1927
2028 // You can also deserialize into a model from a slow response.
21- let team = client:: get_model_response ( ) ?. deserialize_body ( ) . await ?;
29+ let team = client:: get_model ( ) ?. deserialize_body ( ) . await ?;
2230 println ! ( "{team:#?}" ) ;
2331
2432 Ok ( ( ) )
@@ -29,6 +37,7 @@ mod client {
2937 use futures:: Stream ;
3038 use serde:: Deserialize ;
3139 use std:: { cmp:: min, task:: Poll , time:: Duration } ;
40+ use tracing:: debug;
3241 use typespec_client_core:: {
3342 http:: { headers:: Headers , Model , Response , StatusCode } ,
3443 Bytes ,
@@ -47,7 +56,8 @@ mod client {
4756 pub name : Option < String > ,
4857 }
4958
50- pub fn get_binary_data_response ( ) -> typespec_client_core:: Result < Response < ( ) > > {
59+ #[ tracing:: instrument]
60+ pub fn get_binary_data ( ) -> typespec_client_core:: Result < Response < ( ) > > {
5161 let bytes = Bytes :: from_static ( b"Hello, world!" ) ;
5262 let response = SlowResponse {
5363 bytes : bytes. repeat ( 5 ) . into ( ) ,
@@ -62,7 +72,8 @@ mod client {
6272 ) )
6373 }
6474
65- pub fn get_model_response ( ) -> typespec_client_core:: Result < Response < Team > > {
75+ #[ tracing:: instrument]
76+ pub fn get_model ( ) -> typespec_client_core:: Result < Response < Team > > {
6677 let bytes = br#"{
6778 "name": "Contoso Dev Team",
6879 "members": [
@@ -104,7 +115,9 @@ mod client {
104115 ) -> Poll < Option < Self :: Item > > {
105116 let self_mut = self . get_mut ( ) ;
106117 if self_mut. bytes_read < self_mut. bytes . len ( ) {
107- eprintln ! ( "getting partial response..." ) ;
118+ debug ! ( "writing partial response..." ) ;
119+
120+ // Simulate a slow response.
108121 std:: thread:: sleep ( Duration :: from_millis ( 200 ) ) ;
109122
110123 let end = self_mut. bytes_read
@@ -116,7 +129,7 @@ mod client {
116129 self_mut. bytes_read += bytes. len ( ) ;
117130 Poll :: Ready ( Some ( Ok ( bytes) ) )
118131 } else {
119- eprintln ! ( "done" ) ;
132+ debug ! ( "done" ) ;
120133 Poll :: Ready ( None )
121134 }
122135 }
0 commit comments