Repository navigation
handle traceparent and tracestate headers in web #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,32 @@ | ||
| open Opentelemetry | ||
|
|
||
| let ambient_span ?explicit_span () = | ||
| match explicit_span, Trace_core.current_span () with | ||
| | Some (Opentelemetry_trace.Extensions.Span_otel span), _ | ||
| | _, Some (Opentelemetry_trace.Extensions.Span_otel span) -> Some span | ||
| | _ -> None | ||
|
|
||
| module Trace_context = struct | ||
| let header_names = Opentelemetry.Trace_context.[ Traceparent.name; Tracestate.name ] | ||
|
|
||
| let get_ambient_headers ?explicit_span () = | ||
| match ambient_span ?explicit_span () with | ||
| | Some span -> Opentelemetry.Trace_context.headers_of_span_ctx (Span.to_span_ctx span) | ||
| | None -> [] | ||
| end | ||
|
|
||
| module Traceparent = struct | ||
| let name = Trace_context.Traceparent.name | ||
| let name = Opentelemetry.Trace_context.Traceparent.name | ||
|
|
||
| let get_ambient ?explicit_span () = | ||
| match explicit_span, Trace_core.current_span () with | ||
| | Some (Opentelemetry_trace.Extensions.Span_otel span), _ | ||
| | _, Some (Opentelemetry_trace.Extensions.Span_otel span) -> | ||
| let ctx = Trace_context.Traceparent.to_value | ||
| ~trace_id:(Span.trace_id span) ~parent_id:(Span.id span) () in | ||
| Some ctx | ||
| | _ -> None | ||
| match ambient_span ?explicit_span () with | ||
| | Some span -> | ||
| Some (Opentelemetry.Trace_context.Traceparent.to_value | ||
| ~trace_flags:(Span.trace_flags span) ~trace_id:(Span.trace_id span) ~parent_id:(Span.id span) ()) | ||
| | None -> None | ||
| end | ||
|
|
||
| (* no explicit [~parent]: the current span if any, otherwise let the collector look for | ||
| an ambient context (e.g. from an incoming traceparent) instead of forcing a new trace *) | ||
| let enter_manual_span ~__FUNCTION__ ~__FILE__ ~__LINE__ ?data name = | ||
| let parent = Trace_core.current_span () in | ||
| Trace_core.enter_span ~parent ~__FUNCTION__ ~__FILE__ ~__LINE__ ?data name | ||
| Trace_core.enter_span ~__FUNCTION__ ~__FILE__ ~__LINE__ ?data name |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -329,12 +329,12 @@ module Http (IO : IO_TYPE) (Curl_IO : CURL with type 'a t = 'a IO.t) : HTTP with | |
| | `Plain, _ -> verbose_curl_result_plain nr_http action t hr | ||
| | `Logfmt, _ -> verbose_curl_result_logfmt nr_http action t hr | ||
|
|
||
| (* Given a list of strings, check pre-existing entry starting with `~name`; and adds the concatenation of `~name` and `~value` if not. *) | ||
| let add_if_absent ~name ~value strs = | ||
| match strs with | ||
| | Some strs when List.exists (fun s -> Stre.starts_with s (name^":")) strs -> strs | ||
| | Some strs -> (String.concat ": " [name; value]) :: strs | ||
| | None -> [String.concat ": " [name; value]] | ||
| (* Remove from the list of "Name: value" headers those whose name (case-insensitive) is in `names`, then add `pairs`. *) | ||
| let replace_headers ~names pairs strs = | ||
| let is_replaced s = let (n, _) = Stre.dividec s ':' in List.exists (Stre.iequal (String.strip n)) names in | ||
| let strs = Option.default [] strs in | ||
| let strs = if List.exists is_replaced strs then List.filter (fun s -> not (is_replaced s)) strs else strs in | ||
| List.map (fun (n, v) -> sprintf "%s: %s" n v) pairs @ strs | ||
|
Comment on lines
+333
to
+337
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LLM finding
|
||
|
|
||
| (* NOTE don't forget to set http_1_0=true when sending requests to a Httpev-based server *) | ||
| (* Don't use curl_setheaders when using ?headers option *) | ||
|
|
@@ -414,9 +414,10 @@ module Http (IO : IO_TYPE) (Curl_IO : CURL with type 'a t = 'a IO.t) : HTTP with | |
| Possibly_otel.enter_manual_span | ||
| ~__FUNCTION__:"Devkit.Web.Http.http_request_k" ~__FILE__ ~__LINE__ ~data:describe span_name in | ||
|
|
||
| let headers = match Possibly_otel.Traceparent.get_ambient ~explicit_span () with | ||
| | None -> headers | ||
| | Some value -> Some (add_if_absent ~name:(Possibly_otel.Traceparent.name) ~value headers) | ||
| (* trace context headers always override the caller's *) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we document that somewhere e.g in
|
||
| let headers = match Possibly_otel.Trace_context.get_ambient_headers ~explicit_span () with | ||
| | [] -> headers | ||
| | pairs -> Some (replace_headers ~names:Possibly_otel.Trace_context.header_names pairs headers) | ||
| in | ||
|
|
||
| let set_body_and_headers h ct body = | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it be
0.92instead?