Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/nix/host-modules/lib/otlp-gauges.nix

49 lines
1.9 KiB
Nix

# 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
}
''