Skip to content

Instantly share code, notes, and snippets.

@jmcarp
Created July 7, 2026 18:43
Show Gist options
  • Select an option

  • Save jmcarp/7e70932954a9b4e8cf966a7278077264 to your computer and use it in GitHub Desktop.

Select an option

Save jmcarp/7e70932954a9b4e8cf966a7278077264 to your computer and use it in GitHub Desktop.
oximeter_producer_cardinality.sh
#!/usr/bin/env bash
# THROWAWAY — measure per-producer cardinality: print "<id> <kind> <address> <port> <samples>"
# (tab-separated) per producer, where <samples> is the sample count from the producer's last
# successful collection. Straight from the oximeter collector HTTP API. Needs curl + jq;
# omdb only for URL discovery.
#
# URL discovery (in order):
# 1. explicit: ./oximeter_producer_cardinality.sh 'http://[fd00:...]:12223'
# 2. env: OXIMETER_URL='http://[fd00:...]:12223' ./oximeter_producer_cardinality.sh
# 3. fallback: scrape `omdb oximeter list-producers` stderr for the collector URL.
# (We only run one collector today; revisit if that changes.)
#
# Usage: ./oximeter_producer_cardinality.sh [URL] > producers.tsv
set -uo pipefail
API_VERSION="${API_VERSION:-1.0.0}" # collector API is versioned; header is required
command -v jq >/dev/null || { echo "jq not found on this host" >&2; exit 2; }
api() { curl -s -H "api-version: $API_VERSION" "$@"; }
# Discover the collector URL by scraping omdb's stderr note.
discover() {
command -v omdb >/dev/null || { echo "omdb not found; pass a URL explicitly" >&2; exit 2; }
omdb oximeter list-producers 2>&1 >/dev/null | sed -n 's/.*using Oximeter URL //p' | head -1
}
URL="${1:-${OXIMETER_URL:-}}"
[ -n "$URL" ] || URL="$(discover)"
[ -n "$URL" ] || { echo "no collector URL found (pass one explicitly, or set OXIMETER_URL)" >&2; exit 2; }
echo "# collector $URL" >&2
# 1. id + kind + address for every producer, one paginated list call (no per-producer fan-out here).
tok=''
while :; do
r="$(api "$URL/producers${tok:+?page_token=$tok}")"
# If the response isn't a page (e.g. an error doc), .items is null -> stop loudly.
if ! printf '%s' "$r" | jq -e '.items' >/dev/null 2>&1; then
echo "unexpected /producers response:" >&2
printf '%s\n' "$r" | jq '.' >&2 2>/dev/null || printf '%s\n' "$r" >&2
exit 1
fi
printf '%s' "$r" | jq -r '.items[] | [.id, .kind, .address] | @tsv'
tok="$(printf '%s' "$r" | jq -r '.next_page // empty')"
[ -n "$tok" ] || break
done \
| while IFS=$'\t' read -r id kind addr; do
# 2. samples come from each producer's details (last successful collection).
samples="$(api "$URL/producers/$id" | jq -r '.last_success.n_samples // "?"')"
port="${addr##*:}" # after the last ':' ([ipv6]:port and ipv4:port both work)
address="${addr%:*}" # before the last ':'
printf '%s\t%s\t%s\t%s\t%s\n' "$id" "$kind" "$address" "$port" "$samples"
done \
| sort -t$'\t' -k5,5 -rn # highest-cardinality producers first ("?" -> 0, sorts last)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment