Skip to content

Commit fcfefae

Browse files
committed
feature: add stats api in daemon side
Signed-off-by: Allen Sun <[email protected]>
1 parent 0f65a73 commit fcfefae

20 files changed

+1626
-29
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
@@ -55,6 +55,7 @@ func initRoute(s *Server) http.Handler {
5555
s.addRoute(r, http.MethodPost, "/containers/{name:.*}/upgrade", s.upgradeContainer)
5656
s.addRoute(r, http.MethodGet, "/containers/{name:.*}/top", s.topContainer)
5757
s.addRoute(r, http.MethodGet, "/containers/{name:.*}/logs", withCancelHandler(s.logsContainer))
58+
s.addRoute(r, http.MethodGet, "/containers/{name:.*}/stats", withCancelHandler(s.statsContainer))
5859
s.addRoute(r, http.MethodPost, "/containers/{name:.*}/resize", s.resizeContainer)
5960
s.addRoute(r, http.MethodPost, "/containers/{name:.*}/restart", s.restartContainer)
6061
s.addRoute(r, http.MethodPost, "/containers/{name:.*}/wait", withCancelHandler(s.waitContainer))

apis/swagger.yml

Lines changed: 267 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"
@@ -835,6 +873,8 @@ paths:
835873
description: "bad parameter"
836874
schema:
837875
$ref: "#/definitions/Error"
876+
404:
877+
$ref: "#/responses/404ErrorResponse"
838878
tags: ["Container"]
839879

840880
/containers/{id}/checkpoints:
@@ -3351,6 +3391,7 @@ definitions:
33513391
x-nullable: false
33523392

33533393
ContainerLogsOptions:
3394+
description: The parameters to filter the log.
33543395
type: "object"
33553396
properties:
33563397
ShowStdout:
@@ -3379,8 +3420,233 @@ definitions:
33793420
type: "boolean"
33803421

33813422

3382-
description: The parameters to filter the log.
3423+
ContainerStats:
3424+
description: container stats almost from cgroup resource usage.
3425+
type: "object"
3426+
properties:
3427+
read:
3428+
description: read time of container stats.
3429+
type: "string"
3430+
format: date-time
3431+
id:
3432+
description: container id
3433+
type: "string"
3434+
name:
3435+
description: container name
3436+
type: "string"
3437+
pids_stats:
3438+
$ref: "#/definitions/PidsStats"
3439+
networks:
3440+
type: "object"
3441+
additionalProperties:
3442+
$ref: "#/definitions/NetworkStats"
3443+
memory_stats:
3444+
$ref: "#/definitions/MemoryStats"
3445+
blkio_stats:
3446+
$ref: "#/definitions/BlkioStats"
3447+
cpu_stats:
3448+
$ref: "#/definitions/CPUStats"
3449+
precpu_stats:
3450+
$ref: "#/definitions/CPUStats"
3451+
3452+
PidsStats:
3453+
description: PidsStats contains the stats of a container's pids
3454+
type: "object"
3455+
properties:
3456+
current:
3457+
description: Current is the number of pids in the cgroup
3458+
type: "integer"
3459+
format: "uint64"
3460+
limit:
3461+
description: |
3462+
Limit is the hard limit on the number of pids in the cgroup.
3463+
A "Limit" of 0 means that there is no limit.
3464+
type: "integer"
3465+
format: uint64
3466+
3467+
CPUStats:
3468+
description: CPUStats aggregates and wraps all CPU related info of container
3469+
type: "object"
3470+
properties:
3471+
cpu_usage:
3472+
$ref: "#/definitions/CPUUsage"
3473+
syetem_cpu_usage:
3474+
description: System CPU Usage
3475+
type: "integer"
3476+
format: "uint64"
3477+
online_cpus:
3478+
description: onine CPUs
3479+
type: "integer"
3480+
format: "uint32"
3481+
throttling_data:
3482+
$ref: "#/definitions/ThrottlingData"
3483+
3484+
CPUUsage:
3485+
description: CPUUsage stores All CPU stats aggregated since container inception.
3486+
type: "object"
3487+
properties:
3488+
total_usage:
3489+
description: Total CPU time consumed.
3490+
type: "integer"
3491+
format: "uint64"
3492+
percpu_usage:
3493+
description: Total CPU time consumed per core (Linux).
3494+
type: "array"
3495+
items:
3496+
type: "integer"
3497+
format: "uint64"
3498+
usage_in_kernelmode:
3499+
description: |
3500+
Time spent by tasks of the cgroup in kernel mode (Linux).
3501+
Units, nanoseconds (Linux)
3502+
type: "integer"
3503+
format: "uint64"
3504+
usage_in_usermode:
3505+
description: |
3506+
Time spent by tasks of the cgroup in user mode (Linux).
3507+
Units, nanoseconds (Linux)
3508+
type: "integer"
3509+
format: "uint64"
3510+
3511+
ThrottlingData:
3512+
description: ThrottlingData stores CPU throttling stats of one running container.
3513+
type: "object"
3514+
properties:
3515+
periods:
3516+
description: Number of periods with throttling active.
3517+
type: "integer"
3518+
format: "uint64"
3519+
throttled_periods:
3520+
description: Number of periods when the container hits its throttling limit.
3521+
type: "integer"
3522+
format: "uint64"
3523+
throttled_time:
3524+
description: Aggregate time the container was throttled for in nanoseconds.
3525+
type: "integer"
3526+
format: "uint64"
33833527

3528+
BlkioStats:
3529+
description: BlkioStats stores All IO service stats for data read and write.
3530+
type: "object"
3531+
properties:
3532+
io_service_bytes_recursive:
3533+
type: "array"
3534+
items:
3535+
$ref: "#/definitions/BlkioStatEntry"
3536+
io_serviced_recursive:
3537+
type: "array"
3538+
items:
3539+
$ref: "#/definitions/BlkioStatEntry"
3540+
io_queue_recursive:
3541+
type: "array"
3542+
items:
3543+
$ref: "#/definitions/BlkioStatEntry"
3544+
io_service_time_recursive:
3545+
type: "array"
3546+
items:
3547+
$ref: "#/definitions/BlkioStatEntry"
3548+
io_wait_time_recursive:
3549+
type: "array"
3550+
items:
3551+
$ref: "#/definitions/BlkioStatEntry"
3552+
io_merged_recursive:
3553+
type: "array"
3554+
items:
3555+
$ref: "#/definitions/BlkioStatEntry"
3556+
io_time_recursive:
3557+
type: "array"
3558+
items:
3559+
$ref: "#/definitions/BlkioStatEntry"
3560+
sectors_recursive:
3561+
type: "array"
3562+
items:
3563+
$ref: "#/definitions/BlkioStatEntry"
3564+
3565+
BlkioStatEntry:
3566+
description: BlkioStatEntry is one small entity to store a piece of Blkio stats
3567+
type: "object"
3568+
properties:
3569+
major:
3570+
type: "integer"
3571+
format: "uint64"
3572+
minor:
3573+
type: "integer"
3574+
format: "uint64"
3575+
op:
3576+
type: "string"
3577+
value:
3578+
type: "integer"
3579+
format: "uint64"
3580+
3581+
MemoryStats:
3582+
description: MemoryStats aggregates all memory stats since container inception on Linux.
3583+
type: "object"
3584+
properties:
3585+
usage:
3586+
description: current res_counter usage for memory
3587+
type: "integer"
3588+
format: "uint64"
3589+
max_usage:
3590+
description: maximum usage ever recorded.
3591+
type: "integer"
3592+
format: "uint64"
3593+
stats:
3594+
description: all the stats exported via memory.stat.
3595+
type: "object"
3596+
additionalProperties:
3597+
type: "integer"
3598+
format: "uint64"
3599+
failcnt:
3600+
description: number of times memory usage hits limits.
3601+
type: "integer"
3602+
format: "uint64"
3603+
limit:
3604+
description: xxx
3605+
type: "integer"
3606+
format: "uint64"
3607+
3608+
NetworkStats:
3609+
description: container stats almost from cgroup resource usage.
3610+
type: "object"
3611+
properties:
3612+
rx_bytes:
3613+
description: Bytes received.
3614+
type: "integer"
3615+
format: "uint64"
3616+
rx_packets:
3617+
description: Packets received.
3618+
type: "integer"
3619+
format: "uint64"
3620+
rx_errors:
3621+
description: Received errors.
3622+
type: "integer"
3623+
format: "uint64"
3624+
rx_dropped:
3625+
description: Incoming packets dropped.
3626+
type: "integer"
3627+
format: "uint64"
3628+
tx_bytes:
3629+
description: Bytes sent.
3630+
type: "integer"
3631+
format: "uint64"
3632+
tx_packets:
3633+
description: Packets sent.
3634+
type: "integer"
3635+
format: "uint64"
3636+
tx_errors:
3637+
description: Sent errors.
3638+
type: "integer"
3639+
format: "uint64"
3640+
tx_dropped:
3641+
description: Outgoing packets dropped.
3642+
type: "integer"
3643+
format: "uint64"
3644+
endpoint_id:
3645+
description: Endpoint ID.
3646+
type: "string"
3647+
instance_id:
3648+
description: Instance ID.
3649+
type: "string"
33843650

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

0 commit comments

Comments
 (0)