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

add label to force containter stats to stream - allowing podman containers to return precpu_stats #37773

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ahbonsu
Copy link

@ahbonsu ahbonsu commented Jan 29, 2024

Proposed commit message

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding change to the default configuration files
  • [-] I have added tests that prove my fix is effective or that my feature works
  • I have added an entry in CHANGELOG.next.asciidoc or CHANGELOG-developer.next.asciidoc.

Author's Checklist

  • [ ]

How to test this PR locally

Run any container with podman and use the docker plugin pointing to the podman socket like : hosts: ["unix:///run/user/1001/podman/podman.sock"]. There will be an error when using podman:

"log.origin":{"function":"github.com/elastic/beats/v7/metricbeat/mb/module.(*metricSetWrapper).fetch","file.name":"module/wrapper.go","file.line":256},"message":"Error fetching data for metricset docker.memory:>

Use a label on the container: co.elastic.metricbeat.docker.forceContainerStatsStream=true
... and the memory stats are read successfully.

Related issues

I did not see any issues but there are a few posts stackoverflow, etc. which do point out to this "issue"

Use cases

This allows the user to flag their podman container and see the memory usage.

Screenshots

Logs

"log.origin":{"function":"github.com/elastic/beats/v7/metricbeat/mb/module.(*metricSetWrapper).fetch","file.name":"module/wrapper.go","file.line":256},"message":"Error fetching data for metricset docker.memory:>

@ahbonsu ahbonsu requested a review from a team as a code owner January 29, 2024 10:06
@botelastic botelastic bot added the needs_team Indicates that the issue/PR needs a Team:* label label Jan 29, 2024
Copy link
Contributor

mergify bot commented Jan 29, 2024

