我已经为本地开发环境设置了新的遗迹,我想将所有自定义日志转发到新的遗迹。为此,我在 newrelic.js 中进行了以下更改
application_logging: {
forwarding: {
enabled: true
}
}
我正在使用“winston-enricher”作为日志,并且我确实在终端控制台中看到了格式正确的日志,但这些日志没有转发到新的遗迹 UI。我确实在那里看到了正确的 APM 日志,只是不是自定义的。
newrelic.js
'use strict'
/**
* New Relic agent configuration.
*
* See lib/config/default.js in the agent distribution for a more complete
* description of configuration variables and their potential values.
*/
exports.config = {
/**
* Array of application names.
*/
app_name: ['app_name'],
application_logging: {
forwarding: {
enabled: true
}
},
/**
* Your New Relic license key.
*/
license_key: '<API_KEY>',
/**
* Whether the module is enabled.
*
* @env NEW_RELIC_ENABLED
*/
agent_enabled: true,
/**
* This setting controls distributed tracing.
* Distributed tracing lets you see the path that a request takes through your
* distributed system. Enabling distributed tracing changes the behavior of some
* New Relic features, so carefully consult the transition guide before you enable
* this feature: https://docs.newrelic.com/docs/transition-guide-distributed-tracing
* Default is false.
*/
distributed_tracing: {
/**
* Enables/disables distributed tracing.
*
* @env NEW_RELIC_DISTRIBUTED_TRACING_ENABLED
*/
enabled: true
},
logging: {
/**
* Level at which to log. 'trace' is most useful to New Relic when diagnosing
* issues with the agent, 'info' and higher will impose the least overhead on
* production applications.
*/
level: 'info',
/**
* Where to put the log file -- by default just uses process.cwd +
* 'newrelic_agent.log'. A special case is a filepath of 'stdout',
* in which case all logging will go to stdout, or 'stderr', in which
* case all logging will go to stderr.
*
* @env NEW_RELIC_LOG
*/
filepath: require('path').join(process.cwd(), 'newrelic_agent.log'),
/**
* Whether to write to a log file at all
*
* @env NEW_RELIC_LOG_ENABLED
*/
enabled: true
},
/**
* When true, all request headers except for those listed in attributes.exclude
* will be captured for all traces, unless otherwise specified in a destination's
* attributes include/exclude lists.
*/
allow_all_headers: true,
attributes: {
/**
* Prefix of attributes to exclude from all destinations. Allows * as wildcard
* at end.
*
* NOTE: If excluding headers, they must be in camelCase form to be filtered.
*
* @env NEW_RELIC_ATTRIBUTES_EXCLUDE
*/
exclude: [
'request.headers.cookie',
'request.headers.authorization',
'request.headers.proxyAuthorization',
'request.headers.setCookie*',
'request.headers.x*',
'response.headers.cookie',
'response.headers.authorization',
'response.headers.proxyAuthorization',
'response.headers.setCookie*',
'response.headers.x*'
]
}
}
这两个功能(即日志丰富和代理转发)的呈现方式存在一些混乱。
在某种程度上,它们是相互排斥的。您可以让代理直接转发日志,也可以您自己转发日志。如果您自己转发它们,则必须对它们进行装饰,以便它们正确显示在 New Relic 界面中。您可能有理由自己转发日志,但如果您不这样做,New Relic 会认为这是旧选项,并建议您不要再使用 Winston 丰富器。
在最新版本的代理上,如果您只是禁用温斯顿丰富器并删除
application_logging.forwarding.enabled
设置,它将默认转发您的日志。
另请注意,根据文档,您需要
application_logging.enabled
和 application_logging.forwarding.enabled
。需要启用较高级别的前一个选项才能启用后者以及其他功能(例如日志记录指标)。
我注意到 new relic 不会使用 Winston 格式化程序,但会使用 createLogger 的 defaultMeta 属性中的附加数据集。
我们通过创建如下所示的 defaultMeta 解决了为所有日志设置请求 id 的问题:
defaultMeta: {
meta: {
service: config.SERVICE_NAME,
process_id: uuidv4(),
},
tracing: {
get time_written() {
return new Date().toISOString()
},
},
},
并使用
express-http-context
我们在中间件中设置:
if (Logger.defaultMeta?.tracing) {
Logger.defaultMeta.tracing.request_id = req.id
Logger.defaultMeta.tracing.request_path = req.path
}
希望有帮助!