首页 web前端 js教程 使用 OpenTelemetry 监控浏览器应用程序

使用 OpenTelemetry 监控浏览器应用程序

Sep 06, 2024 am 06:45 AM

许多开发团队在服务器端利用 OpenTelemetry (OTeL) 从其应用程序收集日志、跟踪和指标等信号。然而,经常被忽视的是 OTeL 浏览器工具的强大功能。这种客户端可观察性数据可以提供有意义的见解,并通过连接客户端和服务器之间的跟踪来创建应用程序性能的全面视图。

什么是开放遥测?

OpenTelemetry 是一个工具、API 和 SDK 的集合,用于检测、生成、收集和导出遥测数据(指标、日志和跟踪)以进行分析,以便了解软件的性能和行为。

如果您有兴趣深入了解 OTeL,请查看我们最近的文章:什么是 OpenTelemetry 以及我为什么要关心?

浏览器应用程序中的 OpenTelemetry

当涉及到浏览器应用程序时,OpenTelemetry 可以提供以下方面的宝贵见解:

  1. 文档加载性能
  2. 用户互动
  3. 网络请求(XMLHttpRequest 和 Fetch)

这些见解使开发人员能够识别瓶颈、跟踪用户交互并监控网络请求,而无需手动检测。上面提到的是通过利用 OTeL 的自动检测相对容易获得的数据,但您也可以添加手动检测来收集客户端应用程序中任何其他代码的跟踪。

突出显示和 OpenTelemetry

Highlight 的 JavaScript SDK 提供了从客户端应用程序收集 OpenTelemetry 数据的内置支持。这种集成允许您将 OpenTelemetry 跟踪无缝合并到您的 Web 应用程序中。

通过突出显示启用 OpenTelemetry

OTeL 数据收集仍处于测试阶段,因此您需要在初始化 SDK 时通过设置 enableOtelTracing 配置选项来显式启用它:

H.init({
  // ...
  enableOtelTracing: true
})
登录后复制

通过这个简单的配置,Highlight 通过利用自动检测并进行一些额外的处理,自动收集您需要的大部分 OpenTelemetry 数据,以使数据在 Highlight 中更有用。

连接客户端和服务器跟踪

OpenTelemetry 最强大的功能之一是能够跨不同服务和环境连接跟踪。 Highlight 的 SDK 通过上下文传播促进了这一点,允许您创建从浏览器中的用户交互一直到后端服务的端到端跟踪。

其工作原理如下:

  1. 跟踪启动:当在浏览器中发起用户交互或网络请求时,SDK 会创建一个新的跨度或继续现有的跟踪。
  2. 标头注入:对于传出 HTTP 请求(XHR 和 Fetch),SDK 会自动将跟踪上下文标头注入到请求中。这些标头通常包括:
    • traceparent:包含跟踪 ID、父跨度 ID 和跟踪标志。
    • Tracestate:携带特定于供应商的跟踪信息。
  3. 服务器端接收:突出显示服务器端 SDK 提取这些标头并继续跟踪,将服务器端跨度链接到客户端跟踪。
  4. 跟踪完成:当请求完成并返回到客户端时,完整的跟踪(包括客户端和服务器跨度)可以在Highlight的UI中可视化。

客户端和服务器跟踪之间的这种连接提供了端到端的可见性,并且是页面速度洞察和客户端/服务器错误关联所需的链接。

对于服务器端渲染,其中代码在 HTML 发送到浏览器之前在服务器上执行,跟踪上下文通过 传播。添加到 HTML 的标签。

客户端-服务器跟踪生命周期示例

这是一个实际运作方式的简化示例:

  1. 用户单击您的 Web 应用程序中的按钮。
  2. Highlight SDK 会为此用户交互创建一个跨度。
  3. 此交互会触发对您后端的 API 调用。
  4. SDK 自动将跟踪标头注入此 API 调用中。
  5. 您的后端接收请求,提取跟踪上下文,并继续跟踪。
  6. 后端处理请求并发送响应。
  7. 客户端收到响应并完成跨度。

结果是显示请求的完整旅程的单个跟踪,从浏览器中的初始用户交互,通过后端服务,再返回到客户端。

好处

客户端和服务器跟踪之间的这种连接提供了几个好处:

  • End-to-End Visibility: You can trace a user's action all the way through your system, making it easier to diagnose issues and understand performance bottlenecks.
  • Performance Optimization: By seeing the complete picture, you can identify whether performance issues are occurring on the client-side, server-side, or in the network communication between them.
  • Error Correlation: If an error occurs, you can see the full context of what led to that error, including any relevant client-side actions or server-side processing.

