From c950ffc885dbd6858cac2b70386dd40eb9bd33a1 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Fri, 15 Apr 2022 08:59:13 -0400 Subject: [PATCH] feat: use a ref for Globals.global_attributes sometimes it's useful to be able to modify them from the code itself. For example a client or service might generate a unique Uuidm for its whole lifetime, and add that as an attribute to later filter on it. --- src/opentelemetry.ml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/opentelemetry.ml b/src/opentelemetry.ml index 5c5a0e7c..6f210765 100644 --- a/src/opentelemetry.ml +++ b/src/opentelemetry.ml @@ -278,12 +278,15 @@ module Globals = struct ~version:"%%VERSION%%" ~name:"ocaml-opentelemetry" () - (** Global attributes, set via OTEL_RESOURCE_ATTRIBUTES *) - let global_attributes : key_value list = + (** Global attributes, initially set + via OTEL_RESOURCE_ATTRIBUTES and modifiable + by the user code. They will be attached to each outgoing metrics/traces. *) + let global_attributes : key_value list ref = 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 + ref @@ try Sys.getenv "OTEL_RESOURCE_ATTRIBUTES" |> String.split_on_char ',' |> List.map parse_pair @@ -292,7 +295,7 @@ module Globals = struct (* 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 + List.rev_append (List.filter not_redundant !global_attributes) into let mk_attributes ?(service_name = !service_name) ?(attrs=[]) () : _ list = let l = List.map _conv_key_value attrs in