Skip to content

Commit 3276df4

Browse files
committed
pod/promtorture: collector improvements
Add args as labels on deployment/pod collector grabs the args off metrics collector grabs the pod and deployment manifests
1 parent 3c63daa commit 3276df4

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

testcases/promtorture/scripts/clean-collect-promtorture.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
# Deploy a promtorture with the supplied arguments, let it run, and grab some
44
# prometheus metrics + a tsdb snapshot and a metrics dump from it.
55
#
6+
# Args are passed through to promtorture directly. See:
7+
# go build
8+
# ./promtorture --help
9+
# for available options.
610

711
set -e -u -o pipefail
812

@@ -19,6 +23,7 @@ fi
1923
./scripts/kind-deploy.sh "$@"
2024

2125
# Wait for some scrapes
26+
# (should really hit the prom api for this)
2227
sleep 60
2328

2429
# Grab metrics, snapshot etc

testcases/promtorture/scripts/grab-metrics.sh

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#
66
# This should be in a real language not bash, but meh.
77
#
8+
# Set env-var TSDB_DUMP=true to save a full TSDB dump locally if desired.
9+
#
810

911
set -e -u -o pipefail -x
1012

@@ -19,6 +21,13 @@ echo 1>&2 "Dumping metrics to $tmpdir"
1921
# convenient way to record the promtorture invocation
2022
echo "$@" > "$tmpdir/args"
2123

24+
promtorture_pod="$("${kubectl[@]}" get pod -n default -l app=promtorture -o jsonpath='{.items[0].metadata.name}')"
25+
26+
mkdir -p "${tmpdir}/manifests"
27+
"${kubectl[@]}" get -n default deployment/promtorture -o yaml > "$tmpdir/manifests/promtorture-deployment.yaml"
28+
"${kubectl[@]}" get -n default pod/"${promtorture_pod}" -o yaml > "$tmpdir/manifests/promtorture-pod.yaml"
29+
"${kubectl[@]}" get -n monitoring podmonitor/promtorture -o yaml > "$tmpdir/manifests/promtorture-podmonitor.yaml"
30+
2231
socks5_port=31121
2332
socks5_host=localhost
2433
kubectl socks5-proxy -N socks5-proxy -p 31121 &
@@ -28,17 +37,22 @@ socks5_pid=$!
2837
# socks5-proxy script. For now we'll find the child proc and kill it.
2938
# It should also wait with timeout for the wrapper to exit, but bash's wait
3039
# lacks a timeout option...
31-
trap 'kill $(pgrep -P ${socks5_pid}); sleep 10; kill ${socks5_pid}' EXIT
40+
trap 'kill $(pgrep -P ${socks5_pid}); sleep 10; kill ${socks5_pid} >&/dev/null' EXIT
3241
export http_proxy="socks5://${socks5_host}:${socks5_port}"
3342
while : ; do
3443
# wait for proxy to be ready by checking prometheus is reachable
35-
if curl -sL http://prometheus-k8s.monitoring.svc.cluster.local:9090 > /dev/null; then
44+
if curl -sSL http://prometheus-k8s.monitoring.svc.cluster.local:9090 > /dev/null; then
3645
break
3746
fi
3847
sleep 1
3948
done
4049

41-
promtorture_pod="$("${kubectl[@]}" get pod -n default -l app=promtorture -o jsonpath='{.items[0].metadata.name}')"
50+
# Also grab the args from Prometheus, so we see what's really running.
51+
# This will only be reliable if there's only one recent instance of
52+
# promtorture running.
53+
curl -sSL -G --data-urlencode 'query=scrape_duration_seconds{container="promtorture",job="monitoring/promtorture",arguments=~".+"}' \
54+
"http://prometheus-k8s.monitoring.svc.cluster.local:9090/api/v1/query" \
55+
| yq --prettyPrint '.data.result[0].metric.arguments' > "$tmpdir/promtorture-args"
4256

