@data-fair/lib-node 2.4.0 → 2.5.1

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/mongo.d.ts CHANGED
@@ -1,11 +1,12 @@
1
- import type { IndexSpecification, CreateIndexesOptions, MongoClientOptions, IndexDirection } from 'mongodb';
1
+ import type { IndexSpecification, CreateIndexesOptions, MongoClientOptions, IndexDirection, Db } from 'mongodb';
2
2
  import { MongoClient } from 'mongodb';
3
3
  type IndexDirections = Record<string, IndexDirection>;
4
4
  export type IndexDefinitions = Record<string, Record<string, null | IndexDirections | [IndexDirections, CreateIndexesOptions]>>;
5
5
  export declare class Mongo {
6
6
  private _client;
7
+ private _db;
7
8
  get client(): MongoClient;
8
- get db(): import("mongodb").Db;
9
+ get db(): Db;
9
10
  connect: (mongoUrl: string, options?: MongoClientOptions) => Promise<void>;
10
11
  ensureIndex: (collection: string, key: IndexSpecification, options?: CreateIndexesOptions) => Promise<void>;
11
12
  /**
package/mongo.js CHANGED
@@ -1,13 +1,15 @@
1
1
  import { MongoClient, MongoError } from 'mongodb'
2
2
  export class Mongo {
3
3
  _client
4
+ _db
4
5
  get client () {
5
6
  if (!this._client) { throw new Error('db was not connected') }
6
7
  return this._client
7
8
  }
8
9
 
9
10
  get db () {
10
- return this.client.db()
11
+ if (!this._db) { throw new Error('db was not connected') }
12
+ return this._db
11
13
  }
12
14
 
13
15
  connect = async (mongoUrl, options = {}) => {
@@ -17,6 +19,7 @@ export class Mongo {
17
19
  }
18
20
  console.log('connecting to mongodb...')
19
21
  this._client = await MongoClient.connect(mongoUrl, { maxPoolSize: 5, ignoreUndefined: true, ...options })
22
+ this._db = this._client.db()
20
23
  console.log('...ok')
21
24
  }
22
25
 
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-profile -> generate and fetch a sampled heap allocation snapshot
97
+ GET /heap-profile-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.1",
4
4
  "description": "Small nodejs related utility functions for the data-fair stack.",
5
5
  "main": "index.js",
6
6
  "files": [