Skip to content

Commit

Permalink
add prom client and logger fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Thierry DEGREMONT committed Jul 23, 2024
1 parent bbbc95a commit ba9f95b
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 71 deletions.
10 changes: 5 additions & 5 deletions package-lock.json

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

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"build:local:packages": "concurrently \"npm run pack -w directive-spl\" \"npm run pack -w inject-additional-transforms\"",
"preinstall": "npm run build:local:packages",
"start": "npm start -w graphql-mesh",
"startmesh": "npm run startmesh -w graphql-mesh"
"startmesh": "npm run startmesh -w graphql-mesh"
},
"devDependencies": {
"concurrently": "^8.2.2",
Expand All @@ -21,5 +21,8 @@
"engines": {
"node": "18"
},
"packageManager": "npm@9.5.1"
"packageManager": "npm@9.5.1",
"dependencies": {
"prom-client": "^15.1.3"
}
}
4 changes: 2 additions & 2 deletions packages/graphql-mesh/custom-plugins/monitor-envelop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ const formatter = (error: GraphQLError, mask: string): GraphQLError => {
export default ({ options }): Plugin => {
// not allow by default
// do not enabled enabledIntrospection in production
const enabledIntrospection = process.env['IS_PROUCTION_ENV'] != 'true' && ( options.enabledIntrospection || process.env['ENABLED_INTROSPECTION'] || false )
const enabledIntrospection = process.env['IS_PROUCTION_ENV'] != 'true' && ( options?.enabledIntrospection || process.env['ENABLED_INTROSPECTION'] || false )
// low info in log by default
const resultLogInfoLevel= options.resultLogInfoLevel ? options.resultLogInfoLevel : "low"
const resultLogInfoLevel= options?.resultLogInfoLevel ? options.resultLogInfoLevel : "low"

return {
onParse({ params, context }) {
Expand Down
10 changes: 5 additions & 5 deletions packages/graphql-mesh/custom-plugins/monitor-yoga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import { GraphQLError } from 'graphql'
* - remove a eventualy not allowed instropection data in result
*/
export function useYagaMonitoring({ options }): Plugin {
const isMaskErrors = options.maskError?.enabled || process.env['MASK_ERRORS'] || false
const isMaskErrors = options?.maskError?.enabled || process.env['MASK_ERRORS'] || false
// filter in production anyway
const isFilterError = options.filterError?.enabled || process.env['FILTER_ERRORS'] == 'true' || process.env['IS_PROUCTION_ENV'] == 'true' || false
const isFilterError = options?.filterError?.enabled || process.env['FILTER_ERRORS'] == 'true' || process.env['IS_PROUCTION_ENV'] == 'true' || false

const errorMaskMessage = options.maskError?.message ? options.maskError.message : "something goes wrong"
const reponseLogInfoLevel = options.reponseLogInfoLevel ? options.reponseLogInfoLevel : "low"
const resultLogInfoLevel = options.resultLogInfoLevel ? options.resultLogInfoLevel : "low"
const errorMaskMessage = options?.maskError?.message ? options.maskError.message : "something goes wrong"
const reponseLogInfoLevel = options?.reponseLogInfoLevel ? options.reponseLogInfoLevel : "low"
const resultLogInfoLevel = options?.resultLogInfoLevel ? options.resultLogInfoLevel : "low"


return {
Expand Down
7 changes: 2 additions & 5 deletions packages/graphql-mesh/package-lock.json

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

1 change: 1 addition & 0 deletions packages/graphql-mesh/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@graphql-mesh/transform-type-merging": "^0.96.2",
"@graphql-tools/schema": "^10.0.2",
"@graphql-tools/utils": "^10.0.12",
"prom-client": "^15.1.3",
"directive-spl": "file:./local-pkg/directive-spl-1.0.0.tgz",
"monitor-envelop": "file:./custom-plugins/monitor-envelop.ts",
"monitor-fetch": "file:./custom-plugins/monitor-fetch.ts",
Expand Down
107 changes: 55 additions & 52 deletions packages/graphql-mesh/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ export class Logger {
private static maxSkackLogSize = process.env["LogStackTraceMaxSize"] ? parseInt(process.env["LogStackTraceMaxSize"]) : 100
private static trackerOnly: boolean = process.env["LogTrackerHeadersOnly"] ? process.env["LogTrackerHeadersOnly"] == 'true' : false
private static envLog: string = process.env["LogEnvFieldToAdd"] // use to add env extra field in json log ex "app=graphql,env.name=production,env.site=Paris"
private static localDateCountry: string = process.env["LogLocalDateCountry"]
private static logHeaders = process.env["LogHeaders"] || "host,origin,user-agent,content-length"
private static logTrackerHeaders = process.env["LogTrackerHeaders"]
private static localDateCountry: string = process.env["LogLocalDateCountry"] || "fr-FR"
private static logHeaders = process.env["LogHeaders"] || "x-request-id,host,origin,user-agent,content-length,authorization"
private static logTrackerHeaders = process.env["LogTrackerHeaders"] || "x-request-id"
constructor() {

}
/**
* Core logger with Human format or machine one line json string format
*/
private static log(level: string, typeEv: string, message: string, data: any = null) {
private static log(level: string, typeEv: string, message: string, data: any = null, err = null) {

const date = new Date();
const timestamp = date.getTime();
Expand All @@ -39,35 +39,42 @@ export class Logger {
if (data) {
log['data'] = data
}
if (err) {
log['exeception'] = {}
for (const key in Object.keys(err)) {
log['exeception'][key] = err[key]

}
}
// if define add extra env field
if (this.envLog) {
addEnvFieldLog(this.envLog, log)
}
console.log(JSON.stringify(log))
} else {
console.log(date.toLocaleString(this.localDateCountry), level, typeEv, message, data)
console.log(date.toLocaleString(this.localDateCountry), level, typeEv, message, data, err)
}
}

public static error(typeEv: string, source: string, message: string, data: any = null) {
public static error(typeEv: string, source: string, message: string, data: any = null, e = null) {
if (Logger.level == 'ERROR' || Logger.level == 'WARN' || Logger.level == 'INFO' || Logger.level == 'DEBUG') {
Logger.log('ERROR', typeEv, source + ":" + message, data)
Logger.log('ERROR', typeEv, source + ":" + message, data, e)
}
}
public static warn(typeEv: string, source: string, message: string, data: any = null) {
public static warn(typeEv: string, source: string, message: string, data: any = null, e = null) {
if (Logger.level == 'WARN' || Logger.level == 'INFO' || Logger.level == 'DEBUG') {
Logger.log('INFO', typeEv, source + ":" + message, data)
Logger.log('INFO', typeEv, source + ":" + message, data, e)
}
}

public static info(typeEv: string, source: string, message: string, data: any = null) {
public static info(typeEv: string, source: string, message: string, data: any = null, e = null) {
if (Logger.level == 'INFO' || Logger.level == 'DEBUG') {
Logger.log('INFO', typeEv, source + ":" + message, data)
Logger.log('INFO', typeEv, source + ":" + message, data, e)
}
}
public static debug(typeEv: string, source: string, message: string, data: any = null) {
public static debug(typeEv: string, source: string, message: string, data: any = null, e = null) {
if (Logger.level == 'DEBUG') {
Logger.log('DEBUG', typeEv, source + ":" + message, data)
Logger.log('DEBUG', typeEv, source + ":" + message, data, e)
}
}
public static onParse(headers: any) {
Expand All @@ -77,7 +84,7 @@ export class Logger {

Logger.log('INFO', "ON-PARSE", "Request", headersToLog(headers, this.logTrackerHeaders, this.logHeaders, this.trackerOnly))
} catch (e) {
Logger.error('LOGGER_ERROR', 'endExec logger', 'error during log generation', e)
Logger.error('LOGGER_ERROR', 'endExec logger', 'error during log generation', null, e)

}
}
Expand All @@ -95,7 +102,7 @@ export class Logger {
}
Logger.log('INFO', "endExecDone", "Request", toLog)
} catch (e) {
Logger.error('LOGGER_ERROR', 'endExec logger', 'error during log generation', e)
Logger.error('LOGGER_ERROR', 'endExec logger', 'error during log generation', null, e)

}
}
Expand All @@ -115,7 +122,7 @@ export class Logger {

Logger.log('INFO', "onResultProcess", "Result", toLog)
} catch (e) {
Logger.error('LOGGER_ERROR', 'onResponse logger', 'error during log generation', e)
Logger.error('LOGGER_ERROR', 'onResponse logger', 'error during log generation', null, e)
}
}

Expand Down Expand Up @@ -162,7 +169,7 @@ export class Logger {
Logger.log('INFO', "onResponse", "response", toLog)
}
catch (e) {
Logger.error('LOGGER_ERROR', 'onResponse logger', 'error during log generation', e)
Logger.error('LOGGER_ERROR', 'onResponse logger', 'error during log generation', null, e)
}
}

Expand All @@ -182,8 +189,7 @@ export class Logger {

}
catch (e) {
console.log("erreur", e)
Logger.error('LOGGER_ERROR', 'onRequest logger', 'error during log generation', e)
Logger.error('LOGGER_ERROR', 'onRequest logger', 'error during log generation', null, e)
}
}

Expand All @@ -199,7 +205,7 @@ export class Logger {
}
Logger.log('INFO', "onFetch", "fetch", toLog)
} catch (e) {
Logger.error('LOGGER_ERROR', 'onFetch logger', 'error during log generation', e)
Logger.error('LOGGER_ERROR', 'onFetch logger', 'error during log generation', null, e)
}
}

Expand All @@ -214,7 +220,7 @@ export class Logger {
}
Logger.log('INFO', "graphqlQuery", "GraphQL Query", queryTolog)
} catch (e) {
Logger.error('LOGGER_ERROR', 'graphql query logger', 'error during log generation', e)
Logger.error('LOGGER_ERROR', 'graphql query logger', 'error during log generation', null, e)
}
}
}
Expand Down Expand Up @@ -289,49 +295,46 @@ function extractBody(body: String, bodyMaxLogSize: number) {
}
}
function headersToLog(headers: any, trackerHeaders: string, customHeaders: string, trackerOnly: boolean) {
const headersToLog = {}
try {
const headerMap = headers["_map"]

if (trackerOnly == false) {
const headersToLog = {

}
if (customHeaders) {
const cHeaders = customHeaders.split(',')

for (const headerKey in cHeaders) {
const header = cHeaders[headerKey].trim()
if (headerMap.get(header)) {
if (header.toLowerCase() == "authorization") {
mask(headerMap.get('authorization'))
} else {
headersToLog[header] = headerMap.get(header)
if (headerMap) {
if (trackerOnly == false) {
if (customHeaders) {
const cHeaders = customHeaders.split(',')

for (const headerKey in cHeaders) {
const header = cHeaders[headerKey].trim()
if (headerMap.get(header)) {
if (header.toLowerCase() == "authorization") {
headersToLog[header] = mask(headerMap.get('authorization'))
} else {
headersToLog[header] = headerMap.get(header)
}
}
}
}
}
return headersToLog
} else {
const headersToLog = {}
if (trackerHeaders) {
const tHeaders = trackerHeaders.split(',')
for (const headerKey in tHeaders) {
const header = tHeaders[headerKey]
if (headerMap.get(header)) {
if (header.toLowerCase() == "authorization") {
mask(headerMap.get('authorization'))
} else {
headersToLog[header] = headerMap.get(header)
return headersToLog
} else {
if (trackerHeaders) {
const tHeaders = trackerHeaders.split(',')
for (const headerKey in tHeaders) {
const header = tHeaders[headerKey]
if (headerMap.get(header)) {
if (header.toLowerCase() == "authorization") {
headersToLog[header] = mask(headerMap.get('authorization'))
} else {
headersToLog[header] = headerMap.get(header)
}
}
}
}
}

return headersToLog
}
} catch (e) {
Logger.error('LOGGER_ERROR', 'oheadersToLog', 'error during headers log generation', e)
Logger.error('LOGGER_ERROR', 'onheadersToLog', 'error during headers log generation', null, e)
}
return headersToLog
}
/**
* Use to add some environment fields to log, like env.name, app.name ...
Expand Down

0 comments on commit ba9f95b

Please sign in to comment.