Skip to content

Instantly share code, notes, and snippets.

@groner
Created May 30, 2018 19:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save groner/e3b8c93d4b088f617d625b20d3d08112 to your computer and use it in GitHub Desktop.
Save groner/e3b8c93d4b088f617d625b20d3d08112 to your computer and use it in GitHub Desktop.
prometheus query helpers (uses curl and jq)
prom-query () {
local tmp=$(mktemp -t prom-query.XXXXXX)
curl -Ggs cakepile.local:8001/api/v1/namespaces/monitoring/services/prometheus-k8s:web/proxy/api/v1/query --data-urlencode query="$1" >$tmp
if jq -e '.status=="error"' <$tmp >/dev/null; then
jq <$tmp >&2 -r .error
rm -f $tmp
return 1
fi
jq <$tmp '.data.result[]'
rm -f $tmp
}
prom-drop-labels () {
local jlabels=$(printf '%s\0' "$@" | jq -Rc 'split("\u0000") | [.[] | {key: ., value: 1}] | from_entries');
jq --argjson labels "$jlabels" '.metric |= ([to_entries[] | select(.key | in($labels) | not)] | from_entries)'
}
prom-keep () {
local jlabels=$(printf '%s\0' "$@" | jq -Rc 'split("\u0000") | [.[] | {key: ., value: 1}] | from_entries');
jq --argjson labels "$jlabels" '.metric |= ([to_entries[] | select(.key | in($labels))] | from_entries)'
}
prom-list () {
jq -r '"\(.metric | "\(.__name__//""){\(del(.__name__) | [to_entries[] | "\(.key)=\(.value | @json)"] | join(","))}")\t\(.value[0] | todate)\t\(.value[1])"'
}
prom-list-labels () {
jq -r '.metric | "\(.__name__//""){\(del(.__name__) | [to_entries[] | "\(.key)=\(.value | @json)"] | join(","))}"' | sort -u
}
prom-list-names () {
jq -r '.metric.__name__' | sort -u
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment