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

Feat: scrape hot threads CPU consumption percentage #533

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

avee137
Copy link
Contributor

@avee137 avee137 commented Feb 15, 2022

Signed-off-by: avinash kumar [email protected]

We found this information useful in root-causing CPU spikes on our benchmarking and production clusters.
API ( /_nodes/hot_threads ) output from elasticsearch is not JSON, hence some string parsing and formatting in the code. number of hot threads to scrape and scrape interval in configurable through - MAX_HOT_THREADS_COUNT and HOT_THREADS_SECOND_SAMPLING_INTERVAL env variables.

@avee137
Copy link
Contributor Author

avee137 commented Feb 22, 2022

@zwopir could I get some inputs on this PR ?

@avee137
Copy link
Contributor Author

avee137 commented Jun 30, 2022

Is there anything else that needs to be done to merge this PR ?

@baonq-me
Copy link

Is there any updated progress for this PR ?

Copy link
Contributor

@sysadmind sysadmind left a comment

Choose a reason for hiding this comment

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

I think the bones are here, although I'm not sure that the parsing is being properly handled. I would advise trying to make this code more clear as to the intention and I think it needs some solid tests.

@@ -0,0 +1,246 @@
// Copyright 2021 The Prometheus Authors
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Copyright 2021 The Prometheus Authors
// Copyright 2022 The Prometheus Authors

Comment on lines +17 to +18
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/go-kit/log"
"github.com/go-kit/log/level"

"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"io/ioutil"
"net/http"
Copy link
Contributor

Choose a reason for hiding this comment

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

These imports are not formatted properly. The stdlib imports should be first in alphabetical order, followed by an empty line, followed by the other imports in alphabetical order. I believe go fmt should do that automatically, or one of the other go tools.

HotThreads,
}
}
NODE_OUTPUT_SEPERATOR = ":::"
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure why all of this data is being parsed by regex. It seems error prone. I think some tests would be very useful.

Labels func(HotThreadsIndex string, HotThreadsPolicy string, action string, step string) []string
}

func getEnv(key, defaultVal string) string {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think env vars should be read in a collector. All configuration and values should be passed in.

}

sb := string(body)
hotThreadsNodeOp := strings.Split(string(sb), NODE_OUTPUT_SEPERATOR)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is sb being cast to a string? It's cast to a string above.

)
}
t := &HotThreadsRsp{CpuPercentage: cpuPercentage, Node: nodeName, ThreadName: threadName, ThreadId: threadId}
*hotThreads = append(*hotThreads, *t)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are all of these pointer dereferences?

allHotThreads := hotThreadsOpRegex.FindAllString(nodeData, -1)
cpuPercentageRegex := regexp.MustCompile(CPU_PERCENTAGE_REGEX)

for _, v := range allHotThreads {
Copy link
Contributor

Choose a reason for hiding this comment

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

The logic here seems like some complex parsing. I think it would be best for this to be in a separate function with good unit tests. The signature of the function and name should provide a clear definition of what the functionality is.

Is there not an alternative to this parsing? What format is the data in?

// limitations under the License.
package collector

// _nodes/hot_threads response
Copy link
Contributor

Choose a reason for hiding this comment

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

This does not need to be a separate file.

@@ -66,6 +66,7 @@ github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/kit v0.9.0 h1:wDJmvq38kDhkVxi50ni9ykkdUr1PKgqKOoi01fa0Mdk=
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think there are any go.mod changes so I don't believe that there should be any go.sum changes either.

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

Successfully merging this pull request may close these issues.

None yet

3 participants