Skip to content

feat: add transaction and span #460

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

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 18 additions & 0 deletions examples/todos/basic.journey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,21 @@ journey('check if input placeholder is correct', ({ page, params }) => {
);
});
});

journey('Synthetics + APM', ({ page }) => {
step('go to index page', async () => {
await page.goto('http://localhost:8080/index');
// make sure RUM request has been successfully sent to APM server
await page.waitForResponse(response =>
response.url().includes('/rum/events')
);
});

step('go to unknown page', async () => {
await page.goto('http://localhost:8080/unknown');
// make sure RUM request has been successfully sent to APM server
await page.waitForResponse(response =>
response.url().includes('/rum/events')
);
});
});
982 changes: 951 additions & 31 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"commander": "^9.0.0",
"deepmerge": "^4.2.2",
"expect": "^27.0.2",
"elastic-apm-node": "^3.31.0",
"http-proxy": "^1.18.1",
"kleur": "^4.1.4",
"micromatch": "^4.0.4",
Expand Down
46 changes: 46 additions & 0 deletions src/apm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* MIT License
*
* Copyright (c) 2020-present, Elastic NV
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

import agent from 'elastic-apm-node';

agent.start({
serviceName: 'synthetics',
instrument: false,
metricsInterval: '0',
centralConfig: false,
captureSpanStackTraces: false,
serverUrl: '',
secretToken: '',
});

agent.addFilter(payload => {
console.log('Payload', payload);
});

process.on('exit', () => {
agent.flush(() => {
console.log('flushed');
});
});
17 changes: 17 additions & 0 deletions src/core/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export default class Runner extends EventEmitter {
hooks: SuiteHooks = { beforeAll: [], afterAll: [] };
hookError: Error | undefined;
static screenshotPath = join(CACHE_PATH, 'screenshots');
traceparent: string;

static async createContext(options: RunOptions): Promise<JourneyContext> {
const driver = await Gatherer.setupDriver(options);
Expand Down Expand Up @@ -240,6 +241,18 @@ export default class Runner extends EventEmitter {
* step level plugins
*/
const traceEnabled = trace || filmstrips;
this.traceparent = step.span.traceparent;
const handler = (route, request) => {
console.log('traceparent', this.traceparent);
const headers = {
traceparent: this.traceparent,
...request.headers(),
};
route.continue({
headers,
});
};
context.driver.page.route(/index|unknown/, handler);
pluginManager.onStep(step);
traceEnabled && (await pluginManager.start('trace'));
// call the step definition
Expand All @@ -256,6 +269,7 @@ export default class Runner extends EventEmitter {
const traceOutput = await pluginManager.stop('trace');
Object.assign(data, traceOutput);
}
context.driver.page.unroute(/index|unknown/, handler);
} catch (error) {
data.status = 'failed';
data.error = error;
Expand Down Expand Up @@ -309,6 +323,7 @@ export default class Runner extends EventEmitter {
if (options.pauseOnError && data.error) {
await new Promise(r => process.stdin.on('data', r));
}
step.span.end();
results.push(data);
}
return results;
Expand Down Expand Up @@ -353,6 +368,7 @@ export default class Runner extends EventEmitter {
}
// clear screenshots cache after each journey
await rm(Runner.screenshotPath, { recursive: true, force: true });
journey.transaction.end();
}

/**
Expand Down Expand Up @@ -380,6 +396,7 @@ export default class Runner extends EventEmitter {
if (options.reporter === 'json') {
await once(this, 'journey:end:reported');
}
journey.transaction.end();
return result;
}

Expand Down
3 changes: 3 additions & 0 deletions src/dsl/journey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { Browser, Page, BrowserContext, CDPSession } from 'playwright-chromium';
import micromatch, { isMatch } from 'micromatch';
import { Step } from './step';
import { VoidCallback, HooksCallback, Params, Location } from '../common_types';
import { startTransaction, Transaction } from 'elastic-apm-node';

export type JourneyOptions = {
name: string;
Expand All @@ -48,6 +49,7 @@ export class Journey {
name: string;
id?: string;
tags?: string[];
transaction: Transaction;
callback: JourneyCallback;
location?: Location;
steps: Step[] = [];
Expand All @@ -63,6 +65,7 @@ export class Journey {
this.tags = options.tags;
this.callback = callback;
this.location = location;
this.transaction = startTransaction(`Journey: ${this.name}`);
}

addStep(name: string, callback: VoidCallback, location?: Location) {
Expand Down
3 changes: 3 additions & 0 deletions src/dsl/step.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
*/

import { Location, VoidCallback } from '../common_types';
import { startSpan, Span } from 'elastic-apm-node';

export class Step {
name: string;
index: number;
callback: VoidCallback;
location?: Location;
span: Span;

constructor(
name: string,
Expand All @@ -41,5 +43,6 @@ export class Step {
this.index = index;
this.callback = callback;
this.location = location;
this.span = startSpan(`Step: ${this.name}`);
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import { runner } from './core';
import { RunOptions } from './common_types';
import './apm';

export async function run(options: RunOptions) {
return runner.run(options);
Expand Down