Skip to content

feat: Add stats of client and pool to be expored through agent #4157

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

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

Conversation

tdeekens
Copy link

This relates to:

This relates to the issue #4149

Rationale

Integrating existing and exposed Pool stats into a telemetry system is a bit cumbersome. Right now we can hook into the factory function and observe the created pool there. It would be beneficial if a Pool's and Client's stats would be exposed by the Agent owning the Pool or Client respectively.

Changes

Exposing stats through the Agent makes it easier to integrate telemetry systems as we do not have to "hook" into the factory to receive the Pool or Client used by an Agent.

Features

Exposes a new getter called stats on an Agent. The method returns an Array of Arrays with stats by origin.

Additionally, the Client now also exposes available stats similar to a Pool. The initial implementation of Pool stats happened in this PR while the implementation for a Client here follows the Pool's implementation pattenr.

Bug Fixes

Breaking Changes and Deprecations

Status

@@ -64,5 +64,6 @@ module.exports = {
kMaxConcurrentStreams: Symbol('max concurrent streams'),
kNoProxyAgent: Symbol('no proxy agent'),
kHttpProxyAgent: Symbol('http proxy agent'),
kHttpsProxyAgent: Symbol('https proxy agent')
kHttpsProxyAgent: Symbol('https proxy agent'),
kStats: Symbol('stats')
Copy link
Author

Choose a reason for hiding this comment

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

This is used now by Pool and Client. Hence it should be shared in my opinion.

@@ -110,6 +110,17 @@ class Agent extends DispatcherBase {

await Promise.all(destroyPromises)
}

get stats () {
Copy link
Author

Choose a reason for hiding this comment

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

Getter to loop over Clients which can be Pool or Client themselves to assemble and expose stats.

Data structure can be changed. Right now it's [string, [PoolStats, ClientStats]]. It could also be Record<string, PoolStats | ClientStats>.

Copy link
Member

Choose a reason for hiding this comment

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

The current shape seems like a good first iteration, especially for Agent as the number of origins called can vary; caller can reconstruct it into a different data structure if needed

Copy link
Author

Choose a reason for hiding this comment

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

Thanks. Same thinking on my end.

@@ -0,0 +1,28 @@
'use strict'
Copy link
Author

Choose a reason for hiding this comment

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

Follows implementation of PoolStats. Clients however expose a subset of stats. Hence the separate class.

Copy link
Member

Choose a reason for hiding this comment

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

I'll put this class in utils or so, as the files within the dispatcher/ dir are mostly mean for classes that shares dispatcher interface.

Copy link
Author

Choose a reason for hiding this comment

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

Sounds good. Same for the PoolStats then which prior to this change lived here too, right?

@@ -249,6 +251,8 @@ class Client extends DispatcherBase {

this[kResume] = (sync) => resume(this, sync)
this[kOnError] = (err) => onError(this, err)

this[kStats] = new ClientStats(this)
Copy link
Author

Choose a reason for hiding this comment

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

Same as with Pools.

@metcoder95 metcoder95 changed the title Add stats of client and pool to be expored through agent feat: Add stats of client and pool to be expored through agent Apr 13, 2025
Copy link
Member

@metcoder95 metcoder95 left a comment

Choose a reason for hiding this comment

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

Can we add testing for it?

@@ -110,6 +110,17 @@ class Agent extends DispatcherBase {

await Promise.all(destroyPromises)
}

get stats () {
Copy link
Member

Choose a reason for hiding this comment

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

The current shape seems like a good first iteration, especially for Agent as the number of origins called can vary; caller can reconstruct it into a different data structure if needed

@@ -0,0 +1,28 @@
'use strict'
Copy link
Member

Choose a reason for hiding this comment

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

I'll put this class in utils or so, as the files within the dispatcher/ dir are mostly mean for classes that shares dispatcher interface.

@tdeekens
Copy link
Author

Can we add testing for it?

Yip. I followed the Pool Stats addition PR in terms of depth of testing on only existing through tsd.

@tdeekens
Copy link
Author

tdeekens commented Apr 13, 2025

Hi @metcoder95!

In ab942e4 I've moved the pool stats both of pool and client into a utils/stats.js. Happy for feedback.
Furthermore in f2452c4 I added some tests. Let me know if the direction is right and if the depth needs to be expanded on.

Should we also add docs and maybe an example?

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.

2 participants