内存检测 - instrument介绍

内存检测 - instrument

关于Instruments

instruments 是一个强大的、灵活的 性能分析 和测试工具的Xcode工具集的一部分。它的目的是帮助您配置您的OS X和iOS应用程序、进程和设备,以便更好地理解和优化他们的行为和性能。从应用程序开发过程的开始将工具集成到您的工作流中,可以帮助您在开发周期的早期找到问题。

在Instruments中,您使用专门的工具,来跟踪应用程序、进程和设备的不同方面。Instruments收集数据作为它的概况,并向您详细介绍分析结果。

与其他性能和调试工具不同,Instruments允许您收集大量不同类型的数据,并排查看它们。这使得识别可能被忽略的趋势变得更容易。例如,应用程序可能会显示由于多个开放网络连接而导致的大量内存增长。通过使用Allocations 和Connections工具一起,您可以识别不关闭的连接,从而导致快速的内存增长原因。

通过有效地使用Instruments,您可以:

  • 检查一个或多个应用程序或进程的行为
  • 检查设备特定的功能,如Wi-Fi和蓝牙
  • 在模拟器或物理设备上执行概要分析
  • 创建自定义的DTrace仪器分析方面的系统和应用程序的行为
  • 追踪问题在你的源代码
  • 对应用程序进行性能分析
  • 查找应用程序中的内存问题,如泄漏、废弃内存和僵尸
  • 找出优化你的应用程序以提高电源效率的方法
  • 执行一般系统级故障排除
  • 将工具配置保存为模板

虽然它是嵌入在可使用Xcode,但Instruments是一个单独的应用程序,如果需要的话可以使用独立。

The Instruments Workflow

Instruments看起来是一个复杂的app,的确如此,因为它能够用来收集app的各种有用的信息,帮助你诊断和解决问题。然而,从整体上看,instrument的工作流程相对比较简单。如图:

img

概括来说,使用Instruments一般包括以下几步:

  1. 创建一个包含想要使用的工具和环境的记录文档。
  2. 指定一个设备和一个要监测的app。
  3. 监测app。
  4. 分析获取到的数据。
  5. 修复源码中的问题。
了解何时使用Instruments

使用Xcode测试app时,先不启用Instruments,先打开debug navigator(左上角按钮第六个)。这里提供了一些抽象程度较高的信息,这些信息和app的CPU、内存、电量使用等有关系。通常,debug navigator提供的信息是让开发者快速的改善性能和解决一般性问题。当开发者需要更加详细的分析数据的时候,Instruments派上用场了。

img

About This Document 文档结构说明

为了方便开发者快速找到想要的信息,这个文档按照Instruments不同的关注点来组织。

  • 第一部分关注使用Instruments。包括创建一个document、找到一个你需要的具体的instrument工具、分析app、分析数据等等。在这里可以知道Instruments的一般典型操作。
  • 第二部分涵盖了更多具体领域的任务,比如提高性能、解决内存问题、提高电池使用时间等。
  • 最后一部分给出了个性化的instruments和模板的使用指南,还有一些其他的资源和参考。
Prerequisites 先决条件

Background Knowledge 你需要了解的知识

使用Instruments之前,你需要对Xcode的工作十分了解,还有app开发的相关概念,比如building和running一个app,如何使用真机调试等。

你还需要熟悉你想要监测的参数的核心概念。比如你想要查看app的内存使用情况,那么你需要知道一些内存管理的知识,和内存可能潜在的问题(内存泄露、僵尸对象)。如果你想要查看app的性能问题,你需要了解CPU和线程使用。如果你想解决电池使用过快的问题,你需要了解各种影响电池使用的事情,比如屏幕亮度,网络接口(GPS、蓝牙、Wi-Fi)使用,和timer的使用。

这个文档中会提及一些背景知识。外部有些资源可以查看:

  • 相关文档(请查看原文超连接)
  • WWDC Videos
System Requirements 系统要求

Instruments安装在Xcode里。如果你还没有安装Xcode,先下载安装。

如果你打算在真机上分析app,需要provision你的设备。参考Lauching Your App on Decvices(请查看原文连接)。

注意:

Instruments在Xcode3.0以后、OS X v10.5以后可用。

Instruments可以分析iOS6以后的设备。

2. About Memory Analysis