By leveraging Highlight's OpenTelemetry integration, you can gain these insights with minimal configuration, allowing you to focus on improving your application's performance and user experience.

Handling Server-Initiated Traces

When a request for a page is made by fetching a new URL in the browser we don't have the JS SDK initialized in the browser until the server returns the HTML and renders the page, then fetches all the JS assets to render the app. In this case you pass the trace ID from the server to the client in a tag to handoff the trace initiated on the server to the client.

Here is an example of what the meta tag looks like in the browser:

<meta
  name="traceparent"
  content="00-ab42124a3c573678d4d8b21ba52df3bf-d21f7bc17caa5aba-01"
>
登录后复制

Note that the Highlight server SDKs often have helpers to create this tag. Here's an example using the Highlight Ruby SDK

<%= highlight_traceparent_meta %>
登录后复制

The browser OTeL instrumentation gathers timing information from window.performance.timing and creates spans for all the different timing events in the browser. This instrumentation parses the tag and associates all the spans it creates with trace data from the tag. This is illustrated in the screenshot of the flame graph below.

Monitoring Browser Applications with OpenTelemetry

Here's how to parse what's going on in this flame graph:

  1. The documentLoad span shows the full timing from submitting the URL in the browser to be loaded to having all the assets loaded and the page being fully interactive. The timing data for this span is gathered from window.performance.timing since we can't actually initiate a span before the JS loads.
  2. The PagesController#home is the first span created on the server. You can trace the server code execution required to render the HTML that will be returned to the browser. When the HTML is returned to the browser the documentFetch span finishes.
  3. After the HTML is loaded int he browser you can see the requests for the page's resources (all the JS and CSS files), these are the resourceFetch spans.

These resource timings provide a full picture of your app's load time, making it clear where the opportunities are to improve performance and provide a better UX.

Leveraging OpenTelemetry Data in Highlight

Collecting OpenTelemetry data is one thing, but gleaning actionable insights is another. You need some way of visualizing the data (like the flame graph shown above) in order to get actionable insights. Highlight exposes this data a few ways.

Viewing Traces in Highlight

When you open Highlight's UI, you'll find a dedicated section for traces. Here, you can see a list of all the traces collected from your application, including those that span from the browser to your backend services.

  1. Trace List: This view provides an overview of all traces, typically sorted by timestamp. You can filter and search for specific traces based on various criteria such as duration, error status, or custom attributes.

  2. Trace Detail View: Clicking on a specific trace opens a detailed view, showing the full journey of a request or user interaction. This view includes:

    • A flame graph visualization of the trace, showing the hierarchy and timing of spans.
    • Detailed information about each span, including start time, duration, and any custom attributes or events.
    • For spans representing network requests, you can see details like HTTP method, status code, and headers.
  3. Cross-Service Tracing: For traces that span from the browser to your backend services, you'll see a seamless view of the entire request lifecycle. This makes it easy to identify whether performance issues are occurring on the client-side, server-side, or in the network communication between them.

Analyzing Resource Timings and Web Vitals

Highlight's metrics product provides powerful tools for analyzing resource timings and Web Vitals, which are crucial for understanding and optimizing your application's performance.

  1. 资源计时仪表板:此仪表板概述了在网页上加载各种资源所需的时间。您可以看到:

    • 不同类型资源(JS、CSS、图像等)的加载时间
    • 每种资源花费在 DNS 查找、TCP 连接、TLS 协商等方面的时间
    • 可视化有助于识别加载过程中加载缓慢的资源或瓶颈
  2. Web Vitals 指标:突出显示跟踪并显示关键的 Web Vitals 指标,包括:

    • 最大内容绘制 (LCP):衡量加载性能
    • 首次输入延迟 (FID):测量交互性
    • 累积布局偏移 (CLS):测量视觉稳定性
    • 首次内容绘制 (FCP):测量从导航到浏览器呈现第一位内容的时间
  3. 性能趋势:突出显示可让您随着时间的推移跟踪这些指标,帮助您识别:

    • 代码更改或部署对性能的影响
    • 可能会被忽视的逐渐退化
    • 优化工作带来的改进
  4. 细分和过滤:您可以根据各种因素细分和过滤这些指标,例如:

    • 设备类型(移动设备、桌面设备、平板电脑)
    • 浏览器
    • 地理位置
    • 您定义的自定义属性

通过将详细的跟踪数据与这些高级性能指标相结合,您可以全面了解应用程序的性能。这使您能够快速识别问题、了解其根本原因并衡量优化工作的影响。

