API Metrics
The public APIs can be used to retrieve metrics about the cluster and send scaling requests.
See Cloud API usage guide for information about creating a cloud user and key, and how to call available APIs. After creating a valid bearer token, you can automate a way to scale your cluster when a metric hits a certain threshold.
The following is an example script to give you an idea about how you could script something to scale based on a write threshold returned by the metrics endpoint. This is not meant to be a real-world script.
#!/bin/bashget_latest_write_count() { local response=$(curl -s https://api.aerospike.cloud/v2/databases/$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.cloud/v2/databases/$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.cloud/v2/databases/$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.cloud/v2/databases/$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