mirror of
https://github.com/prometheus/prometheus.git
synced 2024-11-09 23:24:05 -08:00
Add parseDuration to template functions
This can be useful when generating rules, a query may use a duration, and it may be useful to template that into a URL parameter. Therefore this allows interfacing with systems that don't implement Prometheus style duration parsing. Signed-off-by: David Leadbeater <dgl@dgl.cx>
This commit is contained in:
parent
69b761a187
commit
89ebb3dcf2
|
@ -74,6 +74,7 @@ versions.
|
|||
| reReplaceAll | pattern, replacement, text | string | [Regexp.ReplaceAllString](https://golang.org/pkg/regexp/#Regexp.ReplaceAllString) Regexp substitution, unanchored. |
|
||||
| graphLink | expr | string | Returns path to graph view in the [expression browser](https://prometheus.io/docs/visualization/browser/) for the expression. |
|
||||
| tableLink | expr | string | Returns path to tabular ("Table") view in the [expression browser](https://prometheus.io/docs/visualization/browser/) for the expression. |
|
||||
| parseDuration | string | string | Parses a duration string such as "1h" into the integer number of seconds it represents. |
|
||||
|
||||
### Others
|
||||
|
||||
|
|
|
@ -304,6 +304,14 @@ func NewTemplateExpander(
|
|||
"externalURL": func() string {
|
||||
return externalURL.String()
|
||||
},
|
||||
"parseDuration": func(d string) (string, error) {
|
||||
v, err := model.ParseDuration(d)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
dur := int64(time.Duration(v) / time.Second)
|
||||
return fmt.Sprintf("%d", dur), nil
|
||||
},
|
||||
},
|
||||
options: options,
|
||||
}
|
||||
|
|
|
@ -449,6 +449,11 @@ func TestTemplateExpansion(t *testing.T) {
|
|||
text: "{{ externalURL }}",
|
||||
output: "http://testhost:9090/path/prefix",
|
||||
},
|
||||
{
|
||||
// parseDuration.
|
||||
text: "{{ parseDuration \"1h2m10ms\" }}",
|
||||
output: "3720",
|
||||
},
|
||||
}
|
||||
|
||||
extURL, err := url.Parse("http://testhost:9090/path/prefix")
|
||||
|
|
Loading…
Reference in a new issue