fix: critical bugs found in code review

Bug #1: Fix worker count logic in generic_consumer
- Was: min 2 (max 500 n_workers) - always created 2 workers
- Now: max 2 (min 500 n_workers) - properly clamps between 2-500
- Impact: Worker configuration was completely ignored

Bug #2: Handle missing dot in __FUNCTION__ name
- Added exception handling for String.rindex in trace span creation
- Prevents crash when tracing top-level or non-module functions
- Uses option type for module_path when no dot is present
- Scoped try/catch to only parsing logic
This commit is contained in:
Simon Cruanes 2026-02-08 06:05:49 +00:00
parent c29ac75a82
commit 1ebd474423
3 changed files with 27 additions and 12 deletions

View file

@ -194,7 +194,7 @@ end = struct
in in
(* start workers *) (* start workers *)
let n_workers = min 2 (max 500 self.config.n_workers) in let n_workers = max 2 (min 500 self.config.n_workers) in
ignore (Atomic.fetch_and_add self.n_workers n_workers : int); ignore (Atomic.fetch_and_add self.n_workers n_workers : int);
for _i = 1 to n_workers do for _i = 1 to n_workers do

View file

@ -71,17 +71,27 @@ open struct
(* add more data if [__FUNCTION__] is present *) (* add more data if [__FUNCTION__] is present *)
(match __FUNCTION__ with (match __FUNCTION__ with
| Some __FUNCTION__ when OTEL.Span.is_not_dummy otel_sp -> | Some __FUNCTION__ when OTEL.Span.is_not_dummy otel_sp ->
let last_dot = String.rindex __FUNCTION__ '.' in let function_name, module_path =
let module_path = String.sub __FUNCTION__ 0 last_dot in try
let function_name = let last_dot = String.rindex __FUNCTION__ '.' in
String.sub __FUNCTION__ (last_dot + 1) let module_path = String.sub __FUNCTION__ 0 last_dot in
(String.length __FUNCTION__ - last_dot - 1) let function_name =
String.sub __FUNCTION__ (last_dot + 1)
(String.length __FUNCTION__ - last_dot - 1)
in
function_name, Some module_path
with Not_found ->
(* __FUNCTION__ has no dot, use it as-is *)
__FUNCTION__, None
in in
OTEL.Span.add_attrs otel_sp let attrs =
[ ("code.function", `String function_name)
"code.function", `String function_name; ::
"code.namespace", `String module_path; (match module_path with
] | Some module_path -> [ "code.namespace", `String module_path ]
| None -> [])
in
OTEL.Span.add_attrs otel_sp attrs
| _ -> ()); | _ -> ());
Span_otel otel_sp Span_otel otel_sp

View file

@ -1,4 +1,9 @@
(tests (tests
(names test_implicit_scope_sync) (names test_implicit_scope_sync)
(package opentelemetry-client-cohttp-lwt) (package opentelemetry-client-cohttp-lwt)
(libraries threads alcotest opentelemetry unix opentelemetry-client-cohttp-lwt)) (libraries
threads
alcotest
opentelemetry
unix
opentelemetry-client-cohttp-lwt))