Cloud Public API metrics
Automate scaling decisions and integrate cluster metrics into your own tooling with the Cloud Public API. Use the metrics endpoint to monitor usage and throughput programmatically and trigger scaling actions when thresholds are crossed.
To get started, create a Cloud API key and bearer token by following the Cloud API usage guide.
Example: Scale clusters based on write throughput
The following script demonstrates how to poll the metrics endpoint and scale your cluster when write throughput exceeds a threshold. Adapt this pattern to your own automation requirements.
#!/bin/bashget_latest_write_count() { local response=$(curl -s https://api.aerospike.com/v1/database/clusters/$ID/metrics --insecure -H "Authorization: Bearer $TOKEN") local latest_count=$(echo "$response" | jq -r '.transactions.writeCount.daily[-1][1]') echo "$latest_count"}
# Function to get cluster detailsadjust_cluster() { echo "Write count exceeded threshold! Fetching cluster details..."
local response=$(curl -s https://api.aerospike.com/v1/database/clusters/$ID --insecure -H "Authorization: Bearer $TOKEN" | jq 'del(.connectionDetails.tlsCertificate)')
local cluster_size=$(echo "$response" | jq -r '.aerospikeCloud.clusterSize') local replication_factor=$(echo "$response" | jq -r '.aerospikeServer.namespaces[0]["replication-factor"]')
echo "Cluster Size: $cluster_size" echo "Replication Factor: $replication_factor"
# Scale up the cluster scale_cluster "$cluster_size" "$replication_factor"}
# Function to scale cluster by increasing cluster size by replication factorscale_cluster() { local current_size=$1 local replication_factor=$2 local new_size=$((current_size + replication_factor))
echo "Scaling cluster from $current_size to $new_size nodes..."
# Create JSON payload for PATCH request local patch_payload=$(cat <<EOF{ "aerospikeCloud": { "clusterSize": $new_size }}EOF) # Execute PATCH request local patch_response=$(curl -s -X PATCH \ https://api.aerospike.com/v1/database/clusters/$ID \ --insecure \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json" \ -d "$patch_payload")
# Check if the request was successful local http_status=$(curl -s -o /dev/null -w "%{http_code}" -X PATCH \ https://api.aerospike.com/v1/database/clusters/$ID \ --insecure \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json" \ -d "$patch_payload")
if [[ "$http_status" -eq 200 || "$http_status" -eq 202 ]]; then echo "Cluster scaling initiated successfully (HTTP $http_status)" else echo "Cluster scaling failed (HTTP $http_status)" fi}
while true; do write_count=$(get_latest_write_count)
if [[ "$write_count" == "null" || "$write_count" == "" ]]; then echo "Error: Could not retrieve write count" elif [[ "$write_count" -gt "$THRESHOLD" ]]; then echo "Write count: $write_count (THRESHOLD EXCEEDED!)" adjust_cluster echo "I only want to scale once, exiting" exit 0 else echo "Write count: $write_count (below threshold)" fi
sleep $CHECK_INTERVALdone