什么是 newrelic?
newrelic 是一个性能管理工具,能够帮助开发者跟踪网站应用程序的性能。通过使用 newrelic,可以获取到应用程序的各种指标,例如响应时间、交易率、数据库请求等,并使用这些信息来优化应用程序,提高性能。
如何使用 newrelic?
下面将介绍如何使用 npm 包 newrelic 进行配置和使用。
步骤 1:安装 newrelic
在使用 newrelic 前,需要在项目中安装 newrelic 包。可通过以下命令在项目中安装它:
--- ------- --------
步骤 2:配置 newrelic
在安装 newrelic 包后,需要对其进行配置,以便能够连接到 newrelic 服务器并跟踪应用程序的性能。
在项目的顶级目录中,新建一个名为 newrelic.js 的文件。该文件的基本结构如下:
---- -------- -------------- - - --------- --------------------------------- ------------ ------------------------------------ -------- - ------ ------- --------- ------ -- -
其中,app_name 和 license_key 是使用 newrelic 服务所需要的名称和许可证密钥。newrelic 提供了两种设置方式:
- 环境变量:可以在项目中设置名为 NEW_RELIC_APP_NAME 和 NEW_RELIC_LICENSE_KEY 的环境变量。newrelic 可以自动检测到这些变量并将其用于配置。
- 配置文件:也可以使用 newrelic.js 文件中的值直接设置这些选项。在这种情况下,需要将环境变量设置为空。
这里提供一份配置文件的示例:
---- -------- -------------- - - --------- ---- -------------- ------------ ----------------- -------- - ------ ------- --------- ------ -- -
步骤 3:应用程序的代码
现在,需要将 newrelic 包添加到应用程序中,以便它可以跟踪应用程序的性能。
在 Chrome、Firefox 或 Safari 浏览器的窗口中打开应用程序,并通过插入以下代码将 newrelic 添加到应用程序中的头部:
------- ----------------------- -------------------------------------------------------------- ------------------ ---------------------------------------------------------- ------------------- ---------------------------- -------------------- -------------------- ------------------------------------------------------------ ---------------------------- --------------------------------------------------------------- ----------------------------------------------------------------- ----------- ------------------------------------------------------------------------------------------------------------------------------- ------------------------------- ----------------------------------------------------------------- ------------------------- -------------------------------------------------------------------------- -------------- -------- ----------- ----------------------------------------------------------------- ------------------ -------------- ----------- -------------- ----------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------ ----------- -------------- --------------- --------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------- ------------------------------------------------ -------- --------------- --------------- ------------------------------------------------------------------------------------------------------------ ----------------- ------------------------- ------------- --------------------------------------------- --------------------------------------------------------------------------- ------------ --------------------------------- ------------- ----------------- ------------- ------------------------------- --------------- -------------------------------------------------------------- ----------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------- ------------------ --------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------- ---------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------- ---------------------------------------------- ------------ ------------------------------------------ ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------- ----------------- ----------- ------------------------ ---------------------- ------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------- ----- ------------------------------------- ---------- ----------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------ --------------------------------------------------------------------------- ------------------------------------------------------------------ ------------------------------------------------ ----------------------- ------------------------------------------------------------------ ------ -- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------- ------------ -------------------------------------- ----------------------- ----------------------- ------------------------------ ------------------------------------ -------------- ------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------- -------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------- ---------------------------- ----- - ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------- ------------------------------------------------------------- ------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------- ------------------------------- ------------------------------------------------------------------------------ ----- --------------------------------------------- ---------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------ ---------------------------------- ----------------------------------------------------------------------------------------------------------------------- ---------------- -------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------ -------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------- -------------------------------------------------- ------------------------------------------------------------------------------------------------------ ----------------------------------- -------------------- ------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------------------------ ------------------ ------------------ ------------------------------------------------------------------------------------------- ---------------------------------------------------------------- ------------ ------------ --- ------------- ---- ------ ---------------- ------------------------------- -------------------- -------------------------------------- ----------------------------------- ---------- -------------- ---------------------------------------------------------------- ---------------- --------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----- --------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------ ---------- --------------- ---------- -------------------------------- ----------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------- ---------- ---------------------------------------------------------------------------------------