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: CXSPA-2067 SSR Logging - Trace Context #17597

Merged
merged 31 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
8d21931
feat: CXSPA-2067 SSR Logging - Trace Context
pawelfras Jun 30, 2023
11ff1c6
Add license header
github-actions[bot] Jun 30, 2023
8332b22
Merge branch 'develop-6.3.x' into feature/CXSPA-2067
pawelfras Jun 30, 2023
2128942
remove testin property
pawelfras Jun 30, 2023
4f070d4
adding more secure parser + refactor after review
pawelfras Aug 8, 2023
4b6a6b9
Add license header
github-actions[bot] Aug 8, 2023
acc37ab
fixing sonar issues
pawelfras Aug 8, 2023
368689a
Merge branch 'develop-6.4.x' into feature/CXSPA-2067
pawelfras Aug 8, 2023
4ccd1be
removing test values
pawelfras Aug 8, 2023
46a09de
adding error handling
pawelfras Aug 9, 2023
1bf0bec
fixing sonar issues
pawelfras Aug 24, 2023
6d312ec
refactor after review
pawelfras Sep 5, 2023
4faaea3
Add license header
github-actions[bot] Sep 5, 2023
a080d01
move request context logic to the separate file
pawelfras Sep 7, 2023
9a79313
refactor after review
pawelfras Sep 8, 2023
46151db
Merge branch 'develop-6.5.x' into feature/CXSPA-2067
pawelfras Sep 8, 2023
a494041
refactor after review
pawelfras Sep 11, 2023
5676812
refactor after review
pawelfras Sep 13, 2023
5d7c49d
Update core-libs/setup/ssr/optimized-engine/request-context.ts
pawelfras Sep 13, 2023
7959822
Update core-libs/setup/ssr/optimized-engine/request-context.ts
pawelfras Sep 13, 2023
47803a1
Merge branch 'develop-6.5.x' into feature/CXSPA-2067
pawelfras Sep 14, 2023
5d1774f
Merge branch 'develop-6.5.x' into feature/CXSPA-2067
Platonn Sep 14, 2023
82a0ab7
Merge branch 'develop-6.5.x' into feature/CXSPA-2067
pawelfras Sep 18, 2023
9f20a21
Merge branch 'develop-6.5.x' into feature/CXSPA-2067
pawelfras Sep 19, 2023
af6d5ad
Merge branch 'develop-6.5.x' into feature/CXSPA-2067
giancorderoortiz Sep 20, 2023
bf761ca
Merge branch 'develop-6.5.x' into feature/CXSPA-2067
pawelfras Sep 21, 2023
f0d45ab
Merge branch 'develop-6.5.x' into feature/CXSPA-2067
pawelfras Sep 27, 2023
5b413d7
Merge branch 'develop-6.5.x' into feature/CXSPA-2067
pawelfras Oct 4, 2023
7438cb1
remove invalid traceparent from logs
pawelfras Oct 5, 2023
b50f2af
Merge branch 'develop-6.6.x' into feature/CXSPA-2067
pawelfras Oct 5, 2023
5d01b6a
Trigger Build
pawelfras Oct 5, 2023
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
116 changes: 103 additions & 13 deletions core-libs/setup/ssr/optimized-engine/optimized-ssr-engine.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1211,16 +1211,19 @@ describe('OptimizedSsrEngine', () => {
engineRunner.optimizedSsrEngine['getRequestContext'](request);

expect(result).toMatchInlineSnapshot(`
{
"timeReceived": "2023-05-26T00:00:00.000Z",
"traceContext": {
"parentId": "1b527c3828976b39",
"traceFlags": "01",
"traceId": "d745f6735b44e81c0ae5410cb1fc8a0c",
"version": "00",
[
{
"timeReceived": "2023-05-26T00:00:00.000Z",
"traceContext": {
"parentId": "1b527c3828976b39",
"traceFlags": "01",
"traceId": "d745f6735b44e81c0ae5410cb1fc8a0c",
"version": "00",
},
"uuid": "ad90db04-a501-4dc5-9b4e-2cc2ab10d49c",
},
"uuid": "ad90db04-a501-4dc5-9b4e-2cc2ab10d49c",
}
null,
]
`);
});

Expand All @@ -1238,10 +1241,37 @@ describe('OptimizedSsrEngine', () => {
);

expect(result).toMatchInlineSnapshot(`
{
"timeReceived": "2023-05-26T00:00:00.000Z",
"uuid": "ad90db04-a501-4dc5-9b4e-2cc2ab10d49c",
}
[
{
"timeReceived": "2023-05-26T00:00:00.000Z",
"uuid": "ad90db04-a501-4dc5-9b4e-2cc2ab10d49c",
},
null,
]
`);
});

