21 Commits

Author SHA1 Message Date
Sebastiaan van Stijn
6a2a1dd6cf api/types/container: StatsResponse: add OSType field
Adds a per-stats OSType field to allow handle the platform-specific fields.
Before this change, the client had to get the OSType field from the server's
API response header and copy it to each record.

Older daemon versions don't have this field, so the client still needs to
handle fallbacks.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-10-27 16:39:32 +01:00
Sebastiaan van Stijn
7439f09cd2 daemon: Daemon.stats: fill-in container ID and Name when collecting
Propagate these fields when collecting the data, instead of patching the
result inside the router.

This also fixes a bug where the ID and Name fields were not set when using
one-shot.

Before this patch, the "id" and "name" fields were not set in one-shot mode:

    curl -s --unix-socket /var/run/docker.sock 'http://localhost/v1.51/containers/foo/stats?stream=false' | jq .id
    "125d7f3df7919c33195fa4ec0636c27cdbe6d9b14339867d2920900565bcaf7e"
    curl -s --unix-socket /var/run/docker.sock 'http://localhost/v1.51/containers/foo/stats?stream=false' | jq .name
    "/foo"

    curl -s --unix-socket /var/run/docker.sock 'http://localhost/v1.51/containers/foo/stats?stream=false&one-shot=true' | jq .id
    null
    curl -s --unix-socket /var/run/docker.sock 'http://localhost/v1.51/containers/foo/stats?stream=false&one-shot=true' | jq .name
    null

With this patch, both are set:

    curl -s --unix-socket /var/run/docker.sock 'http://localhost/v1.51/containers/foo/stats?stream=false&one-shot=true' | jq .id
    "125d7f3df7919c33195fa4ec0636c27cdbe6d9b14339867d2920900565bcaf7e"
    curl -s --unix-socket /var/run/docker.sock 'http://localhost/v1.51/containers/foo/stats?stream=false&one-shot=true' | jq .name
    "/foo"

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-10-27 10:55:01 +01:00
Sebastiaan van Stijn
99410827c7 daemon: use errdefs instead of string-matching in some places
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-10-25 16:13:30 +02:00
Derek McGowan
f74e5d48b3 Create github.com/moby/moby/v2 module
Signed-off-by: Derek McGowan <derek@mcg.dev>
2025-07-31 10:13:29 -07:00
Derek McGowan
afd6487b2e Create github.com/moby/moby/api module
Signed-off-by: Derek McGowan <derek@mcg.dev>
2025-07-21 09:30:05 -07:00
Derek McGowan
5419eb1efc Move container to daemon/container
Signed-off-by: Derek McGowan <derek@mcg.dev>
2025-06-27 14:27:21 -07:00
Matthieu MOREL
381d9d0723 fix use-errors-new from revive
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
2025-06-26 12:07:38 +00:00
Sebastiaan van Stijn
5318877858 daemon: remove // import comments
These comments were added to enforce using the correct import path for
our packages ("github.com/docker/docker", not "github.com/moby/moby").
However, when working in go module mode (not GOPATH / vendor), they have
no effect, so their impact is limited.

Remove these imports in preparation of migrating our code to become an
actual go module.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-05-30 15:59:13 +02:00
Lee Gaines
6d7a370fe5 Refactor CPU usage stats test to use go:embed
Refactor the system CPU usage testing approach for improved maintainability:

1. Extract the core CPU usage parsing logic into a new `readSystemCPUUsage`
   function that accepts an io.Reader, making it more testable and modular.

2. Use go:embed directive to embed the test data file at compile time,
   eliminating runtime file operations and making tests more reliable.

3. Simplify the test by removing global variable mocking in favor of a more
   direct approach with the new reader-based function.

4. Maintain full test coverage for the long "intr" line edge case which was
   crucial for the original bug fix, while making the test more maintainable.

This change preserves the original test behavior while improving code quality,
testability, and making the tests self-contained.

Signed-off-by: Lee Gaines <leetgaines@gmail.com>
2025-05-16 10:23:49 -04:00
Patrik Leifert
e22d04e8a9 Improve CPU usage parsing and error reporting
This fix address issues where the scanner was unable to properly parse longer outputs from /proc/stat. This could happen on an ARM machine with large amount of CPU cores (and interrupts). By switching to reader we have more control over data parsing and dump unnecessary data

Signed-off-by: Patrik Leifert <patrikleifert@hotmail.com>
2025-04-07 16:19:02 +02:00
Sebastiaan van Stijn
85c8fb7fda daemon: getSystemCPUUsage(): fix naked returns, output vars
Remove output variables, and use explicit returns

    daemon/stats_unix.go:359:2: naked return in func `getSystemCPUUsage` with 38 lines of code (nakedret)
        return
        ^

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-03-04 13:56:14 +01:00
Sebastiaan van Stijn
ca06b222e3 api/types/container: merge Stats and StatsResponse
The StatsResponse type  was a compatibility-wrapper introduced in
d3379946ec to differentiate responses
for  API < 1.21 and API >= 1.21. API versions lower than 1.24 are
deprecated, and we can merge these types again.

The Stats type was not used directly, but deprecating it, and
making it an alias for StatsResponse, which provides a superset
of its fields.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-16 19:47:40 +01:00
Sebastiaan van Stijn
0a4277abf4 api/types: move stats-types to api/types/container
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-06-20 10:21:42 +02:00
Xinfeng Liu
95aea39348 Make one-shot stats faster
This commit moves one-shot stats processing out of the publishing
channels, i.e. collect stats directly.

Also changes the method of getSystemCPUUsage() on Linux to return
number of online CPUs also.

Signed-off-by: Xinfeng Liu <XinfengLiu@icloud.com>
2023-09-27 14:10:23 +08:00
Sebastiaan van Stijn
ab35df454d remove pre-go1.17 build-tags
Removed pre-go1.17 build-tags with go fix;

    go mod init
    go fix -mod=readonly ./...
    rm go.mod

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-05-19 20:38:51 +02:00
Sebastiaan van Stijn
f691b13450 daemon: move code related to stats together
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-04-08 19:00:01 +02:00
Sebastiaan van Stijn
686be57d0a Update to Go 1.17.0, and gofmt with Go 1.17
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-08-24 23:33:27 +02:00
Daniel Nephin
4f0d95fa6e Add canonical import comment
Signed-off-by: Daniel Nephin <dnephin@docker.com>
2018-02-05 16:51:57 -05:00
Brian Goff
ebcb7d6b40 Remove string checking in API error handling
Use strongly typed errors to set HTTP status codes.
Error interfaces are defined in the api/errors package and errors
returned from controllers are checked against these interfaces.

Errors can be wraeped in a pkg/errors.Causer, as long as somewhere in the
line of causes one of the interfaces is implemented. The special error
interfaces take precedence over Causer, meaning if both Causer and one
of the new error interfaces are implemented, the Causer is not
traversed.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2017-08-15 16:01:11 -04:00
Sebastiaan van Stijn
45818d6fc4 Removed engine-api leftovers
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2016-09-21 12:36:52 +02:00
John Howard
340e5233b2 Windows: stats support
Signed-off-by: John Howard <jhoward@microsoft.com>
2016-09-16 11:56:15 -07:00