@promster/undici 15.3.0 → 15.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Agent as TUndiciAgent, Pool as TUndiciPool } from 'undici';
|
|
2
|
+
type TAgentMetricsExporterOptions = {
|
|
3
|
+
metricPrefix?: string;
|
|
4
|
+
};
|
|
5
|
+
type TAgentStatsKeys = keyof TUndiciPool.PoolStats;
|
|
6
|
+
declare function addObservedAgent(agent: TUndiciAgent): TUndiciAgent;
|
|
7
|
+
declare const supportedAgentStats: readonly TAgentStatsKeys[];
|
|
8
|
+
declare function createAgentMetricsExporter(initialAgents?: TUndiciAgent[], options?: TAgentMetricsExporterOptions): void;
|
|
9
|
+
export { createAgentMetricsExporter, supportedAgentStats, addObservedAgent, type TAgentMetricsExporterOptions, };
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { type Agent as TUndiciAgent, type Pool as TUndiciPool } from 'undici';
|
|
1
|
+
import { type Agent as TUndiciAgent, type Dispatcher as TUndiciDispatcher, type Pool as TUndiciPool } from 'undici';
|
|
2
2
|
type TPoolsMetricsExporterOptions = {
|
|
3
3
|
metricPrefix?: string;
|
|
4
4
|
};
|
|
5
|
+
type TPoolStatsKeys = keyof TUndiciPool.PoolStats;
|
|
5
6
|
declare function addObservedPool(origin: string, pool: TUndiciPool): TUndiciPool;
|
|
6
|
-
declare function observedPoolFactory(origin: string, options?: TUndiciAgent.Options):
|
|
7
|
-
declare const supportedPoolStats:
|
|
7
|
+
declare function observedPoolFactory(origin: string, options?: TUndiciAgent.Options): TUndiciDispatcher;
|
|
8
|
+
declare const supportedPoolStats: readonly TPoolStatsKeys[];
|
|
8
9
|
declare function createPoolMetricsExporter(initialPools?: Record<string, TUndiciPool>, options?: TPoolsMetricsExporterOptions): void;
|
|
9
10
|
export { createPoolMetricsExporter, supportedPoolStats, addObservedPool, observedPoolFactory, type TPoolsMetricsExporterOptions, };
|
|
@@ -39,7 +39,7 @@ function addObservedPool(origin, pool) {
|
|
|
39
39
|
return observedPools.add(origin, pool);
|
|
40
40
|
}
|
|
41
41
|
function observedPoolFactory(origin, options) {
|
|
42
|
-
if (options.connections === 1) {
|
|
42
|
+
if ((options === null || options === void 0 ? void 0 : options.connections) === 1) {
|
|
43
43
|
return new undici.Client(origin, options);
|
|
44
44
|
}
|
|
45
45
|
return addObservedPool(origin, new undici.Pool(origin, options));
|
|
@@ -83,7 +83,84 @@ function createPoolMetricsExporter(initialPools, options) {
|
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
+
class ObservedAgents {
|
|
87
|
+
constructor(initialAgents) {
|
|
88
|
+
this.agents = [];
|
|
89
|
+
if (initialAgents) {
|
|
90
|
+
this.addMany(initialAgents);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
add(agent) {
|
|
94
|
+
this.agents.push(agent);
|
|
95
|
+
return agent;
|
|
96
|
+
}
|
|
97
|
+
addMany(agents) {
|
|
98
|
+
this.agents.push(...agents);
|
|
99
|
+
}
|
|
100
|
+
remove(agent) {
|
|
101
|
+
const index = this.agents.indexOf(agent);
|
|
102
|
+
if (index !== -1) {
|
|
103
|
+
this.agents.splice(index, 1);
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
get size() {
|
|
109
|
+
return this.agents.length;
|
|
110
|
+
}
|
|
111
|
+
[Symbol.iterator]() {
|
|
112
|
+
return this.agents.values();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
const observedAgents = new ObservedAgents();
|
|
116
|
+
function addObservedAgent(agent) {
|
|
117
|
+
return observedAgents.add(agent);
|
|
118
|
+
}
|
|
119
|
+
const supportedAgentStats = ['connected', 'free', 'pending', 'queued', 'running', 'size'];
|
|
120
|
+
function createAgentMetricsExporter(initialAgents, options) {
|
|
121
|
+
var _options$metricPrefix;
|
|
122
|
+
const metricName = `${(_options$metricPrefix = options === null || options === void 0 ? void 0 : options.metricPrefix) !== null && _options$metricPrefix !== void 0 ? _options$metricPrefix : ''}nodejs_undici_agent`;
|
|
123
|
+
if (initialAgents) {
|
|
124
|
+
observedAgents.addMany(initialAgents);
|
|
125
|
+
}
|
|
126
|
+
new metrics.Prometheus.Gauge({
|
|
127
|
+
name: `${metricName}s_total`,
|
|
128
|
+
help: 'Number of Undici agents.',
|
|
129
|
+
registers: [metrics.defaultRegister],
|
|
130
|
+
collect() {
|
|
131
|
+
this.set(observedAgents.size);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
for (const supportedStat of supportedAgentStats) {
|
|
135
|
+
new metrics.Prometheus.Gauge({
|
|
136
|
+
name: `${metricName}_${supportedStat}`,
|
|
137
|
+
help: `Statistics for Undici agents ${supportedStat} stat. See https://github.com/nodejs/undici/blob/main/docs/docs/api/Agent.md#agentstats`,
|
|
138
|
+
labelNames: ['origin'],
|
|
139
|
+
registers: [metrics.defaultRegister],
|
|
140
|
+
collect() {
|
|
141
|
+
for (const agent of observedAgents) {
|
|
142
|
+
// If the agent has made no requests, it will not have stats
|
|
143
|
+
if (!agent.stats) {
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
for (const [origin, stats] of Object.entries(agent.stats)) {
|
|
147
|
+
// Client stats do not have free property
|
|
148
|
+
// @ts-expect-error
|
|
149
|
+
const statValue = stats[supportedStat];
|
|
150
|
+
if (typeof statValue === 'number' && !Number.isNaN(statValue)) {
|
|
151
|
+
this.labels(origin).set(statValue);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
exports.addObservedAgent = addObservedAgent;
|
|
86
161
|
exports.addObservedPool = addObservedPool;
|
|
162
|
+
exports.createAgentMetricsExporter = createAgentMetricsExporter;
|
|
87
163
|
exports.createPoolMetricsExporter = createPoolMetricsExporter;
|
|
88
164
|
exports.observedPoolFactory = observedPoolFactory;
|
|
165
|
+
exports.supportedAgentStats = supportedAgentStats;
|
|
89
166
|
exports.supportedPoolStats = supportedPoolStats;
|
|
@@ -39,7 +39,7 @@ function addObservedPool(origin, pool) {
|
|
|
39
39
|
return observedPools.add(origin, pool);
|
|
40
40
|
}
|
|
41
41
|
function observedPoolFactory(origin, options) {
|
|
42
|
-
if (options.connections === 1) {
|
|
42
|
+
if ((options === null || options === void 0 ? void 0 : options.connections) === 1) {
|
|
43
43
|
return new undici.Client(origin, options);
|
|
44
44
|
}
|
|
45
45
|
return addObservedPool(origin, new undici.Pool(origin, options));
|
|
@@ -83,7 +83,84 @@ function createPoolMetricsExporter(initialPools, options) {
|
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
+
class ObservedAgents {
|
|
87
|
+
constructor(initialAgents) {
|
|
88
|
+
this.agents = [];
|
|
89
|
+
if (initialAgents) {
|
|
90
|
+
this.addMany(initialAgents);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
add(agent) {
|
|
94
|
+
this.agents.push(agent);
|
|
95
|
+
return agent;
|
|
96
|
+
}
|
|
97
|
+
addMany(agents) {
|
|
98
|
+
this.agents.push(...agents);
|
|
99
|
+
}
|
|
100
|
+
remove(agent) {
|
|
101
|
+
const index = this.agents.indexOf(agent);
|
|
102
|
+
if (index !== -1) {
|
|
103
|
+
this.agents.splice(index, 1);
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
get size() {
|
|
109
|
+
return this.agents.length;
|
|
110
|
+
}
|
|
111
|
+
[Symbol.iterator]() {
|
|
112
|
+
return this.agents.values();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
const observedAgents = new ObservedAgents();
|
|
116
|
+
function addObservedAgent(agent) {
|
|
117
|
+
return observedAgents.add(agent);
|
|
118
|
+
}
|
|
119
|
+
const supportedAgentStats = ['connected', 'free', 'pending', 'queued', 'running', 'size'];
|
|
120
|
+
function createAgentMetricsExporter(initialAgents, options) {
|
|
121
|
+
var _options$metricPrefix;
|
|
122
|
+
const metricName = `${(_options$metricPrefix = options === null || options === void 0 ? void 0 : options.metricPrefix) !== null && _options$metricPrefix !== void 0 ? _options$metricPrefix : ''}nodejs_undici_agent`;
|
|
123
|
+
if (initialAgents) {
|
|
124
|
+
observedAgents.addMany(initialAgents);
|
|
125
|
+
}
|
|
126
|
+
new metrics.Prometheus.Gauge({
|
|
127
|
+
name: `${metricName}s_total`,
|
|
128
|
+
help: 'Number of Undici agents.',
|
|
129
|
+
registers: [metrics.defaultRegister],
|
|
130
|
+
collect() {
|
|
131
|
+
this.set(observedAgents.size);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
for (const supportedStat of supportedAgentStats) {
|
|
135
|
+
new metrics.Prometheus.Gauge({
|
|
136
|
+
name: `${metricName}_${supportedStat}`,
|
|
137
|
+
help: `Statistics for Undici agents ${supportedStat} stat. See https://github.com/nodejs/undici/blob/main/docs/docs/api/Agent.md#agentstats`,
|
|
138
|
+
labelNames: ['origin'],
|
|
139
|
+
registers: [metrics.defaultRegister],
|
|
140
|
+
collect() {
|
|
141
|
+
for (const agent of observedAgents) {
|
|
142
|
+
// If the agent has made no requests, it will not have stats
|
|
143
|
+
if (!agent.stats) {
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
for (const [origin, stats] of Object.entries(agent.stats)) {
|
|
147
|
+
// Client stats do not have free property
|
|
148
|
+
// @ts-expect-error
|
|
149
|
+
const statValue = stats[supportedStat];
|
|
150
|
+
if (typeof statValue === 'number' && !Number.isNaN(statValue)) {
|
|
151
|
+
this.labels(origin).set(statValue);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
exports.addObservedAgent = addObservedAgent;
|
|
86
161
|
exports.addObservedPool = addObservedPool;
|
|
162
|
+
exports.createAgentMetricsExporter = createAgentMetricsExporter;
|
|
87
163
|
exports.createPoolMetricsExporter = createPoolMetricsExporter;
|
|
88
164
|
exports.observedPoolFactory = observedPoolFactory;
|
|
165
|
+
exports.supportedAgentStats = supportedAgentStats;
|
|
89
166
|
exports.supportedPoolStats = supportedPoolStats;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@promster/undici",
|
|
3
|
-
"version": "15.
|
|
3
|
+
"version": "15.4.0",
|
|
4
4
|
"description": "Undici server integrations of promster",
|
|
5
5
|
"main": "dist/promster-undici.cjs.js",
|
|
6
6
|
"typings": "dist/promster-undici.cjs.d.ts",
|
|
@@ -39,18 +39,18 @@
|
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"merge-options": "3.0.4",
|
|
41
41
|
"tslib": "2.6.3",
|
|
42
|
-
"@promster/metrics": "15.
|
|
43
|
-
"@promster/types": "15.
|
|
42
|
+
"@promster/metrics": "15.4.0",
|
|
43
|
+
"@promster/types": "15.4.0"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@types/node": "20.
|
|
46
|
+
"@types/node": "20.17.46",
|
|
47
47
|
"prom-client": "15.1.3",
|
|
48
|
-
"typescript": "5.
|
|
48
|
+
"typescript": "5.8.3",
|
|
49
49
|
"parse-prometheus-text-format": "1.1.1",
|
|
50
|
-
"undici": "7.
|
|
50
|
+
"undici": "7.9.0",
|
|
51
51
|
"express": "4.21.2",
|
|
52
|
-
"@promster/server": "15.
|
|
53
|
-
"@promster/express": "15.
|
|
52
|
+
"@promster/server": "15.4.0",
|
|
53
|
+
"@promster/express": "15.4.0"
|
|
54
54
|
},
|
|
55
55
|
"peerDependencies": {
|
|
56
56
|
"prom-client": "13.x.x || 14.x || 15.x",
|