mirror of
https://github.com/ocaml-tracing/ocaml-opentelemetry.git
synced 2026-03-10 20:48:35 -04:00
feat: set global attributes on ResourceSpans/ResourceMetrics
This commit is contained in:
parent
a683232b5a
commit
eadb95e8fb
2 changed files with 46 additions and 49 deletions
|
|
@ -13,11 +13,9 @@ module Trace = struct
|
||||||
include Trace
|
include Trace
|
||||||
|
|
||||||
(** Emit asynchronously *)
|
(** Emit asynchronously *)
|
||||||
let emit (spans:span list) : unit Lwt.t =
|
let emit ?service_name ?attrs (spans:span list) : unit Lwt.t =
|
||||||
let fut, wake = Lwt.wait() in
|
let fut, wake = Lwt.wait() in
|
||||||
let ils =
|
let rs = make_resource_spans ?service_name ?attrs spans in
|
||||||
default_instrumentation_library_spans ~spans () in
|
|
||||||
let rs = default_resource_spans ~instrumentation_library_spans:[ils] () in
|
|
||||||
Collector.send_trace [rs]
|
Collector.send_trace [rs]
|
||||||
~over:(fun () -> Lwt.wakeup_later wake ())
|
~over:(fun () -> Lwt.wakeup_later wake ())
|
||||||
~ret:(fun () -> fut)
|
~ret:(fun () -> fut)
|
||||||
|
|
@ -36,11 +34,11 @@ module Trace = struct
|
||||||
let span, _ =
|
let span, _ =
|
||||||
Span.create
|
Span.create
|
||||||
?kind ~trace_id ?parent ?links ~id:span_id
|
?kind ~trace_id ?parent ?links ~id:span_id
|
||||||
?trace_state ?service_name ?attrs
|
?trace_state ?attrs
|
||||||
~start_time ~end_time:(Timestamp_ns.now_unix_ns())
|
~start_time ~end_time:(Timestamp_ns.now_unix_ns())
|
||||||
~status
|
~status
|
||||||
name in
|
name in
|
||||||
emit [span]
|
emit ?service_name [span]
|
||||||
in
|
in
|
||||||
Lwt.catch
|
Lwt.catch
|
||||||
(fun () ->
|
(fun () ->
|
||||||
|
|
@ -57,12 +55,9 @@ module Metrics = struct
|
||||||
include Metrics
|
include Metrics
|
||||||
|
|
||||||
(** Emit some metrics to the collector. *)
|
(** Emit some metrics to the collector. *)
|
||||||
let emit (l:t list) : unit Lwt.t =
|
let emit ?attrs (l:t list) : unit Lwt.t =
|
||||||
let fut, wake = Lwt.wait() in
|
let fut, wake = Lwt.wait() in
|
||||||
let lm =
|
let rm = make_resource_metrics ?attrs l in
|
||||||
default_instrumentation_library_metrics ~metrics:l () in
|
|
||||||
let rm = default_resource_metrics
|
|
||||||
~instrumentation_library_metrics:[lm] () in
|
|
||||||
Collector.send_metrics [rm]
|
Collector.send_metrics [rm]
|
||||||
~over:(fun () -> Lwt.wakeup_later wake ())
|
~over:(fun () -> Lwt.wakeup_later wake ())
|
||||||
~ret:(fun () -> fut)
|
~ret:(fun () -> fut)
|
||||||
|
|
|
||||||
|
|
@ -266,7 +266,6 @@ module Span : sig
|
||||||
?kind:kind ->
|
?kind:kind ->
|
||||||
?id:id ->
|
?id:id ->
|
||||||
?trace_state:string ->
|
?trace_state:string ->
|
||||||
?service_name:string ->
|
|
||||||
?attrs:key_value list ->
|
?attrs:key_value list ->
|
||||||
?events:Event.t list ->
|
?events:Event.t list ->
|
||||||
?status:status ->
|
?status:status ->
|
||||||
|
|
@ -314,7 +313,6 @@ end = struct
|
||||||
?(kind=Span_kind_unspecified)
|
?(kind=Span_kind_unspecified)
|
||||||
?(id=Span_id.create())
|
?(id=Span_id.create())
|
||||||
?trace_state
|
?trace_state
|
||||||
?(service_name= !Globals.service_name)
|
|
||||||
?(attrs=[])
|
?(attrs=[])
|
||||||
?(events=[])
|
?(events=[])
|
||||||
?status
|
?status
|
||||||
|
|
@ -323,23 +321,7 @@ end = struct
|
||||||
name : t * id =
|
name : t * id =
|
||||||
let trace_id = Trace_id.to_bytes trace_id in
|
let trace_id = Trace_id.to_bytes trace_id in
|
||||||
let parent_span_id = Option.map Span_id.to_bytes parent in
|
let parent_span_id = Option.map Span_id.to_bytes parent in
|
||||||
|
let attributes = List.map _conv_key_value attrs in
|
||||||
let attributes =
|
|
||||||
let open Proto.Common in
|
|
||||||
let l = List.map _conv_key_value attrs in
|
|
||||||
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
|
|
||||||
| Some v ->
|
|
||||||
default_key_value ~key:"service.namespace"
|
|
||||||
~value:(Some (String_value v)) () :: l
|
|
||||||
in
|
|
||||||
l |> Globals.merge_global_attributes_
|
|
||||||
in
|
|
||||||
|
|
||||||
let links =
|
let links =
|
||||||
List.map
|
List.map
|
||||||
(fun (trace_id,span_id,trace_state) ->
|
(fun (trace_id,span_id,trace_state) ->
|
||||||
|
|
@ -370,13 +352,33 @@ module Trace = struct
|
||||||
|
|
||||||
type span = Span.t
|
type span = Span.t
|
||||||
|
|
||||||
(** Sync emitter *)
|
let make_resource_spans ?(service_name = !Globals.service_name) ?(attrs=[]) spans =
|
||||||
let emit (spans:span list) : unit =
|
|
||||||
let ils =
|
let ils =
|
||||||
default_instrumentation_library_spans
|
default_instrumentation_library_spans
|
||||||
~instrumentation_library:(Some Globals.instrumentation_library)
|
~instrumentation_library:(Some Globals.instrumentation_library)
|
||||||
~spans () in
|
~spans () in
|
||||||
let rs = default_resource_spans ~instrumentation_library_spans:[ils] () in
|
let attributes =
|
||||||
|
let open Proto.Common in
|
||||||
|
let l = List.map _conv_key_value attrs in
|
||||||
|
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
|
||||||
|
| Some v ->
|
||||||
|
default_key_value ~key:"service.namespace"
|
||||||
|
~value:(Some (String_value v)) () :: l
|
||||||
|
in
|
||||||
|
l |> Globals.merge_global_attributes_
|
||||||
|
in
|
||||||
|
let resource = Proto.Resource.default_resource ~attributes () in
|
||||||
|
default_resource_spans
|
||||||
|
~resource:(Some resource) ~instrumentation_library_spans:[ils] ()
|
||||||
|
|
||||||
|
(** Sync emitter *)
|
||||||
|
let emit ?service_name ?attrs (spans:span list) : unit =
|
||||||
|
let rs = make_resource_spans ?service_name ?attrs spans in
|
||||||
Collector.send_trace [rs] ~over:(fun () -> ()) ~ret:(fun () -> ())
|
Collector.send_trace [rs] ~over:(fun () -> ()) ~ret:(fun () -> ())
|
||||||
|
|
||||||
(** Scope to be used with {!with_}. *)
|
(** Scope to be used with {!with_}. *)
|
||||||
|
|
@ -410,13 +412,15 @@ module Trace = struct
|
||||||
| Ok () -> default_status ~code:Status_code_ok ()
|
| Ok () -> default_status ~code:Status_code_ok ()
|
||||||
| Error e -> default_status ~code:Status_code_error ~message:e () in
|
| Error e -> default_status ~code:Status_code_error ~message:e () in
|
||||||
let span, _ =
|
let span, _ =
|
||||||
|
(* TODO: should the attrs passed to with_ go on the Span (in Span.create) or on the ResourceSpan (in emit)?
|
||||||
|
(question also applies to Opentelemetry_lwt.Trace.with) *)
|
||||||
Span.create
|
Span.create
|
||||||
?kind ~trace_id ?parent ?links ~id:span_id
|
?kind ~trace_id ?parent ?links ~id:span_id
|
||||||
?trace_state ?service_name ?attrs ~events:scope.events
|
?trace_state ?attrs ~events:scope.events
|
||||||
~start_time ~end_time:(Timestamp_ns.now_unix_ns())
|
~start_time ~end_time:(Timestamp_ns.now_unix_ns())
|
||||||
~status
|
~status
|
||||||
name in
|
name in
|
||||||
emit [span];
|
emit ?service_name [span];
|
||||||
in
|
in
|
||||||
try
|
try
|
||||||
let x = f scope in
|
let x = f scope in
|
||||||
|
|
@ -440,11 +444,7 @@ module Metrics = struct
|
||||||
?(now=Timestamp_ns.now_unix_ns())
|
?(now=Timestamp_ns.now_unix_ns())
|
||||||
?(attrs=[])
|
?(attrs=[])
|
||||||
(d:float) : number_data_point =
|
(d:float) : number_data_point =
|
||||||
let attributes =
|
let attributes = attrs |> List.map _conv_key_value in
|
||||||
attrs
|
|
||||||
|> List.map _conv_key_value
|
|
||||||
|> Globals.merge_global_attributes_
|
|
||||||
in
|
|
||||||
default_number_data_point
|
default_number_data_point
|
||||||
?start_time_unix_nano ~time_unix_nano:now
|
?start_time_unix_nano ~time_unix_nano:now
|
||||||
~attributes
|
~attributes
|
||||||
|
|
@ -455,11 +455,7 @@ module Metrics = struct
|
||||||
?(now=Timestamp_ns.now_unix_ns())
|
?(now=Timestamp_ns.now_unix_ns())
|
||||||
?(attrs=[])
|
?(attrs=[])
|
||||||
(i:int) : number_data_point =
|
(i:int) : number_data_point =
|
||||||
let attributes =
|
let attributes = attrs |> List.map _conv_key_value in
|
||||||
attrs
|
|
||||||
|> List.map _conv_key_value
|
|
||||||
|> Globals.merge_global_attributes_
|
|
||||||
in
|
|
||||||
default_number_data_point ?start_time_unix_nano ~time_unix_nano:now
|
default_number_data_point ?start_time_unix_nano ~time_unix_nano:now
|
||||||
~attributes
|
~attributes
|
||||||
~value:(As_int (Int64.of_int i)) ()
|
~value:(As_int (Int64.of_int i)) ()
|
||||||
|
|
@ -498,16 +494,22 @@ module Metrics = struct
|
||||||
(* TODO: exemplar *)
|
(* TODO: exemplar *)
|
||||||
|
|
||||||
(** Aggregate metrics into a {!Proto.Metrics.resource_metrics} *)
|
(** Aggregate metrics into a {!Proto.Metrics.resource_metrics} *)
|
||||||
let make_resource_metrics (l:t list) : resource_metrics =
|
let make_resource_metrics ?(attrs=[]) (l:t list) : resource_metrics =
|
||||||
let lm =
|
let lm =
|
||||||
default_instrumentation_library_metrics
|
default_instrumentation_library_metrics
|
||||||
~instrumentation_library:(Some Globals.instrumentation_library)
|
~instrumentation_library:(Some Globals.instrumentation_library)
|
||||||
~metrics:l () in
|
~metrics:l () in
|
||||||
|
let attributes =
|
||||||
|
let open Proto.Common in
|
||||||
|
let l = List.map _conv_key_value attrs in
|
||||||
|
l |> Globals.merge_global_attributes_
|
||||||
|
in
|
||||||
|
let resource = Proto.Resource.default_resource ~attributes () in
|
||||||
default_resource_metrics
|
default_resource_metrics
|
||||||
~instrumentation_library_metrics:[lm] ()
|
~instrumentation_library_metrics:[lm] ~resource:(Some resource) ()
|
||||||
|
|
||||||
(** Emit some metrics to the collector (sync). *)
|
(** Emit some metrics to the collector (sync). *)
|
||||||
let emit (l:t list) : unit =
|
let emit ?attrs (l:t list) : unit =
|
||||||
let rm = make_resource_metrics l in
|
let rm = make_resource_metrics ?attrs l in
|
||||||
Collector.send_metrics [rm] ~over:ignore ~ret:ignore
|
Collector.send_metrics [rm] ~over:ignore ~ret:ignore
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue