I’ve been looking into how to measure the maximum number of concurrent users connected to the app in Device Sync.
In Data Services, I can use this Connections chart, but this could include connection pooling between the application using Device Sync and the Database itself.
The above image looks like it’s measuring the second arrow between “Application” and “Database” :
User Device —> Application —> Database
I’d like to measure the maximum number of concurrent connections between “User Device” and “Application” in the first arrow.
When I go to the application itself, and then go to Metrics, then choose Sync in the drop down menu under View, I can see the current connections:
This looks like what I want, but not sure how to get this historically. I checked the atlas CLI and Admin API logs, but couldn’t quite find what I’m looking for. Most examples seem to reference the connections to the database itself.
Thank you very much, that worked great. Sharing in case anyone else needs it.
#!/bin/bash
#
# Metrics Reference:
# https://www.mongodb.com/docs/atlas/app-services/admin/api/v3/#tag/metrics/operation/adminGetMetrics
# https://www.mongodb.com/docs/atlas/app-services/reference/metrics/#available-metrics
# https://www.mongodb.com/docs/atlas/app-services/admin/api/v3/
#
# API Authorization Setup
# I had no luck using a service account, so I used an actual user account
# https://www.mongodb.com/docs/atlas/configure-api-access/
# https://www.mongodb.com/docs/atlas/app-services/admin/api/v3/#section/Project-and-Application-IDs
#
# Available MongoDB Atlas granularity:
# 10 seconds: Retained for 8 hours
# 1 minute: Retained for 48 hours
# 5 minutes: Retained for 48 hours
# 1 hour: Retained for 63 days
# 1 day: Retained forever (Premium monitoring only)
#
# https://en.wikipedia.org/wiki/ISO_8601#Durations
#
# set -xv
START_DATE=$(date -u -d '63 days ago' +"%Y-%m-%dT%H:%M:%SZ")
END_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
GRANULARITY="PT1H"
WORK_DIRECTORY="${HOME}/mongo/output"
API_OUTPUT_FILE="${WORK_DIRECTORY}/api_output.csv"
FILTERED_OUTPUT_FILE="${WORK_DIRECTORY}/filtered_output.csv"
# Authorization
PUBLIC_KEY="<public key>"
PRIVATE_KEY="<private key>"
GROUP_ID="<group id>"
APP_ID="<app id>"
# Get an access token
ACCESS_RESPONSE=$(curl --request POST \
--header "Content-Type: application/json" \
--header "Accept: application/json" \
--data "{\"username\": \"${PUBLIC_KEY}\", \"apiKey\": \"${PRIVATE_KEY}\"}" \
"https://services.cloud.mongodb.com/api/admin/v3.0/auth/providers/mongodb-cloud/login")
# Extract the access_token and set it to a variable
ACCESS_TOKEN=$(echo ${ACCESS_RESPONSE} | jq -r '.access_token')
# Print the access_token to verify
#echo "Access token: ${ACCESS_TOKEN}"
# List all apps in a group
# You can run this to populate the APP_ID above if you do not know it
# Note this _id is different from the CLIENT_APP_ID !
#curl --request GET \
# --url "https://services.cloud.mongodb.com/api/admin/v3.0/groups/${GROUP_ID}/apps" \
# --header "Authorization: Bearer ${ACCESS_TOKEN}" \
# --header "Accept: application/json"
METRICS_RESPONSE=$(curl --silent --request GET \
--url "https://services.cloud.mongodb.com/api/admin/v3.0/groups/${GROUP_ID}/apps/${APP_ID}/metrics?granularity=${GRANULARITY}&start=${START_DATE}&end=${END_DATE}" \
--header "Authorization: Bearer ${ACCESS_TOKEN}" \
--header "Accept: application/json")
# Convert JSON to CSV using jq
echo "${METRICS_RESPONSE}" | jq -r '
.measurements[] |
.name as $name |
.units as $units |
.data_points[] |
[$name, $units, .timestamp, .value] |
@csv
' > ${API_OUTPUT_FILE}
# Filter lines containing "ACTIVE_OPEN_SYNC_SESSIONS"
grep "ACTIVE_OPEN_SYNC_SESSIONS" ${API_OUTPUT_FILE} > ${FILTERED_OUTPUT_FILE}
# Find maximum value for last column
MAX_CONNECTIONS=$(awk -F, 'NR > 1 {if ($NF > max) max=$NF} END {print max}' ${FILTERED_OUTPUT_FILE})
echo "Maximum number of connections: ${MAX_CONNECTIONS}"