mirror of
https://github.com/ocaml-tracing/ocaml-opentelemetry.git
synced 2026-03-09 12:23:32 -04:00
feat(cohttp): pass scope to callback
This commit is contained in:
parent
75f1612ab0
commit
e17fb1dfb7
1 changed files with 12 additions and 8 deletions
|
|
@ -9,15 +9,19 @@ module Server : sig
|
||||||
Use it like this:
|
Use it like this:
|
||||||
|
|
||||||
let my_server callback =
|
let my_server callback =
|
||||||
let callback =
|
let callback_traced =
|
||||||
Opentelemetry_cohttp_lwt.Server.trace ~service_name:"my-service" callback in
|
Opentelemetry_cohttp_lwt.Server.trace
|
||||||
Cohttp_lwt_unix.Server.create ~mode:(`TCP (`Port 8080))
|
~service_name:"my-service"
|
||||||
(Server.make () ~callback)
|
(fun _scope -> callback)
|
||||||
|
in
|
||||||
|
Cohttp_lwt_unix.Server.create
|
||||||
|
~mode:(`TCP (`Port 8080))
|
||||||
|
(Server.make () ~callback:callback_traced)
|
||||||
*)
|
*)
|
||||||
val trace :
|
val trace :
|
||||||
service_name:string ->
|
service_name:string ->
|
||||||
?attrs:Otel.Span.key_value list ->
|
?attrs:Otel.Span.key_value list ->
|
||||||
('conn -> Request.t -> 'body -> (Response.t * 'body) Lwt.t) ->
|
(Otel.Trace.scope -> 'conn -> Request.t -> 'body -> (Response.t * 'body) Lwt.t) ->
|
||||||
'conn -> Request.t -> 'body -> (Response.t * 'body) Lwt.t
|
'conn -> Request.t -> 'body -> (Response.t * 'body) Lwt.t
|
||||||
end = struct
|
end = struct
|
||||||
let span_attrs (req : Request.t) =
|
let span_attrs (req : Request.t) =
|
||||||
|
|
@ -42,7 +46,7 @@ end = struct
|
||||||
[ ("http.request.header.referer", `String r) ] )
|
[ ("http.request.header.referer", `String r) ] )
|
||||||
]
|
]
|
||||||
|
|
||||||
let trace_context_of_headers req =
|
let trace_context_of_req req =
|
||||||
let module Traceparent = Otel.Trace_context.Traceparent in
|
let module Traceparent = Otel.Trace_context.Traceparent in
|
||||||
match Header.get (Request.headers req) Traceparent.name with
|
match Header.get (Request.headers req) Traceparent.name with
|
||||||
| None -> None, None
|
| None -> None, None
|
||||||
|
|
@ -53,7 +57,7 @@ end = struct
|
||||||
|
|
||||||
let trace ~service_name ?(attrs=[]) callback =
|
let trace ~service_name ?(attrs=[]) callback =
|
||||||
fun conn req body ->
|
fun conn req body ->
|
||||||
let trace_id, parent_id = trace_context_of_headers req in
|
let trace_id, parent_id = trace_context_of_req req in
|
||||||
let open Lwt.Syntax in
|
let open Lwt.Syntax in
|
||||||
Otel_lwt.Trace.with_
|
Otel_lwt.Trace.with_
|
||||||
~service_name
|
~service_name
|
||||||
|
|
@ -63,7 +67,7 @@ end = struct
|
||||||
?parent:parent_id
|
?parent:parent_id
|
||||||
?trace_id
|
?trace_id
|
||||||
(fun scope ->
|
(fun scope ->
|
||||||
let* (res, body) = callback conn req body in
|
let* (res, body) = callback scope conn req body in
|
||||||
Otel.Trace.add_attrs scope (fun () ->
|
Otel.Trace.add_attrs scope (fun () ->
|
||||||
let code = Response.status res in
|
let code = Response.status res in
|
||||||
let code = Code.code_of_status code in
|
let code = Code.code_of_status code in
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue