Skip to content

Commit 895a1a1

Browse files
split metrics in two batches, {,non-}accumulating
1 parent d56d409 commit 895a1a1

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

.github/actions/pushLoggedMetrics/action.yml

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,46 +15,50 @@ runs:
1515
# send some stats whilst we're at it
1616
echo Metrics file found.
1717
nl .sfpowerscripts/logs/metrics.log
18-
metrics=$(jq --slurp --raw-output '
18+
countmetrics=$(jq --slurp --raw-output '
1919
# just keep the last value
2020
reverse | unique_by(.metric, .tags) | reverse | .[] |
21-
(
22-
if .type == "count" then "counter"
23-
# note the spelling
24-
elif .type == "guage" then "gauge"
25-
# statsd-timers are always in ms. prometheus has no such type, so just
26-
# use a gauge
27-
elif .type == "timers" then "gauge"
28-
# anything else, handle it when the time comes
29-
else "unknown" end
30-
) as $type |
21+
select(.type == "count") |
3122
# assume counters with no value given are 1
3223
(
33-
if .value == null and $type == "counter" then 1
34-
elif $type == "gauge" then .value
35-
else null
24+
if .value == null then 1
25+
else .value
3626
end
3727
) as $value |
38-
# accumulate counters
39-
(
40-
if $type == "counter" then {"acctime":.timestamp}
41-
else {}
42-
end
43-
) as $acctime |
44-
select($value != null) |
45-
# only allow alpha and _ in metrics name
46-
(.metric | gsub("[^a-z]"; "_")) as $metric |
28+
# only allow alpha, _, and : in metrics name
29+
(.metric | gsub("[^a-z_]"; ":")) as $metric |
4730
# transform tags into prometheus
48-
($acctime * .tags // {} | to_entries | map(.key + "=\"" + (.value | tostring) + "\"") | join(",")) as $tags |
31+
(.tags // {} | to_entries | map(.key + "=\"" + (.value | tostring) + "\"") | join(",")) as $tags |
4932
# put it all together. one line with description, one line with metric.
50-
"# TYPE \($metric) \($type)\n\($metric){\($tags)} \($value)"
33+
"# TYPE \($metric) counter\n\($metric){\($tags)} \($value)"
34+
' .sfpowerscripts/logs/metrics.log | awk '/^# TYPE / && a[$3]++ {next}; 1')
35+
gaugemetrics=$(jq --slurp --raw-output '
36+
# just keep the last value
37+
reverse | unique_by(.metric, .tags) | reverse | .[] |
38+
select(.type == "guage" or .type == "timers") |
39+
# only allow alpha, _, and : in metrics name
40+
(.metric | gsub("[^a-z_]"; ":")) as $metric |
41+
# transform tags into prometheus
42+
(.tags // {} | to_entries | map(.key + "=\"" + (.value | tostring) + "\"") | join(",")) as $tags |
43+
# put it all together. one line with description, one line with metric.
44+
"# TYPE \($metric) gauge\n\($metric){\($tags)} \(.value)"
5145
' .sfpowerscripts/logs/metrics.log | awk '/^# TYPE / && a[$3]++ {next}; 1')
5246
echo "METRICSKEY len: ${#METRICSKEY}"
5347
keyfile=$(mktemp) || exit 1
5448
cat > "$keyfile" <<< "$METRICSKEY"
55-
sig=$(printf %s "$metrics" | openssl dgst -sha256 -sign "$keyfile" -out - | base64 -w0)
49+
# send count metrics. accumulate by specifying instance per run.
50+
sig=$(printf %s "$countmetrics" | openssl dgst -sha256 -sign "$keyfile" -out - | base64 -w0)
51+
jq --compact-output --null-input \
52+
--arg metrics "$countmetrics" \
53+
--arg runner sf-platform \
54+
--arg sig "$sig" \
55+
'{"runner":$runner,"metrics":$metrics,"signature":$sig}' |
56+
tee >(curl -D- -H 'Content-Type: application/json' --data-binary @- \
57+
"https://sf-github-metrics.ekstern.dev.nav.no/measures/job/sfplatform/instance/${{github.run_id}}")
58+
# send gauge metrics. no instance means metrics sent get overwritten.
59+
sig=$(printf %s "$gaugemetrics" | openssl dgst -sha256 -sign "$keyfile" -out - | base64 -w0)
5660
jq --compact-output --null-input \
57-
--arg metrics "$metrics" \
61+
--arg metrics "$gaugemetrics" \
5862
--arg runner sf-platform \
5963
--arg sig "$sig" \
6064
'{"runner":$runner,"metrics":$metrics,"signature":$sig}' |

0 commit comments

Comments
 (0)