This pull request does not have a backport label.
If this is a bug or security fix, could you label this PR @ahbonsu? 🙏.
For such, you'll need to label your PR with:

  • The upcoming major version of the Elastic Stack
  • The upcoming minor version of the Elastic Stack (if you're not pushing a breaking change)

To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-v8./d.0 is the label to automatically backport to the 8./d branch. /d is the digit

@elasticmachine
Copy link
Collaborator

❕ Build Aborted

The PR is not allowed to run in the CI yet

the below badges are clickable and redirect to their specific view in the CI or DOCS
Pipeline View Test View Changes Artifacts preview

Expand to view the summary

Build stats

  • Start Time: 2024-01-29T10:07:01.340+0000

  • Duration: 5 min 51 sec

Steps errors 2

Expand to view the steps failures

Load a resource file from a library
  • Took 0 min 0 sec . View more details here
  • Description: approval-list/elastic/beats.yml
Error signal
  • Took 0 min 0 sec . View more details here
  • Description: githubApiCall: The REST API call https://api.github.com/orgs/elastic/members/ahbonsu return the message : java.lang.Exception: httpRequest: Failure connecting to the service https://api.github.com/orgs/elastic/members/ahbonsu : httpRequest: Failure connecting to the service https://api.github.com/orgs/elastic/members/ahbonsu : Code: 404Error: {"message":"User does not exist or is not a member of the organization","documentation_url":"https://docs.github.com/rest/orgs/members#check-organization-membership-for-a-user"}

🤖 GitHub comments

Expand to view the GitHub comments

To re-run your PR in the CI, just comment with:

  • /test : Re-trigger the build.

  • /package : Generate the packages and run the E2E tests.

  • /beats-tester : Run the installation tests with beats-tester.

  • run elasticsearch-ci/docs : Re-trigger the docs validation. (use unformatted text in the comment!)

@elasticmachine
Copy link
Collaborator

❕ Build Aborted

Either there was a build timeout or someone aborted the build.

the below badges are clickable and redirect to their specific view in the CI or DOCS
Pipeline View Test View Changes Artifacts preview

Expand to view the summary

Build stats

  • Duration: 5 min 37 sec

Steps errors 2

Expand to view the steps failures

Load a resource file from a library
  • Took 0 min 0 sec . View more details here
  • Description: approval-list/elastic/beats.yml
Error signal
  • Took 0 min 0 sec . View more details here
  • Description: githubApiCall: The REST API call https://api.github.com/orgs/elastic/members/ahbonsu return the message : java.lang.Exception: httpRequest: Failure connecting to the service https://api.github.com/orgs/elastic/members/ahbonsu : httpRequest: Failure connecting to the service https://api.github.com/orgs/elastic/members/ahbonsu : Code: 404Error: {"message":"User does not exist or is not a member of the organization","documentation_url":"https://docs.github.com/rest/orgs/members#check-organization-membership-for-a-user"}

🤖 GitHub comments

Expand to view the GitHub comments

To re-run your PR in the CI, just comment with:

  • /test : Re-trigger the build.

  • /package : Generate the packages and run the E2E tests.

  • /beats-tester : Run the installation tests with beats-tester.

  • run elasticsearch-ci/docs : Re-trigger the docs validation. (use unformatted text in the comment!)

@ahbonsu
Copy link
Author

ahbonsu commented Jan 29, 2024

This pull request does not have a backport label. If this is a bug or security fix, could you label this PR @ahbonsu? 🙏. For such, you'll need to label your PR with:

  • The upcoming major version of the Elastic Stack
  • The upcoming minor version of the Elastic Stack (if you're not pushing a breaking change)

To fixup this pull request, you need to add the backport labels for the needed branches, such as:

  • backport-v8./d.0 is the label to automatically backport to the 8./d branch. /d is the digit

Sorry, I cannot set them - permission denied :(

--> enhancement
--> backport-v8.12.0

var stream bool
if container.Labels != nil {
if labelValue, ok := container.Labels["co.elastic.metricbeat.docker.forceContainerStatsStream"]; ok {
stream, _ = strconv.ParseBool(labelValue)
Copy link
Contributor

Choose a reason for hiding this comment

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

If you set the stream to true , what is the effect on the response time?
Trying it locally the stream=1 parameter in the http client never returns.

curl -s --unix-socket /var/run/docker.sock -H 'Content-Type: application/json' http://localhost/containers/<contId>/stats\?stream\=1

It is like using docker stats vs docker stats --no-stream.
When stream=false the docker engine fetches 2 stats for the container to be able to calculate percentages. See moby/moby#40437.
In general the best approach would be to use the newer https://github.com/moby/moby/blob/f472dda2e9dec18d4bf011fa6b2c5c388d6a20a7/client/container_stats.go#L32 as the percentages are calculated at the collector side (metricbeat).

Regarding the Podman error in fetching memory stats, we will investigate it to understand why this happens

Copy link
Contributor

Choose a reason for hiding this comment

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

FYI until now I have made a local image from this branch code.
I have installed podman in my MacOs and created a local container

I have added the label:

podman container inspect 45394fdd9ab9

"Labels": {
                    "co.elastic.metricbeat.docker.forceContainerStatsStream": "true",
                    "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"

Still error persists :

{"log.level":"error","@timestamp":"2024-01-31T16:43:04.321+0200","log.origin":{"function":"github.com/elastic/beats/v7/metricbeat/mb/module.(*metricSetWrapper).fetch","file.name":"module/wrapper.go","file.line":256},"message":"Error fetching data for metricset docker.memory: No memory stats data available","service.name":"metricbeat","ecs.version":"1.6.0"}

@pierrehilbert pierrehilbert added the Team:Elastic-Agent Label for the Agent team label Jan 31, 2024
@elasticmachine
Copy link
Collaborator

Pinging @elastic/elastic-agent (Team:Elastic-Agent)

@botelastic botelastic bot removed the needs_team Indicates that the issue/PR needs a Team:* label label Jan 31, 2024
// force container stats stream to receive also precpu_stats when using podman
// docker returns precpu_stats with or without streaming, podman does only return precpu_stats when streaming
// this can be done using the label co.elastic.metricbeat.docker.forceContainerStatsStream
var stream bool
Copy link
Contributor

Choose a reason for hiding this comment

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

Because we set stream false by default I would suggest to

Suggested change
var stream bool
stream :=false

// docker returns precpu_stats with or without streaming, podman does only return precpu_stats when streaming
// this can be done using the label co.elastic.metricbeat.docker.forceContainerStatsStream
var stream bool
if container.Labels != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think nil check is not needed as long as is checked in next step

So overall:

stream := false

	if labelValue, ok := container.Labels["co.elastic.metricbeat.docker.forceContainerStatsStream"]; ok {
		streamlabel, err := strconv.ParseBool(labelValue)
		if err != nil {
			stream = streamlabel
		}
	}

@gizas
Copy link
Contributor

gizas commented Jan 31, 2024

This pull request does not have a backport label. If this is a bug or security fix, could you label this PR @ahbonsu? 🙏. For such, you'll need to label your PR with:

  • The upcoming major version of the Elastic Stack
  • The upcoming minor version of the Elastic Stack (if you're not pushing a breaking change)

To fixup this pull request, you need to add the backport labels for the needed branches, such as:

  • backport-v8./d.0 is the label to automatically backport to the 8./d branch. /d is the digit

Sorry, I cannot set them - permission denied :(

--> enhancement --> backport-v8.12.0

@ahbonsu , I set the enhancement for you
For the backport because it is considered an enhancement usually we dont backport it. But in any case we will reconsider it internally.

Additionally while we are investigating the docker.memory: No memory stats data available", can you please also update the description of this PR with any screenshot or evidence for correct functionality of this PR?

Copy link
Contributor

mergify bot commented Feb 5, 2024

This pull request does not have a backport label.
If this is a bug or security fix, could you label this PR @ahbonsu? 🙏.
For such, you'll need to label your PR with:

  • The upcoming major version of the Elastic Stack
  • The upcoming minor version of the Elastic Stack (if you're not pushing a breaking change)

To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-v8./d.0 is the label to automatically backport to the 8./d branch. /d is the digit

@pierrehilbert
Copy link
Collaborator

@ahbonsu Did you have time to look at @gizas comment?

@pierrehilbert
Copy link
Collaborator

Setting back to draft for now.

@pierrehilbert pierrehilbert marked this pull request as draft March 22, 2024 14:34
Copy link
Contributor

mergify bot commented Apr 15, 2024

This pull request is now in conflicts. Could you fix it? 🙏
To fixup this pull request, you can check out it locally. See documentation: https://help.github.com/articles/checking-out-pull-requests-locally/

git fetch upstream
git checkout -b main upstream/main
git merge upstream/main
git push upstream main

Copy link
Contributor

mergify bot commented Dec 26, 2024

backport-8.x has been added to help with the transition to the new branch 8.x.
If you don't need it please use backport-skip label and remove the backport-8.x label.

@mergify mergify bot added the backport-8.x Automated backport to the 8.x branch with mergify label Dec 26, 2024
Copy link
Contributor

mergify bot commented Dec 26, 2024

This pull request is now in conflicts. Could you fix it? 🙏
To fixup this pull request, you can check out it locally. See documentation: https://help.github.com/articles/checking-out-pull-requests-locally/

git fetch upstream
git checkout -b main upstream/main
git merge upstream/main
git push upstream main

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-8.x Automated backport to the 8.x branch with mergify enhancement Team:Elastic-Agent Label for the Agent team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants