@@ -28,21 +28,16 @@ let host_id = ref "localhost"
2828
2929let  set_host_id  id  =  host_id :=  id
3030
31- let  service_name =  ref  None 
31+ let  service_name =  ref  " unknown " 
3232
33- let  set_service_name  name  =  service_name :=  Some   name
33+ let  set_service_name  name  =  service_name :=  name
3434
35- let  get_service_name  ()  = 
36-   match  ! service_name with 
37-   |  None  ->
38-       warn " service name not yet set!" 
39-       " unknown" 
40-   |  Some  name  ->
41-       name
35+ let  get_service_name  ()  =  ! service_name
4236
4337module  Content  =  struct 
4438  module  Json  =  struct 
45-     module  Zipkinv2  =  struct 
39+     module  ZipkinV2  =  struct 
40+       (*  Module that helps export spans under Zipkin protocol, version 2. *) 
4641      module  ZipkinSpan  =  struct 
4742        type  zipkinEndpoint  = {serviceName : string } [@@ deriving rpcty ]
4843
@@ -140,7 +135,7 @@ module Destination = struct
140135
141136    let  lock =  Mutex. create () 
142137
143-     let  new_file_name  ()  = 
138+     let  make_file_name  ()  = 
144139      let  date =  Ptime_clock. now ()  |>  Ptime. to_rfc3339 ~frac_s: 6  in 
145140      let  ( //  ) =  Filename. concat in 
146141      let  name = 
@@ -163,7 +158,7 @@ module Destination = struct
163158    let  export  json  = 
164159      try 
165160        let  file_name = 
166-           match  ! file_name with  None  ->  new_file_name  ()  |  Some  x  ->  x
161+           match  ! file_name with  None  ->  make_file_name  ()  |  Some  x  ->  x
167162        in 
168163        Xapi_stdext_unix.Unixext. mkdir_rec (Filename. dirname file_name) 0o700  ;
169164        let @  fd =  file_name |>  with_fd in 
@@ -173,7 +168,7 @@ module Destination = struct
173168          if  ! compress_tracing_files then 
174169            Zstd.Fast. compress_file Zstd.Fast. compress ~file_path: file_name
175170              ~file_ext: " zst" 
176-           ignore @@  new_file_name  () 
171+           ignore @@  make_file_name  () 
177172        ) ;
178173        Ok  () 
179174      with  e  ->  Error  e
@@ -189,31 +184,36 @@ module Destination = struct
189184    let  export  ~url   json  = 
190185      try 
191186        let  body =  json in 
187+         let  content_headers = 
188+           [
189+             (" Content-Type" " application/json" 
190+           ; (" Content-Length" String. length body))
191+           ]
192+         in 
193+         let  host = 
194+           match  (Uri. host url, Uri. port url) with 
195+           |  None , _  ->
196+               None 
197+           |  Some  host , None  ->
198+               Some  host
199+           |  Some  host , Some  port  ->
200+               Some  (Printf. sprintf " %s:%d" 
201+         in 
202+         let  host_headers = 
203+           Option. fold ~none: []  ~some: (fun  h  -> [(" Host" 
204+         in 
192205        let  headers = 
193-           Cohttp.Header. of_list
194-             ([
195-                (" Content-Type" " application/json" 
196-              ; (" Content-Length" String. length body))
197-              ]
198-             @ 
199-             match  Uri. host url with 
200-             |  None  ->
201-                 [] 
202-             |  Some  h  ->
203-                 let  port = 
204-                   match  Uri. port url with 
205-                   |  Some  p  ->
206-                       " :" ^  string_of_int p
207-                   |  None  ->
208-                       " " 
209-                 in 
210-                 [(" Host" ^  port)]
211-             )
206+           List. concat [content_headers; host_headers] |>  Cohttp.Header. of_list
212207        in 
208+ 
213209        Open_uri. with_open_uri url (fun  fd  ->
214210            let  request = 
215211              Cohttp.Request. make ~meth: `POST  ~version: `HTTP_1_1  ~headers  url
216212            in 
213+             (*  `with_open_uri` already closes the `fd`. And therefore
214+                according to the documentation of `in_channel_of_descr` and 
215+                `out_channel_of_descr` we should not close the channels on top of 
216+                `fd`. *)  
217217            let  ic =  Unix. in_channel_of_descr fd in 
218218            let  oc =  Unix. out_channel_of_descr fd in 
219219            Request. write
@@ -231,19 +231,8 @@ module Destination = struct
231231              when  Cohttp.Code. (response.status |>  code_of_status |>  is_error)
232232              ->
233233                Error  (Failure  (Cohttp.Code. string_of_status response.status))
234-             |  `Ok  response  ->
235-                 let  body =  Buffer. create 128  in 
236-                 let  reader =  Response. make_body_reader response ic in 
237-                 let  rec  loop  ()  = 
238-                   match  Response. read_body_chunk reader with 
239-                   |  Cohttp.Transfer. Chunk  x  ->
240-                       Buffer. add_string body x ; loop () 
241-                   |  Cohttp.Transfer. Final_chunk  x  ->
242-                       Buffer. add_string body x
243-                   |  Cohttp.Transfer. Done  ->
244-                       () 
245-                 in 
246-                 loop ()  ; Ok  () 
234+             |  `Ok  _  ->
235+                 Ok  () 
247236        )
248237      with  e  ->  Error  e
249238  end 
@@ -276,7 +265,7 @@ module Destination = struct
276265          in 
277266          let @  _ =  with_tracing ~parent  ~attributes  ~name  in 
278267          all_spans
279-           |>  Content.Json.Zipkinv2 . content_of
268+           |>  Content.Json.ZipkinV2 . content_of
280269          |>  export
281270          |>  Result. iter_error raise
282271      )
0 commit comments