Skip to content

Commit af2f199

Browse files
committed
CP-48195: Improvements to tracing_export
Small improvements to `tracing_export.ml` and `tracing_exoprt.mli`. Issues found while splitting the library such as: missing documentation and small code refactoring for better readability. Signed-off-by: Gabriel Buica <danutgabriel.buica@cloud.com>
1 parent 577d424 commit af2f199

File tree

2 files changed

+89
-46
lines changed

2 files changed

+89
-46
lines changed

ocaml/libs/tracing/tracing_export.ml

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,16 @@ let host_id = ref "localhost"
2828

2929
let 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

4337
module 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_of_int (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" host port)
201+
in
202+
let host_headers =
203+
Option.fold ~none:[] ~some:(fun h -> [("Host", h)]) host
204+
in
192205
let headers =
193-
Cohttp.Header.of_list
194-
([
195-
("Content-Type", "application/json")
196-
; ("Content-Length", string_of_int (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", h ^ 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
)

ocaml/libs/tracing/tracing_export.mli

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,84 @@
1212
* GNU Lesser General Public License for more details.
1313
*)
1414

15+
(** [Tracing_export] is a module dedicated for the creation and management of
16+
threads that export the tracing data.
17+
*)
18+
1519
val set_export_interval : float -> unit
20+
(** [set_export_interval seconds] sets the time interval between consecutive
21+
exports of the finished spans to [seconds].
22+
23+
Default is every [30.] seconds.
24+
*)
1625

1726
val set_host_id : string -> unit
27+
(** [set_host_id id] sets the id of the host to [id].
28+
29+
Default is ["localhost"].
30+
*)
1831

1932
val set_service_name : string -> unit
33+
(** [set_service_name name] sets the name of the service to [name].
34+
All spans will be exported under this service's name.
35+
36+
Default name is ["unknown"].
37+
*)
2038

39+
(** [Destination] is a module for managing the export of tracing data to
40+
different types of endpoints, whether is exporting it to a [File] or an
41+
[Http] endpoint.
42+
*)
2143
module Destination : sig
44+
(** [File] is a module for managing the files in which the tracing data is
45+
exported.
46+
*)
2247
module File : sig
2348
val set_max_file_size : int -> unit
49+
(** [set_max_file_size n] sets the maximum file size to [n]. If a file is
50+
is already created at the time of export and the file exceeds the
51+
maximum size, a new tracing file is created.
52+
*)
2453

2554
val set_trace_log_dir : string -> unit
55+
(** [set_trace_log_dir log_dir] sets the location to which traces will be
56+
exported.
57+
58+
Default is ["/var/log/dt/zipkinv2/json"]
59+
*)
2660

2761
val get_trace_log_dir : unit -> string
62+
(** [get_trace_log_dir ()] returns the cuurent location to which traces are
63+
exported.
64+
*)
2865

2966
val set_compress_tracing_files : bool -> unit
67+
(** [set_compress_tracing_files flag] sets wheater or not the tracing files
68+
are compressed or not.
69+
*)
3070
end
3171

3272
val flush_spans : unit -> unit
73+
(** [flush_spans ()] forcefully flushes the spans to the current enabled
74+
endpoints.
75+
*)
3376

77+
(** [Http] is a module for managing exporting tracing data to an http
78+
endpoint.
79+
*)
3480
module Http : sig
3581
val export : url:Uri.t -> string -> (unit, exn) result
82+
(** [export ~url json] forcefully flushes json formatted spans [json] to the
83+
given [url] .
84+
*)
3685
end
3786
end
3887

3988
val flush_and_exit : unit -> unit
89+
(** [flush_and_exit ()] sends a signal to flush the finish spans and terminate
90+
the exporter thread.
91+
*)
4092

4193
val main : unit -> Thread.t
94+
(** [main ()] starts the exporter thread.
95+
*)

0 commit comments

Comments
 (0)