您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

什么是分布式跟踪?What is Distributed Tracing?

现代云和微服务体系结构的出现宣告了一种简单且可独立部署的服务的诞生,它可以提高可用性和吞吐量,同时还能降低成本。The advent of modern cloud and microservices architectures has given rise to simple, independently deployable services that can help reduce costs while increasing availability and throughput. 但是,虽然这些转变使那些单个的服务作为整体更易于理解,但也使整个系统更加难以推断和调试。But while these movements have made individual services easier to understand as a whole, they’ve made overall systems more difficult to reason about and debug.

在整体式体系结构中,我们已习惯于通过调用堆栈进行调试。In monolithic architectures, we’ve gotten used to debugging with call stacks. 调用堆栈是很好的工具,可以显示执行流(方法 A 调用了方法 B,方法 B 调用了方法 C),并可显示每个这样的调用的详细信息和参数。Call stacks are brilliant tools for showing the flow of execution (Method A called Method B, which called Method C), along with details and parameters about each of those calls. 这适用于在单个进程上运行的庞大单体结构或服务。但是,如果调用跨进程边界,而不仅仅是本地堆栈上的某个引用,我们该如何调试?This is great for monoliths or services running on a single process, but how do we debug when the call is across a process boundary, not simply a reference on the local stack?

这时候就需要使用分布式跟踪。That’s where distributed tracing comes in.

分布式跟踪就是适合现代云和微服务体系结构的调用堆栈,但是添加了简单的性能探查器。Distributed tracing is the equivalent of call stacks for modern cloud and microservices architectures, with the addition of a simplistic performance profiler thrown in. 在 Azure Monitor 中,我们提供的两种体验适合使用分布式跟踪数据。In Azure Monitor, we provide two experiences for consuming distributed trace data. 第一种是事务诊断视图,这相当于一个增加了时间维度的调用堆栈。The first is our transaction diagnostics view, which is like a call stack with a time dimension added in. 事务诊断视图适用于查看单个事务/请求,并且可以按照单个请求来查找可靠性问题和性能瓶颈的根本原因。The transaction diagnostics view provides visibility into one single transaction/request, and is helpful for finding the root cause of reliability issues and performance bottlenecks on a per request basis.

Azure Monitor 还提供应用程序映射视图,该视图聚合了许多事务,可以通过拓扑视图的方式来显示系统交互情况,以及平均性能和错误率。Azure Monitor also offers an application map view which aggregates many transactions to show a topological view of how the systems interact, and what the average performance and error rates are.

如何启用分布式跟踪How to Enable Distributed Tracing

在应用程序中跨服务启用分布式跟踪很简单,只需根据实现服务时采用的语言为每个服务添加适当的 SDK 或库即可。Enabling distributed tracing across the services in an application is as simple as adding the proper SDK or library to each service, based on the language the service was implemented in.

通过 Application Insights SDK 启用Enabling via Application Insights SDKs

适用于 .NET、.NET Core、Java、Node.js 和 JavaScript 的 Application Insights SDK 都以原生方式支持分布式跟踪。The Application Insights SDKs for .NET, .NET Core, Java, Node.js, and JavaScript all support distributed tracing natively. 每个 Application Insights SDK 的安装和配置说明见下:Instructions for installing and configuring each Application Insights SDK are available below:

安装并配置适当的 Application Insights SDK 以后,系统就会通过 SDK 依赖项自动收集器自动收集常用框架、库和技术的跟踪信息。With the proper Application Insights SDK installed and configured, tracing information is automatically collected for popular frameworks, libraries, and technologies by SDK dependency auto-collectors. 依赖项自动收集文档中提供支持的技术的完整列表。The full list of supported technologies is available in the Dependency auto-collection documentation.

另外,任何技术都可以通过在 TelemetryClient 上调用 TrackDependency 手动进行跟踪。Additionally, any technology can be tracked manually with a call to TrackDependency on the TelemetryClient.

通过 OpenCensus 启用Enable via OpenCensus

除了 Application Insights SDK,Application Insights 还可以通过 OpenCensus 来支持分布式跟踪。In addition to the Application Insights SDKs, Application Insights also supports distributed tracing through OpenCensus. OpenCensus 是库的单发行版,开源且不局限于供应商,可以针对服务进行指标收集和分布式跟踪。OpenCensus is an open source, vendor-agnostic, single distribution of libraries to provide metrics collection and distributed tracing for services. 它还允许开源社区针对 Redis、Memcached 或 MongoDB 之类的常用技术启用分布式跟踪。It also enables the open source community to enable distributed tracing with popular technologies like Redis, Memcached, or MongoDB. Microsoft 与多个其他的监视项目和云项目合作伙伴进行 OpenCensus 协作Microsoft collaborates on OpenCensus with several other monitoring and cloud partners.

若要通过 OpenCensus 向应用程序添加分布式跟踪功能,请先安装并配置 Application Insights 本地转发器To add distributed tracing capabilities to an application with OpenCensus, first install and configure the Application Insights Local Forwarder. 然后对 OpenCensus 进行配置,以便通过本地转发器路由分布式跟踪数据。From there, configure OpenCensus to route distributed trace data through the Local Forwarder. PythonGo 均受支持。Both Python and Go are supported.

OpenCensus 网站保留了 PythonGo 的 API 参考文档,此外还有各种不同的 OpenCensus 使用指南。The OpenCensus website maintains API reference documentation for Python and Go, as well as various different guides for using OpenCensus.

后续步骤Next steps