Skip to content

Commit 78b2162

Browse files
authored
feat: makes execution id accessible in the user's code (#58)
Retrieves execution id from `Synthetic-Execution-Id` and allows it to be used in `syntheticCode`.
1 parent 13f5c97 commit 78b2162

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

packages/synthetics-sdk-api/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"@types/supertest": "^2.0.12",
1717
"chai": "^4.3.7",
1818
"express": "^4.18.2",
19+
"node-mocks-http": "^1.13.0",
1920
"sinon": "^16.0.0",
2021
"supertest": "^6.3.3"
2122
},

packages/synthetics-sdk-api/src/handlers.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,14 @@ import { getInstrumentedLogger } from './auto_instrumentation';
2929
instantiateMetadata();
3030

3131
const asyncFilenamePrefix = 'async ';
32+
const syntheticExecutionIdHeader = 'Synthetic-Execution-Id';
3233
const runSynthetic = async (
33-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
34-
syntheticCode: (args: { logger: Logger }) => any
34+
syntheticCode: (args: {
35+
logger: Logger;
36+
executionId: string | undefined;
37+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
38+
}) => any,
39+
executionId: string | undefined
3540
) => {
3641
const logger = await getInstrumentedLogger();
3742
const startTime = new Date().toISOString();
@@ -40,7 +45,7 @@ const runSynthetic = async (
4045
const synthetic_generic_result = GenericResultV1.create();
4146

4247
try {
43-
await syntheticCode({ logger });
48+
await syntheticCode({ logger, executionId });
4449
synthetic_generic_result.ok = true;
4550
} catch (err: unknown) {
4651
synthetic_generic_result.ok = false;
@@ -93,12 +98,18 @@ const runSynthetic = async (
9398
* returns the results via res.send
9499
*/
95100
export function runSyntheticHandler(
96-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
97-
syntheticCode: (args: { logger: Logger }) => any
101+
syntheticCode: (args: {
102+
logger: Logger;
103+
executionId: string | undefined;
104+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
105+
}) => any
98106
) {
99107
// eslint-disable-next-line @typescript-eslint/no-explicit-any
100-
return async (req: Request, res: Response): Promise<any> =>
101-
res.send(await runSynthetic(syntheticCode));
108+
return async (req: Request, res: Response): Promise<any> => {
109+
res.send(
110+
await runSynthetic(syntheticCode, req.get(syntheticExecutionIdHeader))
111+
);
112+
};
102113
}
103114

104115
export function firstUserErrorStackFrame(

packages/synthetics-sdk-api/test/unit/handlers.spec.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import { SyntheticResult, runSyntheticHandler } from '../../src/index';
1616
import { AssertionError, expect } from 'chai';
1717
import { Request, Response } from 'express';
18+
import { createRequest } from 'node-mocks-http';
1819
import { firstUserErrorStackFrame } from '../../src/handlers';
1920
import ErrorStackParser = require('error-stack-parser');
2021

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

2627
const runHandler = new Promise((resolve) => {
28+
const mockRequest = createRequest({});
29+
2730
let mockResponse = {
2831
send: (body: any) => {
2932
resolve(body);
3033
}
3134
} as Response;
3235

33-
handler({} as Request, mockResponse);
36+
handler(mockRequest, mockResponse);
3437
});
3538

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

5558
const runHandler = new Promise((resolve) => {
59+
const mockRequest = createRequest({});
5660
let mockResponse = {
5761
send: (body: any) => {
5862
resolve(body);
5963
}
6064
} as Response;
6165

62-
handler({} as Request, mockResponse);
66+
handler(mockRequest, mockResponse);
6367
});
6468

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

9498
const runHandler = new Promise((resolve) => {
99+
const mockRequest = createRequest({});
95100
let mockResponse = {
96101
send: (body: any) => {
97102
resolve(body);
98103
}
99104
} as Response;
100105

101-
handler({} as Request, mockResponse);
106+
handler(mockRequest, mockResponse);
102107
});
103108

104109
const syntheticResult = await runHandler as SyntheticResult;
@@ -153,4 +158,29 @@ describe('GCM Synthetics Handler', async () => {
153158
' at async MyClass.Function (/user/my/file.js:6:11)');
154159
});
155160
});
161+
162+
it('has execution id available', async () => {
163+
const executionId = 'deadbeefdeadbeefdeadbeefdeadbeef'
164+
const handler = runSyntheticHandler((args: {executionId: string|undefined}) => {
165+
expect(args.executionId).to.equal(executionId);
166+
});
167+
168+
const runHandler = new Promise((resolve) => {
169+
const mockRequest = createRequest({
170+
headers: {"Synthetic-Execution-Id": executionId}
171+
});
172+
173+
let mockResponse = {
174+
send: (body: any) => {
175+
resolve(body);
176+
}
177+
} as Response;
178+
179+
handler(mockRequest, mockResponse);
180+
});
181+
182+
const syntheticResult = await runHandler as SyntheticResult;
183+
expect(syntheticResult?.synthetic_generic_result_v1?.ok).to.be.true;
184+
expect(syntheticResult?.synthetic_generic_result_v1?.generic_error).to.be.undefined;
185+
});
156186
});

0 commit comments

Comments
 (0)