From a9971e4d416442aaf279970b684be0a2838317b6 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Fri, 6 Sep 2024 09:48:06 -0400 Subject: [PATCH] eio local storage --- .../eio/ambient_context_eio.ml | 37 ----------------- src/ambient-context/eio/dune | 7 ++++ src/ambient-context/eio/dune.tmp | 6 --- .../eio/opentelemetry_ambient_context_eio.ml | 40 +++++++++++++++++++ .../eio/opentelemetry_ambient_context_eio.mli | 2 + 5 files changed, 49 insertions(+), 43 deletions(-) delete mode 100644 src/ambient-context/eio/ambient_context_eio.ml create mode 100644 src/ambient-context/eio/dune delete mode 100644 src/ambient-context/eio/dune.tmp create mode 100644 src/ambient-context/eio/opentelemetry_ambient_context_eio.ml create mode 100644 src/ambient-context/eio/opentelemetry_ambient_context_eio.mli diff --git a/src/ambient-context/eio/ambient_context_eio.ml b/src/ambient-context/eio/ambient_context_eio.ml deleted file mode 100644 index 642be8c8..00000000 --- a/src/ambient-context/eio/ambient_context_eio.ml +++ /dev/null @@ -1,37 +0,0 @@ -module TLS = Ambient_context_thread_local.Thread_local -module Hmap = Ambient_context.Hmap -module Fiber = Eio.Fiber - -let _internal_key : Hmap.t Fiber.key = Fiber.create_key () -let ( let* ) = Option.bind - -module M = struct - let name = "Storage_eio" - let[@inline] get_map () = Fiber.get _internal_key - let[@inline] with_map m cb = Fiber.with_binding _internal_key m cb - let create_key = Hmap.Key.create - - let get k = - let* context = get_map () in - Hmap.find k context - - - let with_binding k v cb = - let new_context = - match get_map () with - | None -> Hmap.singleton k v - | Some old_context -> Hmap.add k v old_context - in - with_map new_context cb - - - let without_binding k cb = - let new_context = - match get_map () with - | None -> Hmap.empty - | Some old_context -> Hmap.rem k old_context - in - with_map new_context cb -end - -let storage () : Ambient_context.storage = (module M) diff --git a/src/ambient-context/eio/dune b/src/ambient-context/eio/dune new file mode 100644 index 00000000..f3f76be7 --- /dev/null +++ b/src/ambient-context/eio/dune @@ -0,0 +1,7 @@ +(library + (name opentelemetry_ambient_context_eio) + (public_name opentelemetry.ambient-context.eio) + (synopsis + "Storage backend for ambient-context using Eio's fibre-local storage") + (optional) ; eio + (libraries eio hmap opentelemetry.ambient-context thread-local-storage)) diff --git a/src/ambient-context/eio/dune.tmp b/src/ambient-context/eio/dune.tmp deleted file mode 100644 index cb379f9a..00000000 --- a/src/ambient-context/eio/dune.tmp +++ /dev/null @@ -1,6 +0,0 @@ -(library - (name ambient_context_eio) - (synopsis - "Storage backend for ambient-context using Eio's fibre-local storage") - (public_name ambient-context-eio) - (libraries eio ambient-context ambient-context.thread_local)) diff --git a/src/ambient-context/eio/opentelemetry_ambient_context_eio.ml b/src/ambient-context/eio/opentelemetry_ambient_context_eio.ml new file mode 100644 index 00000000..41f682e5 --- /dev/null +++ b/src/ambient-context/eio/opentelemetry_ambient_context_eio.ml @@ -0,0 +1,40 @@ +module TLS = Thread_local_storage +module Fiber = Eio.Fiber + +open struct + let _internal_key : Hmap.t Fiber.key = Fiber.create_key () + + let ( let* ) = Option.bind +end + +module M = struct + let name = "Storage_eio" + + let[@inline] get_map () = Fiber.get _internal_key + + let[@inline] with_map m cb = Fiber.with_binding _internal_key m cb + + let create_key = Hmap.Key.create + + let get k = + let* context = get_map () in + Hmap.find k context + + let with_binding k v cb = + let new_context = + match get_map () with + | None -> Hmap.singleton k v + | Some old_context -> Hmap.add k v old_context + in + with_map new_context cb + + let without_binding k cb = + let new_context = + match get_map () with + | None -> Hmap.empty + | Some old_context -> Hmap.rem k old_context + in + with_map new_context cb +end + +let storage () : Opentelemetry_ambient_context.storage = (module M) diff --git a/src/ambient-context/eio/opentelemetry_ambient_context_eio.mli b/src/ambient-context/eio/opentelemetry_ambient_context_eio.mli new file mode 100644 index 00000000..ac5cf8ba --- /dev/null +++ b/src/ambient-context/eio/opentelemetry_ambient_context_eio.mli @@ -0,0 +1,2 @@ +val storage : unit -> Opentelemetry_ambient_context.storage +(** Storage using Eio's fibers local storage *)