1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
type counter_fn = ?labels:(string * string) list -> int -> unit
type gauge_fn = ?labels:(string * string) list -> float -> unit
type histogram_fn = ?labels:(string * string) list -> float -> unit
type level = Debug | Info | Warn | Error
type log_entry = {
timestamp_ns : int64;
level : level;
message : string;
fields : (string * string) list;
}
type span_event = {
trace_ctx : Obs_trace.t;
name : string;
service : string;
start_ns : int64;
end_ns : int64;
status : [ `Ok | `Error of string ];
log_entries : log_entry list;
context : (string * string) list;
}
type metric_event = {
name : string;
help : string;
kind : [ `Counter of int | `Gauge of float | `Histogram of float ];
labels : (string * string) list;
context : (string * string) list;
service : string;
}
type metric_kind = [ `Counter | `Gauge | `Histogram ]
type metric_declaration = {
declaration_name : string;
declaration_help : string;
declaration_kind : metric_kind;
declaration_label_names : string list;
declaration_service : string;
}
type backend = {
emit_span : span_event -> unit;
emit_metric : metric_event -> unit;
declare_metric : metric_declaration -> unit;
}
type backend_op =
| Emit_span of { name : string }
| Emit_metric of { name : string; kind : metric_kind }
| Declare_metric of { name : string; kind : metric_kind }
type label_name = string
type t = {
service : string;
get_time : unit -> Mtime.t;
backend : backend;
context : (string * string) list;
on_backend_error : backend_op -> exn -> unit;
metric_declarations : (string, metric_declaration) Hashtbl.t;
metric_mutex : Mutex.t;
}
type span = {
sp_ctx : Obs_trace.t;
sp_log_buf : log_entry list ref;
sp_closed : bool ref;
sp_now : unit -> int64;
}
let now_ns t = Mtime.to_uint64_ns (t.get_time ())
let level_string = function
| Debug -> "debug" | Info -> "info" | Warn -> "warn" | Error -> "error"
let log_entry_fields entry =
[("log.level", level_string entry.level); ("log.msg", entry.message)]
@ entry.fields
let log_entries_fields entries =
List.concat_map log_entry_fields entries
let noop = {
emit_span = (fun _ -> ());
emit_metric = (fun _ -> ());
declare_metric = (fun _ -> ());
}
let pp_kv pairs =
String.concat " " (List.map (fun (k, v) -> k ^ "=" ^ String.escaped v) pairs)
let stdout =
let pp_trace ctx =
let (hi, lo) = ctx.Obs_trace.trace_id in
Printf.sprintf "trace=%016Lx%016Lx span=%016Lx" hi lo ctx.Obs_trace.span_id
in
{ emit_span = (fun e ->
let dur_ms = Int64.(to_float (sub e.end_ns e.start_ns)) /. 1e6 in
let status = match e.status with `Ok -> "ok" | `Error s -> "error:" ^ s in
let fields = log_entries_fields e.log_entries in
Printf.printf "SPAN svc=%s name=%s %s status=%s dur=%.2fms%s\n%!"
e.service e.name (pp_trace e.trace_ctx) status dur_ms
(if fields = [] then "" else " | " ^ pp_kv fields));
emit_metric = (fun e ->
let kind = match e.kind with
| `Counter n -> Printf.sprintf "counter=%d" n
| `Gauge f -> Printf.sprintf "gauge=%g" f
| `Histogram f -> Printf.sprintf "hist=%g" f
in
Printf.printf "METRIC svc=%s name=%s %s%s%s\n%!"
e.service e.name kind
(if e.labels = [] then "" else " labels={" ^ pp_kv e.labels ^ "}")
(if e.context = [] then "" else " ctx={" ^ pp_kv e.context ^ "}"));
declare_metric = (fun _ -> ());
}
let string_of_metric_kind = function
| `Counter -> "counter"
| `Gauge -> "gauge"
| `Histogram -> "histogram"
let string_of_backend_op = function
| Emit_span { name } -> Printf.sprintf "emit_span name=%S" name
| Emit_metric { name; kind } ->
Printf.sprintf "emit_metric name=%S kind=%s" name (string_of_metric_kind kind)
| Declare_metric { name; kind } ->
Printf.sprintf "declare_metric name=%S kind=%s" name (string_of_metric_kind kind)
let default_backend_error op exn =
Printf.eprintf "Obs_eio: backend_error op=%s exn=%S\n%!"
(string_of_backend_op op) (Printexc.to_string exn)
let report_backend_error t op exn =
try t.on_backend_error op exn with
| Eio.Cancel.Cancelled _ as exn -> raise exn
| (Out_of_memory | Stack_overflow | Sys.Break) as exn -> raise exn
| handler_exn ->
Printf.eprintf
"Obs_eio: backend_error_handler_raised op=%s handler_exn=%S original_exn=%S\n%!"
(string_of_backend_op op)
(Printexc.to_string handler_exn)
(Printexc.to_string exn)
let safe_call ~on_error f =
try f () with
| Eio.Cancel.Cancelled _ as exn -> raise exn
| (Out_of_memory | Stack_overflow | Sys.Break) as exn -> raise exn
| exn -> on_error exn
let compose_call first_error f =
try f () with
| Eio.Cancel.Cancelled _ as exn -> raise exn
| (Out_of_memory | Stack_overflow | Sys.Break) as exn -> raise exn
| exn ->
if !first_error = None then first_error := Some exn
let compose a b = {
emit_span = (fun e ->
let first_error = ref None in
compose_call first_error (fun () -> a.emit_span e);
compose_call first_error (fun () -> b.emit_span e);
match !first_error with Some exn -> raise exn | None -> ());
emit_metric = (fun e ->
let first_error = ref None in
compose_call first_error (fun () -> a.emit_metric e);
compose_call first_error (fun () -> b.emit_metric e);
match !first_error with Some exn -> raise exn | None -> ());
declare_metric = (fun d ->
let first_error = ref None in
compose_call first_error (fun () -> a.declare_metric d);
compose_call first_error (fun () -> b.declare_metric d);
match !first_error with Some exn -> raise exn | None -> ());
}
let create ~service ~mono_clock ?(on_backend_error = default_backend_error) ~backend () = {
service;
get_time = (fun () -> Eio.Time.Mono.now mono_clock);
backend;
context = [];
on_backend_error;
metric_declarations = Hashtbl.create 8;
metric_mutex = Mutex.create ();
}
let with_context t =
let merged = List.fold_left (fun acc (k, v) ->
(k, v) :: List.filter (fun (k2, _) -> k2 <> k) acc
) t.context extra in
{ t with context = merged }
let with_span t ?parent name f =
let sp_ctx = match parent with
| None -> Obs_trace.generate ()
| Some p -> Obs_trace.child_span p
in
let sp_start = now_ns t in
let sp = { sp_ctx; sp_log_buf = ref []; sp_closed = ref false; sp_now = (fun () -> now_ns t) } in
let emit status =
let end_ns = now_ns t in
let log_entries = List.rev !(sp.sp_log_buf) in
sp.sp_closed := true;
safe_call
~on_error:(report_backend_error t (Emit_span { name }))
(fun () ->
t.backend.emit_span {
trace_ctx = sp_ctx; name; service = t.service;
start_ns = sp_start; end_ns;
status;
log_entries;
context = t.context;
})
in
match f sp with
| v -> emit `Ok; v
| exception exn ->
let msg = Printexc.to_string exn in
(match emit (`Error msg) with
| () -> raise exn
| exception emit_exn ->
(match exn with
| Eio.Cancel.Cancelled _ -> ()
| _ ->
Printf.eprintf
"Obs_eio: with_span %S: application exception %s was about to \
close the span, but emit_span itself raised %s first\n%!"
name msg (Printexc.to_string emit_exn));
raise emit_exn)
let log sp level ?(fields = []) message =
if !(sp.sp_closed) then
invalid_arg
"Obs_eio.log: span is already closed — a span value must not be used, or \
escape to another fiber/domain, after its with_span callback returns";
let timestamp_ns = sp.sp_now () in
sp.sp_log_buf := { timestamp_ns; level; message; fields } :: !(sp.sp_log_buf)
let log_standalone t ?parent level ?(fields = []) message =
with_span t ?parent "log" (fun sp -> log sp level ~fields message)
let current_trace_context sp = sp.sp_ctx
let is_metric_initial_char = function
| 'A' .. 'Z' | 'a' .. 'z' | '_' | ':' -> true
| _ -> false
let is_label_initial_char = function
| 'A' .. 'Z' | 'a' .. 'z' | '_' -> true
| _ -> false
let is_metric_char = function
| 'A' .. 'Z' | 'a' .. 'z' | '0' .. '9' | '_' | ':' -> true
| _ -> false
let is_label_char = function
| 'A' .. 'Z' | 'a' .. 'z' | '0' .. '9' | '_' -> true
| _ -> false
let validate_name ~kind ~is_initial ~is_char name =
if name = "" then
invalid_arg ("Obs_eio." ^ kind ^ ": name must not be empty");
if not (is_initial name.[0]) then
invalid_arg
(Printf.sprintf "Obs_eio.%s: invalid Prometheus name %S" kind name);
String.iter (fun c ->
if not (is_char c) then
invalid_arg
(Printf.sprintf "Obs_eio.%s: invalid Prometheus name %S" kind name)
) name;
name
let metric_name name =
validate_name ~kind:"metric_name"
~is_initial:is_metric_initial_char
~is_char:is_metric_char
name
let label_name name =
let name =
validate_name ~kind:"label_name"
~is_initial:is_label_initial_char
~is_char:is_label_char
name
in
if String.length name >= 2 && name.[0] = '_' && name.[1] = '_' then
invalid_arg
(Printf.sprintf
"Obs_eio.label_name: %S starts with \"__\", reserved for Prometheus's \
own internal use (e.g. __name__)" name);
name
let label_name_to_string name = name
let duplicate_name names =
let rec loop seen = function
| [] -> None
| name :: rest ->
if List.mem name seen then Some name else loop (name :: seen) rest
in
loop [] names
let validate_label_names label_names =
List.iter (fun name -> ignore (label_name name)) label_names;
(match duplicate_name label_names with
| None -> ()
| Some name ->
invalid_arg
(Printf.sprintf "Obs_eio.register_metric: duplicate label name %S" name));
label_names
let validate_metric_labels ~name ~label_names labels =
let emitted_label_names = List.map fst labels in
match duplicate_name emitted_label_names with
| Some label ->
invalid_arg
(Printf.sprintf "Obs_eio.emit_metric %S: duplicate label %S" name label)
| None ->
let missing =
List.filter (fun label -> not (List.mem_assoc label labels)) label_names
in
let =
List.filter
(fun label -> not (List.mem label label_names))
emitted_label_names
in
match missing, extra with
| [], [] -> ()
| label :: _, _ ->
invalid_arg
(Printf.sprintf "Obs_eio.emit_metric %S: missing label %S" name label)
| [], label :: _ ->
invalid_arg
(Printf.sprintf "Obs_eio.emit_metric %S: extra label %S" name label)
let metric_event_kind (event : metric_event) =
match event.kind with
| `Counter _ -> `Counter
| `Gauge _ -> `Gauge
| `Histogram _ -> `Histogram
let emit_metric t event =
let kind = metric_event_kind event in
safe_call
~on_error:(report_backend_error t (Emit_metric { name = event.name; kind }))
(fun () -> t.backend.emit_metric event)
let sorted_label_names names = List.sort String.compare names
let validate_declaration t (d : metric_declaration) =
Mutex.lock t.metric_mutex;
Fun.protect ~finally:(fun () -> Mutex.unlock t.metric_mutex) (fun () ->
match Hashtbl.find_opt t.metric_declarations d.declaration_name with
| None ->
Hashtbl.add t.metric_declarations d.declaration_name
{ d with declaration_label_names = sorted_label_names d.declaration_label_names }
| Some existing ->
let label_names = sorted_label_names d.declaration_label_names in
if existing.declaration_kind <> d.declaration_kind then
invalid_arg
(Printf.sprintf
"Obs_eio.register_metric %S: already registered as %s, got %s"
d.declaration_name
(string_of_metric_kind existing.declaration_kind)
(string_of_metric_kind d.declaration_kind));
if existing.declaration_label_names <> label_names then
invalid_arg
(Printf.sprintf
"Obs_eio.register_metric %S: conflicting label names"
d.declaration_name);
if existing.declaration_help <> d.declaration_help then
invalid_arg
(Printf.sprintf
"Obs_eio.register_metric %S: conflicting help text"
d.declaration_name))
let declare_metric t ~name ~help ~kind ~label_names =
let declaration =
{ declaration_name = name; declaration_help = help; declaration_kind = kind;
declaration_label_names = label_names; declaration_service = t.service }
in
validate_declaration t declaration;
safe_call
~on_error:(report_backend_error t (Declare_metric { name; kind }))
(fun () -> t.backend.declare_metric declaration)
let register_counter t ~name ~help ~label_names : counter_fn =
let name = metric_name name in
let label_names = validate_label_names label_names in
declare_metric t ~name ~help ~kind:`Counter ~label_names;
fun ?(labels = []) value ->
validate_metric_labels ~name ~label_names labels;
if value < 0 then
invalid_arg
(Printf.sprintf "Obs_eio.emit_metric %S: counter delta must be >= 0, got %d" name value);
emit_metric t {
name; help; kind = `Counter value; labels; context = t.context; service = t.service;
}
let register_gauge t ~name ~help ~label_names : gauge_fn =
let name = metric_name name in
let label_names = validate_label_names label_names in
declare_metric t ~name ~help ~kind:`Gauge ~label_names;
fun ?(labels = []) value ->
validate_metric_labels ~name ~label_names labels;
emit_metric t {
name; help; kind = `Gauge value; labels; context = t.context; service = t.service;
}
let register_histogram t ~name ~help ~label_names : histogram_fn =
let name = metric_name name in
if List.mem "le" label_names then
invalid_arg
(Printf.sprintf
"Obs_eio.register_histogram %S: label name \"le\" is reserved for the \
Prometheus bucket boundary" name);
let label_names = validate_label_names label_names in
declare_metric t ~name ~help ~kind:`Histogram ~label_names;
fun ?(labels = []) value ->
validate_metric_labels ~name ~label_names labels;
if value < 0.0 then
invalid_arg
(Printf.sprintf "Obs_eio.emit_metric %S: histogram observation must be >= 0, got %g" name value);
emit_metric t {
name; help; kind = `Histogram value; labels; context = t.context; service = t.service;
}
let register_counter_and_histogram t
~counter_name ~counter_help ~counter_labels
~histogram_name ~histogram_help ~histogram_labels =
let counter =
register_counter t
~name:counter_name
~help:counter_help
~label_names:counter_labels
in
let histogram =
register_histogram t
~name:histogram_name
~help:histogram_help
~label_names:histogram_labels
in
(counter, histogram)