Skip to content

Commit f7bfe28

Browse files
refactor metrics push into action
1 parent a9007dc commit f7bfe28

File tree

2 files changed

+67
-47
lines changed

2 files changed

+67
-47
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: Push logged metrics
2+
description: Push logged metrics
3+
inputs:
4+
metricsKey:
5+
description: Private key for signing messages
6+
required: true
7+
runs:
8+
using: composite
9+
steps:
10+
- name: Do it
11+
continue-on-error: true
12+
shell: bash
13+
run: |
14+
export SFPOWERSCRIPTS_STATSD=true
15+
echo "statsd: $SFPOWERSCRIPTS_STATSD"
16+
bash -c 'echo "statsd: $SFPOWERSCRIPTS_STATSD"'
17+
sfp pool prepare --poolconfig "${{ matrix.pool.poolConfigPath }}" --targetdevhubusername devhub
18+
if [[ -f .sfpowerscripts/logs/metrics.log ]]; then
19+
# send some stats whilst we're at it
20+
echo Metrics file found.
21+
nl .sfpowerscripts/logs/metrics.log
22+
metrics=$(jq --slurp --raw-output '
23+
[
24+
.[] | select(.value != null)
25+
] |
26+
# just keep the last value
27+
reverse | unique_by(.metric, .tags) |
28+
group_by(.type)[][] |
29+
(
30+
if .type == "count" then "counter"
31+
# note the spelling
32+
elif .type == "guage" then "gauge"
33+
# statsd-timers are always in ms. prometheus has no such type, so just
34+
# use a gauge
35+
elif .type == "timers" then "gauge"
36+
# anything else, handle it when the time comes
37+
else "unknown" end) as $type |
38+
# only allow alpha and _ in metrics name
39+
(.metric | gsub("[^a-z]"; "_")) as $metric |
40+
# transform tags into prometheus
41+
(.tags // {} | to_entries | map(.key + "=\"" + .value + "\"") | join(",")) as $tags |
42+
# put it all together. one line with description, one line with metric.
43+
"# TYPE \($metric) \($type)\n\($metric){\($tags)} \(.value)"
44+
' .sfpowerscripts/logs/metrics.log | awk '/^# TYPE / && a[$3]++ {next}; 1')
45+
echo "METRICSKEY len: ${#METRICSKEY}"
46+
keyfile=$(mktemp) || exit 1
47+
cat > "$keyfile" <<< "$METRICSKEY"
48+
sig=$(printf %s "$metrics" | openssl dgst -sha256 -sign "$keyfile" -out - | base64 -w0)
49+
jq --compact-output --null-input \
50+
--arg metrics "$metrics" \
51+
--arg runner sf-platform \
52+
--arg sig "$sig" \
53+
'{"runner":$runner,"metrics":$metrics,"signature":$sig}' |
54+
tee >(curl -D- -H 'Content-Type: application/json' --data-binary @- \
55+
https://sf-github-metrics.ekstern.dev.nav.no/measures/job/sfplatform)
56+
else
57+
echo No metrics file this time.
58+
ls -laR .sfpowerscripts
59+
fi
60+
env:
61+
METRICSKEY: ${{ inputs.metricsKey }}

.github/workflows/ciCreatePlatformPool.yml

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -194,58 +194,17 @@ jobs:
194194
deleteJobType: ${{ matrix.pool.deleteJobType }}
195195
isCiPool: ${{ matrix.pool.isCiPool }}
196196

197-
- name: "Create Pool, push metrics"
197+
- name: "Create Pool"
198198
run: |
199-
export SFPOWERSCRIPTS_STATSD=true
200-
echo "statsd: $SFPOWERSCRIPTS_STATSD"
201-
bash -c 'echo "statsd: $SFPOWERSCRIPTS_STATSD"'
202199
sfp pool prepare --poolconfig "${{ matrix.pool.poolConfigPath }}" --targetdevhubusername devhub
203-
if [[ -f .sfpowerscripts/logs/metrics.log ]]; then
204-
# send some stats whilst we're at it
205-
echo Metrics file found.
206-
nl .sfpowerscripts/logs/metrics.log
207-
metrics=$(jq --slurp --raw-output '
208-
[
209-
.[] | select(.value != null)
210-
] |
211-
# just keep the last value
212-
reverse | unique_by(.metric, .tags) |
213-
group_by(.type)[][] |
214-
(
215-
if .type == "count" then "counter"
216-
# note the spelling
217-
elif .type == "guage" then "gauge"
218-
# statsd-timers are always in ms. prometheus has no such type, so just
219-
# use a gauge
220-
elif .type == "timers" then "gauge"
221-
# anything else, handle it when the time comes
222-
else "unknown" end) as $type |
223-
# only allow alpha and _ in metrics name
224-
(.metric | gsub("[^a-z]"; "_")) as $metric |
225-
# transform tags into prometheus
226-
(.tags // {} | to_entries | map(.key + "=\"" + .value + "\"") | join(",")) as $tags |
227-
# put it all together. one line with description, one line with metric.
228-
"# TYPE \($metric) \($type)\n\($metric){\($tags)} \(.value)"
229-
' .sfpowerscripts/logs/metrics.log | awk '/^# TYPE / && a[$3]++ {next}; 1')
230-
echo "METRICSKEY len: ${#METRICSKEY}"
231-
keyfile=$(mktemp) || exit 1
232-
cat > "$keyfile" <<< "$METRICSKEY"
233-
sig=$(printf %s "$metrics" | openssl dgst -sha256 -sign "$keyfile" -out - | base64 -w0)
234-
jq --compact-output --null-input \
235-
--arg metrics "$metrics" \
236-
--arg runner sf-platform \
237-
--arg sig "$sig" \
238-
'{"runner":$runner,"metrics":$metrics,"signature":$sig}' |
239-
tee >(curl -D- -H 'Content-Type: application/json' --data-binary @- \
240-
https://sf-github-metrics.ekstern.dev.nav.no/measures/job/sfplatform)
241-
else
242-
echo No metrics file this time.
243-
ls -laR .sfpowerscripts
244-
fi
245200
env:
246-
METRICSKEY: ${{ secrets.METRICS_KEYS }}
247201
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
248202

203+
- name: Push logged metrics
204+
uses: navikt/sf-platform/.github/actions/pushLoggedMetrics@main
205+
with:
206+
metricsKey: ${{ secrets.METRICS_KEYS }}
207+
249208
- name: "Delete orphans"
250209
uses: navikt/sf-gha-deleteScratchPool@1f0986d2312a2969e658946acb496f6dcd7d4032
251210
with:

0 commit comments

Comments
 (0)