Skip to content

Commit

Permalink
eio local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
c-cube committed Sep 6, 2024
1 parent 1bcea95 commit a9971e4
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 43 deletions.
37 changes: 0 additions & 37 deletions src/ambient-context/eio/ambient_context_eio.ml

This file was deleted.

7 changes: 7 additions & 0 deletions src/ambient-context/eio/dune
Original file line number Diff line number Diff line change
@@ -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))
6 changes: 0 additions & 6 deletions src/ambient-context/eio/dune.tmp

This file was deleted.

40 changes: 40 additions & 0 deletions src/ambient-context/eio/opentelemetry_ambient_context_eio.ml
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 2 additions & 0 deletions src/ambient-context/eio/opentelemetry_ambient_context_eio.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
val storage : unit -> Opentelemetry_ambient_context.storage
(** Storage using Eio's fibers local storage *)

0 comments on commit a9971e4

Please sign in to comment.