-
Notifications
You must be signed in to change notification settings - Fork 1
/
update_metrics
executable file
·275 lines (236 loc) · 12 KB
/
update_metrics
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/bin/bash
## This script spins up a local CRDB cluster using my `spinup` program,
## accesses its http://localhost:8080/_admin/v1/chartcatalog endpoint,
## and extracts all metrics and their descriptions. It then updates the
## existing metrics MD includes (for both core and serverless). Must be run
## from within your docs repo.
##
## Requirements:
##
## - cockroach : Put: somewhere in $PATH, see note in `spinup` to support
## multiple versions:
## - spinup : Get: https://github.com/andf-crl/tools/blob/main/spinup
## - jq : Run: brew install jq
## - GH token : Do: all steps in next section to find your GITHUB_TOKEN
##
## Usage (one of):
##
## update_metrics
## update_metrics -v v23.1
##
## Picking your VERSION:
##
## - If you do not specify a version with -v, update_metrics will use the
## current version of versions.stable from _config_cockroachdb.yml, and
## expects that your `cockroach` binary is the same version!! If not, this
## script errors and exits.
## - If you do specify a version with -v, update_metrics will use that, and
## assume that you have set up versioned `cockroach` symlinks as
## described in my description to `spinup`, like `cockroach-22.2` for
## example. If not, this script errors and exits.
##
## Effects of selecting a VERSION:
##
## - update_metrics will run the specified VERSION of CRDB. You must have set
## up your binary symlinks as described in the description to `spinup`.
## - update_metrics will update the specific VERSION directory of the docs:
## _includes/$VERSION/metric-names.md
## _includes/$VERSION/metric-names-serverless.md
##
## IMPORTANT: This script cannot download updated `cockroach` binaries for you!
## YOU are responsible for making sure the version of `cockroach` that
## you have on your computer is the latest minor revision of the major
## version you are seeking to update. TLDR: go download latest `cockroach`
## binaries before running this!!
##
## --> https://www.cockroachlabs.com/docs/releases/index.html <--
##
############################################################
## YOUR VALUES ##
#################
## You must have a valid unepxired GitHub session token to the `cockroach` repo
## in order for this script to be able to access the upstream file that defines
## which metrics are availble in Serverless. To generate this value:
## 1. In your browser, go to https://github.com/cockroachlabs/managed-service/blob/master/pkg/otel/assets/metrics.yaml
## 2. Click the Raw button.
## 3. Your URL will now contain the GitHub token for your session (valid for ~ 60 min).
## 4. Paste just the token below as the value to GITHUB_TOKEN.
## 5. Save this script, and re-run.
GITHUB_TOKEN=GHSAT0AAAAAABV4W223QUEJCLXZJ7VNOBCGY7BP7EA
############################################################
## BEGIN SCRIPT ##
##################
## No user-servicible parts below :)
TMPSPACE=/tmp/update_metrics$$
SERVERLESS_METRICS_YAML="https://raw.githubusercontent.com/cockroachlabs/managed-service/master/pkg/otel/assets/metrics.yaml?token=$GITHUB_TOKEN"
## Check for arguments passed to this script:
while getopts ":v:" opt; do
case ${opt} in
v)
VERSION=`echo $OPTARG | sed 's%^v%%g'`
;;
\? )
echo -e "\nERROR: Invalid option: $OPTARG. The following are the supported flags:" 1>&2
echo -e " -v: which version of CRDB to update metrics for\n"
exit;
;;
: )
echo -e "\nERROR: If you specify -$OPTARG, you must provide a value." 1>&2
;;
esac
done
## Exit if not in a valid git repo:
GITROOT=`git rev-parse --show-toplevel 2>&1`
if [ `echo $GITROOT | grep -c '^fatal: not a git repository'` -gt 0 ]; then
echo -e "\nERROR: Not in a git repo!"
echo -e " You must run this program from within the git branch that you wish to update.\n"
exit;
fi
## Ensure we have a valid Github token for our curl later:
GITHUB_HTTP_RESPONSE=`curl -s -o /dev/null -w "%{http_code}" $SERVERLESS_METRICS_YAML`
if [ $GITHUB_HTTP_RESPONSE != 200 ]; then
echo -e "\nERROR: Your Github auth token is expired or invalid. You must do the following:"
echo " 1. In your browser, go to:"
echo " https://github.com/cockroachlabs/managed-service/blob/master/pkg/otel/assets/metrics.yaml"
echo " 2. Click the Raw button."
echo " 3. Your URL will now contain the GitHub token for your session (valid for ~ 60 min)."
echo " 4. Paste just the token as the value to the GITHUB_TOKEN variable in $(basename $0)."
echo " 5. Save script, and re-run."
echo ""
echo -e " Exiting ...\n"
exit;
fi
## Determine what to do with VERSION:
if [[ ! -z $VERSION && `echo $VERSION | egrep -c '^[0-9]{1,2}\.[1-2]$'` -ne 1 ]]; then
echo -e "\nERROR: Malformed version. Include a major version like 21.2 or 21.1."
echo -e " Exiting ...\n"
exit;
elif [[ ! -z $VERSION && "$VERSION" = "21.1" || "$VERSION" = "20.2" || "$VERSION" = "20.1" || "$VERSION" = "19.2" || "$VERSION" = "19.1" ]]; then
## Not bothering to check previous to v19.1.
echo -e "\nERROR: Unfortunately, v21.1 and previously do not offer chartcatalog output"
echo " As such, this script can only be used for CRDB versions v21.2 or later."
echo ""
echo -e " Exiting ...\n"
exit;
elif [ -z $VERSION ]; then
STABLE_VERSION=`grep 'stable:' $GITROOT/_config_cockroachdb.yml | awk -F ': ' '{print $2}' | sed 's%^v%%g'`
LOCAL_VERSION=`cockroach --version | grep 'Build Tag:' | awk -F ' v' '{print $2}'`
LOCAL_VERSION_SHORT=`echo $LOCAL_VERSION | egrep -o '[0-9]{1,2}\.[0-9]'`
if [ $STABLE_VERSION == $LOCAL_VERSION_SHORT ]; then
echo -e "\nYou did not specify a version using the -v flag, so using your local \`cockroach\`"
echo "binary version of $LOCAL_VERSION and updating the $STABLE_VERSION version of the docs."
echo ""
echo "IMPORTANT: Is your local $LOCAL_VERSION version of \`cockroach\` the latest version??"
echo ""
echo " If not, you should ^C and go and update it first, before re-running $(basename $0)!"
echo " ... this script cannot update it for you!"
else
echo -e "\nERROR: You did not specify a version using the -v flag, so using your"
echo " local \`cockroach\` binary version of $LOCAL_VERSION. However this does"
echo " NOT match your docs repo's defined version for Stable of $STABLE_VERSION."
echo ""
echo " Please make sure that:"
echo " - docs have already been cut for the $LOCAL_VERSION version,"
echo " - you use the -v flag to $(basename $0) to specify a version of \`cockroach\`"
echo " that you do have locally on your machine (see my description to \`spinup\`"
echo " for instructions on how to set up versioned \`cockroach-XX.X\` symlinks to"
echo " support this), or"
echo " - if you don't use the -v flag to $(basename $0), ensure your local \`cockroach\`"
echo " is the version you expect."
echo ""
echo -e " Exiting ...\n"
exit;
fi
else
LOCAL_VERSION=`cockroach-$VERSION --version | grep 'Build Tag:' | awk -F ' v' '{print $2}'`
LOCAL_VERSION_SHORT=`echo $LOCAL_VERSION | egrep -o '[0-9]{1,2}\.[0-9]'`
echo ""
echo "IMPORTANT: Is your local $LOCAL_VERSION version of \`cockroach\` the latest version??"
echo ""
echo " If not, you should ^C and go and update it first, before re-running $(basename $0)!"
echo " ... this script cannot update it for you!"
fi
## Use `spinup` to build a quick local cluster based on the requested version
## See script comments in `spinup` for guidance on configuring your local
## CRDB versions to support this.
if [ -z $VERSION ]; then
VERSION=$STABLE_VERSION
echo -e "\nSpinning up a CockroachDB $LOCAL_VERSION cluster to get the latest chartcatalog output"
## Both: starts a cluster, and gets its DB Console web URL:
DBCONSOLE_URL=`spinup start | grep -o 'DB Console:.*' | awk -F ': ' '{print $2}'`
else
echo -e "\nSpinning up a CockroachDB $LOCAL_VERSION cluster to get the latest chartcatalog output"
## Both: starts a cluster, and gets its DB Console web URL:
DBCONSOLE_URL=`spinup -v $VERSION start | grep -o 'DB Console:.*' | awk -F ': ' '{print $2}'`
fi
## Give the cluster a few seconds to be ready to serve HTTP endpoints.
## Without this, the HTTP endpoint sometimes 404s:
sleep 5
## Append chartcatalog HTTP endpoint path to returned DB Console landing URL:
CHARTCATALOG_URL="$DBCONSOLE_URL/_admin/v1/chartcatalog"
## Get full nested JSON tree from CHARTCATALOG_RUL and remove / replace all found
## newline characters with their appropriate space or space+period combinations:
curl -s $CHARTCATALOG_URL | sed 's%\.\\n\\n%\. %g' | sed 's%\\n\\n%\. %g' | sed 's%\.\\n%\. %g' | sed 's%\\n% %g' > $TMPSPACE.chartcatalog
## There are two nested depths for metrics definitions in the JSON tree
## so we must ingest our 'name' and 'help' keys from both. Here 'help'
## is our descriptions field:
cat $TMPSPACE.chartcatalog | jq -r '.catalog[] | .subsections[] | .charts[] | .metrics[] | "\(.name)|\(.help)"' > $TMPSPACE.parsedcatalog
cat $TMPSPACE.chartcatalog | jq -r '.catalog[] | .subsections[] | .subsections[] | .charts[] | .metrics[] | "\(.name)|\(.help)"' >> $TMPSPACE.parsedcatalog
# Bash field separator set to newline:
IFS="
"
## Get upstream's Serverless metrics subset (for which we already got our GitHub token, right?):
curl -s $SERVERLESS_METRICS_YAML > $TMPSPACE.metricslistraw
for LINE in `cat $TMPSPACE.metricslistraw`; do
if [[ ! $LINE == "metrics:" ]]; then
echo $LINE | sed 's%^- %%g' >> $TMPSPACE.metricslist
fi
done
#cat $TMPSPACE.metricslist
#exit;
## Recurse through each returned metric and check to see if it is
## already in our metric-names.md file. If not, add it to the running
## list and tally up:
MISSING_METRICS_COUNT=0
echo "Comparing metrics found to metrics listed in metric-names.md"
for LINE in `cat $TMPSPACE.parsedcatalog`; do
METRIC_NAME=`echo $LINE | awk -F '|' '{print $1}'`
METRIC_HELP=`echo $LINE | awk -F '|' '{print $2}'`
## Because upstream chose to list all metrics with underscores representing all of:
## - periods
## - hyphens
## - oh and also underscores too,
## we have to build this regex also for each:
METRIC_NAME_REGEX=`echo $METRIC_NAME | sed 's%\_%\.%g' | sed 's%\-%\.%g'`
## If: Metric exists in $SERVERLESS_METRICS_YAML
## Then: write it to a metrics_serverless file:
#echo "for $METRIC_NAME made regex $METRIC_NAME_REGEX"
if [[ `egrep -c "$METRIC_NAME_REGEX" $TMPSPACE.metricslist` -gt 0 ]]; then
echo "\`$METRIC_NAME\` | $METRIC_HELP" >> $TMPSPACE.metrics_serverless
((MISSING_METRICS_COUNT++))
#echo " --> and we found it upstream"
fi
done
## First, add missed metrics and sort based on first column:
cat $TMPSPACE.metrics_serverless | sort -t\| -k1 | uniq > $TMPSPACE.metrics_serverless_sorted
## Second, add header to blank file, then copy in merged, sorted metrics list:
echo "Name | Description" > $TMPSPACE.new_assembled
echo "-----|------------" >> $TMPSPACE.new_assembled
cat $TMPSPACE.metrics_serverless_sorted >> $TMPSPACE.new_assembled
## Lastly, replace existing metric-names.md file with merged, sorted list:
mv $TMPSPACE.new_assembled _includes/v$VERSION/metric-names-serverless.md
## See if any were specified upstream but not present in chartcatalog, for
## likely further investigation:
for LINE in `cat $TMPSPACE.metricslist`; do
## Again, we have to convert to a regex because upstream uses underscores for all
## of: periods, hyphens, and also underscores:
LINE_REGEX=`echo $LINE | sed 's%\_%\.%g' | sed 's%\-%\.%g'`
echo "for $LINE made regex $LINE_REGEX"
if [ `egrep -c $LINE_REGEX _includes/v$VERSION/metric-names-serverless.md` -lt 1 ]; then
echo "WARNING: $LINE from upstream missing in chartcatalog output for v$VERSION"
fi
done
## Clean up:
rm -rf $TMPSPACE
echo "Found $MISSING_METRICS_COUNT missing metrics"
exit;