Skip to content

Commit adfc051

Browse files
author
Wei Fu
authored
Merge pull request #2112 from allencloud/add-stats
feature: add stats api in daemon side
2 parents 0eed9c4 + 3af42dd commit adfc051

19 files changed

+1606
-27
lines changed

apis/server/container_bridge.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,27 @@ func (s *Server) logsContainer(ctx context.Context, rw http.ResponseWriter, req
351351
return nil
352352
}
353353

354+
func (s *Server) statsContainer(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
355+
name := mux.Vars(req)["name"]
356+
357+
var stream bool
358+
if _, ok := req.Form["stream"]; !ok {
359+
stream = true
360+
}
361+
stream = httputils.BoolValue(req, "stream")
362+
363+
if !stream {
364+
rw.Header().Set("Content-Type", "application/json")
365+
}
366+
367+
config := &mgr.ContainerStatsConfig{
368+
Stream: stream,
369+
OutStream: rw,
370+
}
371+
372+
return s.ContainerMgr.StreamStats(ctx, name, config)
373+
}
374+
354375
func (s *Server) resizeContainer(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
355376
height, err := strconv.Atoi(req.FormValue("h"))
356377
if err != nil {

apis/server/router.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func initRoute(s *Server) http.Handler {
5656
s.addRoute(r, http.MethodPost, "/containers/{name:.*}/upgrade", s.upgradeContainer)
5757
s.addRoute(r, http.MethodGet, "/containers/{name:.*}/top", s.topContainer)
5858
s.addRoute(r, http.MethodGet, "/containers/{name:.*}/logs", withCancelHandler(s.logsContainer))
59+
s.addRoute(r, http.MethodGet, "/containers/{name:.*}/stats", withCancelHandler(s.statsContainer))
5960
s.addRoute(r, http.MethodPost, "/containers/{name:.*}/resize", s.resizeContainer)
6061
s.addRoute(r, http.MethodPost, "/containers/{name:.*}/restart", s.restartContainer)
6162
s.addRoute(r, http.MethodPost, "/containers/{name:.*}/wait", withCancelHandler(s.waitContainer))

apis/swagger.yml

Lines changed: 265 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,44 @@ paths:
814814
default: "all"
815815
tags: ["Container"]
816816

817+
/containers/{id}/stats:
818+
get:
819+
summary: "Get container stats based on resource usage"
820+
description: |
821+
This endpoint returns a live stream of a container’s resource usage
822+
statistics.
823+
824+
The `precpu_stats` is the CPU statistic of the *previous* read, and is
825+
used to calculate the CPU usage percentage. It is not an exact copy
826+
of the `cpu_stats` field.
827+
828+
If either `precpu_stats.online_cpus` or `cpu_stats.online_cpus` is
829+
nil then for compatibility with older daemons the length of the
830+
corresponding `cpu_usage.percpu_usage` array should be used.
831+
operationId: "ContainerStats"
832+
produces: ["application/json"]
833+
responses:
834+
200:
835+
description: container stats
836+
schema:
837+
$ref: "#/definitions/ContainerStats"
838+
404:
839+
$ref: "#/responses/404ErrorResponse"
840+
500:
841+
$ref: "#/responses/500ErrorResponse"
842+
parameters:
843+
- name: "id"
844+
in: "path"
845+
required: true
846+
description: "ID or name of the container"
847+
type: "string"
848+
- name: "stream"
849+
in: "query"
850+
description: "Stream the output. If false, the stats will be output once and then it will disconnect."
851+
type: "boolean"
852+
default: true
853+
tags: ["Container"]
854+
817855
/containers/{id}/resize:
818856
post:
819857
summary: "changes the size of the tty for a container"
@@ -3382,6 +3420,7 @@ definitions:
33823420
x-nullable: false
33833421

33843422
ContainerLogsOptions:
3423+
description: The parameters to filter the log.
33853424
type: "object"
33863425
properties:
33873426
ShowStdout:
@@ -3410,8 +3449,233 @@ definitions:
34103449
type: "boolean"
34113450

34123451

3413-
description: The parameters to filter the log.
3452+
ContainerStats:
3453+
description: container stats almost from cgroup resource usage.
3454+
type: "object"
3455+
properties:
3456+
read:
3457+
description: read time of container stats.
3458+
type: "string"
3459+
format: date-time
3460+
id:
3461+
description: container id
3462+
type: "string"
3463+
name:
3464+
description: container name
3465+
type: "string"
3466+
pids_stats:
3467+
$ref: "#/definitions/PidsStats"
3468+
networks:
3469+
type: "object"
3470+
additionalProperties:
3471+
$ref: "#/definitions/NetworkStats"
3472+
memory_stats:
3473+
$ref: "#/definitions/MemoryStats"
3474+
blkio_stats:
3475+
$ref: "#/definitions/BlkioStats"
3476+
cpu_stats:
3477+
$ref: "#/definitions/CPUStats"
3478+
precpu_stats:
3479+
$ref: "#/definitions/CPUStats"
3480+
3481+
PidsStats:
3482+
description: PidsStats contains the stats of a container's pids
3483+
type: "object"
3484+
properties:
3485+
current:
3486+
description: Current is the number of pids in the cgroup
3487+
type: "integer"
3488+
format: "uint64"
3489+
limit:
3490+
description: |
3491+
Limit is the hard limit on the number of pids in the cgroup.
3492+
A "Limit" of 0 means that there is no limit.
3493+
type: "integer"
3494+
format: uint64
3495+
3496+
CPUStats:
3497+
description: CPUStats aggregates and wraps all CPU related info of container
3498+
type: "object"
3499+
properties:
3500+
cpu_usage:
3501+
$ref: "#/definitions/CPUUsage"
3502+
syetem_cpu_usage:
3503+
description: System CPU Usage
3504+
type: "integer"
3505+
format: "uint64"
3506+
online_cpus:
3507+
description: onine CPUs
3508+
type: "integer"
3509+
format: "uint32"
3510+
throttling_data:
3511+
$ref: "#/definitions/ThrottlingData"
3512+
3513+
CPUUsage:
3514+
description: CPUUsage stores All CPU stats aggregated since container inception.
3515+
type: "object"
3516+
properties:
3517+
total_usage:
3518+
description: Total CPU time consumed.
3519+
type: "integer"
3520+
format: "uint64"
3521+
percpu_usage:
3522+
description: Total CPU time consumed per core (Linux).
3523+
type: "array"
3524+
items:
3525+
type: "integer"
3526+
format: "uint64"
3527+
usage_in_kernelmode:
3528+
description: |
3529+
Time spent by tasks of the cgroup in kernel mode (Linux).
3530+
Units, nanoseconds (Linux)
3531+
type: "integer"
3532+
format: "uint64"
3533+
usage_in_usermode:
3534+
description: |
3535+
Time spent by tasks of the cgroup in user mode (Linux).
3536+
Units, nanoseconds (Linux)
3537+
type: "integer"
3538+
format: "uint64"
3539+
3540+
ThrottlingData:
3541+
description: ThrottlingData stores CPU throttling stats of one running container.
3542+
type: "object"
3543+
properties:
3544+
periods:
3545+
description: Number of periods with throttling active.
3546+
type: "integer"
3547+
format: "uint64"
3548+
throttled_periods:
3549+
description: Number of periods when the container hits its throttling limit.
3550+
type: "integer"
3551+
format: "uint64"
3552+
throttled_time:
3553+
description: Aggregate time the container was throttled for in nanoseconds.
3554+
type: "integer"
3555+
format: "uint64"
3556+
3557+
BlkioStats:
3558+
description: BlkioStats stores All IO service stats for data read and write.
3559+
type: "object"
3560+
properties:
3561+
io_service_bytes_recursive:
3562+
type: "array"
3563+
items:
3564+
$ref: "#/definitions/BlkioStatEntry"
3565+
io_serviced_recursive:
3566+
type: "array"
3567+
items:
3568+
$ref: "#/definitions/BlkioStatEntry"
3569+
io_queue_recursive:
3570+
type: "array"
3571+
items:
3572+
$ref: "#/definitions/BlkioStatEntry"
3573+
io_service_time_recursive:
3574+
type: "array"
3575+
items:
3576+
$ref: "#/definitions/BlkioStatEntry"
3577+
io_wait_time_recursive:
3578+
type: "array"
3579+
items:
3580+
$ref: "#/definitions/BlkioStatEntry"
3581+
io_merged_recursive:
3582+
type: "array"
3583+
items:
3584+
$ref: "#/definitions/BlkioStatEntry"
3585+
io_time_recursive:
3586+
type: "array"
3587+
items:
3588+
$ref: "#/definitions/BlkioStatEntry"
3589+
sectors_recursive:
3590+
type: "array"
3591+
items:
3592+
$ref: "#/definitions/BlkioStatEntry"
3593+
3594+
BlkioStatEntry:
3595+
description: BlkioStatEntry is one small entity to store a piece of Blkio stats
3596+
type: "object"
3597+
properties:
3598+
major:
3599+
type: "integer"
3600+
format: "uint64"
3601+
minor:
3602+
type: "integer"
3603+
format: "uint64"
3604+
op:
3605+
type: "string"
3606+
value:
3607+
type: "integer"
3608+
format: "uint64"
3609+
3610+
MemoryStats:
3611+
description: MemoryStats aggregates all memory stats since container inception on Linux.
3612+
type: "object"
3613+
properties:
3614+
usage:
3615+
description: current res_counter usage for memory
3616+
type: "integer"
3617+
format: "uint64"
3618+
max_usage:
3619+
description: maximum usage ever recorded.
3620+
type: "integer"
3621+
format: "uint64"
3622+
stats:
3623+
description: all the stats exported via memory.stat.
3624+
type: "object"
3625+
additionalProperties:
3626+
type: "integer"
3627+
format: "uint64"
3628+
failcnt:
3629+
description: number of times memory usage hits limits.
3630+
type: "integer"
3631+
format: "uint64"
3632+
limit:
3633+
description: xxx
3634+
type: "integer"
3635+
format: "uint64"
34143636

3637+
NetworkStats:
3638+
description: container stats almost from cgroup resource usage.
3639+
type: "object"
3640+
properties:
3641+
rx_bytes:
3642+
description: Bytes received.
3643+
type: "integer"
3644+
format: "uint64"
3645+
rx_packets:
3646+
description: Packets received.
3647+
type: "integer"
3648+
format: "uint64"
3649+
rx_errors:
3650+
description: Received errors.
3651+
type: "integer"
3652+
format: "uint64"
3653+
rx_dropped:
3654+
description: Incoming packets dropped.
3655+
type: "integer"
3656+
format: "uint64"
3657+
tx_bytes:
3658+
description: Bytes sent.
3659+
type: "integer"
3660+
format: "uint64"
3661+
tx_packets:
3662+
description: Packets sent.
3663+
type: "integer"
3664+
format: "uint64"
3665+
tx_errors:
3666+
description: Sent errors.
3667+
type: "integer"
3668+
format: "uint64"
3669+
tx_dropped:
3670+
description: Outgoing packets dropped.
3671+
type: "integer"
3672+
format: "uint64"
3673+
endpoint_id:
3674+
description: Endpoint ID.
3675+
type: "string"
3676+
instance_id:
3677+
description: Instance ID.
3678+
type: "string"
34153679

34163680
Status:
34173681
description: The status of the container. For example, "running" or "exited".

apis/types/blkio_stat_entry.go

Lines changed: 67 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)