无论什么时候APP创建对象,系统都会为对象分配内存。惯例上,APP将跟踪这些对象,并且在不需要的时候释放它们以至于能够为其它对象重新分配内存。ARC将使得内存事情变得非常简单,将由系统负责内存管理。随着ARC的出现,系统将监控对象,使对象在合适的时候分配和释放,APP将几乎不需要做什么工作。不管内存如何管理,但是,即使是最好的APP在某些情况下也可能遇到难以定位的内存问题。

Instruments能够帮助绘制APP是怎样使用内存的图片,Instruments更容易识别潜在的问题区域。它甚至能够自动检测内存问题的类型并且进行标记,方便我们的分析。使用Instruments来监测和跟踪如下情况:

Overall Memory Use:监控APP如何使用内存并且与系统中其他活跃进程使用内存进行比较。寻找内存使用大的或者意想不到的内存增长。可以看Monitor Memory Usage.

Leaked Memory:在某个点内存已经被分配,但是一直被释放掉并且不再被APP引用。正是因为并没有对象引用该区域内存,因此无法释放此处内存,该内存也不能够被再一次使用。例如:假定我们编写一个APP,在绘制的时候创建了一个矩形对象,但是当绘制结束却从未释放该对象。在这种情况下:我们的APP可能泄露越来越多的内存。为了解决泄露,我们需要知道哪一个对象并没有被释放掉,这时修改APP在适合的适合释放该对象。 Find Memory Leaks

Abandoned Memory:由于某些原因内存被分配了,但是它并不需要和被引用。例如:假设我们的APP中在图片被缓存之后又添加了图片(images)到缓存,使用了两倍的内存存储相同的images。或者,APP中拥有一个包含对象的数组,为了能够在需要的时候访问,但是事实上你从未使用。不像内存泄露,被遗弃的内存(abandoned memory)好像仍然在APP中的某个地方引用。它仅仅是没有目的性,因为在技术上是合理的,所以对于nstruments来说,Abandoned Memory更难识别,需要更多的检测工作。Find Abandoned Memory.

Zombies:这是内存已经被释放并且不再需要了,但是在代码中仍然有某个地方引用了它。例如:假设你的APP中包含了图片缓存,一旦缓存被清理,在APP中不能够尝试着引用不存在的对象。对于这些不存在的对象,我们称之为僵尸对象(zombies) 。Find Zombies.

由于内存问题能够变得非常困难被发现,为了寻找意想不到或者无限制的内存增长,可以对APP的不同阶段进行周期性快拍(snapshots )。我们能够对比快拍来确定对象是怎样被分配和释放,和内存的超时使用。

通过持续执行一系列重复的用户行为,导致APP进入新的状态和回到最初状态,对于测试APP非常重要。这种类型的搜索称为generational analysis,每次APP进入新的状态,“generation”对象将被创建。在大多数情况下,当APP回到最初状态的时候,你应该期待这些对象被释放掉。例如:如果APP打开了Window,当Window被关闭的时候,任意的Window对象都应该被释放掉。如果这些对象没有被释放掉,这是可能会发生内存问题。当执行generational analysis,初始化generationa可能呈现比我们预期的情况产生更大的内存,由于缓存和初始化过程。因此,需要多次重复用户动作,为了获得真实的APP体验感觉。对于更深层次的内存问题,可以看Advanced Memory Management Programming Guide.

Monitor Memory Usage

使用Activity Monitor性能模板来跟踪APP真实和虚拟内存,并且可以对照运行在系统上的其它的进程的。

创建跟踪文件监控整体内存使用情况

1:Launch Instruments. 启动Instruments

2:In the profiling template selection dialog that appears, click Activity Monitor. 选择Activity Monitor

3:Choose your device and app from the target device and process lists. 选择设备和APP

4:Click Choose to create a trace document. 创建跟踪文件

Activity Monitor 性能分析模板包括Activity Monitor 工具,并且预先被配置显示系统加载和虚拟内存的大小。我们能够使用其他内存相关的统计来并使用监控面板来查看。

显示监控的内存统计

1:Press Command-1 to display the record settings area of the inspector pane (if it’s not already opened). 如果录制设置部分没有打开,可以使用 Command-1 进行打开

2:In the statistics to list area of the inspector, select the memory-related statistics you want to see.The following memory-related statistics are available for display:在监控面板区域的statistics to list 可以选择与内存相关的统计选项,下面是可以获取的显示:

**Physical Memory Wired —**The amount of memory being used by the operating system 操作系统所使用的内存情况

Physical Memory Active —The amount of memory being used by processes other than the operating system. 对比操作系统其它进程使用的内存数量

Physical Memory Inactive—The amount of memory that was recently released 最近释放的内存数量

Physical Memory Used—The total amount of memory that was used while performing the sample 当执行采样时,全部使用的内存数量

Physical Memory Free—The amount of available memory. 可获得的内存数量

Total VM Size—The total size of virtual memory. 虚拟内存的全部数量

VM Page In Bytes—The amount of memory calls sent to RAM. 内存调用发送到RAM的数量

VM Page Out Bytes—The amount of memory calls sent to disk. 内存调用发送到磁盘的数量

VM Swap Used—The amount of memory called from disk. 磁盘内存被调用的梳理

3:In the system statistics area, select the memory-related statistics you want to see. If desired, click the shape or color controls to change how the statistics are represented in the timeline. 在系统统计区域,选择我们想统计的与内存相关的选项,点击shape或Color可以改变在timeline的统计显示

为了获取整体内存的使用详情

1:Click the Record button (image: ../Art/inline_record_button_2x.png) in the toolbar or press Command-R to begin recording. 点击录制按钮开始记录

2:Use your app normally. 正常使用APP

​ As the Activity Monitor instrument profiles your app, it performs samples at periodic intervals and displays the results in the timeline and detail panes.

3:Click the Stop button (image: ../Art/inline_stop_button_2x.png) in the toolbar or press Command-R again when you’re ready to stop recording. 停止记录

建议:当APP配置后,选择在timeline中插入旗帜(flag)来指示这里有执行具体的任务。例如:在创建新的document之前添加flag,当我们探索分析获取的数据,可以直接到flag去进行分析。

时间面板(timeline pane)提供了图示显示收集的数据。滚动timeline能够理解APP和其他运行系统上进程是如何使用内存的。对于跟多详情,可以进一步理解详情面板。

To view summarized memory usage by sample

1:Click the Activity Monitor track in the timeline. 在timeline点击Activity Monitor进行跟踪

2:Choose Samples from the detail type list in the navigation bar of the detail pane. 选择采样

To view detailed memory usage by a process at a specific point during profiling

1:Click the Activity Monitor track in the timeline 在timeline点击Activity Monitor进行跟踪

2:Press Command-2 to display the display settings area of the inspector pane (if it’s not already opened). 打开详情面板

3:In the process detail tracking area of the inspector, click “Track inspection head” to configure the detail pane to display results based on the currently selected area in the timeline, rather than the latest sample performed. 在监控面板中的过程详情跟踪,点击“Track inspection head”来配置详情面板,基于当前选中的timeline,而不是最新执行的采样

4:Select a point in the timeline to view the statistics that were captured at that time. 选中timeline中的点查看数据

5:View memory usage statistics in the detail pane. 在详情面板看内存使用统计

Use the detail type list to toggle between a summary view of the collected data by process or a parent child view.

Find Abandoned Memory

Allocations 性能模板使用Allocation和VM Tracker工具来测量APP整体和虚拟内存的使用。但是,为了查找未使用的内存(abandoned memory)即内存已经被分配但是并没有使用,应该集中于Allocations工具,该工具测量堆内存的使用并且追踪分配,包括由类分配的具体对象。

由于未使用的内存是仍然在APP的某个地方被引用,所以Instrument不能够确定它的重要性。为了找到该部分未使用的内存,使用generational analysis确保在重复执行相同的操作时,内存不会持续增长。例如:结束和开始一个新的游戏,打开和关闭Window,创建和删除联系,所有的操作都未设置优先权,得到稳定的内存状态。大量的循环操作不应该导致未知的内存增加。Instrument能够帮助我们找到相关内存增长对应的具体对象,以至于我们能够释放这些对象减低APP的内存足迹。

To look for abandoned memory in your app

1:Launch Instruments. 启动Launch Instruments.

2:In the profiling template selection dialog that appears, click Allocations. 在出现的性能分析框中选中 Allocations

3:Choose your device and app from the target device and process lists. 选中设备和APP

4:Click Choose to create a trace document. 点击Choose创建trace document

5:Click the Allocations instrument in the timeline pane. 在时间面板中点击Allocations工具

6:Press Command-2 to show the display settings area of the inspector pane.This gives you quick access to the Mark Generation button, which you use to flag different generations of actions as you profile your app. 点击 Command-2 显示设置区域,或者直接在最右侧面板中点击中间齿轮⚙按钮,这里我们可以访问Mark Generation按钮,可以用来标记不同的 generations

