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 7 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
7,266 changes: 7,250 additions & 16 deletions package-lock.json

Large diffs are not rendered by default.

16,058 changes: 16,058 additions & 0 deletions packages/synthetics-sdk-api/package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/synthetics-sdk-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@types/sinon": "^10.0.16",
"error-stack-parser": "^2.1.4",
"google-auth-library": "^9.0.0",
"node-mocks-http": "^1.13.0",
"ts-proto": "^1.148.1",
"winston": "^3.10.0"
},
Expand Down
21 changes: 16 additions & 5 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;
}) => 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 @@ -94,11 +99,17 @@ const runSynthetic = async (
*/
export function runSyntheticHandler(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
syntheticCode: (args: { logger: Logger }) => any
syntheticCode: (args: {
logger: Logger;
executionId: string | undefined;
}) => 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;
});
});
16 changes: 16 additions & 0 deletions samples/generic-synthetic-nodejs/.gcloudignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This file specifies files that are *not* uploaded to Google Cloud
# using gcloud. It follows the same syntax as .gitignore, with the addition of
# "#!include" directives (which insert the entries of the given .gitignore-style
# file at that point).
#
# For more information, run:
# $ gcloud topic gcloudignore
#
.gcloudignore
# If you would like to upload your .git directory, .gitignore file or files
# from your .gitignore file, remove the corresponding line
# below:
.git
.gitignore

node_modules
Binary file not shown.
6 changes: 3 additions & 3 deletions samples/generic-synthetic-nodejs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ const functions = require('@google-cloud/functions-framework');
const axios = require('axios');
const assert = require('node:assert');

functions.http('SyntheticFunction', runSyntheticHandler(async ({logger}) => {
functions.http('SyntheticFunction', runSyntheticHandler(async ({logger, executionId}) => {
/*
* This function executes the synthetic code for testing purposes.
* If the code runs without errors, the synthetic test is considered successful.
* If an error is thrown during execution, the synthetic test is considered failed.
*/
logger.info('Making an http request using synthetics');
const url = 'https://www.googfasdffsdfle.com/'; // URL to send the request to
logger.info('Making an http request using synthetics, with execution id: ' + executionId);
const url = 'https://www.google.com/'; // URL to send the request to
return await assert.doesNotReject(axios.get(url));
}));

Expand Down
Loading