Skip to content

Commit

Permalink
CP-46576: Add standard network attributes
Browse files Browse the repository at this point in the history
Add standard http attributes to from opentelemetry such as:

- `network.local.address`;
- `network.local.port`;
- `network.peer.address`;
- `network.peer.port`.

 This improves debuggability associated with client requests.

Signed-off-by: Gabriel Buica <[email protected]>
  • Loading branch information
GabrielBuica committed Apr 15, 2024
1 parent 64fdc74 commit d384dbc
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions ocaml/xapi/context.ml
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,34 @@ let parent_of_origin (origin : origin) span_name =

let attribute_helper_fn f v = Option.fold ~none:[] ~some:f v

let addr_port_of_sock s =
match s with
| None ->
(None, None)
| Some (Unix.ADDR_UNIX "") ->
(None, None)
| Some (Unix.ADDR_UNIX socket_name) ->
(Some socket_name, None)
| Some (Unix.ADDR_INET (addr, port)) ->
(Some (Unix.string_of_inet_addr addr), Some (string_of_int port))

let with_try_get_addr f s =
(try Some (f s) with Unix.Unix_error (Unix.ENOTSOCK, _, _) -> None)
|> addr_port_of_sock

let attr_of_fd s =
let peer_addr, peer_port = s |> with_try_get_addr Unix.getpeername in
let local_addr, local_port = s |> with_try_get_addr Unix.getsockname in
[
attribute_helper_fn
(fun addr -> [("network.local.address", addr)])
local_addr
; attribute_helper_fn (fun port -> [("network.local.port", port)]) local_port
; attribute_helper_fn (fun addr -> [("network.peer.address", addr)]) peer_addr
; attribute_helper_fn (fun port -> [("network.peer.port", port)]) peer_port
]
|> List.concat

let attr_of_req (req : Http.Request.t) =
[
[
Expand Down Expand Up @@ -277,8 +305,8 @@ let make_attributes ?task_name ?task_id ?task_uuid ?session_id ?origin () =
match origin with
| Internal ->
[("xs.xapi.task.origin", "internal")]
| Http (req, _) ->
attr_of_req req
| Http (req, s) ->
[attr_of_req req; attr_of_fd s] |> List.concat
)
origin
]
Expand Down

0 comments on commit d384dbc

Please sign in to comment.