Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: run pushed monitors locally in dry-run mode #991

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 2 additions & 3 deletions __tests__/push/bundler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import { createReadStream } from 'fs';
import { writeFile, unlink, mkdir, rm } from 'fs/promises';
import unzipper from 'unzipper';
import unzip from 'unzip-stream';
import { join } from 'path';
import { generateTempPath } from '../../src/helpers';
import { Bundler } from '../../src/push/bundler';
Expand Down Expand Up @@ -74,11 +74,10 @@ async function validateZip(content) {
await writeFile(pathToZip, decoded);

const files: Array<string> = [];

let contents = '';
await new Promise(r => {
createReadStream(pathToZip)
.pipe(unzipper.Parse())
.pipe(unzip.Parse())
.on('entry', function (entry) {
files.push(entry.path);
contents += '\n' + entry.path + '\n';
Expand Down
14 changes: 7 additions & 7 deletions __tests__/push/monitor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ describe('Monitors', () => {
});

it('build lightweight monitor schema', async () => {
const schema = await buildMonitorSchema(
const { schemas } = await buildMonitorSchema(
[createTestMonitor('heartbeat.yml', 'http')],
true
);
expect(schema[0]).toEqual({
expect(schemas[0]).toEqual({
id: 'test-monitor',
name: 'test',
schedule: 10,
Expand All @@ -89,8 +89,8 @@ describe('Monitors', () => {

it('build browser monitor schema', async () => {
const monitor = createTestMonitor('example.journey.ts');
const schema = await buildMonitorSchema([monitor], true);
expect(schema[0]).toEqual({
const { schemas } = await buildMonitorSchema([monitor], true);
expect(schemas[0]).toEqual({
id: 'test-monitor',
name: 'test',
schedule: 10,
Expand All @@ -106,9 +106,9 @@ describe('Monitors', () => {
fields: { area: 'website' },
});
monitor.update({ locations: ['brazil'], fields: { env: 'dev' } });
const schema1 = await buildMonitorSchema([monitor], true);
expect(schema1[0].hash).not.toEqual(schema[0].hash);
expect(schema1[0].fields).toEqual({
const { schemas: schemas1 } = await buildMonitorSchema([monitor], true);
expect(schemas1[0].hash).not.toEqual(schemas[0].hash);
expect(schemas1[0].fields).toEqual({
area: 'website',
env: 'dev',
});
Expand Down
111 changes: 18 additions & 93 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"source-map-support": "^0.5.21",
"stack-utils": "^2.0.6",
"undici": "^5.28.3",
"unzip-stream": "^0.3.4",
"yaml": "^2.2.2"
},
"devDependencies": {
Expand All @@ -96,8 +97,7 @@
"rimraf": "^3.0.2",
"semantic-release": "^21.1.1",
"ts-jest": "^29.1.1",
"typescript": "^5.1.6",
"unzipper": "^0.10.11"
"typescript": "^5.1.6"
},
"overrides": {
"follow-redirects": "^1.15.6",
Expand Down
12 changes: 12 additions & 0 deletions src/core/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,16 @@ if (!global[SYNTHETICS_RUNNER]) {
global[SYNTHETICS_RUNNER] = new Runner();
}

/**
* Set debug based on DEBUG ENV and namespace - synthetics
*/
if (process.env.DEBUG && process.env.DEBUG.includes('synthetics')) {
process.env['__SYNTHETICS__DEBUG__'] = '1';
}

export const runner: Runner = global[SYNTHETICS_RUNNER];

// is Debug mode enabled
export function inDebugMode() {
return !!process.env['__SYNTHETICS__DEBUG__'];
}
10 changes: 2 additions & 8 deletions src/core/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,10 @@

import { grey, cyan, dim, italic } from 'kleur/colors';
import { now } from '../helpers';

/**
* Set debug based on DEBUG ENV and namespace - synthetics
*/
if (process.env.DEBUG && process.env.DEBUG.includes('synthetics')) {
process.env['__SYNTHETICS__DEBUG__'] = '1';
}
import { inDebugMode } from './globals';

export function log(msg) {
if (!process.env['__SYNTHETICS__DEBUG__'] || !msg) {
if (!inDebugMode() || !msg) {
return;
}
if (typeof msg === 'object') {
Expand Down
9 changes: 8 additions & 1 deletion src/dsl/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class Monitor {
content?: string;
source?: Location;
filter: MonitorFilter;
constructor(public config: MonitorConfig = {}) {}
constructor(public config: MonitorConfig = {}) { }
/**
* Treat the creation time config with `monitor.use` as source of truth by
* merging the values coming from CLI and Synthetics config file
Expand Down Expand Up @@ -158,6 +158,13 @@ export class Monitor {
.digest('base64');
}

/**
* Returns the size of the monitor in bytes which is sent as payload to Kibana
*/
size() {
return JSON.stringify(this).length
}

validate() {
const schedule = this.config.schedule;
if (ALLOWED_SCHEDULES.includes(schedule)) {
Expand Down
Loading
Loading