package opentelemetry-client

  1. Overview
  2. Docs

Source file export_error.ml

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
(** Error that can occur during export *)

type attempt_descr = string

type t =
  [ `Status of int * Opentelemetry.Proto.Status.status * attempt_descr
  | `Failure of string
  | `Sysbreak
  ]

let str_to_hex (s : string) : string =
  Opentelemetry_util.Util_bytes_.bytes_to_hex (Bytes.unsafe_of_string s)

(** Report the error on stderr. *)
let report_err ~level:(provided_level : [ `Debug | `Warning | `Auto ]) (err : t)
    : unit =
  let compute_level lvl =
    match provided_level with
    | `Debug -> Opentelemetry.Self_debug.Debug
    | `Warning -> Opentelemetry.Self_debug.Warning
    | `Auto -> lvl
  in
  match err with
  | `Sysbreak ->
    Opentelemetry.Self_debug.log (compute_level Info) (fun () ->
        "opentelemetry: ctrl-c captured, stopping")
  | `Failure msg ->
    Opentelemetry.Self_debug.log (compute_level Error) (fun () ->
        Printf.sprintf "opentelemetry: export failed:\n%s" msg)
  | `Status
      ( code,
        {
          Opentelemetry.Proto.Status.code = scode;
          message;
          details;
          _presence = _;
        },
        descr ) ->
    Opentelemetry.Self_debug.log (compute_level Error) (fun () ->
        let pp_details out l =
          List.iter
            (fun s -> Format.fprintf out "%S;@ " (Bytes.unsafe_to_string s))
            l
        in

        Format.asprintf
          "@[<2>opentelemetry: export failed with@ http code=%d@ attempt: %s@ \
           status {@[code=%ld;@ message=%S;@ details=[@[%a@]]@]}@]"
          code descr scode
          (Bytes.unsafe_to_string message)
          pp_details details)

let decode_invalid_http_response ~attempt_descr ~code ~url (body : string) : t =
  try
    let dec = Pbrt.Decoder.of_string body in
    let status = Opentelemetry.Proto.Status.decode_pb_status dec in
    `Status (code, status, attempt_descr)
  with e ->
    let bt = Printexc.get_backtrace () in
    `Failure
      (Printf.sprintf
         "http server at %s returned code %d;\n\
          trying to decode the body as protobuf failed:\n\
          %s\n\
          raw HTTP body (hex): %s\n\
          %s"
         url code (Printexc.to_string e) (str_to_hex body) bt)