add more metadata

This commit is contained in:
Simon Cruanes 2022-03-21 10:10:50 -04:00
parent 064951c368
commit 06727ff588
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
3 changed files with 50 additions and 11 deletions

View file

@ -4,6 +4,8 @@ open Opentelemetry
module Span_id = Span_id module Span_id = Span_id
module Trace_id = Trace_id module Trace_id = Trace_id
module Span = Span module Span = Span
module Globals = Globals
module Timestamp_ns = Timestamp_ns
module Trace = struct module Trace = struct
open Proto.Trace open Proto.Trace

View file

@ -153,6 +153,36 @@ end = struct
let of_bytes b = assert(Bytes.length b=8); b let of_bytes b = assert(Bytes.length b=8); b
end end
(** Process-wide metadata, environment variables, etc. *)
module Globals = struct
open Proto.Common
let service_name = ref "unknown_service"
(** Main service name metadata *)
let service_namespace = ref None
(** Namespace for the service *)
let instrumentation_library =
default_instrumentation_library
~name:"ocaml-opentelemetry" () (* TODO: version, perhaps with dune subst? *)
(** Global attributes, set via OTEL_RESOURCE_ATTRIBUTE *)
let global_attributes : key_value list =
let parse_pair s = match String.split_on_char '=' s with
| [a;b] -> default_key_value ~key:a ~value:(Some (String_value b)) ()
| _ -> failwith (Printf.sprintf "invalid attribute: %S" s)
in
try
Sys.getenv "OTEL_RESOURCE_ATTRIBUTE" |> String.split_on_char ','
|> List.map parse_pair
with _ -> []
(* add global attributes to this list *)
let merge_global_attributes_ into : _ list =
let not_redundant kv = List.for_all (fun kv' -> kv.key <> kv'.key) into in
List.rev_append (List.filter not_redundant global_attributes) into
end
(* TODO: Event.t, use it in Span *) (* TODO: Event.t, use it in Span *)
@ -241,7 +271,7 @@ end = struct
?(kind=Span_kind_unspecified) ?(kind=Span_kind_unspecified)
?(id=Span_id.create()) ?(id=Span_id.create())
?trace_state ?trace_state
?service_name ?(service_name= !Globals.service_name)
?(attrs=[]) ?(attrs=[])
?status ?status
~trace_id ?parent ?(links=[]) ~trace_id ?parent ?(links=[])
@ -263,13 +293,17 @@ end = struct
default_key_value ~key:k ~value ()) default_key_value ~key:k ~value ())
attrs attrs
in in
let l = match service_name with let l =
default_key_value ~key:"service.name"
~value:(Some (String_value service_name)) () :: l
in
let l = match !Globals.service_namespace with
| None -> l | None -> l
| Some v -> | Some v ->
default_key_value ~key:"service.name" default_key_value ~key:"service.namespace"
~value:(Some (String_value v)) () :: l ~value:(Some (String_value v)) () :: l
in in
l l |> Globals.merge_global_attributes_
in in
let links = let links =
@ -305,7 +339,9 @@ module Trace = struct
(** Sync emitter *) (** Sync emitter *)
let emit (spans:span list) : unit = let emit (spans:span list) : unit =
let ils = let ils =
default_instrumentation_library_spans ~spans () in default_instrumentation_library_spans
~instrumentation_library:(Some Globals.instrumentation_library)
~spans () in
let rs = default_resource_spans ~instrumentation_library_spans:[ils] () in let rs = default_resource_spans ~instrumentation_library_spans:[ils] () in
Collector.send_trace [rs] ~over:(fun () -> ()) ~ret:(fun () -> ()) Collector.send_trace [rs] ~over:(fun () -> ()) ~ret:(fun () -> ())
@ -396,7 +432,9 @@ module Metrics = struct
(** Emit some metrics to the collector (sync). *) (** Emit some metrics to the collector (sync). *)
let emit (l:t list) : unit = let emit (l:t list) : unit =
let lm = let lm =
default_instrumentation_library_metrics ~metrics:l () in default_instrumentation_library_metrics
~instrumentation_library:(Some Globals.instrumentation_library)
~metrics:l () in
let rm = default_resource_metrics let rm = default_resource_metrics
~instrumentation_library_metrics:[lm] () in ~instrumentation_library_metrics:[lm] () in
Collector.send_metrics [rm] ~over:ignore ~ret:ignore Collector.send_metrics [rm] ~over:ignore ~ret:ignore

View file

@ -3,20 +3,18 @@ module T = Opentelemetry
let spf = Printf.sprintf let spf = Printf.sprintf
let (let@) f x = f x let (let@) f x = f x
let service_name = "t1"
let run () = let run () =
Printf.printf "collector is on %S\n%!" (Opentelemetry_client_ocurl.get_url()); Printf.printf "collector is on %S\n%!" (Opentelemetry_client_ocurl.get_url());
let i = ref 0 in let i = ref 0 in
while true do while true do
let@ (tr,sp) = T.Trace.with_ ~kind:T.Span.Span_kind_producer let@ (tr,sp) = T.Trace.with_ ~kind:T.Span.Span_kind_producer
~service_name "loop.outer" ~attrs:["i", `Int !i] in "loop.outer" ~attrs:["i", `Int !i] in
for j=0 to 4 do for j=0 to 4 do
let@ (_,sp) = T.Trace.with_ ~kind:T.Span.Span_kind_internal let@ (_,sp) = T.Trace.with_ ~kind:T.Span.Span_kind_internal
~trace_id:tr ~parent:sp ~trace_id:tr ~parent:sp
~service_name ~attrs:["j", `Int j] ~attrs:["j", `Int j]
"loop.inner" in "loop.inner" in
Unix.sleepf 2.; Unix.sleepf 2.;
@ -34,7 +32,6 @@ let run () =
let@ _ = let@ _ =
T.Trace.with_ ~kind:T.Span.Span_kind_internal T.Trace.with_ ~kind:T.Span.Span_kind_internal
~trace_id:tr ~parent:sp ~trace_id:tr ~parent:sp
~service_name
"alloc" in "alloc" in
(* allocate some stuff *) (* allocate some stuff *)
let _arr = Sys.opaque_identity @@ Array.make (25 * 25551) 42.0 in let _arr = Sys.opaque_identity @@ Array.make (25 * 25551) 42.0 in
@ -47,4 +44,6 @@ let run () =
done done
let () = let () =
T.Globals.service_name := Some "t1";
T.Globals.service_namespace := Some "ocaml-otel.test";
Opentelemetry_client_ocurl.with_setup run Opentelemetry_client_ocurl.with_setup run