为 .NET、Node.js、Python 和 Java 应用程序启用 Azure Monitor OpenTelemetry
本文介绍了如何在 Application Insights 内启用和配置基于 OpenTelemetry 的数据收集。 Azure Monitor OpenTelemetry 发行版:
- 提供 OpenTelemetry 发行版,其中包括对特定于 Azure Monitor 的功能的支持。
- 通过包含用于收集跟踪、指标、日志和异常的 OpenTelemetry 检测库来实现自动遥测。
- 允许收集自定义遥测数据。
- 还支持使用实时指标监视和收集来自实时生产环境中 Web 应用程序的更多遥测数据。
要详细了解 Azure Monitor OpenTelemetry 发行版的优点,请参阅“我为什么应该使用 Azure Monitor OpenTelemetry 发行版”。
若要了解有关使用 OpenTelemetry 收集数据的详细信息,请参阅数据收集基础知识或 OpenTelemetry 常见问题解答。
OpenTelemetry 产品/服务适用于 .NET、Node.js、Python 和 Java 应用程序。 有关每项功能的发布状态,请参阅常见问题解答。
遵循本部分提供的步骤使用 OpenTelemetry 检测应用程序。 针对特定于语言的说明选择选项卡。
备注
.NET 涉及多个场景,包括经典 ASP.NET、控制台应用、Windows 窗体 (WinForms) 等。
- Azure 订阅:创建 Azure 试用版订阅
- Application Insights 资源:创建 Application Insights 资源
- 应用程序使用 Node.js 运行时的官方支持版本:
• OpenTelemetry 支持的运行时
• Azure Monitor OpenTelemetry 导出程序支持的运行时
备注
如果你不依赖于不支持的表中列出的任何属性,那么一旦脱离 beta 阶段,ApplicationInsights 填充码将是你继续前进的便捷途径。
如果依赖于这些属性,请继续使用 Azure Monitor OpenTelemetry 发行版。 我们即将推出迁移指南。
提示
如果要从 Application Insights Classic API 迁移,请参阅我们的迁移文档。
安装最新的 @azure/monitor-opentelemetry 包:
npm install @azure/monitor-opentelemetry
以下包还可用于本文稍后介绍的一些特定方案:
- @opentelemetry/api
- @opentelemetry/sdk-metrics
- @opentelemetry/resources
- @opentelemetry/semantic-conventions
- @opentelemetry/sdk-trace-base
npm install @opentelemetry/api
npm install @opentelemetry/sdk-metrics
npm install @opentelemetry/resources
npm install @opentelemetry/semantic-conventions
npm install @opentelemetry/sdk-trace-base
// Import the `useAzureMonitor()` function from the `@azure/monitor-opentelemetry` package.
const { useAzureMonitor } = require("@azure/monitor-opentelemetry");
// Call the `useAzureMonitor()` function to configure OpenTelemetry to use Azure Monitor.
useAzureMonitor();
连接字符串是唯一的,它可以指定 Azure Monitor OpenTelemetry 发行版将收集的遥测数据发送到的位置。
要复制连接字符串:
- 转到 Application Insights 资源的“概述”窗格。
- 查找你的连接字符串。
- 将鼠标指针悬停在连接字符串上,并选择“复制到剪贴板”图标。
要粘贴连接字符串,请从以下选项中进行选择:
重要
建议仅在本地开发和测试环境中通过代码设置连接字符串。
对于生产环境,请使用环境变量或配置文件(仅限 Java)。
通过环境变量设置 - 推荐
将以下命令中的
<Your connection string>
替换为你的连接字符串。APPLICATIONINSIGHTS_CONNECTION_STRING=<Your connection string>
通过配置文件设置 - 仅限 Java
创建包含以下内容的、名为
applicationinsights.json
的配置文件,并将其置于applicationinsights-agent-3.6.2.jar
所在的同一目录:{ "connectionString": "<Your connection string>" }
将之前的 JSON 中的
<Your connection string>
替换为你的唯一连接字符串。通过代码设置 - 仅限 ASP.NET Core、Node.js 和 Python
有关通过代码设置连接字符串的示例,请参阅连接字符串配置。
备注
如果在多个位置设置连接字符串,则环境变量的优先顺序如下:
- 代码
- 环境变量
- 配置文件
运行应用程序,然后在 Azure 门户中打开 Application Insights。 数据可能在数分钟后才会显示。
现已为你的应用程序启用了 Application Insights。 以下步骤是可选的,并允许进一步自定义。
重要
如果有两个或更多服务向同一 Application Insights 资源发出遥测数据,则需要设置云角色名称以在应用程序映射中正确表示这些服务。
在使用 Application Insights 检测的过程中,我们会收集诊断数据并将其发送给 Microsoft。 这些数据可帮助我们运行和改进 Application Insights。 若要了解更多信息,请参阅 Azure Application Insights 中的 Statsbeat。
Azure Monitor OpenTelemetry 示例应用程序适用于所有支持的语言:
- 有关添加和修改 Azure Monitor OpenTelemetry 的详细信息,请参阅添加和修改 Azure Monitor OpenTelemetry。
- 要查看源代码,请参阅 Azure Monitor OpenTelemetry GitHub 存储库。
- 若要安装 npm 包并检查更新,请参阅
@azure/monitor-opentelemetry
npm 包页面。 - 若要进一步了解 Azure Monitor Application Insights 和 OpenTelemetry,请参阅 Azure Monitor 示例应用程序。
- 若要详细了解 OpenTelemetry 及其社区,请参阅 OpenTelemetry JavaScript GitHub 存储库。
- 若要开启使用体验,请参阅启用 Web 或浏览器用户监视。
- 若要查看常见问题解答、故障排除步骤、支持选项或提供 OpenTelemetry 反馈,请参阅 有关 Azure Monitor Application Insights 的 OpenTelemetry 帮助、支持和反馈。