it('should receive request context without traceContext and an error if traceparent is invalid', () => {
const requestWithoutTraceparentHeader = {
...request,
get: (_header: string): string | string[] | null | undefined => {
return '00-invalid-traceparent';
},
} as unknown as Request;

const engineRunner = new TestEngineRunner({});
const result = engineRunner.optimizedSsrEngine['getRequestContext'](
requestWithoutTraceparentHeader
);

expect(result).toMatchInlineSnapshot(`
[
{
"timeReceived": "2023-05-26T00:00:00.000Z",
"uuid": "ad90db04-a501-4dc5-9b4e-2cc2ab10d49c",
},
[Error: Traceparent header has invalid length: 22. Expected 55 characters.],
]
`);
});

Expand Down Expand Up @@ -1274,6 +1304,66 @@ describe('OptimizedSsrEngine', () => {
})
);
}));

it('should add requestContext to and log error if occurs during parsing traceparent', fakeAsync(() => {
const requestContext = {
timeReceived: '1970-01-01T00:00:00.500Z',
uuid: 'ad90db04-a501-4dc5-9b4e-2cc2ab10d49c',
};

const engineRunner = new TestEngineRunner({});
const loggerErrorSpy = jest.spyOn(
engineRunner.optimizedSsrEngine['logger'],
'error'
);
jest.spyOn(engineRunner.optimizedSsrEngine as any, 'log');

engineRunner.request('test', {
httpHeaders: { ...headers, traceparent: '00-invalid-traceparent' },
});
tick(200);
expect(engineRunner.optimizedSsrEngine['log']).toHaveBeenCalledWith(
'Rendering started (test)',
true,
expect.objectContaining({
request: expect.objectContaining({
res: expect.objectContaining({
locals: { cx: { request: requestContext } },
}),
}),
})
);
expect(loggerErrorSpy.mock.lastCall).toMatchInlineSnapshot(`
[
"Traceparent header has invalid length: 22. Expected 55 characters.",
{
"request": {
"app": {
"get": [Function],
},
"connection": {},
"get": [Function],
"headers": {
"traceparent": "00-invalid-traceparent",
},
"originalUrl": "test",
"protocol": "https",
"res": {
"locals": {
"cx": {
"request": {
"timeReceived": "1970-01-01T00:00:00.500Z",
"uuid": "ad90db04-a501-4dc5-9b4e-2cc2ab10d49c",
},
},
},
"set": [Function],
},
},
},
]
`);
}));
});

describe('logger option', () => {
Expand Down
44 changes: 35 additions & 9 deletions core-libs/setup/ssr/optimized-engine/optimized-ssr-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,7 @@ export class OptimizedSsrEngine {
options: any,
callback: SsrCallbackFn
): void {
options.req.res.locals = {
cx: { request: this.getRequestContext(options.req) },
};
this.updateWithRequestContext(options);

const request: Request = options.req;
const response: Response = options.req.res;
Expand Down Expand Up @@ -491,21 +489,49 @@ export class OptimizedSsrEngine {
* the "W3C TraceContext" document. See https://www.w3.org/TR/trace-context/#traceparent-header
* for more details.
* @param request - the request object
* @returns the context of the request
* @returns the context of the request and error if occurred during parsing traceparent header
* @private
*/
private getRequestContext(request: Request): ExpressServerLoggerContext {
private getRequestContext(
Platonn marked this conversation as resolved.
Show resolved Hide resolved
request: Request
): [ExpressServerLoggerContext, Error | null] {
let error: Error | null = null;
// request.headers['traceparent'] =
// '00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-0';
const requestContext: ExpressServerLoggerContext = {
uuid: randomUUID(),
timeReceived: new Date().toISOString(),
};

const traceContext = parseTraceparent(request.get('traceparent'));
if (traceContext) {
requestContext.traceContext = traceContext;
try {
const traceContext = parseTraceparent(request.get('traceparent'));
if (traceContext) {
requestContext.traceContext = traceContext;
}
} catch (e) {
error =
e instanceof Error
? e
: new Error('Unexpected error during parsing traceparent header');
}

return requestContext;
return [requestContext, error];
Platonn marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Provides the request context returned from `getRequestContext` methods to be available in SSR scope.
* Method handles the error that may occur during parsing traceparent header.
* @param options - the options of the request
* @private
*/
private updateWithRequestContext(options: any) {
const [requestContext, error] = this.getRequestContext(options.req);
options.req.res.locals = {
cx: { request: requestContext },
};
if (error) {
this.logger.error(error.message, { request: options.req });
pawelfras marked this conversation as resolved.
Show resolved Hide resolved
}
pawelfras marked this conversation as resolved.
Show resolved Hide resolved
}

//CXSPA-3680 - remove this method in 7.0
Expand Down
Loading