@data-fair/lib-node 2.4.0 → 2.5.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.
Files changed (2) hide show
  1. package/observer.js +31 -7
  2. package/package.json +1 -1
package/observer.js CHANGED
@@ -36,8 +36,7 @@ const serveRegistry = async (res, registry) => {
36
36
  res.end()
37
37
  }
38
38
  // live CPU performance inspection
39
- const serveCPUProfile = async (res, duration = 2000) => {
40
- // @ts-ignore
39
+ const serveCPUProfile = async (res, duration) => {
41
40
  const { Session } = await import('node:inspector/promises')
42
41
  const session = new Session()
43
42
  session.connect()
@@ -45,21 +44,44 @@ const serveCPUProfile = async (res, duration = 2000) => {
45
44
  await session.post('Profiler.start')
46
45
  await new Promise(resolve => setTimeout(resolve, duration))
47
46
  const { profile } = await session.post('Profiler.stop')
47
+ await session.post('Profiler.disable')
48
48
  session.disconnect()
49
49
  res.setHeader('Content-Disposition', `attachment; filename="data-fair-${hostname()}-${new Date().toISOString()}.cpuprofile"`)
50
50
  res.write(JSON.stringify(profile))
51
51
  res.end()
52
52
  }
53
+ // live heap allocation sampler
54
+ const serveHeapSnapshot = async (res, includeCollected, duration) => {
55
+ const { Session } = await import('node:inspector/promises')
56
+ const session = new Session()
57
+ session.connect()
58
+ await session.post('HeapProfiler.enable')
59
+ await session.post('HeapProfiler.startSampling', { includeObjectsCollectedByMajorGC: includeCollected, includeObjectsCollectedByMinorGC: true })
60
+ await new Promise(resolve => setTimeout(resolve, duration))
61
+ const { profile } = await session.post('HeapProfiler.stopSampling')
62
+ await session.post('HeapProfiler.disable')
63
+ session.disconnect()
64
+ res.setHeader('Content-Disposition', `attachment; filename="data-fair-${hostname()}-${new Date().toISOString()}.heapprofile"`)
65
+ res.write(JSON.stringify(profile))
66
+ res.end()
67
+ }
53
68
  let server
54
69
  export const startObserver = async (port = 9090) => {
55
70
  server = createServer((req, res) => {
56
- if (req.method === 'GET' && req.url === '/metrics') {
71
+ const url = new URL(req.url || '', `http://${req.headers.host}`)
72
+ if (req.method === 'GET' && url.pathname === '/metrics') {
57
73
  serveRegistry(res, register).catch(err => httpErrorHandler(res, err))
58
- } else if (req.method === 'GET' && (req.url === '/global-metrics' || req.url === '/service-metrics')) {
74
+ } else if (req.method === 'GET' && (url.pathname === '/global-metrics' || url.pathname === '/service-metrics')) {
59
75
  serveRegistry(res, servicePromRegistry).catch(err => httpErrorHandler(res, err))
60
- } else if (req.method === 'GET' && req.url === '/cpu-profile') {
61
- // TODO: duration
62
- serveCPUProfile(res).catch(err => httpErrorHandler(res, err))
76
+ } else if (req.method === 'GET' && url.pathname === '/cpu-profile') {
77
+ const duration = parseInt(url.searchParams.get('duration') || '2000')
78
+ serveCPUProfile(res, duration).catch(err => httpErrorHandler(res, err))
79
+ } else if (req.method === 'GET' && url.pathname === '/heap-profile') {
80
+ const duration = parseInt(url.searchParams.get('duration') || '2000')
81
+ serveHeapSnapshot(res, false, duration).catch(err => httpErrorHandler(res, err))
82
+ } else if (req.method === 'GET' && url.pathname === '/heap-profile-all') {
83
+ const duration = parseInt(url.searchParams.get('duration') || '2000')
84
+ serveHeapSnapshot(res, true, duration).catch(err => httpErrorHandler(res, err))
63
85
  } else {
64
86
  res.writeHead(404)
65
87
  res.end()
@@ -71,6 +93,8 @@ export const startObserver = async (port = 9090) => {
71
93
  GET /metrics -> get prometheus metrics for this instance
72
94
  GET /service-metrics -> get prometheus metrics shared accross all instances
73
95
  GET /cpu-profile -> generate and fetch a CPU profile
96
+ GET /heap-snapshot -> generate and fetch a sampled heap allocation snapshot
97
+ GET /heap-snapshot-all -> generate and fetch a sampled heap allocation snapshot including objects collected by GC
74
98
  `)
75
99
  }
76
100
  export const stopObserver = async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@data-fair/lib-node",
3
- "version": "2.4.0",
3
+ "version": "2.5.0",
4
4
  "description": "Small nodejs related utility functions for the data-fair stack.",
5
5
  "main": "index.js",
6
6
  "files": [