Skip to content

feat: makes execution id accessible in the user's code #58

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

Merged
merged 11 commits into from
Oct 13, 2023
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
1 change: 1 addition & 0 deletions packages/synthetics-sdk-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@types/supertest": "^2.0.12",
"chai": "^4.3.7",
"express": "^4.18.2",
"node-mocks-http": "^1.13.0",
"sinon": "^16.0.0",
"supertest": "^6.3.3"
},
Expand Down
25 changes: 18 additions & 7 deletions packages/synthetics-sdk-api/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ import { getInstrumentedLogger } from './auto_instrumentation';
instantiateMetadata();

const asyncFilenamePrefix = 'async ';
const syntheticExecutionIdHeader = 'Synthetic-Execution-Id';
const runSynthetic = async (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
syntheticCode: (args: { logger: Logger }) => any
syntheticCode: (args: {
logger: Logger;
executionId: string | undefined;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}) => any,
executionId: string | undefined
) => {
const logger = await getInstrumentedLogger();
const startTime = new Date().toISOString();
Expand All @@ -40,7 +45,7 @@ const runSynthetic = async (
const synthetic_generic_result = GenericResultV1.create();

try {
await syntheticCode({ logger });
await syntheticCode({ logger, executionId });
synthetic_generic_result.ok = true;
} catch (err: unknown) {
synthetic_generic_result.ok = false;
Expand Down Expand Up @@ -93,12 +98,18 @@ const runSynthetic = async (
* returns the results via res.send
*/
export function runSyntheticHandler(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
syntheticCode: (args: { logger: Logger }) => any
syntheticCode: (args: {
logger: Logger;
executionId: string | undefined;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}) => any
) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return async (req: Request, res: Response): Promise<any> =>
res.send(await runSynthetic(syntheticCode));
return async (req: Request, res: Response): Promise<any> => {
res.send(
await runSynthetic(syntheticCode, req.get(syntheticExecutionIdHeader))
);
};
}

export function firstUserErrorStackFrame(
Expand Down
36 changes: 33 additions & 3 deletions packages/synthetics-sdk-api/test/unit/handlers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import { SyntheticResult, runSyntheticHandler } from '../../src/index';
import { AssertionError, expect } from 'chai';
import { Request, Response } from 'express';
import { createRequest } from 'node-mocks-http';
import { firstUserErrorStackFrame } from '../../src/handlers';
import ErrorStackParser = require('error-stack-parser');

Expand All @@ -24,13 +25,15 @@ describe('GCM Synthetics Handler', async () => {
const handler = runSyntheticHandler(() => true);

const runHandler = new Promise((resolve) => {
const mockRequest = createRequest({});

let mockResponse = {
send: (body: any) => {
resolve(body);
}
} as Response;

handler({} as Request, mockResponse);
handler(mockRequest, mockResponse);
});

const syntheticResult = await runHandler as SyntheticResult;
Expand All @@ -53,13 +56,14 @@ describe('GCM Synthetics Handler', async () => {
});

const runHandler = new Promise((resolve) => {
const mockRequest = createRequest({});
let mockResponse = {
send: (body: any) => {
resolve(body);
}
} as Response;

handler({} as Request, mockResponse);
handler(mockRequest, mockResponse);
});

const syntheticResult = await runHandler as SyntheticResult;
Expand Down Expand Up @@ -92,13 +96,14 @@ describe('GCM Synthetics Handler', async () => {
const handler = runSyntheticHandler(handlerFunction);

const runHandler = new Promise((resolve) => {
const mockRequest = createRequest({});
let mockResponse = {
send: (body: any) => {
resolve(body);
}
} as Response;

handler({} as Request, mockResponse);
handler(mockRequest, mockResponse);
});

const syntheticResult = await runHandler as SyntheticResult;
Expand Down Expand Up @@ -153,4 +158,29 @@ describe('GCM Synthetics Handler', async () => {
' at async MyClass.Function (/user/my/file.js:6:11)');
});
});

it('has execution id available', async () => {
const executionId = 'deadbeefdeadbeefdeadbeefdeadbeef'
const handler = runSyntheticHandler((args: {executionId: string|undefined}) => {
expect(args.executionId).to.equal(executionId);
});

const runHandler = new Promise((resolve) => {
const mockRequest = createRequest({
headers: {"Synthetic-Execution-Id": executionId}
});

let mockResponse = {
send: (body: any) => {
resolve(body);
}
} as Response;

handler(mockRequest, mockResponse);
});

const syntheticResult = await runHandler as SyntheticResult;
expect(syntheticResult?.synthetic_generic_result_v1?.ok).to.be.true;
expect(syntheticResult?.synthetic_generic_result_v1?.generic_error).to.be.undefined;
});
});