7:Click the Record button in the toolbar (or press Command-R) to begin recording. 点击录制按钮开始记录

8:Perform a short sequence of repeatable actions in your app.In order to accurately generate trends, this should be a set of actions that starts and finishes with the app in the same state. 在APP中执行一系列重复的行为,来获取准确的数据

9:Click the Mark Generation button in the inspector pane. 点击右侧监督面板中,设置区域下的Mark Generation按钮,实现快照。快照成功显示在详情面板

​ A flag appears in the track pane to identify the generation. 一个红旗标志将显示在轨道上标识generation

​ A list of generations you’ve marked is shown in the detail pane. Each generation includes a list of allocations that has occurred since the previous generation.

​ 详情面板会显示一系列的generation,每一个generation都包括从之前generation开始的一系列分配。

​ You can also mark generations after you’re done recording by dragging the inspection head in the track pane’s timeline to the desired location and clicking Mark Generation. 我们也可以在录制结束之后 mark generations,通过拖动检测头到时间轴面板中需要的位置并进行Mark Generation。

10:Perform steps 8 and 9 several times while monitoring the detail pane until you see whether memory is growing without limit. 当监控详情面板执行步骤8和9几次一直到你看到内存是否无限制地增长。

11:Click the Stop button in the toolbar (or press Command-R again) when you’re ready to stop recording. 点击停止按钮停止记录

12:Scan through the generations in the detail pane and find one that seems representative of repeated memory growth. 扫描详情面板的generations 并找到一个反复内存增长的

​ The Growth and # Persistent columns tell you how much additional memory and how many allocations have occurred since the previous generation. If your app returns to its original state after an operation, you shouldn’t expect there to be growth from generation to generation. Growth 和 # Persistent 专栏将告诉我们对比之前的快照又增加了多少内存,又有多少被分配。如果在执行一次操作之后返回了最初的状态,那么这里将不会有内存的增长。

13:Click the disclosure triangle of a generation to display new objects that have been allocated since the prior generation. 点击三角符号来显示已经被分配的新对象

14:Look for objects that are persisting. If you identify one, click the disclosure triangle to display its instances. 寻找持久化的对象,如果确认了,点击三角显示对应的实例

15:Select an object instance. 选择对象实例

16:Press Command-3 to display a stack trace for the selected instance in the extended detail area of the inspector. 按压Command-3在监督的扩展面板区域中显示选中实例的轨道踪迹

This stack trace provides the complete list of method calls responsible allocating the instance. 该轨道跟踪提供了完整的创建实例的方法列表

17:Click the Collapse button in the extended detail area to hide system calls in the stack trace. This makes it easier to locate your app’s methods.

Calls made by your app are colored black and preceded by a user code icon 点击按钮隐藏系统在轨道踪迹中的的调用,这样更加容易定位APP的方法

18:Double-click a method in the stack trace to display its code in Instruments. 双击在堆栈轨道中现实的方法

19:Click the Xcode button at the top of the detail pane to open the code in Xcode for editing. 点击顶部的Xcode按钮打开工程进行编辑

20:Determine whether the allocation is useful. If it’s not, it’s abandoned memory that needs to be resolved. 确定分配是否有用,如果没有用,那么背抛弃的内存需要被处理

Find Memory Leaks

Leaks分析模板使用了Allocations和Leaks工具来测量APP整体的内存使用并检查泄露,泄漏即内存已分配给对象但是对象不在被引用和获取。

注意:如果Instruments有权利获取APP中的源码信息,那么内存泄露会通过类名报道。否则,内存泄露将使用内存地址显示对应的部分,比如:Malloc-size.为了确保Instruments有权利获取APP代码信息,在Xcode中初始化profiling(seeProfile from Xcode) 或者 配置符号映射文件(configure a symbol mappings file see Map Data to Source Code).

To look for memory leaks

1:Launch Instruments.

2:In the profiling template selection dialog that appears, click Leaks.

3:Choose your device and app from the target device and process lists.

4:Click Choose to create a trace document.

5:Click the Record button (image: ../Art/inline_record_button_2x.png) in the toolbar (or press Command-R) to begin recording.

6:Use your app normally.

7:Watch the Leaks instrument in the timeline pane for leaks. A leak appears as a red bar. 在时间轴面板观察Leaks工具,如果有内存泄露将会显示红色小状态图形

8:Click the Leaks instrument in the timeline pane to display leak-related information in the detail pane. 点击 Leaks instrument,在详情面板查看相关信息

