|
| 1 | +import { expect, test } from '@playwright/test'; |
| 2 | +import { waitForMetric } from '@sentry-internal/test-utils'; |
| 3 | + |
| 4 | +const EXPECTED_ATTRIBUTES = { |
| 5 | + 'sentry.environment': { value: 'qa', type: 'string' }, |
| 6 | + 'sentry.sdk.name': { value: 'sentry.javascript.node', type: 'string' }, |
| 7 | + 'sentry.sdk.version': { value: expect.any(String), type: 'string' }, |
| 8 | + 'sentry.origin': { value: 'auto.node.runtime_metrics', type: 'string' }, |
| 9 | +}; |
| 10 | + |
| 11 | +test('Should emit node runtime memory metrics', async ({ baseURL }) => { |
| 12 | + const rssPromise = waitForMetric('node-express-v5', metric => { |
| 13 | + return metric.name === 'node.runtime.mem.rss'; |
| 14 | + }); |
| 15 | + |
| 16 | + const heapUsedPromise = waitForMetric('node-express-v5', metric => { |
| 17 | + return metric.name === 'node.runtime.mem.heap_used'; |
| 18 | + }); |
| 19 | + |
| 20 | + const heapTotalPromise = waitForMetric('node-express-v5', metric => { |
| 21 | + return metric.name === 'node.runtime.mem.heap_total'; |
| 22 | + }); |
| 23 | + |
| 24 | + // Trigger a request to ensure the server is running and metrics start being collected |
| 25 | + await fetch(`${baseURL}/test-success`); |
| 26 | + |
| 27 | + const rss = await rssPromise; |
| 28 | + const heapUsed = await heapUsedPromise; |
| 29 | + const heapTotal = await heapTotalPromise; |
| 30 | + |
| 31 | + expect(rss).toMatchObject({ |
| 32 | + timestamp: expect.any(Number), |
| 33 | + trace_id: expect.any(String), |
| 34 | + name: 'node.runtime.mem.rss', |
| 35 | + type: 'gauge', |
| 36 | + unit: 'byte', |
| 37 | + value: expect.any(Number), |
| 38 | + attributes: expect.objectContaining(EXPECTED_ATTRIBUTES), |
| 39 | + }); |
| 40 | + |
| 41 | + expect(heapUsed).toMatchObject({ |
| 42 | + timestamp: expect.any(Number), |
| 43 | + trace_id: expect.any(String), |
| 44 | + name: 'node.runtime.mem.heap_used', |
| 45 | + type: 'gauge', |
| 46 | + unit: 'byte', |
| 47 | + value: expect.any(Number), |
| 48 | + attributes: expect.objectContaining(EXPECTED_ATTRIBUTES), |
| 49 | + }); |
| 50 | + |
| 51 | + expect(heapTotal).toMatchObject({ |
| 52 | + timestamp: expect.any(Number), |
| 53 | + trace_id: expect.any(String), |
| 54 | + name: 'node.runtime.mem.heap_total', |
| 55 | + type: 'gauge', |
| 56 | + unit: 'byte', |
| 57 | + value: expect.any(Number), |
| 58 | + attributes: expect.objectContaining(EXPECTED_ATTRIBUTES), |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +test('Should emit node runtime CPU utilization metric', async ({ baseURL }) => { |
| 63 | + const cpuUtilPromise = waitForMetric('node-express-v5', metric => { |
| 64 | + return metric.name === 'node.runtime.cpu.utilization'; |
| 65 | + }); |
| 66 | + |
| 67 | + await fetch(`${baseURL}/test-success`); |
| 68 | + |
| 69 | + const cpuUtil = await cpuUtilPromise; |
| 70 | + |
| 71 | + expect(cpuUtil).toMatchObject({ |
| 72 | + timestamp: expect.any(Number), |
| 73 | + trace_id: expect.any(String), |
| 74 | + name: 'node.runtime.cpu.utilization', |
| 75 | + type: 'gauge', |
| 76 | + value: expect.any(Number), |
| 77 | + attributes: expect.objectContaining(EXPECTED_ATTRIBUTES), |
| 78 | + }); |
| 79 | +}); |
| 80 | + |
| 81 | +test('Should emit node runtime event loop metrics', async ({ baseURL }) => { |
| 82 | + const elDelayP50Promise = waitForMetric('node-express-v5', metric => { |
| 83 | + return metric.name === 'node.runtime.event_loop.delay.p50'; |
| 84 | + }); |
| 85 | + |
| 86 | + const elDelayP99Promise = waitForMetric('node-express-v5', metric => { |
| 87 | + return metric.name === 'node.runtime.event_loop.delay.p99'; |
| 88 | + }); |
| 89 | + |
| 90 | + const elUtilPromise = waitForMetric('node-express-v5', metric => { |
| 91 | + return metric.name === 'node.runtime.event_loop.utilization'; |
| 92 | + }); |
| 93 | + |
| 94 | + await fetch(`${baseURL}/test-success`); |
| 95 | + |
| 96 | + const elDelayP50 = await elDelayP50Promise; |
| 97 | + const elDelayP99 = await elDelayP99Promise; |
| 98 | + const elUtil = await elUtilPromise; |
| 99 | + |
| 100 | + expect(elDelayP50).toMatchObject({ |
| 101 | + timestamp: expect.any(Number), |
| 102 | + trace_id: expect.any(String), |
| 103 | + name: 'node.runtime.event_loop.delay.p50', |
| 104 | + type: 'gauge', |
| 105 | + unit: 'second', |
| 106 | + value: expect.any(Number), |
| 107 | + attributes: expect.objectContaining(EXPECTED_ATTRIBUTES), |
| 108 | + }); |
| 109 | + |
| 110 | + expect(elDelayP99).toMatchObject({ |
| 111 | + timestamp: expect.any(Number), |
| 112 | + trace_id: expect.any(String), |
| 113 | + name: 'node.runtime.event_loop.delay.p99', |
| 114 | + type: 'gauge', |
| 115 | + unit: 'second', |
| 116 | + value: expect.any(Number), |
| 117 | + attributes: expect.objectContaining(EXPECTED_ATTRIBUTES), |
| 118 | + }); |
| 119 | + |
| 120 | + expect(elUtil).toMatchObject({ |
| 121 | + timestamp: expect.any(Number), |
| 122 | + trace_id: expect.any(String), |
| 123 | + name: 'node.runtime.event_loop.utilization', |
| 124 | + type: 'gauge', |
| 125 | + value: expect.any(Number), |
| 126 | + attributes: expect.objectContaining(EXPECTED_ATTRIBUTES), |
| 127 | + }); |
| 128 | +}); |
| 129 | + |
| 130 | +test('Should emit node runtime uptime counter', async ({ baseURL }) => { |
| 131 | + const uptimePromise = waitForMetric('node-express-v5', metric => { |
| 132 | + return metric.name === 'node.runtime.process.uptime'; |
| 133 | + }); |
| 134 | + |
| 135 | + await fetch(`${baseURL}/test-success`); |
| 136 | + |
| 137 | + const uptime = await uptimePromise; |
| 138 | + |
| 139 | + expect(uptime).toMatchObject({ |
| 140 | + timestamp: expect.any(Number), |
| 141 | + trace_id: expect.any(String), |
| 142 | + name: 'node.runtime.process.uptime', |
| 143 | + type: 'counter', |
| 144 | + unit: 'second', |
| 145 | + value: expect.any(Number), |
| 146 | + attributes: expect.objectContaining(EXPECTED_ATTRIBUTES), |
| 147 | + }); |
| 148 | +}); |
0 commit comments