-
Notifications
You must be signed in to change notification settings - Fork 10
feat: add edge urls #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -141,28 +141,61 @@ PAYLOAD=$(jq -cn \ | |
| # shellcheck disable=SC1090 | ||
| eval "$("$SCRIPT_DIR/config" axiom "$DEPLOYMENT")" | ||
|
|
||
| CACHE_DIR="${TMPDIR:-/tmp}/axiom-edge-cache" | ||
| REGIONS_CACHE="$CACHE_DIR/regions__${DEPLOYMENT}" | ||
| if [[ ! -f "$REGIONS_CACHE" ]]; then | ||
| if [[ "$AXIOM_URL" == *"cloud.axiom.co"* ]]; then | ||
| APP_URL="https://app.axiom.co" | ||
| else | ||
| APP_URL="${AXIOM_URL/api./app.}" | ||
| fi | ||
| REGIONS_JSON=$("$SCRIPT_DIR/curl-auth" axiom "$DEPLOYMENT" -X GET "${APP_URL}/api/internal/regions/axiom" 2>/dev/null) || true | ||
| if [[ -n "$REGIONS_JSON" ]]; then | ||
| mkdir -p "$CACHE_DIR" | ||
| echo "$REGIONS_JSON" > "$REGIONS_CACHE" | ||
| fi | ||
| fi | ||
|
|
||
| LAST_EDGE_CACHE="$CACHE_DIR/last-edge__${DEPLOYMENT}" | ||
| if [[ -f "$LAST_EDGE_CACHE" ]]; then | ||
| FIRST_EDGE=$(cat "$LAST_EDGE_CACHE") | ||
| elif [[ -f "$REGIONS_CACHE" ]]; then | ||
| FIRST_EDGE=$(jq -r '.axiom[] | select(.environmentDefault == true) | .domain // empty' "$REGIONS_CACHE" 2>/dev/null) | ||
| fi | ||
| EDGE_URLS="${FIRST_EDGE:-}" | ||
| if [[ -f "$REGIONS_CACHE" ]]; then | ||
| OTHER_URLS=$(jq -r '.axiom[].domain // empty' "$REGIONS_CACHE" 2>/dev/null | grep -v "^${FIRST_EDGE:-}$" || true) | ||
| EDGE_URLS="$EDGE_URLS | ||
| $OTHER_URLS" | ||
| fi | ||
|
|
||
| RESP_HEADERS=$(mktemp) | ||
| RESP_BODY=$(mktemp) | ||
| cleanup() { | ||
| rm -f "$RESP_HEADERS" "$RESP_BODY" | ||
| } | ||
| trap cleanup EXIT | ||
|
|
||
| # Execute query and pipe to formatter | ||
| HTTP_CODE=$(curl -sS -o "$RESP_BODY" -D "$RESP_HEADERS" -w "%{http_code}" \ | ||
| -X POST "$AXIOM_URL/v1/datasets/_apl?format=tabular" \ | ||
| -H "Authorization: Bearer $AXIOM_TOKEN" \ | ||
| -H "X-Axiom-Org-Id: $AXIOM_ORG_ID" \ | ||
| -H "Content-Type: application/json" \ | ||
| -d "$PAYLOAD") | ||
| QUERY_OK=false | ||
| for QUERY_URL in $EDGE_URLS; do | ||
| HTTP_CODE=$(curl -sS -o "$RESP_BODY" -D "$RESP_HEADERS" -w "%{http_code}" \ | ||
| -X POST "${QUERY_URL}/v1/query/_apl?format=tabular" \ | ||
| -H "Authorization: Bearer $AXIOM_TOKEN" \ | ||
| -H "X-Axiom-Org-Id: $AXIOM_ORG_ID" \ | ||
| -H "Content-Type: application/json" \ | ||
| -d "$PAYLOAD") | ||
| if [[ "$HTTP_CODE" -ge 200 && "$HTTP_CODE" -lt 300 ]]; then | ||
| echo "$QUERY_URL" > "$LAST_EDGE_CACHE" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Problem: Agents can run multiple queries concurrently. This would be a race condition, this would clobber that file. We need a different solution. Is there a backend endpoint that returns the canonical edge URL for each dataset in an org? We already run the |
||
| QUERY_OK=true | ||
| break | ||
| fi | ||
| done | ||
|
|
||
| if [[ "$HTTP_CODE" -lt 200 || "$HTTP_CODE" -ge 300 ]]; then | ||
| if [[ "$QUERY_OK" != true ]]; then | ||
aisha331 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| msg=$(jq -r '.message // empty' "$RESP_BODY" 2>/dev/null) | ||
| trace=$(grep -i '^x-axiom-trace-id:' "$RESP_HEADERS" | tail -1 | awk '{print $2}' | tr -d '\r') | ||
| echo "error: ${msg:-http $HTTP_CODE}" >&2 | ||
| if [[ -n "$trace" ]]; then | ||
| echo "trace_id: $trace" >&2 | ||
| fi | ||
| [[ -n "${trace:-}" ]] && echo "trace_id: $trace" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| #!/usr/bin/env bash | ||
| # resolve-url: Resolve the regional edge URL for a dataset | ||
| # | ||
| # Usage: resolve-url <deployment> <dataset> | ||
| # | ||
| # Two-step resolution: | ||
| # 1. GET /v1/datasets → find dataset's region (e.g. cloud.us-east-1.aws) | ||
| # 2. GET /api/internal/regions/axiom → map region to edge domain | ||
| # | ||
| # Both steps are cached to avoid repeated API calls. | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
|
|
||
| DEPLOYMENT="${1:-}" | ||
| DATASET="${2:-}" | ||
|
|
||
| if [[ -z "$DEPLOYMENT" || -z "$DATASET" ]]; then | ||
| echo "Usage: resolve-url <deployment> <dataset>" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| CACHE_DIR="${TMPDIR:-/tmp}/axiom-edge-cache" | ||
|
|
||
| # Check dataset edge URL cache (valid for 1 hour) | ||
| DATASET_CACHE="$CACHE_DIR/edge__${DEPLOYMENT}__${DATASET}" | ||
| if [[ -f "$DATASET_CACHE" ]]; then | ||
| if [[ "$(uname)" == "Darwin" ]]; then | ||
| AGE=$(( $(date +%s) - $(stat -f %m "$DATASET_CACHE") )) | ||
| else | ||
| AGE=$(( $(date +%s) - $(stat -c %Y "$DATASET_CACHE") )) | ||
| fi | ||
| if [[ $AGE -lt 3600 ]]; then | ||
| cat "$DATASET_CACHE" | ||
| exit 0 | ||
| fi | ||
| fi | ||
|
|
||
| eval "$("$SCRIPT_DIR/config" axiom "$DEPLOYMENT")" | ||
|
|
||
| # Step 1: get dataset region | ||
| REGION=$("$SCRIPT_DIR/curl-auth" axiom "$DEPLOYMENT" -X GET "${AXIOM_URL}/v1/datasets" \ | ||
| | jq -r --arg name "$DATASET" '.[] | select(.name == $name) | .edgeDeployment // .region // empty' 2>/dev/null) | ||
|
|
||
| if [[ -z "$REGION" || "$REGION" == "null" ]]; then | ||
| mkdir -p "$CACHE_DIR" | ||
| echo "$AXIOM_URL" > "$DATASET_CACHE" | ||
| echo "$AXIOM_URL" | ||
| exit 0 | ||
| fi | ||
aisha331 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Step 2: look up edge domain from regions API (cached 24h per deployment) | ||
| REGIONS_CACHE="$CACHE_DIR/regions__${DEPLOYMENT}" | ||
| REGIONS_JSON="" | ||
| if [[ -f "$REGIONS_CACHE" ]]; then | ||
| if [[ "$(uname)" == "Darwin" ]]; then | ||
| AGE=$(( $(date +%s) - $(stat -f %m "$REGIONS_CACHE") )) | ||
| else | ||
| AGE=$(( $(date +%s) - $(stat -c %Y "$REGIONS_CACHE") )) | ||
| fi | ||
| if [[ $AGE -lt 86400 ]]; then | ||
| REGIONS_JSON=$(cat "$REGIONS_CACHE") | ||
| fi | ||
| fi | ||
|
|
||
| if [[ -z "$REGIONS_JSON" ]]; then | ||
| if [[ "$AXIOM_URL" == *"cloud.axiom.co"* ]]; then | ||
| APP_URL="https://app.axiom.co" | ||
| else | ||
| APP_URL="${AXIOM_URL/api./app.}" | ||
| fi | ||
aisha331 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| REGIONS_JSON=$("$SCRIPT_DIR/curl-auth" axiom "$DEPLOYMENT" -X GET "${APP_URL}/api/internal/regions/axiom" 2>/dev/null) || true | ||
| if [[ -n "$REGIONS_JSON" ]]; then | ||
| mkdir -p "$CACHE_DIR" | ||
| echo "$REGIONS_JSON" > "$REGIONS_CACHE" | ||
| fi | ||
aisha331 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| fi | ||
|
|
||
| EDGE_DOMAIN="" | ||
| if [[ -n "$REGIONS_JSON" ]]; then | ||
| EDGE_DOMAIN=$(echo "$REGIONS_JSON" | jq -r --arg region "$REGION" '.axiom[] | select(.id == $region) | .domain // empty' 2>/dev/null) | ||
| fi | ||
|
|
||
| if [[ -z "$EDGE_DOMAIN" ]]; then | ||
| mkdir -p "$CACHE_DIR" | ||
| echo "$AXIOM_URL" > "$DATASET_CACHE" | ||
| echo "$AXIOM_URL" | ||
| exit 0 | ||
| fi | ||
|
|
||
| # Cache the resolved edge URL | ||
| mkdir -p "$CACHE_DIR" | ||
| if [[ "$EDGE_DOMAIN" != https://* ]]; then | ||
| EDGE_DOMAIN="https://${EDGE_DOMAIN}" | ||
| fi | ||
| echo "$EDGE_DOMAIN" > "$DATASET_CACHE" | ||
|
|
||
| echo "$EDGE_DOMAIN" | ||
Uh oh!
There was an error while loading. Please reload this page.