Display Formatting
Canary checker can format the results of checks using the display
field. All expressions must return a string (or a Go struct with a .String()
method)
Go Text Templates
In this example we get the current exchange rate:
display-with-gotemplate.yaml---
apiVersion: canaries.flanksource.com/v1
kind: Canary
metadata:
name: currency-converter-display-gotemplate
spec:
http:
- name: USD
url: https://api.frankfurter.app/latest?from=USD&to=GBP,EUR,ILS,ZAR
display:
template: "$1 = €{{.json.rates.EUR}}, £{{.json.rates.GBP}}, ₪{{.json.rates.ILS}}"
Which would display:
$1 = €0.94913, £0.82394, ₪3.8455
See Go Template
CEL Expressions
The equivalent using CEL expressions would be:
display-with-cel.yaml---
apiVersion: canaries.flanksource.com/v1
kind: Canary
metadata:
name: currency-converter-display-cel
spec:
http:
- name: USD
url: https://api.frankfurter.app/latest?from=USD&to=GBP,EUR,ILS,ZAR
display:
expr: "'$1 = €' + string(json.rates.EUR) + ', £' + string(json.rates.GBP) + ', ₪' + string(json.rates.ILS)"
See Cel Expressions for a function reference
Javascript
display-with-javascript.yamlapiVersion: canaries.flanksource.com/v1
kind: Canary
metadata:
name: currency-converter-display-js
spec:
http:
- name: USD
url: https://api.frankfurter.app/latest?from=USD&to=GBP,EUR,ILS
display:
javascript: |
currencyCodes = { "EUR": "€", "GBP": "£", "ILS": "₪"}
display = []
for (var currency in json.rates) {
display.push(currency + " = " + currencyCodes[currency] + json.rates[currency])
}
// final output to display
"$1 = " + display.join(", ")
info
Do not return anything from the function, e.g. instead of return "hello" + "world"
use "hello" + "world"
See JavaScript for more details and a function reference.
Variables
Each check exposes different variables to use in the display
expression, See the Result Variables section for each check.