Templating
Templating is:
Simply a way to represent data in different forms.
Templating comes in handy when you want the results of your operation displayed in your desired configuration and order. Canary-checker and Config DB supports this feature for rendering results. They can be specified in display
, test
, transform
fields for the Canary
object, id
, test
, transform
, display
for SystemTemplate
object. The template can be configured In the next section, you'll see the different supported templates.
Canary Template Types
To specify the template to be used for representing your data, the following options are available:
Expression
The Go Expression Evaluation Language, Expr comes builtin with the Canary checker and is used for working with expressions. You use can expressions to render your data the way you see fit.
Below is an example for the supported field test
for the Canary
object.
exec:
- description: "exec dummy check"
script: |
echo "hello"
name: exec-pass-check
test:
expr: 'results.Stdout == "hello"'
expr: 'results.Stdout == "hello"'
performs a test to check if the command in the script:
field ran successfully the result returns the string hello
.
Go Template
The Go template comes supported with the Canary checker to customize your output however you want it.
Below is an example for the display
field.
prometheus:
- host: https://prometheus.canary.lab.flanksource.com/
name: prometheus-check
query: kubernetes_build_info{job!~"kube-dns|coredns"}
display:
template: "{{ (index .results 0).git_version }}"
template
under display
. The template extracts the git_version
field from the initial field in .results
output.
Javascript
In-line javascript does come supported with the Canary checker. Javascript code can be ran side-by-side with the checks to filter or render your data. Below is an example of in-line Javascript in use.
properties:
- name: pod-metrics
lookup:
kubernetes:
- kind: PodMetrics
ready: false
name: podmetrics
display:
javascript: JSON.stringify(k8s.getPodMetrics(results))
JSON.stringify()
is used in the display
field to convert the value of k8s.getPodMetrics(results)
to a JSON string.
JSONPath
A JSONPath expression similar to the XPath with XML, is used to extract data from a JSON file by specifying a path to an element(s) in a JSON structure. This comes supported with the Canary-checker, and Config DB. Below is an example of the JSONPath in use with the File config type for Config DB .
file:
- type: $.Config.InstanceType
id: $.Config.InstanceId
path:
- config*.json
- test*.json
type
, and id
require a JSONPath expression to run. The string $.Config.InstanceType
and $.Config.InstanceId
specify the JSON value for InstanceType
and InstanceId
respectively contained in the files set in path
.
Transformation
The transform
can be used to convert one canary check into multiple checks programatically.
Example
apiVersion: canaries.flanksource.com/v1
kind: Canary
metadata:
name: alertmanager-check
spec:
schedule: "*/5 * * * *"
alertmanager:
- host: alertmanager.example.com
alerts:
- .*
ignore:
- KubeScheduler.*
transform:
javascript: |
var out = _.map(results, function(r) {
return {
name: r.name,
labels: r.labels,
icon: 'alert',
message: r.message,
description: r.message,
}
})
JSON.stringify(out);
In the above example, the check will return multiple alerts from alertmanager. By default, all of those alerts will be grouped in a single check.
But if we want to display each alert as its own check in the UI, we can use the transform
function for this. The transform function takes a Template
as input and the output from the template expected
are the checks in JSON format. Only the name
, labels
, icon
, message
and description
fields need to be populated, others fields are generated automatically.
For example, if there are 9 different alerts, each alert will have its own check that can be managed and interacted with equally.