Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion web/backend/eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

module.exports = require('neostandard')({ ignores: ['dist', 'node_modules'], ts: true })
module.exports = require('neostandard')({
ignores: ['dist', 'node_modules'], ts: true
})
3 changes: 2 additions & 1 deletion web/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"eslint": "^9.17.0",
"fastify": "^5.0.0",
"neostandard": "^0.12.0",
"typescript": "^5.5.4"
"typescript": "^5.5.4",
"wattpm": "^2.30.1"
},
"dependencies": {
"@platformatic/control": "^2.30.1",
Expand Down
24 changes: 14 additions & 10 deletions web/backend/plugins/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,26 @@ export default async function (fastify: FastifyInstance, opts: FastifyPluginOpti
fastify.decorate('mappedMetrics', {})

const api = new RuntimeApiClient()
const runtimes = await api.getRuntimes()

const metricsInterval = setInterval(async () => {
const runtimes = await api.getRuntimes()
for (const { pid } of runtimes) {
// TODO: add more strict types into `@platformatic/control` to avoid casting to `any`
const runtimeMetrics: any = await api.getRuntimeMetrics(pid, { format: 'json' })
try {
// TODO: add more strict types into `@platformatic/control` to avoid casting to `any`
const runtimeMetrics: any = await api.getRuntimeMetrics(pid, { format: 'json' })

for (const { name, type, aggregator, values } of runtimeMetrics) {
if (!fastify.mappedMetrics[pid]) {
fastify.mappedMetrics[pid] = []
}
for (const { name, type, aggregator, values } of runtimeMetrics) {
if (!fastify.mappedMetrics[pid]) {
fastify.mappedMetrics[pid] = []
}

const serviceId = values[0]?.labels?.serviceId
if (serviceId) {
fastify.mappedMetrics[pid].push({ name, time: new Date(), type, aggregator, values, serviceId, pid })
const serviceId = values[0]?.labels?.serviceId
if (serviceId) {
fastify.mappedMetrics[pid].push({ name, time: new Date(), type, aggregator, values, serviceId, pid })
}
}
} catch (error) {
fastify.log.warn(error, 'Unable to get runtime metrics. Retry will start soon...')
}
}
}, 1000)
Expand Down
30 changes: 30 additions & 0 deletions web/backend/test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { join } from 'node:path'
import { readFile } from 'node:fs/promises'
import { buildServer } from '@platformatic/service'
import { test } from 'node:test'
import { spawn } from 'node:child_process'

type testfn = Parameters<typeof test>[0]
type TestContext = Parameters<Exclude<testfn, undefined>>[0]
Expand All @@ -22,3 +23,32 @@ export async function getServer (t: TestContext) {

return server
}

export async function startWatt (t: TestContext): Promise<string> {
const fixturesPath = join(__dirname, '..', '..', '..', '..', 'node_modules', '.bin', 'wattpm')
const process = spawn(fixturesPath, ['start'])
t.after(() => process.kill('SIGKILL'))

return new Promise((resolve, reject) => {
const onData = (data: Buffer) => {
const input = data.toString()
if (input.includes('Platformatic is now listening at ')) {
removeListeners()
resolve(input)
}
}

const onError = (error: Error) => {
removeListeners()
reject(error)
}

const removeListeners = () => {
process.stdout.removeListener('data', onData)
process.removeListener('error', onError)
}

process.stdout.on('data', onData)
process.on('error', onError)
})
}
71 changes: 66 additions & 5 deletions web/backend/test/plugins/metrics.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,72 @@
import test from 'node:test'
import assert from 'node:assert'
import { getServer } from '../helper'
import { getServer, startWatt } from '../helper'

// TODO: add setup and test on a up & running wattpm instance
test('metrics plugin', async (t) => {
const server = await getServer(t)
const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))

test('metrics without runtime', async (t) => {
const server = await getServer(t)
assert.ok('_idleTimeout' in server.metricsInterval, 'interval for metrics are defined')
assert.deepEqual(server.mappedMetrics, {}, 'mapped metrics are defined')
const metricsKeys = Object.keys(server.mappedMetrics)
assert.ok(metricsKeys.length === 0, 'mapped metrics are defined, and are empty')
})

test('metrics with runtime', async (t) => {
await startWatt(t)
const server = await getServer(t)

await wait(1200)
const metricsKeys = Object.keys(server.mappedMetrics)
const [pid] = metricsKeys
assert.ok(metricsKeys.length > 0, 'mapped metrics are defined, and contain values')
assert.strictEqual(server.mappedMetrics[pid][0].pid, parseInt(pid))

const res = await server.inject({
method: 'GET',
url: `/runtimes/${pid}/metrics`
})
assert.strictEqual(res.statusCode, 200)

const metrics = res.json()
const expectedNames = [
'process_cpu_user_seconds_total',
'process_cpu_system_seconds_total',
'process_cpu_seconds_total',
'process_start_time_seconds',
'process_resident_memory_bytes',
'nodejs_eventloop_lag_seconds',
'nodejs_eventloop_lag_min_seconds',
'nodejs_eventloop_lag_max_seconds',
'nodejs_eventloop_lag_mean_seconds',
'nodejs_eventloop_lag_stddev_seconds',
'nodejs_eventloop_lag_p50_seconds',
'nodejs_eventloop_lag_p90_seconds',
'nodejs_eventloop_lag_p99_seconds',
'nodejs_active_resources',
'nodejs_active_resources_total',
'nodejs_active_handles',
'nodejs_active_handles_total',
'nodejs_active_requests_total',
'nodejs_heap_size_total_bytes',
'nodejs_heap_size_used_bytes',
'nodejs_external_memory_bytes',
'nodejs_heap_space_size_total_bytes',
'nodejs_heap_space_size_used_bytes',
'nodejs_heap_space_size_available_bytes',
'nodejs_version_info',
'nodejs_gc_duration_seconds',
'nodejs_eventloop_utilization',
'process_cpu_percent_usage',
'thread_cpu_user_system_seconds_total',
'thread_cpu_system_seconds_total',
'thread_cpu_percent_usage',
'thread_cpu_seconds_total',
'http_cache_hit_count',
'http_cache_miss_count'
]
expectedNames.forEach(expectedName => {
// TODO: remove cast to any
const exists = metrics.some((metric: any) => metric.name === expectedName)
assert.ok(exists, `Expected metric: ${expectedName}`)
})
})
20 changes: 16 additions & 4 deletions web/backend/test/routes/root.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import test from 'node:test'
import assert from 'node:assert'
import { getServer } from '../helper'
import { getServer, startWatt } from '../helper'

// TODO: add setup and test on a up & running wattpm instance
test('root', async (t) => {
test('no runtime running', async (t) => {
const server = await getServer(t)
const res = await server.inject({
method: 'GET',
url: '/runtimes'
})
assert.strictEqual(res.statusCode, 200)
assert.deepStrictEqual(res.json(), [], 'with no runtime running')
})

test('runtime is running', async (t) => {
await startWatt(t)
const server = await getServer(t)
const res = await server.inject({
method: 'GET',
url: '/runtimes'
})
assert.strictEqual(res.statusCode, 200)
assert.deepStrictEqual(res.json(), [])
const [runtime] = res.json()
assert.strictEqual(typeof runtime.packageName, 'string')
assert.strictEqual(typeof runtime.pid, 'number')
assert.strictEqual(runtime.packageVersion, null)
})