9:Choose Call Tree from the detail type list in the navigation bar of the detail pane. 在详情面板的高航条中选择 Call Tree

​ A list of method calls related to any detected leaks is displayed. 容易和内存泄露被检测到都会显示在详情面板中

10:Press Command-2 to show the display settings area of the inspector pane.

11:Under the Call Tree display settings, select Invert Call Tree and Hide System Libraries. 在Call Tree显示设置区域,选中 Invert Call Tree 和 Hide System Libraries

The most recent method calls are shown first. It also helps narrow down the list of method calls to ones made by your app. Method calls made by your app are colored black and preceded by a user code icon (image: ../Art/inline_usercode_icon_2x.png). 最近的方法调用将首先显示。它还有助于缩小的列表方法调用的应用程序

12:In the call tree, select a method call you want to investigate. 在 call tree中,选择我们想调查的方法

13:Press Command-3 to display a stack trace for the selected method call in the extended detail area of the inspector. Command-3在监督区域的扩展面板中查看选中方法的轨道踪迹

14:Double-click the method call in the stack trace to display its code in Instruments. 双击方法定位代码

15:Click the Xcode button (image: ../Art/inline_xcode_2x.png) at the top of the detail pane to open the code in Xcode for review and editing. 点击顶部的Xcode按钮调到对应代码部分进行编辑和修改。

在打开Xcode查看代码所创建的内存泄露,内存泄露可能仍然不清楚,即不是那么明显,Leaks instrument允许我们使用详情面板中的 Cycles & Roots查看循环泄露

To investigate a leaked object using cycles and roots 使用cycles and roots 查找泄露对象

1:Click the Leaks instrument in the timeline pane to display leak-related information in the detail pane. 点击Leaks instrument 显示相关的内存泄露信息

2:Choose Cycles & Roots from the detail type list in the navigation bar of the detail pane. 选择详情面板导航条上的Cycles & Roots

A list of leaked objects by cycle is displayed. 可以看到循环引用的对象,下图是两个对象发生循环引用之后显示的图表:

3:Select an object you want to investigate. 选择想要调查的对象

4:If available, view the object graph for the object. 如果可以,查看对象的图表

5:Click the focus arrow (image: ../Art/icon_detail_pane_gotoarrow_2x.png) next to the object to display the memory history of the object in the detail pane, along with corresponding reference counts and method calls.

6:Press Command-3 to display a stack trace for the selected object in the extended detail area of the inspector.

7:Click the Collapse button (image: ../Art/inline_hidesystemcalls_button_2x.png) in the extended detail area to hide system calls in the stack trace. This makes it easier to locate your app’s methods.

8:Double-click a method in the stack trace to display its code in Instruments.

9:Click the Xcode button (image: ../Art/inline_xcode_2x.png) at the top of the detail pane to open the code in Xcode for review and editing.


To investigate a leak using the call tree 使用call tree查找泄露

1:Click the Leaks instrument in the timeline pane to display leak-related information in the detail pane.

2:Select Call Tree from the detail type list in the navigation bar of the detail pane.A list of method calls related to any detected leaks is displayed.

3:Press Command-2 to show the display settings area in the inspector pane.

4:Under the Call Tree display settings, select Invert Call Tree and Hide System Libraries.

The most recent method calls are shown first. It also helps narrow down the list of method calls to ones made by your app. Method calls made by your app are colored black and preceded by a user code icon

5:Select a method call you want to investigate.

6:Press Command-3 to display a stack trace for the selected method call in the extended detail area of the inspector.

7:Double-click the method call in the stack trace to display its code in Instruments.

8:Click the Xcode button at the top of the detail pane to open the code in Xcode for review and editing.

虽然Instruments能够帮助我们检测内存泄露,但是我们仍然需要仔细查看相关的内存历史和代码来确定并解决问题,下面是一些常常导致泄露的情形:

1:Retain has been called on an object without a corresponding release call when the object is no longer referenced. 循环是当一个对象不再被引用的时候,被没有进行对应的释放

2:An object has been allocated and initialized with APIs that don’t cause the object to autorelease. 对象有被创建和初始化但是并没有被释放

3:If a leak isn’t an object, you may be calling an API that assumes ownership of a malloc-created memory block, and you are missing a corresponding call to free().

Find Zombies

Zombies性能分析模板使用Allocations工具测量APP的内存使用,集中监测“zombie”对象,该对象是对象被调用,但是该对象已经被释放之后并不存在。

