Add HTTP Endpoint Request and Response Payloads to Auto-Documentation
Get Started with Auto-Documentationโ
With Multiplayerโs System Auto-Documentation you can automatically discover, track, and document your entire system architecture - including all components, APIs, dependencies, platforms, and environments.
Follow these steps to set up Auto-Documentation.
Add HTTP Endpoint Request and Response Payloads to Auto-Documentationโ
Auto-Documentation automatically collects the HTTP URL, however, OTel will not automatically collect the HTTPS endpoint request and response payloads.
As an optional step you can collect the full schema and add this information into your System Dashboard.
This requires adding the following to your source code so that the request and response information is added to the trace.
Please note:
- We only support JSON format for now
- We recommend protecting your PII and sensitive data with schemification. Should you not set this up, be reassured that Multiplayer also implements schemification on our end, so that the payload we display contains no sensitive information
Here are the two pieces of code to add:
Capture response body:
responseHook: (span, response) => {
const _response = response as ServerResponse
_response.setHeader('X-Trace-Id', span.spanContext().traceId)
// const contentEncoding = _response.getHeader('content-encoding')
// span.setAttribute('http.response.content_encoding', contentEncoding as string)
const [oldWrite, oldEnd] = [_response.write, _response.end]
const chunks: Buffer[] = [];
(_response.write as unknown) = function(...restArgs) {
chunks.push(Buffer.from(restArgs[0]))
// eslint-disable-next-line
// @ts-ignore
oldWrite.apply(_response, restArgs)
}
// eslint-disable-next-line
// @ts-ignore
_response.end = function(...restArgs) {
if (restArgs[0]) {
chunks.push(Buffer.from(restArgs[0]))
}
const responseBodySchema = schemifyJson(Buffer.concat(chunks).toString('utf8'))
if (responseBodySchema.length) {
span.setAttribute(
'multiplayer.http.response.body',
responseBodySchema,
)
}
// eslint-disable-next-line
// @ts-ignore
oldEnd.apply(_response, restArgs)
}
},
Capture request body:
requestHook: (span, request) => {
const _request = request as IncomingMessage
const contentType = _request.headers['content-type']
if (!contentType?.includes('application/json')) {
return
}
let body = ''
_request.on('data', (chunk) => {
body += chunk
})
_request.on('end', () => {
const requestBodySchema = schemifyJson(body)
if (requestBodySchema.length) {
// expect body to be a json schema
// https://json-schema.org/learn/miscellaneous-examples#basic
span.setAttribute(
'multiplayer.http.request.body',
requestBodySchema,
)
}
})
},
Next Stepsโ
You did it! Whatโs next?