@platformatic/control 2.74.3 → 2.75.0-alpha.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.
package/lib/errors.js CHANGED
@@ -22,4 +22,8 @@ module.exports = {
22
22
  FailedToGetRuntimeAllLogs: createError(`${ERROR_PREFIX}_FAILED_TO_GET_RUNTIME_ALL_LOGS`, 'Failed to get runtime all logs %s.'),
23
23
  FailedToGetRuntimeLogIndexes: createError(`${ERROR_PREFIX}_FAILED_TO_GET_HISTORY_LOGS_COUNT`, 'Failed to get history logs count %s.'),
24
24
  FailedToGetRuntimeMetrics: createError(`${ERROR_PREFIX}_FAILED_TO_GET_RUNTIME_METRICS`, 'Failed to get runtime metrics %s.'),
25
+ ProfilingAlreadyStarted: createError(`${ERROR_PREFIX}_PROFILING_ALREADY_STARTED`, 'Profiling is already started for service "%s".'),
26
+ ProfilingNotStarted: createError(`${ERROR_PREFIX}_PROFILING_NOT_STARTED`, 'Profiling not started for service "%s".'),
27
+ FailedToStartProfiling: createError(`${ERROR_PREFIX}_FAILED_TO_START_PROFILING`, 'Failed to start profiling for service "%s": %s'),
28
+ FailedToStopProfiling: createError(`${ERROR_PREFIX}_FAILED_TO_STOP_PROFILING`, 'Failed to stop profiling for service "%s": %s'),
25
29
  }
@@ -201,6 +201,79 @@ class RuntimeApiClient {
201
201
  return serviceConfig
202
202
  }
203
203
 
204
+ async startServiceProfiling (pid, serviceId, options = {}) {
205
+ const client = this.#getUndiciClient(pid)
206
+
207
+ const { statusCode, body } = await client.request({
208
+ path: `/api/v1/services/${serviceId}/pprof/start`,
209
+ method: 'POST',
210
+ headers: {
211
+ 'content-type': 'application/json'
212
+ },
213
+ body: JSON.stringify(options)
214
+ })
215
+
216
+ if (statusCode !== 200) {
217
+ const error = await body.text()
218
+ let jsonError
219
+ try {
220
+ jsonError = JSON.parse(error)
221
+ } catch {
222
+ // No-op
223
+ }
224
+
225
+ const message = jsonError?.message || error
226
+ const code = jsonError?.code
227
+
228
+ if (code === 'PLT_RUNTIME_SERVICE_NOT_FOUND' || code === 'PLT_RUNTIME_SERVICE_WORKER_NOT_FOUND') {
229
+ throw new errors.ServiceNotFound(message)
230
+ }
231
+
232
+ if (code === 'PLT_PPROF_PROFILING_ALREADY_STARTED') {
233
+ throw new errors.ProfilingAlreadyStarted(serviceId)
234
+ }
235
+
236
+ throw new errors.FailedToStartProfiling(serviceId, message)
237
+ }
238
+
239
+ return await body.json()
240
+ }
241
+
242
+ async stopServiceProfiling (pid, serviceId) {
243
+ const client = this.#getUndiciClient(pid)
244
+
245
+ const { statusCode, body } = await client.request({
246
+ path: `/api/v1/services/${serviceId}/pprof/stop`,
247
+ method: 'POST'
248
+ })
249
+
250
+ if (statusCode !== 200) {
251
+ const error = await body.text()
252
+ let jsonError
253
+ try {
254
+ jsonError = JSON.parse(error)
255
+ } catch {
256
+ // No-op
257
+ }
258
+
259
+ const message = jsonError?.message || error
260
+ const code = jsonError?.code
261
+
262
+ if (code === 'PLT_RUNTIME_SERVICE_NOT_FOUND' || code === 'PLT_RUNTIME_SERVICE_WORKER_NOT_FOUND') {
263
+ throw new errors.ServiceNotFound(message)
264
+ }
265
+
266
+ if (code === 'PLT_PPROF_PROFILING_NOT_STARTED') {
267
+ throw new errors.ProfilingNotStarted(serviceId)
268
+ }
269
+
270
+ throw new errors.FailedToStopProfiling(serviceId, message)
271
+ }
272
+
273
+ // Return the binary profile data as ArrayBuffer
274
+ return await body.arrayBuffer()
275
+ }
276
+
204
277
  async reloadRuntime (pid, options = {}) {
205
278
  const runtime = await this.getMatchingRuntime({ pid })
206
279
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/control",
3
- "version": "2.74.3",
3
+ "version": "2.75.0-alpha.0",
4
4
  "description": "Platformatic Control",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -23,10 +23,10 @@
23
23
  "execa": "^9.0.0",
24
24
  "neostandard": "^0.12.0",
25
25
  "split2": "^4.2.0",
26
- "tsd": "^0.32.0",
26
+ "tsd": "^0.33.0",
27
27
  "typescript": "^5.5.4",
28
- "@platformatic/runtime": "2.74.3",
29
- "@platformatic/service": "2.74.3"
28
+ "@platformatic/runtime": "2.75.0-alpha.0",
29
+ "@platformatic/service": "2.75.0-alpha.0"
30
30
  },
31
31
  "dependencies": {
32
32
  "@fastify/error": "^4.0.0",
@@ -37,7 +37,7 @@
37
37
  "table": "^6.8.1",
38
38
  "undici": "^7.0.0",
39
39
  "ws": "^8.16.0",
40
- "@platformatic/utils": "2.74.3"
40
+ "@platformatic/utils": "2.75.0-alpha.0"
41
41
  },
42
42
  "scripts": {
43
43
  "test": "pnpm run lint && pnpm run unit && tsd",