在这个模板中,Allocations工具已经配置了能够检测 NSZombie检测。这是调试模型(debug mode),需要设置环境变量 NSZombieEnabled 为true,指引编译器使用任意对象替代NSZombie类型的对象,该NSZombie类型的对象已经被释放并且引用计数为0(即对象不存在)。如果向这些已经被释放的对象发送信息,这些对象将被标记,当APP崩溃,记录停止,Zombie信息对话显示。我们能够检测Zombie对象的历史retain和release,确定问题发生的准确位置。

**注意:**Zombies模板由于改变了环境,以至于应该被释放的对象并没有真正的被释放,所以导致内存持续增长。这是预期的行为,这也意味除了标志zombie和内存地址,其它由Allocation工具提供的数据应该被忽略,我们不应该添加Leaks工具到Zombies模板,这将导致不准确的结果,对于iOS APP,使用Zombies模板需要使用iOS模拟器而不是真机。

To look for zombies in your app

1:Launch Instruments.

2:In the profiling template selection dialog that appears, click Zombies.

3:Choose your device and app from the target device and process lists.

4:Click Choose to create a trace document.

5:Click the Record button (image: ../Art/inline_record_button_2x.png) in the toolbar (or press Command-R) to begin recording.

6:Use your app normally.

如果调用了已经被释放的对象,那么将会在时间轴面板中插入标记,并且显示 Zombie 信息对话框,在具体内存地址显示信息被发送给了已经释放的对象。

如果关闭Zombie 信息对话框,可以点击标识能够重新打开。

7:Click the focus arrow (image: ../Art/icon_detail_pane_gotoarrow_2x.png) next to the zombie’s memory address to display the memory history of the zombie object in the detail pane, along with corresponding reference counts and method calls.

8:Select the Zombie event (or any other event you want to investigate) in the detail pane.

9:Press Command-3 to display a stack trace for the selected event in the extended detail area of the inspector.

10:Click the Collapse button (image: ../Art/inline_hidesystemcalls_button_2x.png) in the extended detail area to hide system calls in the stack trace. This makes it easier to locate your app’s methods.Calls made by your app are colored black and preceded by a user code icon (image: ../Art/inline_usercode_icon_2x.png).

11:Double-click a method in the stack trace to display its code in Instruments.

12:Click the Xcode button (image: ../Art/inline_xcode_2x.png) at the top of the detail pane to open the code in Xcode for editing.

虽然Instruments能够帮助我们检测zombies,,但是我们仍然需要仔细查看相关的内存历史和代码来确定并解决问题,下面是一些常常导致泄露的情形:

1:An object has already been released (or autoreleased), and your app tries to release it again. 对象已经被释放掉,APP尝试再release

2:An object hasn’t been retained when it should have been. 当对象需要被retained的时候并没有被retained

3:Some other call is made to an object after it has been released. 在对象释放之后,被再次使用

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
opentelemetry-javaagent.jar 是一个用于自动化和 Java 应用程序的分布式跟踪的工具。我们可以使用自定义 instrument 来扩展其功能。 自定义 instrument 可以帮助我们实现一些自定义的行为,例如,我们可以在代码中插入额外的标记信息,或者在特定的函数或方法中加入额外的追踪逻辑。 要实现自定义 instrument,我们需要进行以下步骤: 1. 创建一个 Java 类,并继承 `OtelInstrumenter` 类。这是一个由 OpenTelemetry 提供的接口,用于定义自定义 instrument 的行为。 2. 在该类中,我们需要实现 `applyInstrumentation` 方法。该方法会被调用来应用自定义的 instrument 到目标应用程序中。 3. 在 `applyInstrumentation` 方法中,我们可以使用 OpenTelemetry 提供的 API 来修改目标应用程序的代码,例如,在特定的函数或方法调用前后插入追踪代码。 4. 编译并打包自定义 instrument 的代码,并将其作为 `-javaagent` 参数传递给 `opentelemetry-javaagent.jar`。当目标应用程序启动时,这个自定义 instrument 会被加载和应用。 通过使用自定义 instrument,我们可以根据自己的需求对目标应用程序的代码进行修改和增强。这样,我们就能够更好地实现跟踪和监控,并获得更加详细和准确的跟踪数据。 总结起来,opentelemetry-javaagent.jar 提供了一种灵活和可扩展的方式来实现自定义 instrument。我们可以通过创建自定义 instrument 类,并在其中实现特定的逻辑来修改目标应用程序的代码,从而实现更精确和详细的分布式跟踪。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值