-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: prep for reply shenanigans
- Loading branch information
1 parent
3493dcf
commit c971b49
Showing
3 changed files
with
94 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { CommonUtils, ScrubContext } from '@lumigo/node-core'; | ||
import { Span } from '@opentelemetry/api'; | ||
|
||
import { contentType, scrubHttpPayload } from '../../tools/payloads'; | ||
import { getSpanAttributeMaxLength, safeExecute } from '../../utils'; | ||
import { InstrumentationIfc } from '../hooksIfc'; | ||
|
||
type FastifyRequestType = { request: any; reply?: any }; | ||
|
||
export const FastifyHooks: InstrumentationIfc<FastifyRequestType, any> = { | ||
requestHook(span: Span, { request, reply }: FastifyRequestType): void { | ||
if (request.query) | ||
span.setAttribute( | ||
'http.request.query', | ||
CommonUtils.payloadStringify( | ||
request.query, | ||
ScrubContext.HTTP_REQUEST_QUERY, | ||
getSpanAttributeMaxLength() | ||
) | ||
); | ||
if (request.headers) | ||
span.setAttribute( | ||
'http.request.headers', | ||
CommonUtils.payloadStringify( | ||
request.headers, | ||
ScrubContext.HTTP_REQUEST_HEADERS, | ||
getSpanAttributeMaxLength() | ||
) | ||
); | ||
if (request.body) { | ||
span.setAttribute( | ||
'http.request.body', | ||
scrubHttpPayload(request.body, contentType(request.headers), ScrubContext.HTTP_REQUEST_BODY) | ||
); | ||
} | ||
|
||
let response; | ||
if (reply) { | ||
const oldReplyEnd = reply.end; | ||
const oldReplySend = reply.send; | ||
reply.send = function (data: any) { | ||
response = data; | ||
reply.send = oldReplySend; | ||
// eslint-disable-next-line prefer-rest-params | ||
return oldReplySend.apply(reply, arguments); | ||
}; | ||
reply.end = function () { | ||
return safeExecute(() => { | ||
// eslint-disable-next-line prefer-rest-params | ||
const origRes = oldReplyEnd.apply(reply, arguments); | ||
if (reply.getHeaders()) | ||
span.setAttribute( | ||
'http.response.headers', | ||
CommonUtils.payloadStringify( | ||
reply.getHeaders(), | ||
ScrubContext.HTTP_RESPONSE_HEADERS, | ||
getSpanAttributeMaxLength() | ||
) | ||
); // TODO This is not compliant with the HTTP semantic conventions | ||
if (response) | ||
span.setAttribute( | ||
'http.response.body', | ||
scrubHttpPayload( | ||
response, | ||
contentType(reply.getHeaders()), | ||
ScrubContext.HTTP_RESPONSE_BODY | ||
) | ||
); | ||
if (reply.body) | ||
span.setAttribute( | ||
'http.request.body', | ||
scrubHttpPayload( | ||
reply.body, | ||
contentType(reply.headers), | ||
ScrubContext.HTTP_REQUEST_BODY | ||
) | ||
); | ||
reply.end = oldReplyEnd; | ||
return origRes; | ||
})(); | ||
}; | ||
} | ||
}, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function | ||
responseHook(span: Span, response: any): void {}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters