-
-
Notifications
You must be signed in to change notification settings - Fork 312
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
Update libp2p to ESM version #4114
Changes from all commits
377a0e8
984584a
1253a9e
764ecad
e0018e1
449d134
03a8554
e56001d
2b1bd00
b4ee2b3
ec3afb1
611309c
6c6b9cb
bd898be
65f703f
dd404cf
d7b0620
56fcb0d
b28aca6
1f2c75b
7189aa4
824ce9c
fbf39e1
f84b569
f513f09
b534502
3e27e94
8ac985d
252a148
982711f
497a4f9
a0eef4c
19bd339
b95b702
67da674
e219ad5
71538ba
679e998
07fe735
dec8c4c
f8e4320
d5d93e2
8aceeb0
fcce170
7308ba9
093e759
0a85eb6
7531aae
a346ac3
03d2429
afd17d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import {Libp2p} from "libp2p"; | ||
import {GaugeExtra} from "../utils/gauge.js"; | ||
import {RegistryMetricCreator} from "../utils/registryMetricCreator.js"; | ||
|
||
export type ILibp2pMetrics = ReturnType<typeof createLibp2pMetrics>; | ||
|
||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/explicit-function-return-type | ||
export function createLibp2pMetrics(libp2p: Libp2p, register: RegistryMetricCreator) { | ||
const libp2pMetrics = libp2p.metrics; | ||
if (libp2pMetrics === undefined) { | ||
return; | ||
} | ||
|
||
const metrics: Record<string, GaugeExtra<string>> = {}; | ||
|
||
const ensureLibp2pMetrics = async (): Promise<void> => { | ||
// protocol metrics | ||
const protocols = libp2pMetrics.getProtocols(); | ||
protocols.forEach((protocol) => { | ||
const protocolStat = libp2pMetrics.forProtocol(protocol); | ||
if (protocolStat === undefined) { | ||
return; | ||
} | ||
|
||
// create metric if undefined | ||
if (metrics[protocol] === undefined) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't we just add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think not, bc they split out the direction. eg: would be helpful to see if we're spammed with identify requests, but not responding. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, just add |
||
const name = `libp2p_protocol_${protocol}_bytes`.replace(/(\/|-|\.)/g, "_"); | ||
metrics[protocol] = register.gauge<"direction">({ | ||
name, | ||
help: name, | ||
labelNames: ["direction"], | ||
}); | ||
} | ||
|
||
// set metric | ||
const protocolSnapshot = protocolStat.getSnapshot(); | ||
metrics[protocol].set({direction: "received"}, Number(protocolSnapshot.dataReceived)); | ||
metrics[protocol].set({direction: "sent"}, Number(protocolSnapshot.dataSent)); | ||
}); | ||
|
||
// component metrics | ||
for (const [systemName, systemMetrics] of libp2pMetrics.getComponentMetrics().entries()) { | ||
for (const [componentName, componentMetrics] of systemMetrics.entries()) { | ||
for (const [metricName, trackedMetric] of componentMetrics.entries()) { | ||
// In practice `systemName` is always libp2p | ||
const name = `${systemName}_${componentName}_${metricName}`.replace(/-/g, "_"); | ||
|
||
// create metric if undefined | ||
if (metrics[name] === undefined) { | ||
metrics[name] = register.gauge({ | ||
name, | ||
help: trackedMetric.help ?? name, | ||
labelNames: trackedMetric.label !== undefined ? [trackedMetric.label] : [], | ||
}); | ||
} | ||
|
||
// set metric | ||
const m = await trackedMetric.calculate(); | ||
if (typeof m === "number") { | ||
metrics[name].set(m); | ||
} else { | ||
const labelName = trackedMetric.label ?? name; | ||
Object.entries(m).forEach(([label, value]) => { | ||
metrics[name].set({[labelName]: label}, value); | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
metrics.global = register.gauge<"direction">({ | ||
name: "libp2p_global_stats", | ||
help: "libp2p global stats", | ||
labelNames: ["direction"], | ||
collect: async () => { | ||
const globalSnapshot = libp2pMetrics.getGlobal().getSnapshot(); | ||
metrics.global.set({direction: "received"}, Number(globalSnapshot.dataReceived)); | ||
metrics.global.set({direction: "sent"}, Number(globalSnapshot.dataSent)); | ||
|
||
await ensureLibp2pMetrics(); | ||
}, | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, why is leveldown required? For libp2p datastore? If so, @wemeetagain can you motivate upstream dep to move to level >= 8.0.0?