@platformatic/control 3.0.0-alpha.6 → 3.0.0-alpha.8

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/lib/errors.js CHANGED
@@ -77,3 +77,23 @@ export const FailedToGetRuntimeMetrics = createError(
77
77
  `${ERROR_PREFIX}_FAILED_TO_GET_RUNTIME_METRICS`,
78
78
  'Failed to get runtime metrics %s.'
79
79
  )
80
+
81
+ export const ProfilingAlreadyStarted = createError(
82
+ `${ERROR_PREFIX}_PROFILING_ALREADY_STARTED`,
83
+ 'Profiling is already started for service "%s".'
84
+ )
85
+
86
+ export const ProfilingNotStarted = createError(
87
+ `${ERROR_PREFIX}_PROFILING_NOT_STARTED`,
88
+ 'Profiling not started for service "%s".'
89
+ )
90
+
91
+ export const FailedToStartProfiling = createError(
92
+ `${ERROR_PREFIX}_FAILED_TO_START_PROFILING`,
93
+ 'Failed to start profiling for service "%s": %s'
94
+ )
95
+
96
+ export const FailedToStopProfiling = createError(
97
+ `${ERROR_PREFIX}_FAILED_TO_STOP_PROFILING`,
98
+ 'Failed to stop profiling for service "%s": %s'
99
+ )
package/lib/index.js CHANGED
@@ -20,8 +20,12 @@ import {
20
20
  FailedToGetRuntimeMetrics,
21
21
  FailedToGetRuntimeOpenapi,
22
22
  FailedToReloadRuntime,
23
+ FailedToStartProfiling,
24
+ FailedToStopProfiling,
23
25
  FailedToStopRuntime,
24
26
  FailedToStreamRuntimeLogs,
27
+ ProfilingAlreadyStarted,
28
+ ProfilingNotStarted,
25
29
  RuntimeNotFound
26
30
  } from './errors.js'
27
31
 
@@ -276,6 +280,79 @@ export class RuntimeApiClient {
276
280
  return applicationConfig
277
281
  }
278
282
 
283
+ async startApplicationProfiling (pid, applicationId, options = {}) {
284
+ const client = this.#getUndiciClient(pid)
285
+
286
+ const { statusCode, body } = await client.request({
287
+ path: `/api/v1/applications/${applicationId}/pprof/start`,
288
+ method: 'POST',
289
+ headers: {
290
+ 'content-type': 'application/json'
291
+ },
292
+ body: JSON.stringify(options)
293
+ })
294
+
295
+ if (statusCode !== 200) {
296
+ const error = await body.text()
297
+ let jsonError
298
+ try {
299
+ jsonError = JSON.parse(error)
300
+ } catch {
301
+ // No-op
302
+ }
303
+
304
+ const message = jsonError?.message || error
305
+ const code = jsonError?.code
306
+
307
+ if (code === 'PLT_RUNTIME_APPLICATION_NOT_FOUND' || code === 'PLT_RUNTIME_APPLICATION_WORKER_NOT_FOUND') {
308
+ throw new ApplicationNotFound(message)
309
+ }
310
+
311
+ if (code === 'PLT_PPROF_PROFILING_ALREADY_STARTED') {
312
+ throw new ProfilingAlreadyStarted(applicationId)
313
+ }
314
+
315
+ throw new FailedToStartProfiling(applicationId, message)
316
+ }
317
+
318
+ return await body.json()
319
+ }
320
+
321
+ async stopApplicationProfiling (pid, applicationId) {
322
+ const client = this.#getUndiciClient(pid)
323
+
324
+ const { statusCode, body } = await client.request({
325
+ path: `/api/v1/applications/${applicationId}/pprof/stop`,
326
+ method: 'POST'
327
+ })
328
+
329
+ if (statusCode !== 200) {
330
+ const error = await body.text()
331
+ let jsonError
332
+ try {
333
+ jsonError = JSON.parse(error)
334
+ } catch {
335
+ // No-op
336
+ }
337
+
338
+ const message = jsonError?.message || error
339
+ const code = jsonError?.code
340
+
341
+ if (code === 'PLT_RUNTIME_APPLICATION_NOT_FOUND' || code === 'PLT_RUNTIME_APPLICATION_WORKER_NOT_FOUND') {
342
+ throw new ApplicationNotFound(message)
343
+ }
344
+
345
+ if (code === 'PLT_PPROF_PROFILING_NOT_STARTED') {
346
+ throw new ProfilingNotStarted(applicationId)
347
+ }
348
+
349
+ throw new FailedToStopProfiling(applicationId, message)
350
+ }
351
+
352
+ // Return the binary profile data as ArrayBuffer
353
+ return await body.arrayBuffer()
354
+ }
355
+
279
356
  async reloadRuntime (pid, options = {}) {
280
357
  const runtime = await this.getMatchingRuntime({ pid })
281
358
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/control",
3
- "version": "3.0.0-alpha.6",
3
+ "version": "3.0.0-alpha.8",
4
4
  "description": "Platformatic Control",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -21,10 +21,10 @@
21
21
  "execa": "^9.0.0",
22
22
  "neostandard": "^0.12.0",
23
23
  "split2": "^4.2.0",
24
- "tsd": "^0.32.0",
24
+ "tsd": "^0.33.0",
25
25
  "typescript": "^5.5.4",
26
- "@platformatic/runtime": "3.0.0-alpha.6",
27
- "@platformatic/service": "3.0.0-alpha.6"
26
+ "@platformatic/service": "3.0.0-alpha.8",
27
+ "@platformatic/runtime": "3.0.0-alpha.8"
28
28
  },
29
29
  "dependencies": {
30
30
  "@fastify/error": "^4.0.0",
@@ -34,7 +34,7 @@
34
34
  "table": "^6.8.1",
35
35
  "undici": "^7.0.0",
36
36
  "ws": "^8.16.0",
37
- "@platformatic/foundation": "3.0.0-alpha.6"
37
+ "@platformatic/foundation": "3.0.0-alpha.8"
38
38
  },
39
39
  "engines": {
40
40
  "node": ">=22.18.0"