Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Collate:
'lazy.R'
'page.R'
'parse.R'
'processes.R'
'promote.R'
'remote.R'
'runtime-caches.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export(get_my_permission)
export(get_oauth_content_credentials)
export(get_oauth_credentials)
export(get_packages)
export(get_processes)
export(get_procs)
export(get_runtime_caches)
export(get_runtimes)
Expand Down
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
- Corrected the `get_procs()` documentation, which incorrectly stated that only
processes on the responding node of a Highly Available cluster are returned
(#529).
- New `get_processes()` returns the processes Connect is running for content,
using the `GET /v1/system/processes` endpoint added in Connect 2026.11.0. It
can filter by `content_guid`, `owner_guid`, and `hostname`. On older Connect
versions, it falls back to the unversioned `GET /metrics/procs` endpoint and
returns the same columns (#531).
- `get_procs()` and the `Connect$procs()` method are deprecated in favor of
`get_processes()`. `get_procs()` now returns the server's `pid` instead of
the row index, parses `start_time` and `heartbeat` as datetimes, and its
documentation lists the columns it actually returns (#532).

# connectapi 0.12.1

Expand Down
9 changes: 7 additions & 2 deletions R/connect.R
Original file line number Diff line number Diff line change
Expand Up @@ -792,9 +792,14 @@ Connect <- R6::R6Class(
self$GET(path, query = query)
},

#' @description Get running processes.
#' @description Get running processes. Deprecated; use
#' [get_processes()] instead.
procs = function() {
warn_experimental("procs")
lifecycle::deprecate_warn(
"0.13.0",
I("`Connect$procs()`"),
"get_processes()"
)
path <- unversioned_url("metrics", "procs")
self$GET(path)
},
Expand Down
54 changes: 0 additions & 54 deletions R/get.R
Original file line number Diff line number Diff line change
Expand Up @@ -760,60 +760,6 @@ get_audit_logs <- function(
return(out)
}

#' Get Real-Time Process Data
#'
#' `r lifecycle::badge('experimental')`
#' This returns real-time process data from the Posit Connect API. It requires
#' administrator privileges to use. Processes are returned across all nodes in
#' a Highly Available cluster and across execution modes (including off-host
#' execution, e.g. Kubernetes).
#'
#' Note that:
#'
#' * Usage metrics (e.g. CPU and RAM) are the values most recently reported by
#' each process, so they may be stale, or `NA` if the process has not yet
#' reported any metrics.
#' * Processes that have recently ended but have not yet been finalized may
#' still appear in the results.
#' * This function uses an internal, unversioned API endpoint, so its behavior
#' and the returned columns may change in future versions of Connect.
#'
#' @param src The source object
#'
#' @return
#' A tibble with the following columns:
#'
#' * `pid`: The PID of the current process
#' * `appId`: The application ID
#' * `appGuid`: The application GUID
#' * `appName`: The application name
#' * `appUrl`: The application URL
#' * `appRunAs`: The application RunAs user
#' * `type`: The type of process
#' * `cpuCurrent`: The current CPU usage
#' * `cpuTotal`: The total CPU usage
#' * `ram`: The current RAM usage
#'
#' @export
get_procs <- function(src) {
validate_R6_class(src, "Connect")
warn_experimental("get_procs")

scoped_experimental_silence()
raw_proc_data <- src$procs()

proc_prep <- purrr::imap(
raw_proc_data,
function(x, y) {
c(list(pid = y), x)
}
)
tbl_data <- parse_connectapi_typed(proc_prep)
tbl_data <- coerce_fs_bytes(tbl_data, "ram")

return(tbl_data)
}

#' Perform an OAuth credential exchange to obtain a visitor's OAuth access token.
#'
#' @param connect A Connect R6 object.
Expand Down
284 changes: 284 additions & 0 deletions R/processes.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@
#' Get running processes
#'
#' Returns the processes that Connect has started for content and that have
#' not yet finished, with their most recent CPU and memory use. This is the
#' data shown on the **System > Processes** page of the Connect dashboard. It
#' requires administrator privileges.
#'
#' The results cover every node in a Highly Available cluster and include
#' processes that run with off-host execution (e.g. Kubernetes).
#'
#' Note that:
#'
#' * `cpu_current`, `ram`, and `heartbeat` hold the last measurement each
#' process reported, so they can be a few seconds old. They are `NA` until a
#' process reports its first measurement.
#' * A process that exited recently can still appear until Connect finishes
#' cleaning up after it.
#'
#' On Connect versions without the `GET /v1/system/processes` endpoint, this
#' falls back to the older, unversioned `GET /metrics/procs` endpoint. The
#' results are converted to the same columns, and the filters are applied
#' client-side.
#'
#' When more than one filter is given, a process must match all of them.
#'
#' @param src A Connect R6 client object.
#' @param content_guid Optional. Return only processes for the content item
#' with this GUID. It is an error if no content item has this GUID.
#' @param owner_guid Optional. Return only processes for content owned by the
#' user with this GUID. It is an error if no user has this GUID.
#' @param hostname Optional. Return only processes running on the Connect node
#' with this hostname.
#'
#' @return
#' A tibble with one row per process and the following columns:
#'
#' * `content_guid`: The GUID of the content item the process belongs to.
#' `NA` when the content item no longer exists.
#' * `content_name`: The title of the content item, or its name when it has
#' no title.
#' * `content_url`: The URL of the content item, including its custom URL if
#' it has one.
#' * `run_as`: The Unix user the process runs as.
#' * `type`: The kind of work the process does, such as `run_app` or
#' `build_report`.
#' * `pid`: The process identifier on the Connect host. `NA` when Connect
#' has not recorded one, such as for off-host execution.
#' * `hostname`: The name of the Connect node that started the process.
#' * `job_key`: The key of the job for this process.
#' * `image`: The container image the process runs in, when using off-host
#' execution.
#' * `start_time`: When the process started.
#' * `heartbeat`: When the last CPU and memory measurement was taken.
#' * `cpu_current`: The fraction of one CPU the process used over the last
#' measurement interval.
#' * `ram`: The resident memory of the process at the last measurement, as
#' [fs::fs_bytes].
#'
#' @examples
#' \dontrun{
#' client <- connect()
#'
#' get_processes(client)
#'
#' # Only the processes for one content item
#' get_processes(client, content_guid = "6f300623-1e0c-48e6-a473-ddf630c0c0c3")
#' }
#'
#' @export
get_processes <- function(
src,
content_guid = NULL,
owner_guid = NULL,
hostname = NULL
) {
validate_R6_class(src, "Connect")
for (arg in c("content_guid", "owner_guid", "hostname")) {
value <- get(arg)
if (!is.null(value) && !(is_string(value) && nzchar(value))) {
stop(
"`",
arg,
"` must be a single non-empty string or `NULL`.",
call. = FALSE
)
}
}

res <- src$GET(
v1_url("system", "processes"),
httr::add_headers(Accept = "text/csv"),
query = list(
content_guid = content_guid,
owner_guid = owner_guid,
hostname = hostname
),
parser = NULL
)

if (endpoint_does_not_exist(res)) {
# Connect versions before the v1 endpoint only have the unversioned one.
return(legacy_processes(src, content_guid, owner_guid, hostname))
}
src$raise_error(res)
type_processes(parse_processes_csv(httr::content(
res,
as = "text",
encoding = "UTF-8"
)))
}

#' Get Real-Time Process Data
#'
#' `r lifecycle::badge('deprecated')`
#'
#' This function is deprecated; please use [get_processes()], which uses the
#' supported `GET /v1/system/processes` endpoint, can filter its results, and
#' returns the same columns across Connect versions.
#'
#' `get_procs()` uses the internal, unversioned `GET /metrics/procs` endpoint,
#' so its behavior and the returned columns may change in future versions of
#' Connect. It requires administrator privileges, and returns processes across
#' all nodes in a Highly Available cluster.
#'
#' @param src The source object
#'
#' @return
#' A tibble with one row per process. The columns are those sent by the server,
#' which in recent versions of Connect are `pid`, `app_id`, `app_guid`,
#' `app_name`, `app_url`, `app_run_as`, `type`, `cpu_current`, `ram`,
#' `hostname`, `job_key`, `image`, `start_time`, and `heartbeat`.
#'
#' @keywords internal
#' @export
get_procs <- function(src) {
lifecycle::deprecate_warn("0.13.0", "get_procs()", "get_processes()")
validate_R6_class(src, "Connect")
fetch_legacy_procs(src)
}

# Columns returned by `GET /v1/system/processes`, in order.
process_columns <- c(
"content_guid",
"content_name",
"content_url",
"run_as",
"type",
"pid",
"hostname",
"job_key",
"image",
"start_time",
"heartbeat",
"cpu_current",
"ram"
)

# Read the CSV body of `GET /v1/system/processes`. Every column is read as
# character so that types do not depend on the values present; Connect writes
# null values as empty fields, so those become `NA`.
#
# Types are applied afterwards by `type_processes()` rather than through
# `colClasses`, because:
# - `type_processes()` also types the `GET /metrics/procs` fallback, whose
# values arrive from JSON with different types, so it is needed either way.
# - `colClasses` can't produce the `start_time`/`heartbeat` or `ram` types:
# "POSIXct" drops the time from RFC 3339 strings, and there is no class for
# `fs_bytes`.
# - Columns not named in `colClasses` would get their types guessed (all-NA
# columns become logical), and naming a column the server doesn't send is a
# warning.
parse_processes_csv <- function(text) {
if (!nzchar(trimws(text))) {
return(tibble::tibble())
}
df <- utils::read.csv(
text = text,
colClasses = "character",
na.strings = "",
check.names = FALSE,
stringsAsFactors = FALSE,
encoding = "UTF-8"
)
tibble::as_tibble(df)
}

# Give a processes table its documented column types, adding any missing
# columns so that empty results have the same shape as non-empty ones. Columns
# the server sends that we don't know about are kept at the end.
type_processes <- function(df) {
n <- nrow(df)
for (col in setdiff(process_columns, names(df))) {
df[[col]] <- rep(NA_character_, n)
}
df <- df[c(process_columns, setdiff(names(df), process_columns))]

chr_cols <- c(
"content_guid",
"content_name",
"content_url",
"run_as",
"type",
"hostname",
"job_key",
"image"
)
for (col in chr_cols) {
df[[col]] <- as.character(df[[col]])
}
df$pid <- as.integer(df$pid)
df$cpu_current <- as.double(df$cpu_current)
df$ram <- fs::as_fs_bytes(as.double(df$ram))
df$start_time <- coerce_datetime(df$start_time)
df$heartbeat <- coerce_datetime(df$heartbeat)
df
}

# Fetch `GET /metrics/procs` as a tibble with the server's column names.
fetch_legacy_procs <- function(src) {
raw <- src$GET(unversioned_fallback_url("metrics", "procs"))
if (!is.null(names(raw))) {
# Old Connect versions returned an object keyed by PID rather than an
# array of processes that each carry a `pid` field.
raw <- unname(purrr::imap(raw, function(x, pid) c(list(pid = pid), x)))
}
out <- parse_connectapi_typed(raw, datetime_cols = c("start_time", "heartbeat"))
coerce_fs_bytes(out, "ram")
}

# Fallback for `get_processes()` on Connect versions without
# `GET /v1/system/processes`: fetch `GET /metrics/procs`, convert it to the v1
# columns and conventions, and apply the filters client-side.
legacy_processes <- function(src, content_guid, owner_guid, hostname) {
# Like the v1 endpoint, reject a content or owner GUID that names nothing
# rather than returning no processes. These requests raise a 404 if so.
if (!is.null(content_guid)) {
src$content(guid = content_guid, include = NULL)
}
if (!is.null(owner_guid)) {
src$user(owner_guid)
owned <- src$content(owner_guid = owner_guid, include = NULL)
owned_guids <- purrr::map_chr(owned, "guid")
}

df <- fetch_legacy_procs(src)

renames <- c(
app_guid = "content_guid",
app_name = "content_name",
app_url = "content_url",
app_run_as = "run_as",
appGuid = "content_guid",
appName = "content_name",
appUrl = "content_url",
appRunAs = "run_as",
cpuCurrent = "cpu_current"
)
to_rename <- intersect(names(renames), names(df))
names(df)[match(to_rename, names(df))] <- renames[to_rename]
df <- df[setdiff(names(df), c("app_id", "appId", "cpuTotal"))]
df <- type_processes(df)

# The v1 endpoint returns null for values Connect doesn't know, where the
# unversioned endpoint uses placeholders. Match the v1 endpoint, whose CSV
# output also cannot distinguish an empty string from null.
for (col in names(df)[purrr::map_lgl(df, is.character)]) {
df[[col]][!is.na(df[[col]]) & df[[col]] == ""] <- NA_character_
}
df$run_as[df$run_as %in% "unknown"] <- NA_character_
df$hostname[df$hostname %in% "unknown host"] <- NA_character_
df$pid[!is.na(df$pid) & df$pid <= 0L] <- NA_integer_

keep <- rep(TRUE, nrow(df))
if (!is.null(content_guid)) {
keep <- keep & df$content_guid %in% content_guid
}
if (!is.null(hostname)) {
keep <- keep & df$hostname %in% hostname
}
if (!is.null(owner_guid)) {
keep <- keep & df$content_guid %in% owned_guids
}
df[keep, ]
}
Loading
Loading