Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[receiver/dockerstats] Fix memory usage/percent calculation #21166

Conversation

gsanchezgavier
Copy link
Contributor

Description:

This PR implements the following proposals:

  • container.memory.usage.total: The proposal is to support both versions of cgroups and use the same method of the docker cli is using which represents better usage for some use-cases according to this discussion.

  • container.memory.percent: Similar proposal to use the same method of the docker cli, based on the total usage previously calculated.

Link to tracking Issue:
#21097

Testing:
I extended the current metrics tests.

Output from docker stats of the added test:

CONTAINER ID   NAME                    CPU %     MEM USAGE / LIMIT   MEM %     NET I/O       BLOCK I/O    PIDS
f97ed5bca0a5   compassionate_mcnulty   0.00%     223.8MiB / 256MiB   87.41%    1.58kB / 0B   168kB / 0B   1

Documentation:

Copy link
Contributor

@jamesmoessis jamesmoessis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, just a couple of minor things to address

// See https://github.com/moby/moby/issues/40727 for the background.
func calculateMemUsageNoCache(memoryStats *dtypes.MemoryStats) uint64 {
// cgroup v1
if v, isCgroup1 := memoryStats.Stats["total_inactive_file"]; isCgroup1 && v < memoryStats.Usage {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So is the logic that we know it's cgroup v1 if total_inactive_file exists? And if it doesn't then we know it's v2?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes total_inactive_file is not present with cgroups v2

func calculateMemoryPercent(memoryStats *dtypes.MemoryStats) float64 {
if float64(memoryStats.Limit) == 0 {
return 0
// calculateMemUsageNoCache calculate memory usage of the container.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you've taken this function from the Docker CLI code, which is under the Apache license, it will need a copyright notice similar to calculateCPUPercent().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the notice that is on top of calculateCPUPercent which is copied from the same place to avoid having this notice multiple times, perhaps I can move all these three functions calculateCPUPercent, calculateMemUsageNoCache, and calculateMemoryPercent to a single file with one notice in the top WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that idea

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 6f3b680

@@ -562,7 +562,7 @@ resourceMetrics:
sum:
aggregationTemporality: 2
dataPoints:
- asInt: "352256"
- asInt: "425984"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description of this metric is now a bit misleading, can you change that in metadata.yml?

It can probably just be "Total memory usage of the container".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps replacing the total cache ?:
Memory usage of the container. This excludes the inactive file memory.

Or just mentioning that excludes "cache" like Docker CLI docs does in the note?
Memory usage of the container. This excludes the cache.

WDYT

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either one works for me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in a756251

@jamesmoessis
Copy link
Contributor

Hi @mx-psi - this has had codeowner approval for a couple of weeks now. It needs an approver or maintainer to look at it for it to get through.

@mx-psi
Copy link
Member

mx-psi commented May 10, 2023

Hi @mx-psi - this has had codeowner approval for a couple of weeks now. It needs an approver or maintainer to look at it for it to get through.

Woops, totally slipped through the cracks, thanks for the ping! It looks good to me so we can merge :)

@mx-psi mx-psi merged commit 3092a04 into open-telemetry:main May 10, 2023
@github-actions github-actions bot added this to the next release milestone May 10, 2023
@mx-psi
Copy link
Member

mx-psi commented May 10, 2023

This broke the lint job, presumably because #21384 was not present on this branch. #21732 should fix things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants