Skip to content

Instantly share code, notes, and snippets.

@jcabmora
Created May 26, 2016 01:11
Show Gist options
  • Save jcabmora/d7bbab37f82290f67b8b9501e6fa40d2 to your computer and use it in GitHub Desktop.
Save jcabmora/d7bbab37f82290f67b8b9501e6fa40d2 to your computer and use it in GitHub Desktop.
Script to test Heroic's query/metrics API
#!/bin/bash
echo "Execution time:$(date)"
#step 1: get an approximation of the server date in millis
DATE0=`curl -s -H "Content-Type: application/json" http://localhost:8080/query/metrics -d '{"range": {"type": "relative", "unit": "SECONDS", "value": 1}}' | jq -r '.range.end'`
echo "Now: $DATE0"
#step 2: call write API
let DATE1=$DATE0-1000
DATA='{"series": {"key": "foo", "tags": {"site": "lon", "host": "www.example.com"}}, "data": {"type": "points", "data": [['${DATE0}', 0.0], ['${DATE1}', 1.0]]}}'
echo "Calling /write API with data: $DATA"
WRITE_RESP=`curl -s -H "Content-Type: application/json" http://localhost:8080/write -d "$DATA"`
echo "Write Response: $WRITE_RESP"
let RANGEEND=$DATE0+1500
let RANGESTART=$DATE0-1500
ABSRANGE='{"range":{"type": "absolute", "start":'${RANGESTART}',"end":'${RANGEEND}'}}'
echo $ABSRANGE
#step 3: call the query/metrics API to get datapoints in the last 5 seconds, last 1 minute and last 3 days
QUERY0_RESP=`curl -s -H "Content-Type: application/json" http://localhost:8080/query/metrics -d "$ABSRANGE"`
QUERY0_RESULTS=`echo $QUERY0_RESP | jq -r '.result[].values'`
QUERY0_DATERANGE=`echo $QUERY0_RESP | jq -r '.range'`
echo "query/metrics API response with absolute range of +-1500 ms (range: $QUERY0_DATERANGE): $QUERY0_RESULTS"
QUERY1_RESP=`curl -s -H "Content-Type: application/json" http://localhost:8080/query/metrics -d '{"range": {"type": "relative", "unit": "SECONDS", "value": 5}}' `
QUERY1_RESULTS=`echo $QUERY1_RESP | jq -r '.result[].values'`
QUERY1_DATERANGE=`echo $QUERY1_RESP | jq -r '.range'`
echo "query/metrics API response with 5 seconds (range: $QUERY1_DATERANGE): $QUERY1_RESULTS"
QUERY2_RESP=`curl -s -H "Content-Type: application/json" http://localhost:8080/query/metrics -d '{"range": {"type": "relative", "unit": "MINUTES", "value": 5}}'`
QUERY2_RESULTS=`echo $QUERY2_RESP | jq -r '.result[].values'`
QUERY2_DATERANGE=`echo $QUERY2_RESP | jq -r '.range'`
echo "query/metrics API response with 5 minutes (range: $QUERY2_DATERANGE): $QUERY2_RESULTS"
QUERY3_RESP=`curl -s -H "Content-Type: application/json" http://localhost:8080/query/metrics -d '{"range": {"type": "relative", "unit": "DAYS", "value": 3}}'`
QUERY3_RESULTS=`echo $QUERY3_RESP | jq -r '.result[].values'`
QUERY3_DATERANGE=`echo $QUERY3_RESP | jq -r '.range'`
echo "query/metrics API response with 3 days (range: $QUERY3_DATERANGE): $QUERY3_RESULTS"
echo "End time: $(date)"
@jcabmora
Copy link
Author

You'll need jq installed on the system where this is run, and you'll also need to update the localhost:8080 to the instance where you are running Heroic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment