Debian Codegraph @

R/otel-session.R

raw
1 # Semantic conventions for session: https://opentelemetry.io/docs/specs/semconv/general/session/
2
3 #' Create and use session span and events
4 #'
5 #' If otel is disabled, the session span and events will not be created,
6 #' however the expression will still be evaluated.
7 #'
8 #' Span: `session_start`, `session_end`
9 #' @param expr Expression to evaluate within the session span
10 #' @param ... Ignored
11 #' @param domain The reactive domain
12 #' @noRd
13 otel_span_session_start <- function(expr, ..., domain) {
14
15 if (!has_otel_collect("session")) {
16 return(force(expr))
17 }
18
19 # Wrap the server initialization
20 with_otel_span(
21 "session_start",
22 expr,
23 attributes = otel::as_attributes(c(
24 otel_session_id_attrs(domain),
25 otel_session_attrs(domain)
26 ))
27 )
28 }
29
30
31 otel_span_session_end <- function(expr, ..., domain) {
32 if (!has_otel_collect("session")) {
33 return(force(expr))
34 }
35
36 id_attrs <- otel_session_id_attrs(domain)
37 with_otel_span(
38 "session_end",
39 expr,
40 attributes = id_attrs
41 )
42 }
43
44 # -- Helpers -------------------------------
45
46
47 # Occurs when the websocket connection is established
48 otel_session_attrs <- function(domain) {
49 # TODO: Future: Posit Connect integration
50 # > we are still trying to identify all of the information we want to track/expose
51 #
52 # * `POSIT_PRODUCT` (Fallback to RSTUDIO_PRODUCT) for host environment
53 # * `CONNECT_SERVER` envvar to get the `session.address`.
54 # * `CONNECT_CONTENT_GUID` for the consistent app distinguisher
55 # * Maybe `CONNECT_CONTENT_JOB_KEY`?
56 # * Maybe `user.id` to be their user name: https://opentelemetry.io/docs/specs/semconv/registry/attributes/user/
57 attrs <- list(
58 server.path =
59 sub(
60 "/websocket/$", "/",
61 domain[["request"]][["PATH_INFO"]] %||% ""
62 ),
63 server.address = domain[["request"]][["HTTP_HOST"]] %||% "",
64 server.origin = domain[["request"]][["HTTP_ORIGIN"]] %||% "",
65 ## Currently, Shiny does not expose QUERY_STRING when connecting the websocket
66 # so we do not provide it here.
67 # QUERY_STRING = domain[["request"]][["QUERY_STRING"]] %||% "",
68 server.port = domain[["request"]][["SERVER_PORT"]] %||% NA_integer_
69 )
70 # Safely convert SERVER_PORT to integer
71 # If conversion fails, leave as-is (string or empty)
72 # This avoids warnings/errors if SERVER_PORT is not a valid integer
73 server_port <- suppressWarnings(as.integer(attrs$server.port))
74 if (!is.na(server_port)) {
75 attrs$server.port <- server_port
76 }
77
78 attrs
79 }
80
81 otel_session_id_attrs <- function(domain) {
82 token <- domain$token
83 if (is.null(token)) {
84 return(list())
85 }
86
87 list(
88 # Convention for client-side with session.start and session.end events
89 # https://opentelemetry.io/docs/specs/semconv/general/session/
90 #
91 # Since we are the server, we'll add them as an attribute to _every_ span
92 # within the session as we don't know exactly when they will be called.
93 # Given it's only a single attribute, the cost should be minimal, but it ties every reactive calculation together.
94 session.id = token
95 )
96 }