Skip to content

Commit

Permalink
refacto logger to add context headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Thierry DEGREMONT committed Jul 30, 2024
1 parent 7316231 commit 77b3444
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 144 deletions.
16 changes: 15 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"packageManager": "[email protected]",
"dependencies": {
"prom-client": "^15.1.3"
"prom-client": "^15.1.3",
"uuid": "^10.0.0"
}
}
14 changes: 8 additions & 6 deletions packages/graphql-mesh/custom-plugins/monitor-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ export default ({ options }) => {
const start = Date.now();
let rawSource = context[info.sourceName]
let description = info.parentType._fields[info.path.key].description

return (fetch: any) => {
const duration = Date.now() - start;
let fetchInfo = {}
let httpStatus=null
let url=null
if (options.fullFetchInfo) {
fetchInfo = {
fieldName: info.fieldName,
Expand All @@ -42,20 +44,20 @@ export default ({ options }) => {
endpoint: rawSource.rawSource.handler.config.endpoint,
}
}
const fetchResponseInfo = {}
//const fetchResponseInfo = {}
if (fetch.response) {

fetchResponseInfo['status'] = fetch.response.status
fetchResponseInfo['url'] = fetch.response.url
httpStatus=fetch.response.status
url=fetch.response.url
const options = fetch.response.options
if (options) {
fetchResponseInfo['options'] = {
fetchInfo['options'] = {
requestId: options.headers['x-request-id'],
server: options.headers['server']
}
}
}
Logger.onFetch(context.request, fetchInfo, fetchResponseInfo, duration)
Logger.onFetch(context.request, url, httpStatus,duration,fetchInfo)
};
},

Expand Down
7 changes: 6 additions & 1 deletion packages/graphql-mesh/custom-plugins/monitor-yoga.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Plugin } from 'graphql-yoga'
import { Logger } from '../utils/logger'
import { GraphQLError } from 'graphql'

import { v4 as uuidv4 } from 'uuid';
/**
* monitor plugin in order to get event contextual log and add some security rules
* useful to :
Expand Down Expand Up @@ -31,6 +31,11 @@ export function useYagaMonitoring({ options }): Plugin {
const timestamp = new Date().getTime();
request.headers.append("requestTimestamp", String(timestamp))

// add x-request-id in header if not present
if (!request.headers.get('x-request-id')){
request.headers.append("x-request-id", uuidv4())
}

},
onRequestParse(args) {
const beforeTimestamp = new Date().getTime();
Expand Down
Loading

0 comments on commit 77b3444

Please sign in to comment.