azify-logger 1.0.54-test → 1.0.54-test-2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "azify-logger",
3
- "version": "1.0.54-test",
3
+ "version": "1.0.54-test-2",
4
4
  "description": "Azify Logger Client - Centralized logging for OpenSearch",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -37,6 +37,45 @@ try {
37
37
  return a === b || a.startsWith(b + '/')
38
38
  }
39
39
 
40
+ /** undici.request(url, opts) / request(opts) — muitas chamadas não trazem origin; path pode ser URL absoluta ou só path + hostname. */
41
+ function resolveUrlStrFromUndiciRequestArgs(url, options) {
42
+ const opts = (typeof url === 'object' && url !== null && !(url instanceof URL)) ? url : options
43
+ const urlArg = (typeof url === 'string' || (url && (url.href || url instanceof URL))) ? url : (opts && (opts.url || opts.uri))
44
+ if (typeof urlArg === 'string') return urlArg
45
+ if (urlArg && typeof urlArg.href === 'string') return urlArg.href
46
+ if (urlArg && typeof urlArg.toString === 'function') return urlArg.toString()
47
+ if (opts && typeof opts.origin === 'string' && opts.path != null) {
48
+ return opts.origin + (String(opts.path).startsWith('/') ? '' : '/') + String(opts.path)
49
+ }
50
+ if (opts && typeof opts.path === 'string' && /^https?:\/\//i.test(opts.path)) {
51
+ return opts.path
52
+ }
53
+ if (opts && (opts.hostname || opts.host)) {
54
+ const h = String(opts.hostname || String(opts.host || '').split(':')[0]).trim()
55
+ if (h) {
56
+ const proto = (opts.protocol && String(opts.protocol).replace(/:$/, '')) || 'https'
57
+ const pnum = opts.port != null && opts.port !== '' ? Number(opts.port) : NaN
58
+ const port = !Number.isNaN(pnum) && pnum !== 80 && pnum !== 443 ? ':' + pnum : ''
59
+ const p = opts.path != null ? String(opts.path) : '/'
60
+ return proto + '://' + h + port + (p.startsWith('/') ? p : '/' + p)
61
+ }
62
+ }
63
+ return 'unknown'
64
+ }
65
+
66
+ function resolveUrlStrFromDispatcherOpts(opts) {
67
+ if (!opts || typeof opts !== 'object') return 'unknown'
68
+ const origin = (opts.origin) || ''
69
+ const pathPart = opts.path != null ? opts.path : '/'
70
+ const pathStr = String(pathPart)
71
+ if (/^https?:\/\//i.test(pathStr)) return pathStr
72
+ if (origin) return origin + (pathStr.startsWith('/') ? pathStr : '/' + pathStr)
73
+ if (opts.hostname || opts.host) {
74
+ return resolveUrlStrFromUndiciRequestArgs(null, opts)
75
+ }
76
+ return 'unknown'
77
+ }
78
+
40
79
  function send(level, message, meta) {
41
80
  try {
42
81
  const msgStr = String(message)
@@ -118,12 +157,7 @@ try {
118
157
  exports.request = function (url, options, callback) {
119
158
  const opts = (typeof url === 'object' && url !== null && !(url instanceof URL)) ? url : options
120
159
  const method = ((opts && opts.method) || 'GET').toUpperCase()
121
- let urlStr = 'unknown'
122
- const urlArg = (typeof url === 'string' || (url && (url.href || url instanceof URL))) ? url : (opts && (opts.url || opts.uri))
123
- if (typeof urlArg === 'string') urlStr = urlArg
124
- else if (urlArg && typeof urlArg.href === 'string') urlStr = urlArg.href
125
- else if (urlArg && typeof urlArg.toString === 'function') urlStr = urlArg.toString()
126
- else if (opts && typeof opts.origin === 'string' && opts.path != null) urlStr = opts.origin + (String(opts.path).startsWith('/') ? '' : '/') + String(opts.path)
160
+ const urlStr = resolveUrlStrFromUndiciRequestArgs(url, options)
127
161
  if (process.env.AZIFY_LOGGER_DEBUG === '1') { try { process.stderr.write('[AZIFY-PATCH] undici.request ' + method + ' ' + urlStr.slice(0, 80) + '\n') } catch (_) {} }
128
162
  const _ep = process.env.AZIFY_DEBUG_LOG_PATH
129
163
  if (_ep) { try { require('fs').appendFileSync(_ep, new Date().toISOString() + ' [AZIFY] WRAPPER exports.request ' + method + ' ' + urlStr.slice(0, 100) + '\n') } catch (_) {} }
@@ -174,9 +208,7 @@ try {
174
208
  const origDisp = proto.request
175
209
  proto.request = function (opts, callback) {
176
210
  const methodStr = String((opts && opts.method) || 'GET').toUpperCase()
177
- const origin = (opts && opts.origin) || ''
178
- const path = (opts && opts.path) != null ? opts.path : '/'
179
- const urlStr = origin ? (origin + (path.startsWith('/') ? path : '/' + path)) : 'unknown'
211
+ const urlStr = resolveUrlStrFromDispatcherOpts(opts)
180
212
  httpLog('WRAPPER Dispatcher.request ' + methodStr + ' ' + urlStr.slice(0, 80))
181
213
  const ctx = getRequestContext() || getLastJobContext()
182
214
  const otelCtx = getOtelTraceContext()
@@ -255,9 +287,7 @@ try {
255
287
  if (typeof origReq !== 'function') return d
256
288
  d.request = function (opts, callback) {
257
289
  const methodStr = String((opts && opts.method) || 'GET').toUpperCase()
258
- const origin = (opts && opts.origin) || ''
259
- const pathPart = (opts && opts.path) != null ? opts.path : '/'
260
- const urlStr = origin ? (origin + (pathPart.startsWith('/') ? pathPart : '/' + pathPart)) : 'unknown'
290
+ const urlStr = resolveUrlStrFromDispatcherOpts(opts)
261
291
  httpLog('WRAPPER getGlobalDispatcher().request ' + methodStr + ' ' + urlStr.slice(0, 80))
262
292
  const ctx = getRequestContext ? getRequestContext() : (getLastJobContext ? getLastJobContext() : null)
263
293
  const otelCtx = getOtelTraceContext()
@@ -333,9 +363,7 @@ try {
333
363
  const HTTP_CLIENT_MODE = deps ? deps.HTTP_CLIENT_MODE : 'all'
334
364
  d.request = function (opts, callback) {
335
365
  const methodStr = String((opts && opts.method) || 'GET').toUpperCase()
336
- const origin = (opts && opts.origin) || ''
337
- const pathPart = (opts && opts.path) != null ? opts.path : '/'
338
- const urlStr = origin ? (origin + (pathPart.startsWith('/') ? pathPart : '/' + pathPart)) : 'unknown'
366
+ const urlStr = resolveUrlStrFromDispatcherOpts(opts)
339
367
  httpLog('WRAPPER globalThis.dispatcher.request ' + methodStr + ' ' + urlStr.slice(0, 80))
340
368
  const _dp = process.env.AZIFY_DEBUG_LOG_PATH
341
369
  if (_dp) { try { require('fs').appendFileSync(_dp, new Date().toISOString() + ' [AZIFY] WRAPPER globalDispatcher.request ' + methodStr + ' ' + urlStr.slice(0, 100) + '\n') } catch (_) {} }
@@ -608,14 +636,12 @@ try {
608
636
  const RequestHandler = mod.exports.RequestHandler
609
637
  mod.exports = function wrappedApiRequest(opts, callback) {
610
638
  const methodStr = String((opts && opts.method) || 'GET').toUpperCase()
611
- const urlPart = (opts && opts.origin) ? String(opts.origin).slice(0, 80) : (opts && opts.path) || ''
639
+ const urlStr = resolveUrlStrFromDispatcherOpts(opts)
640
+ const urlPart = urlStr !== 'unknown' ? urlStr.slice(0, 80) : ((opts && opts.origin) ? String(opts.origin).slice(0, 80) : (opts && opts.path) || '')
612
641
  if (HTTP_DEBUG) try { process.stderr.write('[AZIFY-PATCH] api-request ' + methodStr + ' ' + urlPart + '\n') } catch (_) {}
613
642
  if (HTTP_VERBOSE || HTTP_DEBUG) {
614
643
  try { process.stderr.write('[AZIFY] HTTP wrapper api-request ' + methodStr + ' ' + urlPart + '\n') } catch (_) {}
615
644
  }
616
- const origin = (opts && opts.origin) || ''
617
- const pathPart = (opts && opts.path) != null ? opts.path : '/'
618
- const urlStr = origin ? (origin + (pathPart.startsWith('/') ? pathPart : '/' + pathPart)) : 'unknown'
619
645
  httpLog('WRAPPER api-request ' + methodStr + ' ' + urlStr.slice(0, 80))
620
646
  const ctx = getRequestContext() || getLastJobContext()
621
647
  const otelCtx = getOtelTraceContext()
package/register.js CHANGED
@@ -10,7 +10,12 @@ try {
10
10
  }
11
11
  const ModuleForRequire = require('module')
12
12
  const nodeRequireOriginal = ModuleForRequire.prototype.require
13
- const bunyan = require('bunyan')
13
+ let bunyan = null
14
+ try {
15
+ bunyan = require('bunyan')
16
+ } catch (_) {
17
+ /* bunyan é opcional; sem ele o patch Bunyan não aplica, mas HTTP/Pino/axios devem continuar */
18
+ }
14
19
  const createBunyanStream = require('./streams/bunyan')
15
20
  const { createHttpLoggerTransport } = require('./streams/httpQueue')
16
21
  const { getRequestContext } = require('./store')
@@ -146,7 +151,6 @@ try {
146
151
  const debug = process.env.AZIFY_LOGGER_DEBUG === '1'
147
152
  const httpVerbose = process.env.AZIFY_LOGGER_HTTP_VERBOSE === '1'
148
153
 
149
- /** Quando o patch não consegue ler a URL dos args (undici/http), meta.url fica 'unknown', mas a mensagem ainda traz a URL. */
150
154
  function extractUrlFromReqResMessage(msgStr) {
151
155
  const s = String(msgStr)
152
156
  const m1 = s.match(/\[(?:REQUEST|RESPONSE)\]\s+\S+\s+(https?:\/\/\S+)/i)
@@ -156,6 +160,8 @@ try {
156
160
  return null
157
161
  }
158
162
 
163
+ const OPAQUE_NO_URL = 'https://opaque.invalid/azify-logger-no-url'
164
+
159
165
  function sendOutboundLog(level, message, meta) {
160
166
  try {
161
167
  const msgStr = String(message)
@@ -168,13 +174,10 @@ try {
168
174
  String(u).toLowerCase() === 'unknown'
169
175
  if (badUrl) {
170
176
  const fromMsg = extractUrlFromReqResMessage(msgStr)
171
- if (fromMsg) {
172
- meta = { ...meta, url: fromMsg }
173
- }
177
+ meta = fromMsg ? { ...meta, url: fromMsg } : { ...meta, url: OPAQUE_NO_URL }
174
178
  }
175
179
  }
176
180
  if (isReqRes && meta && meta.url && isLoggerApiCall({ url: meta.url })) return
177
- if (isReqRes && meta && (meta.url === 'unknown' || !String(meta.url || '').trim() || String(meta.url).toLowerCase() === 'unknown')) return
178
181
  const source = meta && meta.__source
179
182
  if (debug && source === 'pino-stdout') {
180
183
  if (debug) try { process.stderr.write('[azify-logger] SENDOUTBOUND_ENTER level=' + level + ' source=' + source + ' msg=' + msgStr.slice(0, 60) + '\n') } catch (_) {}
@@ -387,22 +390,24 @@ try {
387
390
  return false
388
391
  }
389
392
 
390
- const originalCreate = bunyan.createLogger
391
- bunyan.createLogger = function patchedCreateLogger(options) {
392
- const logger = originalCreate.call(bunyan, options)
393
- try {
394
- const level = process.env.AZIFY_LOG_LEVEL || (options && options.level) || 'info'
395
- const loggerUrl = process.env.AZIFY_LOGGER_URL
396
- const serviceName = process.env.APP_NAME || (options && options.name)
397
- const environment = process.env.NODE_ENV
398
-
399
- logger.addStream({
400
- level,
401
- type: 'raw',
402
- stream: createBunyanStream({ loggerUrl, serviceName, environment })
403
- })
404
- } catch (_) {}
405
- return logger
393
+ if (bunyan && typeof bunyan.createLogger === 'function') {
394
+ const originalCreate = bunyan.createLogger
395
+ bunyan.createLogger = function patchedCreateLogger(options) {
396
+ const logger = originalCreate.call(bunyan, options)
397
+ try {
398
+ const level = process.env.AZIFY_LOG_LEVEL || (options && options.level) || 'info'
399
+ const loggerUrl = process.env.AZIFY_LOGGER_URL
400
+ const serviceName = process.env.APP_NAME || (options && options.name)
401
+ const environment = process.env.NODE_ENV
402
+
403
+ logger.addStream({
404
+ level,
405
+ type: 'raw',
406
+ stream: createBunyanStream({ loggerUrl, serviceName, environment })
407
+ })
408
+ } catch (_) {}
409
+ return logger
410
+ }
406
411
  }
407
412
 
408
413
  const getOtelTraceContext = () => {