-
Notifications
You must be signed in to change notification settings - Fork 2
/
Treblle.ts
72 lines (68 loc) · 2.36 KB
/
Treblle.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import type { ConfigContract } from '@ioc:Adonis/Core/Config'
import { inject } from '@adonisjs/core/build/standalone'
import {
sendPayloadToTreblle,
generateFieldsToMask,
maskSensitiveValues,
getRequestDuration,
generateTrebllePayload,
getResponsePayload,
} from '@treblle/utils'
@inject(['Adonis/Core/Config'])
export default class Treblle {
constructor(private config: ConfigContract) {}
public async handle({ request, response }: HttpContextContract, next: () => Promise<void>) {
const requestStartTime = process.hrtime()
const payload = request.all()
const protocol = `${request.protocol()}/${request.request.httpVersion}`
const fieldsToMask = generateFieldsToMask(this.config.get('treblle.additionalFieldsToMask'))
const maskedRequestPayload = maskSensitiveValues(payload, fieldsToMask)
let errors = []
response.response.on('finish', () => {
const { payload: maskedResponseBody, error: invalidResponseBodyError } = getResponsePayload(
response.lazyBody[0],
fieldsToMask
)
if (invalidResponseBodyError) {
// @ts-ignore
errors.push(invalidResponseBodyError)
}
const trebllePayload = generateTrebllePayload(
{
api_key: this.config.get('treblle.apiKey'),
project_id: this.config.get('treblle.projectId'),
version: process.env.npm_package_version,
sdk: 'adonisjs',
},
{
server: {
protocol,
},
request: {
ip: request.ip(),
url: request.completeUrl(),
user_agent: request.header('user-agent'),
method: request.method(),
headers: maskSensitiveValues(request.headers(), fieldsToMask),
body: maskedRequestPayload,
},
response: {
headers: maskSensitiveValues(response.getHeaders(), fieldsToMask),
code: response.getStatus(),
size: response.getHeader('Content-Length'),
load_time: getRequestDuration(requestStartTime),
body: maskedResponseBody ?? null,
},
errors,
}
)
try {
sendPayloadToTreblle(trebllePayload, this.config.get('treblle.apiKey'))
} catch (error) {
console.log(error)
}
})
await next()
}
}