archive: push restic backup and prune metrics, and add a backups dashboard
This commit is contained in:
parent
9ac6024a67
commit
723ce866f1
1 changed files with 1288 additions and 4 deletions
49
nix/host-modules/lib/otlp-gauges.nix
Normal file
49
nix/host-modules/lib/otlp-gauges.nix
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
# Shell text defining `otlp_gauges`, which pushes gauges to this host's OTLP/HTTP
|
||||
# metrics receiver:
|
||||
#
|
||||
# otlp_gauges <service.name> <attr=value,attr=value> <metric=value>...
|
||||
#
|
||||
# One resource, one set of data-point attributes, one data point per metric.
|
||||
# Values are numbers. A failed build of the payload or a failed push prints a
|
||||
# warning on stderr and returns 0, so a unit's own result never depends on
|
||||
# telemetry. The swarm collector stamps `hive` on every sample it receives from
|
||||
# a hive, so callers do not set it.
|
||||
#
|
||||
# `endpoint` is the receiver's base URL, or null on a host without one, where
|
||||
# the function does nothing.
|
||||
#
|
||||
# Pure function, NOT a NixOS module. Needs `jq`, `date` and `curl` on the
|
||||
# unit's `path`.
|
||||
{ lib }:
|
||||
{ endpoint }:
|
||||
if endpoint == null then
|
||||
''
|
||||
otlp_gauges() { :; }
|
||||
''
|
||||
else
|
||||
''
|
||||
otlp_gauges() {
|
||||
local service="$1" attrs="$2" body
|
||||
shift 2
|
||||
body="$(jq -n -c --arg service "$service" --arg attrs "$attrs" --arg now "$(date +%s%N)" '
|
||||
($attrs | split(",") | map(select(length > 0) | index("=") as $i
|
||||
| {key: .[:$i], value: {stringValue: .[$i + 1:]}})) as $attributes
|
||||
| {resourceMetrics: [{
|
||||
resource: {attributes: [{key: "service.name", value: {stringValue: $service}}]},
|
||||
scopeMetrics: [{metrics: ($ARGS.positional | map(index("=") as $i
|
||||
| {name: .[:$i], gauge: {dataPoints: [{
|
||||
asDouble: (.[$i + 1:] | tonumber),
|
||||
attributes: $attributes,
|
||||
timeUnixNano: $now}]}}))}]}]}
|
||||
' --args "$@")" || {
|
||||
echo "warning: could not build the metrics payload for $service: $*" >&2
|
||||
return 0
|
||||
}
|
||||
printf '%s' "$body" |
|
||||
curl --fail --silent --show-error --max-time 10 \
|
||||
-H 'Content-Type: application/json' --data-binary @- \
|
||||
${lib.escapeShellArg "${endpoint}/v1/metrics"} ||
|
||||
echo "warning: could not push metrics for $service" >&2
|
||||
return 0
|
||||
}
|
||||
''
|
||||
Loading…
Reference in a new issue