结论

OpenTelemetry 提供了用于监控和优化浏览器应用程序的强大工具。通过利用 Highlight 的 OpenTelemetry 集成,开发人员可以通过最少的配置收集可操作的见解。

无论您是在处理客户端性能问题、服务器端瓶颈,还是跨多个服务的复杂用户旅程,OpenTelemetry 和 Highlight 都能为您提供交付卓越 Web 应用程序所需的可见性。

以上是使用 OpenTelemetry 监控浏览器应用程序的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1673
14
CakePHP 教程
1429
52
Laravel 教程
1333
25
PHP教程
1278
29
C# 教程
1257
24
Python vs. JavaScript:学习曲线和易用性 Python vs. JavaScript:学习曲线和易用性 Apr 16, 2025 am 12:12 AM

Python更适合初学者,学习曲线平缓,语法简洁;JavaScript适合前端开发,学习曲线较陡,语法灵活。1.Python语法直观,适用于数据科学和后端开发。2.JavaScript灵活,广泛用于前端和服务器端编程。

JavaScript和Web:核心功能和用例 JavaScript和Web:核心功能和用例 Apr 18, 2025 am 12:19 AM

JavaScript在Web开发中的主要用途包括客户端交互、表单验证和异步通信。1)通过DOM操作实现动态内容更新和用户交互;2)在用户提交数据前进行客户端验证,提高用户体验;3)通过AJAX技术实现与服务器的无刷新通信。

JavaScript在行动中:现实世界中的示例和项目 JavaScript在行动中:现实世界中的示例和项目 Apr 19, 2025 am 12:13 AM

JavaScript在现实世界中的应用包括前端和后端开发。1)通过构建TODO列表应用展示前端应用,涉及DOM操作和事件处理。2)通过Node.js和Express构建RESTfulAPI展示后端应用。

了解JavaScript引擎:实施详细信息 了解JavaScript引擎:实施详细信息 Apr 17, 2025 am 12:05 AM

理解JavaScript引擎内部工作原理对开发者重要,因为它能帮助编写更高效的代码并理解性能瓶颈和优化策略。1)引擎的工作流程包括解析、编译和执行三个阶段;2)执行过程中,引擎会进行动态优化,如内联缓存和隐藏类;3)最佳实践包括避免全局变量、优化循环、使用const和let,以及避免过度使用闭包。

Python vs. JavaScript:社区,图书馆和资源 Python vs. JavaScript:社区,图书馆和资源 Apr 15, 2025 am 12:16 AM

Python和JavaScript在社区、库和资源方面的对比各有优劣。1)Python社区友好,适合初学者,但前端开发资源不如JavaScript丰富。2)Python在数据科学和机器学习库方面强大,JavaScript则在前端开发库和框架上更胜一筹。3)两者的学习资源都丰富,但Python适合从官方文档开始,JavaScript则以MDNWebDocs为佳。选择应基于项目需求和个人兴趣。

Python vs. JavaScript:开发环境和工具 Python vs. JavaScript:开发环境和工具 Apr 26, 2025 am 12:09 AM

Python和JavaScript在开发环境上的选择都很重要。1)Python的开发环境包括PyCharm、JupyterNotebook和Anaconda,适合数据科学和快速原型开发。2)JavaScript的开发环境包括Node.js、VSCode和Webpack,适用于前端和后端开发。根据项目需求选择合适的工具可以提高开发效率和项目成功率。

C/C在JavaScript口译员和编译器中的作用 C/C在JavaScript口译员和编译器中的作用 Apr 20, 2025 am 12:01 AM

C和C 在JavaScript引擎中扮演了至关重要的角色,主要用于实现解释器和JIT编译器。 1)C 用于解析JavaScript源码并生成抽象语法树。 2)C 负责生成和执行字节码。 3)C 实现JIT编译器,在运行时优化和编译热点代码,显着提高JavaScript的执行效率。

Python vs. JavaScript:比较用例和应用程序 Python vs. JavaScript:比较用例和应用程序 Apr 21, 2025 am 12:01 AM

Python更适合数据科学和自动化,JavaScript更适合前端和全栈开发。1.Python在数据科学和机器学习中表现出色,使用NumPy、Pandas等库进行数据处理和建模。2.Python在自动化和脚本编写方面简洁高效。3.JavaScript在前端开发中不可或缺,用于构建动态网页和单页面应用。4.JavaScript通过Node.js在后端开发中发挥作用,支持全栈开发。

See all articles