Skip to content

Commit e198ddc

Browse files
feat: run pushed monitors locally in dry-run mode (#991)
* feat: run pushed monitors locally in dry-run mode * move lookup code * unpack all journeys and run local * fix tests * some cleanup * review comments * fix formatting issues
1 parent a1ec69c commit e198ddc

File tree

12 files changed

+308
-159
lines changed

12 files changed

+308
-159
lines changed

__tests__/push/bundler.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
import { createReadStream } from 'fs';
2727
import { writeFile, unlink, mkdir, rm } from 'fs/promises';
28-
import unzipper from 'unzipper';
28+
import unzip from 'unzip-stream';
2929
import { join } from 'path';
3030
import { generateTempPath } from '../../src/helpers';
3131
import { Bundler } from '../../src/push/bundler';
@@ -74,11 +74,10 @@ async function validateZip(content) {
7474
await writeFile(pathToZip, decoded);
7575

7676
const files: Array<string> = [];
77-
7877
let contents = '';
7978
await new Promise(r => {
8079
createReadStream(pathToZip)
81-
.pipe(unzipper.Parse())
80+
.pipe(unzip.Parse())
8281
.on('entry', function (entry) {
8382
files.push(entry.path);
8483
contents += '\n' + entry.path + '\n';

__tests__/push/monitor.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ describe('Monitors', () => {
7070
});
7171

7272
it('build lightweight monitor schema', async () => {
73-
const schema = await buildMonitorSchema(
73+
const { schemas } = await buildMonitorSchema(
7474
[createTestMonitor('heartbeat.yml', 'http')],
7575
true
7676
);
77-
expect(schema[0]).toEqual({
77+
expect(schemas[0]).toEqual({
7878
id: 'test-monitor',
7979
name: 'test',
8080
schedule: 10,
@@ -89,8 +89,8 @@ describe('Monitors', () => {
8989

9090
it('build browser monitor schema', async () => {
9191
const monitor = createTestMonitor('example.journey.ts');
92-
const schema = await buildMonitorSchema([monitor], true);
93-
expect(schema[0]).toEqual({
92+
const { schemas } = await buildMonitorSchema([monitor], true);
93+
expect(schemas[0]).toEqual({
9494
id: 'test-monitor',
9595
name: 'test',
9696
schedule: 10,
@@ -106,9 +106,9 @@ describe('Monitors', () => {
106106
fields: { area: 'website' },
107107
});
108108
monitor.update({ locations: ['brazil'], fields: { env: 'dev' } });
109-
const schema1 = await buildMonitorSchema([monitor], true);
110-
expect(schema1[0].hash).not.toEqual(schema[0].hash);
111-
expect(schema1[0].fields).toEqual({
109+
const { schemas: schemas1 } = await buildMonitorSchema([monitor], true);
110+
expect(schemas1[0].hash).not.toEqual(schemas[0].hash);
111+
expect(schemas1[0].fields).toEqual({
112112
area: 'website',
113113
env: 'dev',
114114
});

package-lock.json

Lines changed: 18 additions & 93 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"source-map-support": "^0.5.21",
7676
"stack-utils": "^2.0.6",
7777
"undici": "^5.28.3",
78+
"unzip-stream": "^0.3.4",
7879
"yaml": "^2.2.2"
7980
},
8081
"devDependencies": {
@@ -99,8 +100,7 @@
99100
"rimraf": "^3.0.2",
100101
"semantic-release": "^21.1.1",
101102
"ts-jest": "^29.1.1",
102-
"typescript": "^5.1.6",
103-
"unzipper": "^0.10.11"
103+
"typescript": "^5.1.6"
104104
},
105105
"overrides": {
106106
"follow-redirects": "^1.15.6",

src/core/globals.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,16 @@ if (!global[SYNTHETICS_RUNNER]) {
3434
global[SYNTHETICS_RUNNER] = new Runner();
3535
}
3636

37+
/**
38+
* Set debug based on DEBUG ENV and namespace - synthetics
39+
*/
40+
if (process.env.DEBUG && process.env.DEBUG.includes('synthetics')) {
41+
process.env['__SYNTHETICS__DEBUG__'] = '1';
42+
}
43+
3744
export const runner: Runner = global[SYNTHETICS_RUNNER];
45+
46+
// is Debug mode enabled
47+
export function inDebugMode() {
48+
return !!process.env['__SYNTHETICS__DEBUG__'];
49+
}

src/core/logger.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,10 @@
2525

2626
import { grey, cyan, dim, italic } from 'kleur/colors';
2727
import { now } from '../helpers';
28-
29-
/**
30-
* Set debug based on DEBUG ENV and namespace - synthetics
31-
*/
32-
if (process.env.DEBUG && process.env.DEBUG.includes('synthetics')) {
33-
process.env['__SYNTHETICS__DEBUG__'] = '1';
34-
}
28+
import { inDebugMode } from './globals';
3529

3630
export function log(msg) {
37-
if (!process.env['__SYNTHETICS__DEBUG__'] || !msg) {
31+
if (!inDebugMode() || !msg) {
3832
return;
3933
}
4034
if (typeof msg === 'object') {

src/dsl/monitor.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ export class Monitor {
158158
.digest('base64');
159159
}
160160

161+
/**
162+
* Returns the size of the monitor in bytes which is sent as payload to Kibana
163+
*/
164+
size() {
165+
return JSON.stringify(this).length;
166+
}
167+
161168
validate() {
162169
const schedule = this.config.schedule;
163170
if (ALLOWED_SCHEDULES.includes(schedule)) {

0 commit comments

Comments
 (0)