Logging API Examples

Examples for the Logging API

Here are some common usage examples for the Logging API.

Prerequisites:

  • Bash

  • Curl

  • jq

  • DC/OS

  • DC/OS CLI must be installed, configured, and logged in.

  • Extract DCOS_URL and DCOS_AUTH_TOKEN from the DC/OS CLI:

    1. DCOS_URL="$(dcos config show core.dcos_url)"
    2. DCOS_URL="${DCOS_URL%/}" # strip trailing slash, if present
    3. DCOS_AUTH_TOKEN="$(dcos config show core.dcos_acs_token)"

Node Logs

Get the last 100 journal entries from a single node:

  1. curl -k -H "Authorization: token=${DCOS_AUTH_TOKEN}" "${DCOS_URL}/system/v1/logs/v1/range/?skip_prev=100"

Component Logs

Get the last 100 journal entries from a single component service:

Leading Mesos Master:

  1. curl -k -H "Authorization: token=${DCOS_AUTH_TOKEN}" "${DCOS_URL}/system/v1/leader/mesos/logs/v1/range/?skip_prev=100&filter=_SYSTEMD_UNIT:dcos-mesos-master.service"

Leading Marathon:

  1. curl -k -H "Authorization: token=${DCOS_AUTH_TOKEN}" "${DCOS_URL}/system/v1/leader/marathon/logs/v1/range/?skip_prev=100&filter=_SYSTEMD_UNIT:dcos-mesos-master.service"

Agent DNS Forwarder (Spartan):

  1. # select an agent ID
  2. AGENT_ID="$(dcos node --json | jq -r '.[0].id)')"
  3. curl -k -H "Authorization: token=${DCOS_AUTH_TOKEN}" "${DCOS_URL}/system/v1/agent/${AGENT_ID}/logs/v1/range/?skip_prev=1&filter=_SYSTEMD_UNIT:dcos-spartan.service"

Container Logs

IMPORTANT: The following example requires journald task logging, which by default is [disabled](/mesosphere/dcos/2.1/monitoring/logging/logging-reference/#compatibility).

Get the last 100 journal entries from a single service container:

  1. FRAMEWORK_NAME="marathon"
  2. APP_ID="nginx"
  3. # get the mesos task state json
  4. MESOS_STATE="$(curl -k -H "Authorization: token=${DCOS_AUTH_TOKEN}" ${DCOS_URL}/mesos/state)"
  5. TASK_STATE="$(echo "${MESOS_STATE}" | jq ".frameworks[] | select(.name == \"${FRAMEWORK_NAME}\") | .tasks[] | select(.name == \"${APP_ID}\")")"
  6. # extract values from the task json
  7. AGENT_ID="$(echo "${TASK_STATE}" | jq -r '.slave_id')"
  8. TASK_ID="$(echo "${TASK_STATE}" | jq -r '.id')"
  9. FRAMEWORK_ID="$(echo "${TASK_STATE}" | jq -r '.framework_id')"
  10. EXECUTOR_ID="$(echo "${TASK_STATE}" | jq -r '.executor_id')"
  11. CONTAINER_ID="$(echo "${TASK_STATE}" | jq -r '.statuses[0].container_status.container_id.value')"
  12. # default to container ID when executor ID is empty
  13. EXECUTOR_ID="${EXECUTOR_ID:-${TASK_ID}}"
  14. # get container/task logs
  15. curl -k -H "Authorization: token=${DCOS_AUTH_TOKEN}" "${DCOS_URL}/system/v1/agent/${AGENT_ID}/logs/v1/range/framework/${FRAMEWORK_ID}/executor/${EXECUTOR_ID}/container/${CONTAINER_ID}?skip_prev=100"

Tail

Get the last 10 journal entries and follow new events:

  1. curl -k -H "Authorization: token=${DCOS_AUTH_TOKEN}" "${DCOS_URL}/system/v1/logs/v1/stream/?skip_prev=10"

Range

Skip 100 entries from the beginning of the journal and return the next 10 entries:

  1. curl -k -H "Authorization: token=${DCOS_AUTH_TOKEN}" "${DCOS_URL}/system/v1/logs/v1/range/?skip_next=100&limit=10"

Plain Text

Skip 200 entries from the beginning of the journal and return the next entry in plain text:

  1. curl -k -H "Accept: text/plain" -H "Authorization: token=${DCOS_AUTH_TOKEN}" "${DCOS_URL}/system/v1/logs/v1/range/?skip_next=200&limit=1"

JSON

Skip 200 entries from the beginning of the journal and return the next entry in JSON:

  1. curl -k -H "Accept: application/json" -H "Authorization: token=${DCOS_AUTH_TOKEN}" "${DCOS_URL}/system/v1/logs/v1/range/?skip_next=200&limit=1"

Event Stream

Skip 200 entries from the beginning of the journal and return the next entry as an event stream:

  1. curl -k -H "Accept: text/event-stream" -H "Authorization: token=${DCOS_AUTH_TOKEN}" "${DCOS_URL}/system/v1/logs/v1/range/?skip_next=200&limit=1"

Event Stream Cursor

Get all journal entries after a specific cursor and stream new entries:

  1. # Get the 10th line in json and parse its cursor
  2. CURSOR="$(curl -k -H "Accept: application/json" -H "Authorization: token=${DCOS_AUTH_TOKEN}" "${DCOS_URL}/system/v1/logs/v1/range/?skip_next=9&limit=1" | jq -r ".cursor")"
  3. # Follow the stream in plain text starting at the 11th line using the cursor
  4. curl -k -H "Authorization: token=${DCOS_AUTH_TOKEN}" "${DCOS_URL}/system/v1/logs/v1/stream/" --get --data-urlencode "cursor=${CURSOR}"

The cursor must be URL encoded.

Range Cursor

Get 1,000 journal entries, 100 at a time:

  1. TIMES=10 # number of times to call the endpoint
  2. LINES=100 # number of log lines to retrieve per call
  3. CURSOR="" # first call uses an empty cursor to start from the beginning
  4. for i in $(seq 1 ${TIMES}); do
  5. BUFFER="$(curl -k -H "Accept: application/json" -H "Authorization: token=${DCOS_AUTH_TOKEN}" "${DCOS_URL}/system/v1/logs/v1/range/?limit=${LINES}" --get --data-urlencode "cursor=${CURSOR}")"
  6. echo "${BUFFER}"
  7. CURSOR="$(echo "${BUFFER}" | jq -r ".cursor" | tail -1)"
  8. done

Each call uses the cursor of the last entry from the previous call. The cursor must be URL encoded.