[scuba] support string payloads

Summary: Add a string-valued log message.

Reviewed By: jvillard

Differential Revision: D20895742

fbshipit-source-id: 0eab54c6d
master
Nikos Gorogiannis 5 years ago committed by Facebook GitHub Bot
parent a152a6131b
commit d8f446a25e

@ -11,7 +11,9 @@ type count_entry_data = {value: int}
type time_entry_data = {duration_ms: int}
type entry_data = Count of count_entry_data | Time of time_entry_data
type string_data = {message: string}
type entry_data = Count of count_entry_data | Time of time_entry_data | String of string_data
type t = {label: string; created_at_ts: int; data: entry_data}
@ -27,6 +29,12 @@ let mk_time ~label ~duration_ms =
{label; created_at_ts; data}
let mk_string ~label ~message =
let created_at_ts = Unix.time () |> int_of_float in
let data = String {message} in
{label; created_at_ts; data}
(** What _global_ mean at this point is subject to discussion. Right now there is only one use-case
which is Scuba+Scribe logging at the end of execution. But there might be more. Let's change the
naming accordingly when the purpose gets clearer. *)

@ -14,7 +14,9 @@ type count_entry_data = {value: int}
type time_entry_data = {duration_ms: int}
type entry_data = Count of count_entry_data | Time of time_entry_data
type string_data = {message: string}
type entry_data = Count of count_entry_data | Time of time_entry_data | String of string_data
(** created_at_ts is a unix timestamp (in seconds) *)
type t = {label: string; created_at_ts: int; data: entry_data}
@ -23,6 +25,8 @@ val mk_count : label:string -> value:int -> t
val mk_time : label:string -> duration_ms:int -> t
val mk_string : label:string -> message:string -> t
val global_log_get : unit -> t list
val global_log_erase : unit -> unit

@ -39,17 +39,21 @@ let set_common_fields sample =
let sample_from_event ({label; created_at_ts; data} : LogEntry.t) =
let event_name, value =
match data with
| LogEntry.Count {value} ->
(Printf.sprintf "count.%s" label, value)
| LogEntry.Time {duration_ms} ->
(Printf.sprintf "time.%s" label, duration_ms)
let create_sample_with_label label =
Scuba.new_sample ~time:(Some created_at_ts)
|> set_common_fields |> set_command_line_normales |> set_command_line_tagsets
|> Scuba.add_normal ~name:"event" ~value:label
in
Scuba.new_sample ~time:(Some created_at_ts)
|> set_common_fields |> set_command_line_normales |> set_command_line_tagsets
|> Scuba.add_normal ~name:"event" ~value:event_name
|> Scuba.add_int ~name:"value" ~value
match data with
| Count {value} ->
create_sample_with_label (Printf.sprintf "count.%s" label)
|> Scuba.add_int ~name:"value" ~value
| Time {duration_ms} ->
create_sample_with_label (Printf.sprintf "time.%s" label)
|> Scuba.add_int ~name:"value" ~value:duration_ms
| String {message} ->
create_sample_with_label (Printf.sprintf "msg.%s" label)
|> Scuba.add_normal ~name:"message" ~value:message
(** Consider buffering or batching if proves to be a problem *)
@ -66,6 +70,8 @@ let log_one entry = log_many [entry]
let log_count ~label ~value = log_one (LogEntry.mk_count ~label ~value)
let log_message ~label ~message = log_one (LogEntry.mk_string ~label ~message)
let execute_with_time_logging label f =
let ret_val, duration_ms = Utils.timeit ~f in
let entry = LogEntry.mk_time ~label ~duration_ms in

@ -16,7 +16,11 @@ val log_many : LogEntry.t list -> unit
aggregated results at once. *)
val log_count : label:string -> value:int -> unit
(** Log anything that can be counted. Events will be prefixed with "count." *)
(** Log anything that can be counted. Events will be prefixed with ["count."] *)
val log_message : label:string -> message:string -> unit
[@@warning "-32"]
(** Log a [string]. Event is prefixed with ["msg."] *)
val execute_with_time_logging : string -> (unit -> 'a) -> 'a
(** A helper to log execution time of a particular function. Use this to measure a performance of a

Loading…
Cancel
Save