日志记录 API 示例

BETA

日志记录 API 示例

以下是日志记录 API 的一些常见使用示例。

先决条件:

  • Bash

  • Curl

  • jq

  • DC/OS

  • 必须安装、配置和登录 DC/OS CLI

  • 从 DC/OS CLI 提取 DCOS_URLDCOS_AUTH_TOKEN

    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)"

节点日志

从单个节点获取最后 100 个日志条目:

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

组件日志

从单个组件服务获取最后 100 个日志条目:

领导 Mesos 管理节点:

  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"

领导 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"

代理 DNS 转发器 (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"

容器日志

重要信息:以下示例需要进行 journald 任务日志记录,它默认为 禁用

从单个组件容器获取最后 100 个日志条目:

  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"

结尾

获取最后 10 个日记账条目并追踪新事件:

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

范围

从日志开头跳过 100 个条目,并返回接下来的 10 个条目:

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

纯文本

从日志开头跳过 200 个条目,并以纯文本返回下一个条目:

  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

从日志开头跳过 200 个条目,并以 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"

事件流

从日志开头跳过 200 个条目,并返回作为事件流的下一个条目:

  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"

事件流游标

获取特定游标后的所有日志条目并流式传输新条目:

  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}"

游标必须为 URL 编码。

范围游标

获取 1,000 个日志条目,每次 100 个:

  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

每次调用都使用上次调用中最后一个条目的光标。游标必须为 URL 编码。