15
15
# ' @param instance A value for the `instance` label applied to all pushed
16
16
# ' metrics, or `NA` to leave it unset.
17
17
# ' @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.
19
20
# '
20
21
# ' @return `NULL`, invisibly.
21
22
# '
31
32
# ' @export
32
33
push_to_gateway <- function (url , job , instance = NA ,
33
34
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
+ )
39
39
rendered <- registry $ render_all(format = " pushgateway" )
40
40
response <- httr :: RETRY(
41
41
" POST" , url , path = path , body = rendered , encode = " form" ,
@@ -50,12 +50,19 @@ push_to_gateway <- function(url, job, instance = NA,
50
50
# ' @rdname pushgateway
51
51
# ' @export
52
52
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
+ )
58
57
response <- httr :: RETRY(" DELETE" , url , path = path )
59
58
httr :: warn_for_status(response )
60
59
invisible (NULL )
61
60
}
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
+ }
0 commit comments