Skip to content

Commit 90b8c40

Browse files
committed
Add support for passing additional labels to the Pushgateway.
1 parent 931c342 commit 90b8c40

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

R/pushgateway.R

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
#' @param instance A value for the `instance` label applied to all pushed
1616
#' metrics, or `NA` to leave it unset.
1717
#' @param registry A `Registry` object, defaulting to the shared global one.
18-
#' @param ... Currently ignored.
18+
#' @param ... Additional named string arguments converted to labels. Beware
19+
#' that these are not yet checked for URL safety.
1920
#'
2021
#' @return `NULL`, invisibly.
2122
#'
@@ -31,11 +32,10 @@
3132
#' @export
3233
push_to_gateway <- function(url, job, instance = NA,
3334
registry = global_registry(), ...) {
34-
if (is.na(instance)) {
35-
path <- sprintf("/metrics/job/%s", job)
36-
} else {
37-
path <- sprintf("/metrics/job/%s/instance/%s", job, instance)
38-
}
35+
labels <- pushgateway_labels(job = job, instance = instance, ...)
36+
path <- sprintf(
37+
"/metrics/%s", paste(names(labels), labels, sep = "/", collapse = "/")
38+
)
3939
rendered <- registry$render_all(format = "pushgateway")
4040
response <- httr::RETRY(
4141
"POST", url, path = path, body = rendered, encode = "form",
@@ -50,12 +50,19 @@ push_to_gateway <- function(url, job, instance = NA,
5050
#' @rdname pushgateway
5151
#' @export
5252
delete_from_gateway <- function(url, job, instance = NA, ...) {
53-
if (is.na(instance)) {
54-
path <- sprintf("/metrics/job/%s", job)
55-
} else {
56-
path <- sprintf("/metrics/job/%s/instance/%s", job, instance)
57-
}
53+
labels <- pushgateway_labels(job = job, instance = instance, ...)
54+
path <- sprintf(
55+
"/metrics/%s", paste(names(labels), labels, sep = "/", collapse = "/")
56+
)
5857
response <- httr::RETRY("DELETE", url, path = path)
5958
httr::warn_for_status(response)
6059
invisible(NULL)
6160
}
61+
62+
pushgateway_labels <- function(job, instance, ...) {
63+
labels <- list(job = job)
64+
labels$instance <- if (!is.na(instance)) instance
65+
labels <- c(labels, list(...))
66+
# TODO: Base64 encode URL-breaking labels.
67+
parse_labels(labels)
68+
}

man/pushgateway.Rd

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-pushgateway.R

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,16 @@ testthat::test_that("Pushgateway interaction works as expected", {
1616
testthat::expect_silent(
1717
push_to_gateway(
1818
"http://localhost:9091", job = "openmetrics-tests-1", instance = "2",
19-
registry = reg
19+
pod = Sys.info()["nodename"], registry = reg
2020
)
2121
)
2222
testthat::expect_silent(
2323
delete_from_gateway(
2424
"http://localhost:9091", job = "openmetrics-tests-1", instance = "2",
25-
registry = reg
25+
pod = Sys.info()["nodename"]
2626
)
2727
)
2828
testthat::expect_silent(
29-
delete_from_gateway(
30-
"http://localhost:9091", job = "openmetrics-tests-1", registry = reg
31-
)
29+
delete_from_gateway("http://localhost:9091", job = "openmetrics-tests-1")
3230
)
3331
})

0 commit comments

Comments
 (0)