4357
# metrics queries
4458
mkdir "$tmpdir/metrics"
@@ -50,7 +64,7 @@ function instant_query_promtool() {
5064

5165
function instant_query_curl() {
5266
echo '# query: ' "$1" > "$tmpdir/$2"
53-
curl -sL -G --data-urlencode "query=$1" "http://prometheus-k8s.monitoring.svc.cluster.local:9090/api/v1/query" | yq --prettyPrint .data >> "$tmpdir/$2"
67+
curl -sSL -G --data-urlencode "query=$1" "http://prometheus-k8s.monitoring.svc.cluster.local:9090/api/v1/query" | yq --prettyPrint .data >> "$tmpdir/$2"
5468
}
5569

5670
function instant_query() {
@@ -112,10 +126,19 @@ snap_name="$(./scripts/promapi -m snapshot | yq .data.name)"
112126
echo 1>&2 "Created tsdb snapshot ${snap_name}"
113127
./scripts/promcmd du -ks "/prometheus/snapshots/${snap_name}" > "$tmpdir/tsdb-snapshot/size-kb"
114128
mkdir "$tmpdir/tsdb-snapshot/raw"
115-
"${kubectl[@]}" cp "monitoring/prometheus-k8s-0:/prometheus/snapshots/${snap_name}/" "${tmpdir}/tsdb-snapshot/raw/"
129+
# work around https://github.com/kubernetes/kubernetes/pull/78622
130+
# and https://github.com/kubernetes/kubernetes/issues/77310 by cd'ing into the dir
131+
# containing colons in the pathname, avoiding the error
132+
# error: one of src or dest must be a local file specification
133+
# when kubectl cp misinterprets the path as a remote spec.
134+
(cd "${tmpdir}" && "${kubectl[@]}" cp "monitoring/prometheus-k8s-0:/prometheus/snapshots/${snap_name}/" "tsdb-snapshot/raw/")
135+
136+
# promtool has some tsdb analytics that should tell us a lot
137+
./scripts/promcmd promtool tsdb analyze "/prometheus/snapshots/${snap_name}" --limit=100 > "$tmpdir/tsdb-snapshot/analyze-all"
138+
./scripts/promcmd promtool tsdb analyze "/prometheus/snapshots/${snap_name}" --limit=100 --match='{job="monitoring/promtorture"}' > "$tmpdir/tsdb-snapshot/analyze-job-promtorture"
116139

117140
# metrics dump
118-
if [[ "${TSDB_DUMP}" == "true" ]]; then
141+
if [[ "${TSDB_DUMP:-}" == "true" ]]; then
119142
# we're dumping the tsdb snapshot here so we don't have to deal with running promtool's own
120143
# dump and grabbing the file from the container FS.
121144
#
@@ -130,4 +153,6 @@ if [[ "${TSDB_DUMP}" == "true" ]]; then
130153
./scripts/promcmd promtool tsdb dump --sandbox-dir-root="/prometheus/replay" "/prometheus/snapshots/${snap_name}" | ${compress} -c > "$tmpdir/tsdb-snapshot/tsdb-dump"."${compress}"
131154
fi
132155

156+
echo 1>&2 "Results written to ${tmpdir}"
157+
133158
# vim: set ts=2 sw=2 et ai :

testcases/promtorture/scripts/kind-deploy.sh

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,32 @@ __END__
4343

4444
# Templating the args in is a bit ugly
4545
ARGS_JSON="$(jq -c --null-input '$ARGS.positional' --args -- "$@")"
46-
ARGS_JSON="${ARGS_JSON}" yq --prettyPrint '.spec.template.spec.containers[0].args=env(ARGS_JSON)' > "${tmpdir}/patch-promtorture-deployment.yaml" <<__END__
46+
ARGS_JSON="${ARGS_JSON}" ARGS="$*" yq --prettyPrint '
47+
.spec.template.spec.containers[0].args=env(ARGS_JSON)
48+
| .metadata.annotations["promtorture/arguments"]=strenv(ARGS)
49+
| .spec.template.metadata.annotations["promtorture/arguments"]=strenv(ARGS)
50+
' \
51+
> "${tmpdir}/patch-promtorture-deployment.yaml" \
52+
<<__END__
4753
apiVersion: apps/v1
4854
kind: Deployment
4955
metadata:
5056
name: promtorture
5157
namespace: default
58+
annotations:
59+
promtorture/arguments: ""
5260
spec:
5361
template:
5462
spec:
63+
metadata:
64+
annotations:
65+
promtorture/arguments: ""
5566
containers:
5667
- name: promtorture
5768
__END__
5869

5970
kustomize build "${tmpdir}" \
71+
| tee /dev/stderr \
6072
| kapp deploy -a promtorture -f - -y \
6173
--default-label-scoping-rules=false \
6274
--apply-default-update-strategy=fallback-on-replace \

0 commit comments

Comments
 (0)