From 0ad2d3f64178dd4ff93e0d1e80fd708ec321f4d6 Mon Sep 17 00:00:00 2001 From: Matt Bray Date: Thu, 24 Mar 2022 23:10:11 +0000 Subject: [PATCH] feat(cohttp): trace post_form --- .../cohttp/opentelemetry_cohttp_lwt.ml | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/integrations/cohttp/opentelemetry_cohttp_lwt.ml b/src/integrations/cohttp/opentelemetry_cohttp_lwt.ml index 57432fe4..f2293c67 100644 --- a/src/integrations/cohttp/opentelemetry_cohttp_lwt.ml +++ b/src/integrations/cohttp/opentelemetry_cohttp_lwt.ml @@ -147,8 +147,7 @@ end = struct end let client ?(scope : Otel.Trace.scope option) (module C : Cohttp_lwt.S.Client) = - let module Traced : Cohttp_lwt.S.Client = struct - include C + let module Traced = struct open Lwt.Syntax let attrs_for ~uri ~meth () = @@ -168,6 +167,8 @@ let client ?(scope : Otel.Trace.scope option) (module C : Cohttp_lwt.S.Client) Header.add headers Traceparent.name (Traceparent.to_value ~trace_id:scope.trace_id ~parent_id:scope.span_id ()) + type ctx = C.ctx + let call ?ctx ?headers ?body ?chunked meth (uri : Uri.t) : (Response.t * Cohttp_lwt.Body.t) Lwt.t = let (trace_id, parent, attrs) = context_for ~uri ~meth in Otel_lwt.Trace.with_ "request" @@ -200,6 +201,27 @@ let client ?(scope : Otel.Trace.scope option) (module C : Cohttp_lwt.S.Client) call ?ctx ?headers ?body ?chunked `PUT uri let patch ?ctx ?body ?chunked ?headers uri = - call ?ctx ?headers ?body ?chunked `PATCH uri end + call ?ctx ?headers ?body ?chunked `PATCH uri + + let post_form ?ctx ?headers ~params uri = + let (trace_id, parent, attrs) = context_for ~uri ~meth:`POST in + Otel_lwt.Trace.with_ "request" + ~kind:Span_kind_client + ?trace_id + ?parent + ~attrs + (fun scope -> + let headers = add_traceparent scope headers in + let* (res, body) = + C.post_form ?ctx ~headers ~params uri + in + Otel.Trace.add_attrs scope (fun () -> + let code = Response.status res in + let code = Code.code_of_status code in + [ ("http.status_code", `Int code) ]) ; + Lwt.return (res, body)) + + let callv = C.callv (* TODO *) + end in (module Traced : Cohttp_lwt.S.Client)