@platformatic/watt-extra 1.15.0-alpha.2 → 1.15.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.
@@ -275,11 +275,8 @@ test('sendFlamegraphs should upload flamegraphs from all services', async (t) =>
275
275
 
276
276
  const mockProfile = new Uint8Array([1, 2, 3, 4, 5])
277
277
 
278
- app.watt.runtime.sendCommandToApplication = async (serviceId, command) => {
279
- if (command === 'getLastProfile') {
280
- return mockProfile
281
- }
282
- return { success: false }
278
+ app.watt.runtime.getApplicationLastProfile = async (workerId, options) => {
279
+ return { profile: mockProfile, timestamp: Date.now(), preserved: false }
283
280
  }
284
281
 
285
282
  // Mock HTTP server to receive flamegraphs
@@ -328,12 +325,9 @@ test('sendFlamegraphs should handle missing profile data', async (t) => {
328
325
  errors.push(obj)
329
326
  }
330
327
 
331
- app.watt.runtime.sendCommandToApplication = async (serviceId, command) => {
332
- if (command === 'getLastProfile') {
333
- // Return invalid data (not Uint8Array)
334
- return null
335
- }
336
- return { success: false }
328
+ app.watt.runtime.getApplicationLastProfile = async (workerId, options) => {
329
+ // Return invalid data (not Uint8Array)
330
+ return { profile: null, timestamp: null, preserved: false }
337
331
  }
338
332
 
339
333
  await flamegraphsPlugin(app)
@@ -348,12 +342,9 @@ test('sendFlamegraphs should filter by workerIds when provided', async (t) => {
348
342
  const app = createMockApp(port + 12)
349
343
  const getProfileCalls = []
350
344
 
351
- app.watt.runtime.sendCommandToApplication = async (workerId, command) => {
352
- if (command === 'getLastProfile') {
353
- getProfileCalls.push(workerId)
354
- return new Uint8Array([1, 2, 3])
355
- }
356
- return { success: false }
345
+ app.watt.runtime.getApplicationLastProfile = async (workerId, options) => {
346
+ getProfileCalls.push(workerId)
347
+ return { profile: new Uint8Array([1, 2, 3]), timestamp: Date.now(), preserved: false }
357
348
  }
358
349
 
359
350
  // Mock HTTP server
@@ -383,15 +374,12 @@ test('sendFlamegraphs should try to get the profile from a service if worker is
383
374
  const app = createMockApp(port + 12)
384
375
  const getProfileCalls = []
385
376
 
386
- app.watt.runtime.sendCommandToApplication = async (workerId, command) => {
387
- if (command === 'getLastProfile') {
388
- getProfileCalls.push(workerId)
389
- if (workerId === 'service-1:2') {
390
- throw new Error('Worker not available')
391
- }
392
- return new Uint8Array([1, 2, 3])
377
+ app.watt.runtime.getApplicationLastProfile = async (workerId, options) => {
378
+ getProfileCalls.push(workerId)
379
+ if (workerId === 'service-1:2') {
380
+ throw new Error('Worker not available')
393
381
  }
394
- return { success: false }
382
+ return { profile: new Uint8Array([1, 2, 3]), timestamp: Date.now(), preserved: false }
395
383
  }
396
384
 
397
385
  // Mock HTTP server
@@ -424,12 +412,9 @@ test('sendFlamegraphs should skip when PLT_DISABLE_FLAMEGRAPHS is set', async (t
424
412
 
425
413
  const getProfileCalls = []
426
414
 
427
- app.watt.runtime.sendCommandToApplication = async (workerId, command) => {
428
- if (command === 'getLastProfile') {
429
- getProfileCalls.push(workerId)
430
- return new Uint8Array([1, 2, 3])
431
- }
432
- return { success: false }
415
+ app.watt.runtime.getApplicationLastProfile = async (workerId, options) => {
416
+ getProfileCalls.push(workerId)
417
+ return { profile: new Uint8Array([1, 2, 3]), timestamp: Date.now(), preserved: false }
433
418
  }
434
419
 
435
420
  await flamegraphsPlugin(app)
@@ -484,16 +469,17 @@ test('should handle trigger-flamegraph command and upload flamegraphs from servi
484
469
  if (command === 'startProfiling') {
485
470
  return { success: true }
486
471
  }
487
- if (command === 'getLastProfile') {
488
- getFlamegraphReqs.push({ serviceId })
489
- if (getFlamegraphReqs.length === 2) {
490
- uploadResolve()
491
- }
492
- return new Uint8Array([1, 2, 3, 4, 5])
493
- }
494
472
  return { success: false }
495
473
  }
496
474
 
475
+ app.watt.runtime.getApplicationLastProfile = async (serviceId, options) => {
476
+ getFlamegraphReqs.push({ serviceId })
477
+ if (getFlamegraphReqs.length === 2) {
478
+ uploadResolve()
479
+ }
480
+ return { profile: new Uint8Array([1, 2, 3, 4, 5]), timestamp: Date.now(), preserved: false }
481
+ }
482
+
497
483
  await updatePlugin(app)
498
484
  await flamegraphsPlugin(app)
499
485
 
@@ -593,6 +579,10 @@ test('should handle trigger-flamegraph when flamegraph upload fails', async (t)
593
579
  return { success: false }
594
580
  }
595
581
 
582
+ app.watt.runtime.getApplicationLastProfile = async (serviceId, options) => {
583
+ return { profile: new Uint8Array([1, 2, 3, 4, 5]), timestamp: Date.now(), preserved: false }
584
+ }
585
+
596
586
  await updatePlugin(app)
597
587
  await flamegraphsPlugin(app)
598
588
 
@@ -645,16 +635,18 @@ test('should handle trigger-heapprofile command and upload heap profiles from se
645
635
  if (command === 'startProfiling') {
646
636
  return { success: true }
647
637
  }
648
- if (command === 'getLastProfile') {
649
- getHeapProfileReqs.push({ serviceId })
650
- if (getHeapProfileReqs.length === 2) {
651
- uploadResolve()
652
- }
653
- return new Uint8Array([1, 2, 3, 4, 5])
654
- }
655
638
  return { success: false }
656
639
  }
657
640
 
641
+ app.watt.runtime.getApplicationLastProfile = async (serviceId, options) => {
642
+ equal(options.type, 'heap')
643
+ getHeapProfileReqs.push({ serviceId })
644
+ if (getHeapProfileReqs.length === 2) {
645
+ uploadResolve()
646
+ }
647
+ return { profile: new Uint8Array([1, 2, 3, 4, 5]), timestamp: Date.now(), preserved: false }
648
+ }
649
+
658
650
  await updatePlugin(app)
659
651
  await flamegraphsPlugin(app)
660
652
 
@@ -728,18 +720,19 @@ test('should handle PLT_PPROF_NO_PROFILE_AVAILABLE error with info log', async (
728
720
  if (command === 'startProfiling') {
729
721
  return { success: true }
730
722
  }
731
- if (command === 'getLastProfile') {
732
- const now = Date.now()
733
- if (now < profileGenerationDate) {
734
- const error = new Error('No profile available - wait for profiling to complete or trigger manual capture')
735
- error.code = 'PLT_PPROF_NO_PROFILE_AVAILABLE'
736
- throw error
737
- }
738
- return mockProfile
739
- }
740
723
  return { success: false }
741
724
  }
742
725
 
726
+ app.watt.runtime.getApplicationLastProfile = async (serviceId, options) => {
727
+ const now = Date.now()
728
+ if (now < profileGenerationDate) {
729
+ const error = new Error('No profile available - wait for profiling to complete or trigger manual capture')
730
+ error.code = 'PLT_PPROF_NO_PROFILE_AVAILABLE'
731
+ throw error
732
+ }
733
+ return { profile: mockProfile, timestamp: now, preserved: false }
734
+ }
735
+
743
736
  await updatePlugin(app)
744
737
  await flamegraphsPlugin(app)
745
738
 
@@ -844,14 +837,15 @@ test('should handle PLT_PPROF_NOT_ENOUGH_ELU error with info log', async (t) =>
844
837
  if (command === 'startProfiling') {
845
838
  return { success: true }
846
839
  }
847
- if (command === 'getLastProfile') {
848
- const error = new Error('No profile available - event loop utilization has been below threshold for too long')
849
- error.code = 'PLT_PPROF_NOT_ENOUGH_ELU'
850
- throw error
851
- }
852
840
  return { success: false }
853
841
  }
854
842
 
843
+ app.watt.runtime.getApplicationLastProfile = async (serviceId, options) => {
844
+ const error = new Error('No profile available - event loop utilization has been below threshold for too long')
845
+ error.code = 'PLT_PPROF_NOT_ENOUGH_ELU'
846
+ throw error
847
+ }
848
+
855
849
  await updatePlugin(app)
856
850
  await flamegraphsPlugin(app)
857
851
 
@@ -1016,11 +1010,8 @@ test('sendFlamegraphs should include alertId in query params when provided', asy
1016
1010
 
1017
1011
  const app = createMockApp(port + 18)
1018
1012
 
1019
- app.watt.runtime.sendCommandToApplication = async (serviceId, command) => {
1020
- if (command === 'getLastProfile') {
1021
- return new Uint8Array([1, 2, 3])
1022
- }
1023
- return { success: false }
1013
+ app.watt.runtime.getApplicationLastProfile = async (workerId, options) => {
1014
+ return { profile: new Uint8Array([1, 2, 3]), timestamp: Date.now(), preserved: false }
1024
1015
  }
1025
1016
 
1026
1017
  // Mock HTTP server to capture requests
@@ -1050,3 +1041,81 @@ test('sendFlamegraphs should include alertId in query params when provided', asy
1050
1041
  ok(req.url.includes('alertId=test-alert-123'), 'URL should include alertId query param')
1051
1042
  }
1052
1043
  })
1044
+
1045
+ test('sendFlamegraphs should log warn when worker is blocked and no preserved profile exists', async (t) => {
1046
+ setUpEnvironment()
1047
+
1048
+ const app = createMockApp(port + 19)
1049
+ const warnLogs = []
1050
+ const getProfileCalls = []
1051
+
1052
+ const originalWarn = app.log.warn
1053
+ app.log.warn = (...args) => {
1054
+ originalWarn(...args)
1055
+ warnLogs.push(args)
1056
+ }
1057
+
1058
+ app.watt.runtime.getApplicationLastProfile = async (workerId, options) => {
1059
+ getProfileCalls.push(workerId)
1060
+ const error = new Error('Timed out while waiting for the last profile')
1061
+ error.code = 'PLT_RUNTIME_LAST_PROFILE_TIMEOUT'
1062
+ throw error
1063
+ }
1064
+
1065
+ await flamegraphsPlugin(app)
1066
+ await app.sendFlamegraphs({ workerIds: ['service-1:0'] })
1067
+
1068
+ equal(getProfileCalls.length, 1, 'Should not retry on profile timeout')
1069
+
1070
+ const timeoutLog = warnLogs.find(
1071
+ ([, message]) => typeof message === 'string' && message.includes('Worker event loop is not responding')
1072
+ )
1073
+ ok(timeoutLog, 'Should log warn about the profile timeout')
1074
+ })
1075
+
1076
+ test('sendFlamegraphs should upload preserved profiles and log the preserved flag', async (t) => {
1077
+ setUpEnvironment()
1078
+
1079
+ const uploadedFlamegraphs = []
1080
+ const httpPort = port + 20
1081
+ const app = createMockApp(httpPort)
1082
+ const infoLogs = []
1083
+
1084
+ const originalInfo = app.log.info
1085
+ app.log.info = (...args) => {
1086
+ originalInfo(...args)
1087
+ infoLogs.push(args)
1088
+ }
1089
+
1090
+ const mockProfile = new Uint8Array([9, 8, 7])
1091
+
1092
+ app.watt.runtime.getApplicationLastProfile = async (workerId, options) => {
1093
+ return { profile: mockProfile, timestamp: Date.now(), preserved: true }
1094
+ }
1095
+
1096
+ // Mock HTTP server to receive flamegraphs
1097
+ const { createServer } = await import('node:http')
1098
+ const server = createServer((req, res) => {
1099
+ const body = []
1100
+ req.on('data', chunk => body.push(chunk))
1101
+ req.on('end', () => {
1102
+ uploadedFlamegraphs.push({ url: req.url, body: Buffer.concat(body) })
1103
+ res.writeHead(200)
1104
+ res.end()
1105
+ })
1106
+ })
1107
+
1108
+ await new Promise(resolve => server.listen(httpPort, resolve))
1109
+ t.after(() => server.close())
1110
+
1111
+ await flamegraphsPlugin(app)
1112
+ await app.sendFlamegraphs()
1113
+
1114
+ equal(uploadedFlamegraphs.length, 2, 'Should upload preserved profiles for both services')
1115
+ deepEqual(uploadedFlamegraphs[0].body, Buffer.from(mockProfile))
1116
+
1117
+ const preservedLog = infoLogs.find(
1118
+ ([options, message]) => message === 'Got profile from worker' && options.preserved === true
1119
+ )
1120
+ ok(preservedLog, 'Should log that the profile is a preserved one')
1121
+ })
@@ -0,0 +1,54 @@
1
+ import assert from 'node:assert'
2
+ import { test } from 'node:test'
3
+ import { join, dirname } from 'node:path'
4
+ import { fileURLToPath } from 'node:url'
5
+
6
+ import { randomUUID } from 'node:crypto'
7
+ import { setUpEnvironment, startICC, installDeps } from './helper.js'
8
+ import { start } from '../index.js'
9
+
10
+ const __filename = fileURLToPath(import.meta.url)
11
+ const __dirname = dirname(__filename)
12
+
13
+ // watt-extra always starts the runtime with isProduction: true, so the runtime
14
+ // must resolve watch to false. The runtime defaults watch to !isProduction, and
15
+ // reads isProduction from the options passed to its transform, so a transform
16
+ // that does not forward them silently enables file watching in production.
17
+ // This needs a @platformatic/runtime app: a single-application config instead
18
+ // goes through wrapInRuntimeConfig, which never calls the custom transform.
19
+ test('should disable file watching in production', async (t) => {
20
+ const appName = 'test-app'
21
+ const applicationId = randomUUID()
22
+ const applicationPath = join(__dirname, 'fixtures', 'runtime-service')
23
+
24
+ await installDeps(t, applicationPath)
25
+
26
+ const icc = await startICC(t, {
27
+ applicationId,
28
+ port: 3011
29
+ })
30
+
31
+ setUpEnvironment({
32
+ PLT_APP_NAME: appName,
33
+ PLT_APP_DIR: applicationPath,
34
+ PLT_ICC_URL: icc.iccUrl,
35
+ PLT_CONTROL_PLANE_URL: 'http://127.0.0.1:3012',
36
+ PLT_APP_PORT: 3053,
37
+ PLT_METRICS_PORT: 9102
38
+ })
39
+
40
+ const app = await start()
41
+
42
+ t.after(async () => {
43
+ await app.close()
44
+ await icc.close()
45
+ })
46
+
47
+ const mainConfig = app.watt.runtime.getRuntimeConfig(true)
48
+
49
+ assert.strictEqual(mainConfig.watch, false)
50
+
51
+ for (const application of mainConfig.applications ?? []) {
52
+ assert.strictEqual(application.watch, false, `application ${application.id} must not watch files`)
53
+ }
54
+ })