azify-logger 1.0.52 → 1.0.54-test

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.
Files changed (3) hide show
  1. package/package.json +4 -3
  2. package/register.js +24 -0
  3. package/server.js +56 -10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "azify-logger",
3
- "version": "1.0.52",
3
+ "version": "1.0.54-test",
4
4
  "description": "Azify Logger Client - Centralized logging for OpenSearch",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -50,7 +50,7 @@
50
50
  "@opentelemetry/sdk-trace-base": "1.25.0",
51
51
  "@opentelemetry/semantic-conventions": "1.25.0",
52
52
  "adm-zip": "^0.5.16",
53
- "archiver": "^6.0.1",
53
+ "archiver": "^4.0.2",
54
54
  "axios": "^1.7.9",
55
55
  "bull": "^4.16.5",
56
56
  "cors": "^2.8.5",
@@ -77,7 +77,8 @@
77
77
  "ioredis": "^5.8.2"
78
78
  },
79
79
  "overrides": {
80
- "semver": "^7.5.2"
80
+ "semver": "^7.5.2",
81
+ "glob": "^13.0.0"
81
82
  },
82
83
  "engines": {
83
84
  "node": ">=20 <=22"
package/register.js CHANGED
@@ -145,10 +145,34 @@ try {
145
145
 
146
146
  const debug = process.env.AZIFY_LOGGER_DEBUG === '1'
147
147
  const httpVerbose = process.env.AZIFY_LOGGER_HTTP_VERBOSE === '1'
148
+
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
+ function extractUrlFromReqResMessage(msgStr) {
151
+ const s = String(msgStr)
152
+ const m1 = s.match(/\[(?:REQUEST|RESPONSE)\]\s+\S+\s+(https?:\/\/\S+)/i)
153
+ if (m1) return m1[1].replace(/[)\]}>.,;]+$/, '')
154
+ const m2 = s.match(/\[(?:REQUEST|RESPONSE)\]\s+\S+\s+(\S+)/i)
155
+ if (m2 && /^https?:\/\//i.test(m2[1])) return m2[1].replace(/[)\]}>.,;]+$/, '')
156
+ return null
157
+ }
158
+
148
159
  function sendOutboundLog(level, message, meta) {
149
160
  try {
150
161
  const msgStr = String(message)
151
162
  const isReqRes = msgStr.includes('[REQUEST]') || msgStr.includes('[RESPONSE]')
163
+ if (isReqRes && meta && typeof meta === 'object') {
164
+ const u = meta.url
165
+ const badUrl =
166
+ u === 'unknown' ||
167
+ !String(u || '').trim() ||
168
+ String(u).toLowerCase() === 'unknown'
169
+ if (badUrl) {
170
+ const fromMsg = extractUrlFromReqResMessage(msgStr)
171
+ if (fromMsg) {
172
+ meta = { ...meta, url: fromMsg }
173
+ }
174
+ }
175
+ }
152
176
  if (isReqRes && meta && meta.url && isLoggerApiCall({ url: meta.url })) return
153
177
  if (isReqRes && meta && (meta.url === 'unknown' || !String(meta.url || '').trim() || String(meta.url).toLowerCase() === 'unknown')) return
154
178
  const source = meta && meta.__source
package/server.js CHANGED
@@ -186,7 +186,11 @@ async function ensureIndexTemplate() {
186
186
  settings: {
187
187
  number_of_shards: 1,
188
188
  number_of_replicas: 0,
189
- 'index.refresh_interval': '5s'
189
+ 'index.refresh_interval': '5s',
190
+ 'index.mapping.total_fields.limit': Math.min(
191
+ Math.max(Number(process.env.AZIFY_LOGGER_OPENSEARCH_TOTAL_FIELDS_LIMIT) || 5000, 1000),
192
+ 20000
193
+ )
190
194
  },
191
195
  mappings: {
192
196
  properties: {
@@ -2053,7 +2057,7 @@ async function handleLog(req, res) {
2053
2057
 
2054
2058
  res.json({ success: true, message: 'Log enviado com sucesso', index: indexName })
2055
2059
 
2056
- const docToSend = _trimLogEntryForOpenSearch(logEntry)
2060
+ const docToSend = _normalizeLogEntryForOpenSearch(_trimLogEntryForOpenSearch(logEntry))
2057
2061
  _enqueueOpenSearchWrite(osUrl, indexName, docToSend, serviceName)
2058
2062
  }
2059
2063
 
@@ -2106,6 +2110,40 @@ function _trimLogEntryForOpenSearch(doc) {
2106
2110
  return out
2107
2111
  }
2108
2112
 
2113
+ function _normalizeLogEntryForOpenSearch(doc) {
2114
+ if (!doc || typeof doc !== 'object') return doc
2115
+ function coerceToString(v) {
2116
+ if (v == null) return v
2117
+ if (typeof v === 'string') return v
2118
+ if (Buffer.isBuffer(v)) return v.toString('utf8')
2119
+ if (typeof v === 'object') {
2120
+ try {
2121
+ return JSON.stringify(v)
2122
+ } catch (_) {
2123
+ return String(v)
2124
+ }
2125
+ }
2126
+ return String(v)
2127
+ }
2128
+ function normalizeNested(obj) {
2129
+ if (!obj || typeof obj !== 'object' || Buffer.isBuffer(obj)) return obj
2130
+ const o = { ...obj }
2131
+ for (const k of ['responseBody', 'requestBody']) {
2132
+ if (o[k] != null && typeof o[k] !== 'string') o[k] = coerceToString(o[k])
2133
+ }
2134
+ if (o.body != null && typeof o.body === 'object' && !Buffer.isBuffer(o.body)) {
2135
+ o.bodyJson = coerceToString(o.body)
2136
+ delete o.body
2137
+ }
2138
+ return o
2139
+ }
2140
+ const out = normalizeNested(doc)
2141
+ if (out.meta != null && typeof out.meta === 'object') {
2142
+ out.meta = normalizeNested(out.meta)
2143
+ }
2144
+ return out
2145
+ }
2146
+
2109
2147
  const _openSearchWriteQueue = []
2110
2148
  let _openSearchWriteInFlight = 0
2111
2149
  const _openSearchWriteMaxConcurrent = 10
@@ -2293,14 +2331,22 @@ function _drainOpenSearchQueue() {
2293
2331
  if (_openSearchCircuitRetryTimer.unref) _openSearchCircuitRetryTimer.unref()
2294
2332
  }
2295
2333
  }
2296
- if (_openSearchFailuresInRow <= 2) {
2297
- const status = error?.response?.status
2298
- const errorMsg = error?.response?.data?.error?.reason ||
2299
- error?.response?.data?.message ||
2300
- error?.message ||
2301
- 'Erro desconhecido'
2302
- console.error('❌ Falha ao enviar log para OpenSearch', { status, message: errorMsg })
2303
- if (error?.code) console.error('⚙️ Código de erro:', error.code)
2334
+ const status = error?.response?.status
2335
+ const rawMsg = error?.response?.data?.error?.reason ||
2336
+ error?.response?.data?.error?.type ||
2337
+ error?.response?.data?.message ||
2338
+ error?.message ||
2339
+ 'Erro desconhecido'
2340
+ const errorMsg = typeof rawMsg === 'string' ? rawMsg.slice(0, 800) : String(rawMsg).slice(0, 800)
2341
+ const logPayload = {
2342
+ index: job.indexName,
2343
+ serviceName: job.serviceName,
2344
+ status,
2345
+ message: errorMsg
2346
+ }
2347
+ console.error('❌ Falha ao enviar log para OpenSearch', logPayload)
2348
+ if (error?.code && _openSearchFailuresInRow <= 10) {
2349
+ console.error('⚙️ Código de erro:', error.code)
2304
2350
  }
2305
2351
  })
2306
2352
  .finally(() => {