@promster/undici 15.5.1 → 15.5.2
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.
- package/dist/index.cjs +151 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +23 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +144 -0
- package/dist/index.js.map +1 -0
- package/package.json +32 -10
- package/dist/declarations/src/agent-metrics.d.ts +0 -9
- package/dist/declarations/src/index.d.ts +0 -2
- package/dist/declarations/src/pool-metrics.d.ts +0 -10
- package/dist/promster-undici.cjs.d.ts +0 -2
- package/dist/promster-undici.cjs.dev.js +0 -166
- package/dist/promster-undici.cjs.js +0 -7
- package/dist/promster-undici.cjs.prod.js +0 -166
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let _promster_metrics = require("@promster/metrics");
|
|
3
|
+
let undici = require("undici");
|
|
4
|
+
//#region src/agent-metrics.ts
|
|
5
|
+
var ObservedAgents = class {
|
|
6
|
+
agents;
|
|
7
|
+
constructor(initialAgents) {
|
|
8
|
+
this.agents = [];
|
|
9
|
+
if (initialAgents) this.addMany(initialAgents);
|
|
10
|
+
}
|
|
11
|
+
add(agent) {
|
|
12
|
+
this.agents.push(agent);
|
|
13
|
+
return agent;
|
|
14
|
+
}
|
|
15
|
+
addMany(agents) {
|
|
16
|
+
this.agents.push(...agents);
|
|
17
|
+
}
|
|
18
|
+
remove(agent) {
|
|
19
|
+
const index = this.agents.indexOf(agent);
|
|
20
|
+
if (index !== -1) {
|
|
21
|
+
this.agents.splice(index, 1);
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
get size() {
|
|
27
|
+
return this.agents.length;
|
|
28
|
+
}
|
|
29
|
+
[Symbol.iterator]() {
|
|
30
|
+
return this.agents.values();
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
const observedAgents = new ObservedAgents();
|
|
34
|
+
function addObservedAgent(agent) {
|
|
35
|
+
return observedAgents.add(agent);
|
|
36
|
+
}
|
|
37
|
+
const supportedAgentStats = [
|
|
38
|
+
"connected",
|
|
39
|
+
"free",
|
|
40
|
+
"pending",
|
|
41
|
+
"queued",
|
|
42
|
+
"running",
|
|
43
|
+
"size"
|
|
44
|
+
];
|
|
45
|
+
function createAgentMetricsExporter(initialAgents, options) {
|
|
46
|
+
const metricName = `${options?.metricPrefix ?? ""}nodejs_undici_agent`;
|
|
47
|
+
if (initialAgents) observedAgents.addMany(initialAgents);
|
|
48
|
+
new _promster_metrics.Prometheus.Gauge({
|
|
49
|
+
name: `${metricName}s_total`,
|
|
50
|
+
help: "Number of Undici agents.",
|
|
51
|
+
registers: [_promster_metrics.defaultRegister],
|
|
52
|
+
collect() {
|
|
53
|
+
this.set(observedAgents.size);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
for (const supportedStat of supportedAgentStats) new _promster_metrics.Prometheus.Gauge({
|
|
57
|
+
name: `${metricName}_${supportedStat}`,
|
|
58
|
+
help: `Statistics for Undici agents ${supportedStat} stat. See https://github.com/nodejs/undici/blob/main/docs/docs/api/Agent.md#agentstats`,
|
|
59
|
+
labelNames: ["origin"],
|
|
60
|
+
registers: [_promster_metrics.defaultRegister],
|
|
61
|
+
collect() {
|
|
62
|
+
for (const agent of observedAgents) {
|
|
63
|
+
if (!agent.stats) continue;
|
|
64
|
+
for (const [origin, stats] of Object.entries(agent.stats)) {
|
|
65
|
+
const statValue = stats[supportedStat];
|
|
66
|
+
if (typeof statValue === "number" && !Number.isNaN(statValue)) this.labels(origin).set(statValue);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region src/pool-metrics.ts
|
|
74
|
+
var ObservedPools = class {
|
|
75
|
+
pools;
|
|
76
|
+
constructor(initialPools) {
|
|
77
|
+
this.pools = /* @__PURE__ */ new Map();
|
|
78
|
+
if (initialPools) this.addMany(initialPools);
|
|
79
|
+
}
|
|
80
|
+
add(origin, pool) {
|
|
81
|
+
this.pools.set(origin, pool);
|
|
82
|
+
return pool;
|
|
83
|
+
}
|
|
84
|
+
addMany(pools) {
|
|
85
|
+
for (const [origin, pool] of Object.entries(pools)) this.add(origin, pool);
|
|
86
|
+
}
|
|
87
|
+
remove(origin) {
|
|
88
|
+
return this.pools.delete(origin);
|
|
89
|
+
}
|
|
90
|
+
get(origin) {
|
|
91
|
+
return this.pools.get(origin);
|
|
92
|
+
}
|
|
93
|
+
get size() {
|
|
94
|
+
return this.pools.size;
|
|
95
|
+
}
|
|
96
|
+
[Symbol.iterator]() {
|
|
97
|
+
return this.pools.entries();
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
const observedPools = new ObservedPools();
|
|
101
|
+
function addObservedPool(origin, pool) {
|
|
102
|
+
return observedPools.add(origin, pool);
|
|
103
|
+
}
|
|
104
|
+
function observedPoolFactory(origin, options) {
|
|
105
|
+
if (options?.connections === 1) return new undici.Client(origin, options);
|
|
106
|
+
return addObservedPool(origin, new undici.Pool(origin, options));
|
|
107
|
+
}
|
|
108
|
+
const supportedPoolStats = [
|
|
109
|
+
"connected",
|
|
110
|
+
"free",
|
|
111
|
+
"pending",
|
|
112
|
+
"queued",
|
|
113
|
+
"running",
|
|
114
|
+
"size"
|
|
115
|
+
];
|
|
116
|
+
function createPoolMetricsExporter(initialPools, options) {
|
|
117
|
+
const metricName = `${options?.metricPrefix ?? ""}nodejs_undici_pool`;
|
|
118
|
+
if (initialPools) observedPools.addMany(initialPools);
|
|
119
|
+
new _promster_metrics.Prometheus.Gauge({
|
|
120
|
+
name: `${metricName}s_total`,
|
|
121
|
+
help: "Number of Undici connection pools.",
|
|
122
|
+
registers: [_promster_metrics.defaultRegister],
|
|
123
|
+
collect() {
|
|
124
|
+
this.set(observedPools.size);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
for (const supportedStat of supportedPoolStats) new _promster_metrics.Prometheus.Gauge({
|
|
128
|
+
name: `${metricName}_${supportedStat}`,
|
|
129
|
+
help: `Statistics for Undici connection pools ${supportedStat} stat. See https://github.com/nodejs/undici/blob/main/docs/docs/api/PoolStats.md`,
|
|
130
|
+
labelNames: ["origin"],
|
|
131
|
+
registers: [_promster_metrics.defaultRegister],
|
|
132
|
+
collect() {
|
|
133
|
+
for (const [origin, pool] of observedPools) {
|
|
134
|
+
const stats = pool.stats;
|
|
135
|
+
if (!stats) continue;
|
|
136
|
+
const statValue = stats[supportedStat];
|
|
137
|
+
if (typeof statValue === "number" && !Number.isNaN(statValue)) this.labels(origin).set(statValue);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
//#endregion
|
|
143
|
+
exports.addObservedAgent = addObservedAgent;
|
|
144
|
+
exports.addObservedPool = addObservedPool;
|
|
145
|
+
exports.createAgentMetricsExporter = createAgentMetricsExporter;
|
|
146
|
+
exports.createPoolMetricsExporter = createPoolMetricsExporter;
|
|
147
|
+
exports.observedPoolFactory = observedPoolFactory;
|
|
148
|
+
exports.supportedAgentStats = supportedAgentStats;
|
|
149
|
+
exports.supportedPoolStats = supportedPoolStats;
|
|
150
|
+
|
|
151
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["Prometheus","defaultRegister","UndiciClient","UndiciPool","Prometheus","defaultRegister"],"sources":["../src/agent-metrics.ts","../src/pool-metrics.ts"],"sourcesContent":["import { defaultRegister, Prometheus } from '@promster/metrics';\nimport type { Agent as TUndiciAgent, Pool as TUndiciPool } from 'undici';\n\ntype TAgentMetricsExporterOptions = {\n metricPrefix?: string;\n};\ntype TAgentStatsKeys = keyof TUndiciPool.PoolStats;\n\nclass ObservedAgents {\n private agents: TUndiciAgent[];\n\n constructor(initialAgents?: TUndiciAgent[]) {\n this.agents = [];\n\n if (initialAgents) {\n this.addMany(initialAgents);\n }\n }\n\n add(agent: TUndiciAgent): TUndiciAgent {\n this.agents.push(agent);\n\n return agent;\n }\n\n addMany(agents: TUndiciAgent[]): void {\n this.agents.push(...agents);\n }\n\n remove(agent: TUndiciAgent): boolean {\n const index = this.agents.indexOf(agent);\n if (index !== -1) {\n this.agents.splice(index, 1);\n return true;\n }\n return false;\n }\n\n get size(): number {\n return this.agents.length;\n }\n\n [Symbol.iterator](): IterableIterator<TUndiciAgent> {\n return this.agents.values();\n }\n}\n\nconst observedAgents = new ObservedAgents();\n\nfunction addObservedAgent(agent: TUndiciAgent) {\n return observedAgents.add(agent);\n}\n\nconst supportedAgentStats: readonly TAgentStatsKeys[] = [\n 'connected',\n 'free',\n 'pending',\n 'queued',\n 'running',\n 'size',\n];\n\nfunction createAgentMetricsExporter(\n initialAgents?: TUndiciAgent[],\n options?: TAgentMetricsExporterOptions,\n): void {\n const metricName = `${options?.metricPrefix ?? ''}nodejs_undici_agent`;\n\n if (initialAgents) {\n observedAgents.addMany(initialAgents);\n }\n\n const _totalGauge = new Prometheus.Gauge({\n name: `${metricName}s_total`,\n help: 'Number of Undici agents.',\n registers: [defaultRegister],\n collect() {\n this.set(observedAgents.size);\n },\n });\n\n for (const supportedStat of supportedAgentStats) {\n const _statGauge = new Prometheus.Gauge({\n name: `${metricName}_${supportedStat}`,\n help: `Statistics for Undici agents ${supportedStat} stat. See https://github.com/nodejs/undici/blob/main/docs/docs/api/Agent.md#agentstats`,\n labelNames: ['origin'],\n registers: [defaultRegister],\n collect() {\n for (const agent of observedAgents) {\n // If the agent has made no requests, it will not have stats\n if (!agent.stats) {\n continue;\n }\n\n for (const [origin, stats] of Object.entries(agent.stats)) {\n // Client stats do not have free property\n // @ts-expect-error\n const statValue = stats[supportedStat];\n\n if (typeof statValue === 'number' && !Number.isNaN(statValue)) {\n this.labels(origin).set(statValue);\n }\n }\n }\n },\n });\n }\n}\n\nexport {\n createAgentMetricsExporter,\n supportedAgentStats,\n addObservedAgent,\n type TAgentMetricsExporterOptions,\n};\n","import { defaultRegister, Prometheus } from '@promster/metrics';\nimport {\n type Agent as TUndiciAgent,\n type Dispatcher as TUndiciDispatcher,\n type Pool as TUndiciPool,\n Client as UndiciClient,\n Pool as UndiciPool,\n} from 'undici';\n\ntype TPoolsMetricsExporterOptions = {\n metricPrefix?: string;\n};\ntype TPoolStatsKeys = keyof TUndiciPool.PoolStats;\n\nclass ObservedPools {\n private pools: Map<string, TUndiciPool>;\n\n constructor(initialPools?: Record<string, TUndiciPool>) {\n this.pools = new Map();\n\n if (initialPools) {\n this.addMany(initialPools);\n }\n }\n\n add(origin: string, pool: TUndiciPool): TUndiciPool {\n this.pools.set(origin, pool);\n\n return pool;\n }\n\n addMany(pools: Record<string, TUndiciPool>): void {\n for (const [origin, pool] of Object.entries(pools)) {\n this.add(origin, pool);\n }\n }\n\n remove(origin: string): boolean {\n return this.pools.delete(origin);\n }\n\n get(origin: string): TUndiciPool | undefined {\n return this.pools.get(origin);\n }\n\n get size(): number {\n return this.pools.size;\n }\n\n [Symbol.iterator](): IterableIterator<[string, TUndiciPool]> {\n return this.pools.entries();\n }\n}\n\nconst observedPools = new ObservedPools();\n\nfunction addObservedPool(origin: string, pool: TUndiciPool) {\n return observedPools.add(origin, pool);\n}\n\nfunction observedPoolFactory(\n origin: string,\n options?: TUndiciAgent.Options,\n): TUndiciDispatcher {\n if (options?.connections === 1) {\n return new UndiciClient(origin, options);\n }\n\n return addObservedPool(origin, new UndiciPool(origin, options));\n}\n\nconst supportedPoolStats: readonly TPoolStatsKeys[] = [\n 'connected',\n 'free',\n 'pending',\n 'queued',\n 'running',\n 'size',\n];\n\nfunction createPoolMetricsExporter(\n initialPools?: Record<string, TUndiciPool>,\n options?: TPoolsMetricsExporterOptions,\n): void {\n const metricName = `${options?.metricPrefix ?? ''}nodejs_undici_pool`;\n\n if (initialPools) {\n observedPools.addMany(initialPools);\n }\n\n const _totalGauge = new Prometheus.Gauge({\n name: `${metricName}s_total`,\n help: 'Number of Undici connection pools.',\n registers: [defaultRegister],\n collect() {\n this.set(observedPools.size);\n },\n });\n\n for (const supportedStat of supportedPoolStats) {\n const _statGauge = new Prometheus.Gauge({\n name: `${metricName}_${supportedStat}`,\n help: `Statistics for Undici connection pools ${supportedStat} stat. See https://github.com/nodejs/undici/blob/main/docs/docs/api/PoolStats.md`,\n labelNames: ['origin'],\n registers: [defaultRegister],\n collect() {\n for (const [origin, pool] of observedPools) {\n const stats = pool.stats;\n\n // If the pool has made no requests, it will not have stats\n if (!stats) {\n continue;\n }\n\n const statValue = stats[supportedStat];\n\n if (typeof statValue === 'number' && !Number.isNaN(statValue)) {\n this.labels(origin).set(statValue);\n }\n }\n },\n });\n }\n}\n\nexport {\n createPoolMetricsExporter,\n supportedPoolStats,\n addObservedPool,\n observedPoolFactory,\n type TPoolsMetricsExporterOptions,\n};\n"],"mappings":";;;;AAQA,IAAM,iBAAN,MAAqB;CACnB;CAEA,YAAY,eAAgC;EAC1C,KAAK,SAAS,CAAC;EAEf,IAAI,eACF,KAAK,QAAQ,aAAa;CAE9B;CAEA,IAAI,OAAmC;EACrC,KAAK,OAAO,KAAK,KAAK;EAEtB,OAAO;CACT;CAEA,QAAQ,QAA8B;EACpC,KAAK,OAAO,KAAK,GAAG,MAAM;CAC5B;CAEA,OAAO,OAA8B;EACnC,MAAM,QAAQ,KAAK,OAAO,QAAQ,KAAK;EACvC,IAAI,UAAU,IAAI;GAChB,KAAK,OAAO,OAAO,OAAO,CAAC;GAC3B,OAAO;EACT;EACA,OAAO;CACT;CAEA,IAAI,OAAe;EACjB,OAAO,KAAK,OAAO;CACrB;CAEA,CAAC,OAAO,YAA4C;EAClD,OAAO,KAAK,OAAO,OAAO;CAC5B;AACF;AAEA,MAAM,iBAAiB,IAAI,eAAe;AAE1C,SAAS,iBAAiB,OAAqB;CAC7C,OAAO,eAAe,IAAI,KAAK;AACjC;AAEA,MAAM,sBAA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+
import { Agent, Dispatcher, Pool } from "undici";
|
|
2
|
+
|
|
3
|
+
//#region src/agent-metrics.d.ts
|
|
4
|
+
type TAgentMetricsExporterOptions = {
|
|
5
|
+
metricPrefix?: string;
|
|
6
|
+
};
|
|
7
|
+
type TAgentStatsKeys = keyof Pool.PoolStats;
|
|
8
|
+
declare function addObservedAgent(agent: Agent): Agent;
|
|
9
|
+
declare const supportedAgentStats: readonly TAgentStatsKeys[];
|
|
10
|
+
declare function createAgentMetricsExporter(initialAgents?: Agent[], options?: TAgentMetricsExporterOptions): void;
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/pool-metrics.d.ts
|
|
13
|
+
type TPoolsMetricsExporterOptions = {
|
|
14
|
+
metricPrefix?: string;
|
|
15
|
+
};
|
|
16
|
+
type TPoolStatsKeys = keyof Pool.PoolStats;
|
|
17
|
+
declare function addObservedPool(origin: string, pool: Pool): Pool;
|
|
18
|
+
declare function observedPoolFactory(origin: string, options?: Agent.Options): Dispatcher;
|
|
19
|
+
declare const supportedPoolStats: readonly TPoolStatsKeys[];
|
|
20
|
+
declare function createPoolMetricsExporter(initialPools?: Record<string, Pool>, options?: TPoolsMetricsExporterOptions): void;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { addObservedAgent, addObservedPool, createAgentMetricsExporter, createPoolMetricsExporter, observedPoolFactory, supportedAgentStats, supportedPoolStats };
|
|
23
|
+
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Agent, Dispatcher, Pool } from "undici";
|
|
2
|
+
|
|
3
|
+
//#region src/agent-metrics.d.ts
|
|
4
|
+
type TAgentMetricsExporterOptions = {
|
|
5
|
+
metricPrefix?: string;
|
|
6
|
+
};
|
|
7
|
+
type TAgentStatsKeys = keyof Pool.PoolStats;
|
|
8
|
+
declare function addObservedAgent(agent: Agent): Agent;
|
|
9
|
+
declare const supportedAgentStats: readonly TAgentStatsKeys[];
|
|
10
|
+
declare function createAgentMetricsExporter(initialAgents?: Agent[], options?: TAgentMetricsExporterOptions): void;
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/pool-metrics.d.ts
|
|
13
|
+
type TPoolsMetricsExporterOptions = {
|
|
14
|
+
metricPrefix?: string;
|
|
15
|
+
};
|
|
16
|
+
type TPoolStatsKeys = keyof Pool.PoolStats;
|
|
17
|
+
declare function addObservedPool(origin: string, pool: Pool): Pool;
|
|
18
|
+
declare function observedPoolFactory(origin: string, options?: Agent.Options): Dispatcher;
|
|
19
|
+
declare const supportedPoolStats: readonly TPoolStatsKeys[];
|
|
20
|
+
declare function createPoolMetricsExporter(initialPools?: Record<string, Pool>, options?: TPoolsMetricsExporterOptions): void;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { addObservedAgent, addObservedPool, createAgentMetricsExporter, createPoolMetricsExporter, observedPoolFactory, supportedAgentStats, supportedPoolStats };
|
|
23
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { Prometheus, defaultRegister } from "@promster/metrics";
|
|
2
|
+
import { Client, Pool } from "undici";
|
|
3
|
+
//#region src/agent-metrics.ts
|
|
4
|
+
var ObservedAgents = class {
|
|
5
|
+
agents;
|
|
6
|
+
constructor(initialAgents) {
|
|
7
|
+
this.agents = [];
|
|
8
|
+
if (initialAgents) this.addMany(initialAgents);
|
|
9
|
+
}
|
|
10
|
+
add(agent) {
|
|
11
|
+
this.agents.push(agent);
|
|
12
|
+
return agent;
|
|
13
|
+
}
|
|
14
|
+
addMany(agents) {
|
|
15
|
+
this.agents.push(...agents);
|
|
16
|
+
}
|
|
17
|
+
remove(agent) {
|
|
18
|
+
const index = this.agents.indexOf(agent);
|
|
19
|
+
if (index !== -1) {
|
|
20
|
+
this.agents.splice(index, 1);
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
get size() {
|
|
26
|
+
return this.agents.length;
|
|
27
|
+
}
|
|
28
|
+
[Symbol.iterator]() {
|
|
29
|
+
return this.agents.values();
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
const observedAgents = new ObservedAgents();
|
|
33
|
+
function addObservedAgent(agent) {
|
|
34
|
+
return observedAgents.add(agent);
|
|
35
|
+
}
|
|
36
|
+
const supportedAgentStats = [
|
|
37
|
+
"connected",
|
|
38
|
+
"free",
|
|
39
|
+
"pending",
|
|
40
|
+
"queued",
|
|
41
|
+
"running",
|
|
42
|
+
"size"
|
|
43
|
+
];
|
|
44
|
+
function createAgentMetricsExporter(initialAgents, options) {
|
|
45
|
+
const metricName = `${options?.metricPrefix ?? ""}nodejs_undici_agent`;
|
|
46
|
+
if (initialAgents) observedAgents.addMany(initialAgents);
|
|
47
|
+
new Prometheus.Gauge({
|
|
48
|
+
name: `${metricName}s_total`,
|
|
49
|
+
help: "Number of Undici agents.",
|
|
50
|
+
registers: [defaultRegister],
|
|
51
|
+
collect() {
|
|
52
|
+
this.set(observedAgents.size);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
for (const supportedStat of supportedAgentStats) new Prometheus.Gauge({
|
|
56
|
+
name: `${metricName}_${supportedStat}`,
|
|
57
|
+
help: `Statistics for Undici agents ${supportedStat} stat. See https://github.com/nodejs/undici/blob/main/docs/docs/api/Agent.md#agentstats`,
|
|
58
|
+
labelNames: ["origin"],
|
|
59
|
+
registers: [defaultRegister],
|
|
60
|
+
collect() {
|
|
61
|
+
for (const agent of observedAgents) {
|
|
62
|
+
if (!agent.stats) continue;
|
|
63
|
+
for (const [origin, stats] of Object.entries(agent.stats)) {
|
|
64
|
+
const statValue = stats[supportedStat];
|
|
65
|
+
if (typeof statValue === "number" && !Number.isNaN(statValue)) this.labels(origin).set(statValue);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
//#endregion
|
|
72
|
+
//#region src/pool-metrics.ts
|
|
73
|
+
var ObservedPools = class {
|
|
74
|
+
pools;
|
|
75
|
+
constructor(initialPools) {
|
|
76
|
+
this.pools = /* @__PURE__ */ new Map();
|
|
77
|
+
if (initialPools) this.addMany(initialPools);
|
|
78
|
+
}
|
|
79
|
+
add(origin, pool) {
|
|
80
|
+
this.pools.set(origin, pool);
|
|
81
|
+
return pool;
|
|
82
|
+
}
|
|
83
|
+
addMany(pools) {
|
|
84
|
+
for (const [origin, pool] of Object.entries(pools)) this.add(origin, pool);
|
|
85
|
+
}
|
|
86
|
+
remove(origin) {
|
|
87
|
+
return this.pools.delete(origin);
|
|
88
|
+
}
|
|
89
|
+
get(origin) {
|
|
90
|
+
return this.pools.get(origin);
|
|
91
|
+
}
|
|
92
|
+
get size() {
|
|
93
|
+
return this.pools.size;
|
|
94
|
+
}
|
|
95
|
+
[Symbol.iterator]() {
|
|
96
|
+
return this.pools.entries();
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
const observedPools = new ObservedPools();
|
|
100
|
+
function addObservedPool(origin, pool) {
|
|
101
|
+
return observedPools.add(origin, pool);
|
|
102
|
+
}
|
|
103
|
+
function observedPoolFactory(origin, options) {
|
|
104
|
+
if (options?.connections === 1) return new Client(origin, options);
|
|
105
|
+
return addObservedPool(origin, new Pool(origin, options));
|
|
106
|
+
}
|
|
107
|
+
const supportedPoolStats = [
|
|
108
|
+
"connected",
|
|
109
|
+
"free",
|
|
110
|
+
"pending",
|
|
111
|
+
"queued",
|
|
112
|
+
"running",
|
|
113
|
+
"size"
|
|
114
|
+
];
|
|
115
|
+
function createPoolMetricsExporter(initialPools, options) {
|
|
116
|
+
const metricName = `${options?.metricPrefix ?? ""}nodejs_undici_pool`;
|
|
117
|
+
if (initialPools) observedPools.addMany(initialPools);
|
|
118
|
+
new Prometheus.Gauge({
|
|
119
|
+
name: `${metricName}s_total`,
|
|
120
|
+
help: "Number of Undici connection pools.",
|
|
121
|
+
registers: [defaultRegister],
|
|
122
|
+
collect() {
|
|
123
|
+
this.set(observedPools.size);
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
for (const supportedStat of supportedPoolStats) new Prometheus.Gauge({
|
|
127
|
+
name: `${metricName}_${supportedStat}`,
|
|
128
|
+
help: `Statistics for Undici connection pools ${supportedStat} stat. See https://github.com/nodejs/undici/blob/main/docs/docs/api/PoolStats.md`,
|
|
129
|
+
labelNames: ["origin"],
|
|
130
|
+
registers: [defaultRegister],
|
|
131
|
+
collect() {
|
|
132
|
+
for (const [origin, pool] of observedPools) {
|
|
133
|
+
const stats = pool.stats;
|
|
134
|
+
if (!stats) continue;
|
|
135
|
+
const statValue = stats[supportedStat];
|
|
136
|
+
if (typeof statValue === "number" && !Number.isNaN(statValue)) this.labels(origin).set(statValue);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
//#endregion
|
|
142
|
+
export { addObservedAgent, addObservedPool, createAgentMetricsExporter, createPoolMetricsExporter, observedPoolFactory, supportedAgentStats, supportedPoolStats };
|
|
143
|
+
|
|
144
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["UndiciClient","UndiciPool"],"sources":["../src/agent-metrics.ts","../src/pool-metrics.ts"],"sourcesContent":["import { defaultRegister, Prometheus } from '@promster/metrics';\nimport type { Agent as TUndiciAgent, Pool as TUndiciPool } from 'undici';\n\ntype TAgentMetricsExporterOptions = {\n metricPrefix?: string;\n};\ntype TAgentStatsKeys = keyof TUndiciPool.PoolStats;\n\nclass ObservedAgents {\n private agents: TUndiciAgent[];\n\n constructor(initialAgents?: TUndiciAgent[]) {\n this.agents = [];\n\n if (initialAgents) {\n this.addMany(initialAgents);\n }\n }\n\n add(agent: TUndiciAgent): TUndiciAgent {\n this.agents.push(agent);\n\n return agent;\n }\n\n addMany(agents: TUndiciAgent[]): void {\n this.agents.push(...agents);\n }\n\n remove(agent: TUndiciAgent): boolean {\n const index = this.agents.indexOf(agent);\n if (index !== -1) {\n this.agents.splice(index, 1);\n return true;\n }\n return false;\n }\n\n get size(): number {\n return this.agents.length;\n }\n\n [Symbol.iterator](): IterableIterator<TUndiciAgent> {\n return this.agents.values();\n }\n}\n\nconst observedAgents = new ObservedAgents();\n\nfunction addObservedAgent(agent: TUndiciAgent) {\n return observedAgents.add(agent);\n}\n\nconst supportedAgentStats: readonly TAgentStatsKeys[] = [\n 'connected',\n 'free',\n 'pending',\n 'queued',\n 'running',\n 'size',\n];\n\nfunction createAgentMetricsExporter(\n initialAgents?: TUndiciAgent[],\n options?: TAgentMetricsExporterOptions,\n): void {\n const metricName = `${options?.metricPrefix ?? ''}nodejs_undici_agent`;\n\n if (initialAgents) {\n observedAgents.addMany(initialAgents);\n }\n\n const _totalGauge = new Prometheus.Gauge({\n name: `${metricName}s_total`,\n help: 'Number of Undici agents.',\n registers: [defaultRegister],\n collect() {\n this.set(observedAgents.size);\n },\n });\n\n for (const supportedStat of supportedAgentStats) {\n const _statGauge = new Prometheus.Gauge({\n name: `${metricName}_${supportedStat}`,\n help: `Statistics for Undici agents ${supportedStat} stat. See https://github.com/nodejs/undici/blob/main/docs/docs/api/Agent.md#agentstats`,\n labelNames: ['origin'],\n registers: [defaultRegister],\n collect() {\n for (const agent of observedAgents) {\n // If the agent has made no requests, it will not have stats\n if (!agent.stats) {\n continue;\n }\n\n for (const [origin, stats] of Object.entries(agent.stats)) {\n // Client stats do not have free property\n // @ts-expect-error\n const statValue = stats[supportedStat];\n\n if (typeof statValue === 'number' && !Number.isNaN(statValue)) {\n this.labels(origin).set(statValue);\n }\n }\n }\n },\n });\n }\n}\n\nexport {\n createAgentMetricsExporter,\n supportedAgentStats,\n addObservedAgent,\n type TAgentMetricsExporterOptions,\n};\n","import { defaultRegister, Prometheus } from '@promster/metrics';\nimport {\n type Agent as TUndiciAgent,\n type Dispatcher as TUndiciDispatcher,\n type Pool as TUndiciPool,\n Client as UndiciClient,\n Pool as UndiciPool,\n} from 'undici';\n\ntype TPoolsMetricsExporterOptions = {\n metricPrefix?: string;\n};\ntype TPoolStatsKeys = keyof TUndiciPool.PoolStats;\n\nclass ObservedPools {\n private pools: Map<string, TUndiciPool>;\n\n constructor(initialPools?: Record<string, TUndiciPool>) {\n this.pools = new Map();\n\n if (initialPools) {\n this.addMany(initialPools);\n }\n }\n\n add(origin: string, pool: TUndiciPool): TUndiciPool {\n this.pools.set(origin, pool);\n\n return pool;\n }\n\n addMany(pools: Record<string, TUndiciPool>): void {\n for (const [origin, pool] of Object.entries(pools)) {\n this.add(origin, pool);\n }\n }\n\n remove(origin: string): boolean {\n return this.pools.delete(origin);\n }\n\n get(origin: string): TUndiciPool | undefined {\n return this.pools.get(origin);\n }\n\n get size(): number {\n return this.pools.size;\n }\n\n [Symbol.iterator](): IterableIterator<[string, TUndiciPool]> {\n return this.pools.entries();\n }\n}\n\nconst observedPools = new ObservedPools();\n\nfunction addObservedPool(origin: string, pool: TUndiciPool) {\n return observedPools.add(origin, pool);\n}\n\nfunction observedPoolFactory(\n origin: string,\n options?: TUndiciAgent.Options,\n): TUndiciDispatcher {\n if (options?.connections === 1) {\n return new UndiciClient(origin, options);\n }\n\n return addObservedPool(origin, new UndiciPool(origin, options));\n}\n\nconst supportedPoolStats: readonly TPoolStatsKeys[] = [\n 'connected',\n 'free',\n 'pending',\n 'queued',\n 'running',\n 'size',\n];\n\nfunction createPoolMetricsExporter(\n initialPools?: Record<string, TUndiciPool>,\n options?: TPoolsMetricsExporterOptions,\n): void {\n const metricName = `${options?.metricPrefix ?? ''}nodejs_undici_pool`;\n\n if (initialPools) {\n observedPools.addMany(initialPools);\n }\n\n const _totalGauge = new Prometheus.Gauge({\n name: `${metricName}s_total`,\n help: 'Number of Undici connection pools.',\n registers: [defaultRegister],\n collect() {\n this.set(observedPools.size);\n },\n });\n\n for (const supportedStat of supportedPoolStats) {\n const _statGauge = new Prometheus.Gauge({\n name: `${metricName}_${supportedStat}`,\n help: `Statistics for Undici connection pools ${supportedStat} stat. See https://github.com/nodejs/undici/blob/main/docs/docs/api/PoolStats.md`,\n labelNames: ['origin'],\n registers: [defaultRegister],\n collect() {\n for (const [origin, pool] of observedPools) {\n const stats = pool.stats;\n\n // If the pool has made no requests, it will not have stats\n if (!stats) {\n continue;\n }\n\n const statValue = stats[supportedStat];\n\n if (typeof statValue === 'number' && !Number.isNaN(statValue)) {\n this.labels(origin).set(statValue);\n }\n }\n },\n });\n }\n}\n\nexport {\n createPoolMetricsExporter,\n supportedPoolStats,\n addObservedPool,\n observedPoolFactory,\n type TPoolsMetricsExporterOptions,\n};\n"],"mappings":";;;AAQA,IAAM,iBAAN,MAAqB;CACnB;CAEA,YAAY,eAAgC;EAC1C,KAAK,SAAS,CAAC;EAEf,IAAI,eACF,KAAK,QAAQ,aAAa;CAE9B;CAEA,IAAI,OAAmC;EACrC,KAAK,OAAO,KAAK,KAAK;EAEtB,OAAO;CACT;CAEA,QAAQ,QAA8B;EACpC,KAAK,OAAO,KAAK,GAAG,MAAM;CAC5B;CAEA,OAAO,OAA8B;EACnC,MAAM,QAAQ,KAAK,OAAO,QAAQ,KAAK;EACvC,IAAI,UAAU,IAAI;GAChB,KAAK,OAAO,OAAO,OAAO,CAAC;GAC3B,OAAO;EACT;EACA,OAAO;CACT;CAEA,IAAI,OAAe;EACjB,OAAO,KAAK,OAAO;CACrB;CAEA,CAAC,OAAO,YAA4C;EAClD,OAAO,KAAK,OAAO,OAAO;CAC5B;AACF;AAEA,MAAM,iBAAiB,IAAI,eAAe;AAE1C,SAAS,iBAAiB,OAAqB;CAC7C,OAAO,eAAe,IAAI,KAAK;AACjC;AAEA,MAAM,sBAAkD;CACtD;CACA;CACA;CACA;CACA;CACA;AACF;AAEA,SAAS,2BACP,eACA,SACM;CACN,MAAM,aAAa,GAAG,SAAS,gBAAgB,GAAG;CAElD,IAAI,eACF,eAAe,QAAQ,aAAa;CAGlB,IAAI,WAAW,MAAM;EACvC,MAAM,GAAG,WAAW;EACpB,MAAM;EACN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AS,oBACP,QACA,SACmB;CACnB,IAAI,SAAS,gBAAgB,GAC3B,OAAO,IAAIA,OAAa,QAAQ,OAAO;CAGzC,OAAO,gBAAgB,QAAQ,IAAIC,KAAW,QAAQ,OAAO,CAAC;AAChE;AAEA,MAAM,qBAAgD;CACpD;CACA;CACA;CACA;CACA;CACA;AACF;AAEA,SAAS,0BACP,cACA,SACM;CACN,MAAM,aAAa,GAAG,SAAS,gBAAgB,GAAG;CAElD,IAAI,cACF,cAAc,QAAQ,YAAY;CAGhB,IAAI,WAAW,MAAM;EACvC,MAAM,GAAG,WAAW;EACpB,MAAM;EACN,WAAW,CAAC,eAAe;EAC3B,UAAU;GACR,KAAK,IAAI,cAAc,IAAI;EAC7B;CACF,CAAC;CAED,KAAK,MAAM,iBAAiB,oBACP,IAAI,WAAW,MAAM;EACtC,MAAM,GAAG,WAAW,GAAG;EACvB,MAAM,0CAA0C,cAAc;EAC9D,YAAY,CAAC,QAAQ;EACrB,WAAW,CAAC,eAAe;EAC3B,UAAU;GACR,KAAK,MAAM,CAAC,QAAQ,SAAS,eAAe;IAC1C,MAAM,QAAQ,KAAK;IAGnB,IAAI,CAAC,OACH;IAGF,MAAM,YAAY,MAAM;IAExB,IAAI,OAAO,cAAc,YAAY,CAAC,OAAO,MAAM,SAAS,GAC1D,KAAK,OAAO,MAAM,CAAC,CAAC,IAAI,SAAS;GAErC;EACF;CACF,CAAC;AAEL"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@promster/undici",
|
|
3
|
-
"version": "15.5.
|
|
3
|
+
"version": "15.5.2",
|
|
4
4
|
"description": "Undici server integrations of promster",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"continousdelivery",
|
|
@@ -23,30 +23,45 @@
|
|
|
23
23
|
"readme.md",
|
|
24
24
|
"package.json",
|
|
25
25
|
"LICENSE",
|
|
26
|
-
"dist/**"
|
|
27
|
-
"modules/**"
|
|
26
|
+
"dist/**"
|
|
28
27
|
],
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
28
|
+
"type": "module",
|
|
29
|
+
"sideEffects": false,
|
|
30
|
+
"main": "./dist/index.js",
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"import": {
|
|
35
|
+
"types": "./dist/index.d.ts",
|
|
36
|
+
"default": "./dist/index.js"
|
|
37
|
+
},
|
|
38
|
+
"require": {
|
|
39
|
+
"types": "./dist/index.d.cts",
|
|
40
|
+
"default": "./dist/index.cjs"
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
},
|
|
32
44
|
"publishConfig": {
|
|
33
45
|
"access": "public"
|
|
34
46
|
},
|
|
35
47
|
"dependencies": {
|
|
36
48
|
"merge-options": "3.0.4",
|
|
37
49
|
"tslib": "2.8.1",
|
|
38
|
-
"@promster/metrics": "15.5.
|
|
50
|
+
"@promster/metrics": "15.5.2"
|
|
39
51
|
},
|
|
40
52
|
"devDependencies": {
|
|
41
53
|
"@types/node": "24.12.2",
|
|
42
54
|
"express": "5.2.1",
|
|
43
55
|
"parse-prometheus-text-format": "1.1.1",
|
|
44
56
|
"prom-client": "15.1.3",
|
|
57
|
+
"tsdown": "0.22.2",
|
|
45
58
|
"typescript": "6.0.3",
|
|
46
59
|
"undici": "8.1.0",
|
|
47
|
-
"@promster/express": "15.5.
|
|
48
|
-
"@promster/server": "15.5.
|
|
49
|
-
"@promster/
|
|
60
|
+
"@promster/express": "15.5.2",
|
|
61
|
+
"@promster/server": "15.5.2",
|
|
62
|
+
"@promster/tsconfig": "15.5.2",
|
|
63
|
+
"@promster/tsdown-config": "15.5.2",
|
|
64
|
+
"@promster/types": "15.5.2"
|
|
50
65
|
},
|
|
51
66
|
"peerDependencies": {
|
|
52
67
|
"prom-client": "13.x.x || 14.x || 15.x",
|
|
@@ -55,5 +70,12 @@
|
|
|
55
70
|
"engines": {
|
|
56
71
|
"node": ">=20",
|
|
57
72
|
"pnpm": ">=11"
|
|
73
|
+
},
|
|
74
|
+
"scripts": {
|
|
75
|
+
"build": "tsdown",
|
|
76
|
+
"check-types": "tsc --noEmit",
|
|
77
|
+
"test": "vitest --run",
|
|
78
|
+
"test:watch": "vitest",
|
|
79
|
+
"dev": "tsdown --watch"
|
|
58
80
|
}
|
|
59
81
|
}
|
|
@@ -1,9 +0,0 @@
|
|
|
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,10 +0,0 @@
|
|
|
1
|
-
import { type Agent as TUndiciAgent, type Dispatcher as TUndiciDispatcher, type Pool as TUndiciPool } from 'undici';
|
|
2
|
-
type TPoolsMetricsExporterOptions = {
|
|
3
|
-
metricPrefix?: string;
|
|
4
|
-
};
|
|
5
|
-
type TPoolStatsKeys = keyof TUndiciPool.PoolStats;
|
|
6
|
-
declare function addObservedPool(origin: string, pool: TUndiciPool): TUndiciPool;
|
|
7
|
-
declare function observedPoolFactory(origin: string, options?: TUndiciAgent.Options): TUndiciDispatcher;
|
|
8
|
-
declare const supportedPoolStats: readonly TPoolStatsKeys[];
|
|
9
|
-
declare function createPoolMetricsExporter(initialPools?: Record<string, TUndiciPool>, options?: TPoolsMetricsExporterOptions): void;
|
|
10
|
-
export { createPoolMetricsExporter, supportedPoolStats, addObservedPool, observedPoolFactory, type TPoolsMetricsExporterOptions, };
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
export * from "./declarations/src/index.js";
|
|
2
|
-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicHJvbXN0ZXItdW5kaWNpLmNqcy5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi9kZWNsYXJhdGlvbnMvc3JjL2luZGV4LmQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEifQ==
|
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var metrics = require('@promster/metrics');
|
|
6
|
-
var undici = require('undici');
|
|
7
|
-
|
|
8
|
-
class ObservedAgents {
|
|
9
|
-
constructor(initialAgents) {
|
|
10
|
-
this.agents = [];
|
|
11
|
-
if (initialAgents) {
|
|
12
|
-
this.addMany(initialAgents);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
add(agent) {
|
|
16
|
-
this.agents.push(agent);
|
|
17
|
-
return agent;
|
|
18
|
-
}
|
|
19
|
-
addMany(agents) {
|
|
20
|
-
this.agents.push(...agents);
|
|
21
|
-
}
|
|
22
|
-
remove(agent) {
|
|
23
|
-
const index = this.agents.indexOf(agent);
|
|
24
|
-
if (index !== -1) {
|
|
25
|
-
this.agents.splice(index, 1);
|
|
26
|
-
return true;
|
|
27
|
-
}
|
|
28
|
-
return false;
|
|
29
|
-
}
|
|
30
|
-
get size() {
|
|
31
|
-
return this.agents.length;
|
|
32
|
-
}
|
|
33
|
-
[Symbol.iterator]() {
|
|
34
|
-
return this.agents.values();
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
const observedAgents = new ObservedAgents();
|
|
38
|
-
function addObservedAgent(agent) {
|
|
39
|
-
return observedAgents.add(agent);
|
|
40
|
-
}
|
|
41
|
-
const supportedAgentStats = ['connected', 'free', 'pending', 'queued', 'running', 'size'];
|
|
42
|
-
function createAgentMetricsExporter(initialAgents, options) {
|
|
43
|
-
var _options$metricPrefix;
|
|
44
|
-
const metricName = `${(_options$metricPrefix = options === null || options === void 0 ? void 0 : options.metricPrefix) !== null && _options$metricPrefix !== void 0 ? _options$metricPrefix : ''}nodejs_undici_agent`;
|
|
45
|
-
if (initialAgents) {
|
|
46
|
-
observedAgents.addMany(initialAgents);
|
|
47
|
-
}
|
|
48
|
-
new metrics.Prometheus.Gauge({
|
|
49
|
-
name: `${metricName}s_total`,
|
|
50
|
-
help: 'Number of Undici agents.',
|
|
51
|
-
registers: [metrics.defaultRegister],
|
|
52
|
-
collect() {
|
|
53
|
-
this.set(observedAgents.size);
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
for (const supportedStat of supportedAgentStats) {
|
|
57
|
-
new metrics.Prometheus.Gauge({
|
|
58
|
-
name: `${metricName}_${supportedStat}`,
|
|
59
|
-
help: `Statistics for Undici agents ${supportedStat} stat. See https://github.com/nodejs/undici/blob/main/docs/docs/api/Agent.md#agentstats`,
|
|
60
|
-
labelNames: ['origin'],
|
|
61
|
-
registers: [metrics.defaultRegister],
|
|
62
|
-
collect() {
|
|
63
|
-
for (const agent of observedAgents) {
|
|
64
|
-
// If the agent has made no requests, it will not have stats
|
|
65
|
-
if (!agent.stats) {
|
|
66
|
-
continue;
|
|
67
|
-
}
|
|
68
|
-
for (const [origin, stats] of Object.entries(agent.stats)) {
|
|
69
|
-
// Client stats do not have free property
|
|
70
|
-
// @ts-expect-error
|
|
71
|
-
const statValue = stats[supportedStat];
|
|
72
|
-
if (typeof statValue === 'number' && !Number.isNaN(statValue)) {
|
|
73
|
-
this.labels(origin).set(statValue);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
class ObservedPools {
|
|
83
|
-
constructor(initialPools) {
|
|
84
|
-
this.pools = new Map();
|
|
85
|
-
if (initialPools) {
|
|
86
|
-
this.addMany(initialPools);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
add(origin, pool) {
|
|
90
|
-
this.pools.set(origin, pool);
|
|
91
|
-
return pool;
|
|
92
|
-
}
|
|
93
|
-
addMany(pools) {
|
|
94
|
-
for (const [origin, pool] of Object.entries(pools)) {
|
|
95
|
-
this.add(origin, pool);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
remove(origin) {
|
|
99
|
-
return this.pools.delete(origin);
|
|
100
|
-
}
|
|
101
|
-
get(origin) {
|
|
102
|
-
return this.pools.get(origin);
|
|
103
|
-
}
|
|
104
|
-
get size() {
|
|
105
|
-
return this.pools.size;
|
|
106
|
-
}
|
|
107
|
-
[Symbol.iterator]() {
|
|
108
|
-
return this.pools.entries();
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
const observedPools = new ObservedPools();
|
|
112
|
-
function addObservedPool(origin, pool) {
|
|
113
|
-
return observedPools.add(origin, pool);
|
|
114
|
-
}
|
|
115
|
-
function observedPoolFactory(origin, options) {
|
|
116
|
-
if ((options === null || options === void 0 ? void 0 : options.connections) === 1) {
|
|
117
|
-
return new undici.Client(origin, options);
|
|
118
|
-
}
|
|
119
|
-
return addObservedPool(origin, new undici.Pool(origin, options));
|
|
120
|
-
}
|
|
121
|
-
const supportedPoolStats = ['connected', 'free', 'pending', 'queued', 'running', 'size'];
|
|
122
|
-
function createPoolMetricsExporter(initialPools, options) {
|
|
123
|
-
var _options$metricPrefix;
|
|
124
|
-
const metricName = `${(_options$metricPrefix = options === null || options === void 0 ? void 0 : options.metricPrefix) !== null && _options$metricPrefix !== void 0 ? _options$metricPrefix : ''}nodejs_undici_pool`;
|
|
125
|
-
if (initialPools) {
|
|
126
|
-
observedPools.addMany(initialPools);
|
|
127
|
-
}
|
|
128
|
-
new metrics.Prometheus.Gauge({
|
|
129
|
-
name: `${metricName}s_total`,
|
|
130
|
-
help: 'Number of Undici connection pools.',
|
|
131
|
-
registers: [metrics.defaultRegister],
|
|
132
|
-
collect() {
|
|
133
|
-
this.set(observedPools.size);
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
for (const supportedStat of supportedPoolStats) {
|
|
137
|
-
new metrics.Prometheus.Gauge({
|
|
138
|
-
name: `${metricName}_${supportedStat}`,
|
|
139
|
-
help: `Statistics for Undici connection pools ${supportedStat} stat. See https://github.com/nodejs/undici/blob/main/docs/docs/api/PoolStats.md`,
|
|
140
|
-
labelNames: ['origin'],
|
|
141
|
-
registers: [metrics.defaultRegister],
|
|
142
|
-
collect() {
|
|
143
|
-
for (const [origin, pool] of observedPools) {
|
|
144
|
-
const stats = pool.stats;
|
|
145
|
-
|
|
146
|
-
// If the pool has made no requests, it will not have stats
|
|
147
|
-
if (!stats) {
|
|
148
|
-
continue;
|
|
149
|
-
}
|
|
150
|
-
const statValue = stats[supportedStat];
|
|
151
|
-
if (typeof statValue === 'number' && !Number.isNaN(statValue)) {
|
|
152
|
-
this.labels(origin).set(statValue);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
exports.addObservedAgent = addObservedAgent;
|
|
161
|
-
exports.addObservedPool = addObservedPool;
|
|
162
|
-
exports.createAgentMetricsExporter = createAgentMetricsExporter;
|
|
163
|
-
exports.createPoolMetricsExporter = createPoolMetricsExporter;
|
|
164
|
-
exports.observedPoolFactory = observedPoolFactory;
|
|
165
|
-
exports.supportedAgentStats = supportedAgentStats;
|
|
166
|
-
exports.supportedPoolStats = supportedPoolStats;
|
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var metrics = require('@promster/metrics');
|
|
6
|
-
var undici = require('undici');
|
|
7
|
-
|
|
8
|
-
class ObservedAgents {
|
|
9
|
-
constructor(initialAgents) {
|
|
10
|
-
this.agents = [];
|
|
11
|
-
if (initialAgents) {
|
|
12
|
-
this.addMany(initialAgents);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
add(agent) {
|
|
16
|
-
this.agents.push(agent);
|
|
17
|
-
return agent;
|
|
18
|
-
}
|
|
19
|
-
addMany(agents) {
|
|
20
|
-
this.agents.push(...agents);
|
|
21
|
-
}
|
|
22
|
-
remove(agent) {
|
|
23
|
-
const index = this.agents.indexOf(agent);
|
|
24
|
-
if (index !== -1) {
|
|
25
|
-
this.agents.splice(index, 1);
|
|
26
|
-
return true;
|
|
27
|
-
}
|
|
28
|
-
return false;
|
|
29
|
-
}
|
|
30
|
-
get size() {
|
|
31
|
-
return this.agents.length;
|
|
32
|
-
}
|
|
33
|
-
[Symbol.iterator]() {
|
|
34
|
-
return this.agents.values();
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
const observedAgents = new ObservedAgents();
|
|
38
|
-
function addObservedAgent(agent) {
|
|
39
|
-
return observedAgents.add(agent);
|
|
40
|
-
}
|
|
41
|
-
const supportedAgentStats = ['connected', 'free', 'pending', 'queued', 'running', 'size'];
|
|
42
|
-
function createAgentMetricsExporter(initialAgents, options) {
|
|
43
|
-
var _options$metricPrefix;
|
|
44
|
-
const metricName = `${(_options$metricPrefix = options === null || options === void 0 ? void 0 : options.metricPrefix) !== null && _options$metricPrefix !== void 0 ? _options$metricPrefix : ''}nodejs_undici_agent`;
|
|
45
|
-
if (initialAgents) {
|
|
46
|
-
observedAgents.addMany(initialAgents);
|
|
47
|
-
}
|
|
48
|
-
new metrics.Prometheus.Gauge({
|
|
49
|
-
name: `${metricName}s_total`,
|
|
50
|
-
help: 'Number of Undici agents.',
|
|
51
|
-
registers: [metrics.defaultRegister],
|
|
52
|
-
collect() {
|
|
53
|
-
this.set(observedAgents.size);
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
for (const supportedStat of supportedAgentStats) {
|
|
57
|
-
new metrics.Prometheus.Gauge({
|
|
58
|
-
name: `${metricName}_${supportedStat}`,
|
|
59
|
-
help: `Statistics for Undici agents ${supportedStat} stat. See https://github.com/nodejs/undici/blob/main/docs/docs/api/Agent.md#agentstats`,
|
|
60
|
-
labelNames: ['origin'],
|
|
61
|
-
registers: [metrics.defaultRegister],
|
|
62
|
-
collect() {
|
|
63
|
-
for (const agent of observedAgents) {
|
|
64
|
-
// If the agent has made no requests, it will not have stats
|
|
65
|
-
if (!agent.stats) {
|
|
66
|
-
continue;
|
|
67
|
-
}
|
|
68
|
-
for (const [origin, stats] of Object.entries(agent.stats)) {
|
|
69
|
-
// Client stats do not have free property
|
|
70
|
-
// @ts-expect-error
|
|
71
|
-
const statValue = stats[supportedStat];
|
|
72
|
-
if (typeof statValue === 'number' && !Number.isNaN(statValue)) {
|
|
73
|
-
this.labels(origin).set(statValue);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
class ObservedPools {
|
|
83
|
-
constructor(initialPools) {
|
|
84
|
-
this.pools = new Map();
|
|
85
|
-
if (initialPools) {
|
|
86
|
-
this.addMany(initialPools);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
add(origin, pool) {
|
|
90
|
-
this.pools.set(origin, pool);
|
|
91
|
-
return pool;
|
|
92
|
-
}
|
|
93
|
-
addMany(pools) {
|
|
94
|
-
for (const [origin, pool] of Object.entries(pools)) {
|
|
95
|
-
this.add(origin, pool);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
remove(origin) {
|
|
99
|
-
return this.pools.delete(origin);
|
|
100
|
-
}
|
|
101
|
-
get(origin) {
|
|
102
|
-
return this.pools.get(origin);
|
|
103
|
-
}
|
|
104
|
-
get size() {
|
|
105
|
-
return this.pools.size;
|
|
106
|
-
}
|
|
107
|
-
[Symbol.iterator]() {
|
|
108
|
-
return this.pools.entries();
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
const observedPools = new ObservedPools();
|
|
112
|
-
function addObservedPool(origin, pool) {
|
|
113
|
-
return observedPools.add(origin, pool);
|
|
114
|
-
}
|
|
115
|
-
function observedPoolFactory(origin, options) {
|
|
116
|
-
if ((options === null || options === void 0 ? void 0 : options.connections) === 1) {
|
|
117
|
-
return new undici.Client(origin, options);
|
|
118
|
-
}
|
|
119
|
-
return addObservedPool(origin, new undici.Pool(origin, options));
|
|
120
|
-
}
|
|
121
|
-
const supportedPoolStats = ['connected', 'free', 'pending', 'queued', 'running', 'size'];
|
|
122
|
-
function createPoolMetricsExporter(initialPools, options) {
|
|
123
|
-
var _options$metricPrefix;
|
|
124
|
-
const metricName = `${(_options$metricPrefix = options === null || options === void 0 ? void 0 : options.metricPrefix) !== null && _options$metricPrefix !== void 0 ? _options$metricPrefix : ''}nodejs_undici_pool`;
|
|
125
|
-
if (initialPools) {
|
|
126
|
-
observedPools.addMany(initialPools);
|
|
127
|
-
}
|
|
128
|
-
new metrics.Prometheus.Gauge({
|
|
129
|
-
name: `${metricName}s_total`,
|
|
130
|
-
help: 'Number of Undici connection pools.',
|
|
131
|
-
registers: [metrics.defaultRegister],
|
|
132
|
-
collect() {
|
|
133
|
-
this.set(observedPools.size);
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
for (const supportedStat of supportedPoolStats) {
|
|
137
|
-
new metrics.Prometheus.Gauge({
|
|
138
|
-
name: `${metricName}_${supportedStat}`,
|
|
139
|
-
help: `Statistics for Undici connection pools ${supportedStat} stat. See https://github.com/nodejs/undici/blob/main/docs/docs/api/PoolStats.md`,
|
|
140
|
-
labelNames: ['origin'],
|
|
141
|
-
registers: [metrics.defaultRegister],
|
|
142
|
-
collect() {
|
|
143
|
-
for (const [origin, pool] of observedPools) {
|
|
144
|
-
const stats = pool.stats;
|
|
145
|
-
|
|
146
|
-
// If the pool has made no requests, it will not have stats
|
|
147
|
-
if (!stats) {
|
|
148
|
-
continue;
|
|
149
|
-
}
|
|
150
|
-
const statValue = stats[supportedStat];
|
|
151
|
-
if (typeof statValue === 'number' && !Number.isNaN(statValue)) {
|
|
152
|
-
this.labels(origin).set(statValue);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
exports.addObservedAgent = addObservedAgent;
|
|
161
|
-
exports.addObservedPool = addObservedPool;
|
|
162
|
-
exports.createAgentMetricsExporter = createAgentMetricsExporter;
|
|
163
|
-
exports.createPoolMetricsExporter = createPoolMetricsExporter;
|
|
164
|
-
exports.observedPoolFactory = observedPoolFactory;
|
|
165
|
-
exports.supportedAgentStats = supportedAgentStats;
|
|
166
|
-
exports.supportedPoolStats = supportedPoolStats;
|