安排演示

iOS 移动应用监控


Applications Manager iOS 移动 APM 让您能够追踪实际终端用户设备上本地移动应用的性能。例如,新闻阅读应用可能会内部执行以下操作:

  • 导航到表格视图以显示文章列表
  • 使用 REST API 调用加载文章列表
  • 将文章列表缓存到 SQLite 数据库中
  • 下载每篇文章的缩略图
  • 将缩略图缓存到文件系统
  • 构建复杂的 UI,比如自定义表格视图单元格样式

以上所有操作都是潜在的长时间运行操作,会影响用户体验,因此在各种设备上对其进行基准测试和优化非常重要。Applications Manager 移动 APM 通过在您的应用中嵌入以库形式存在的 APM 代理,收集并汇总来自全球所有用户的指标。

APM 代理通过事务和组件测量代码的执行时间。在上述示例中,从开始导航到渲染最终 UI 的整个操作序列可视为一个事务。各个操作可以分组到不同的组件类型中,如 HTTP、SQLite、文件系统、UI 等。简单操作可以仅用事务测量,复杂操作则可以使用带组件的事务进行测量。

浏览以下主题,了解更多关于配置 iOS 移动应用监控的信息:

使用 Swift Package Manager 集成 MEAPMInsight

按照以下步骤,使用 Swift Package Manager 从仓库集成 MEAPMInsight 包:

  1. Xcode.
  2. 中打开您的项目 从菜单栏选择文件 → 添加包
  3. 输入包仓库 URL: https://github.com/ManageEngine/MEAPMInsight
  4. 依赖规则 设置为 直到下一个主要版本。
  5. 选择要添加包的 应用目标
  6. 点击 添加包.

配置 MEAPMInsight

使用 Swift Package Manager 添加 MEAPMInsight 后,完成以下配置步骤:

1. 创建移动应用监控

  • 在 ManageEngine Applications Manager 中创建一个 移动应用监控
  • 下载生成的配置文件(apm_config.plist)。

2. 将配置文件添加到您的 Xcode 项目

  • 将下载的配置文件(apm_config.plist)拖放到 Xcode 项目根目录。
  • 在对话框中:
    • 设置 操作 设置为 复制文件到目标.
    • 选择所需的 应用目标.
  • 点击 完成.

3. 自签名证书(如适用)

如果您的 DEM Collector 使用自签名 SSL 证书: 

i. 将证书添加到 Xcode 项目:

  • 从 DEM Collector 安装目录中复制 apm_cert.cer /conf/sslcerts 并移动到打开 Xcode 项目的服务器。
  • apm_cert.cer 拖入 Xcode 项目根目录。
  • 在对话框中:
    • 设置 操作 设置为 复制文件到目标.
    • 选择所需的 应用目标.
  • 点击 完成.

这允许 MEAPMInsight SDK 安全地与 DEM Collector 通信。

ii. 添加应用传输安全异常

使用自签名证书时,必须为 DEM Collector 域名在 Info.plist 文件中允许例外。为此,打开 Info.plist 该文件并添加以下配置:

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSExceptionDomains</key>
  <dict>
    <key>[collectordomain].com</key>
    <dict>
      <key>NSExceptionAllowsInsecureHTTPLoads</key>
      <true/>
    </dict>
  </dict>
</dict>

替换 [collectordomain].com 为您的 DEM Collector 域名。

4. Initialize the SDK in AppDelegate class

Initialize the SDK in your AppDelegate class using the following code to start monitoring:

import MEAPMInsight
  
@main
class AppDelegate: UIApplicationDelegate {
  
	func application(
		_ application: UIApplication,
		didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
	) -> Bool {
		#if DEBUG
		APMInsight.setDebugLoggingEnabled(true)
		APMInsight.setEnvironment("debug")
		#endif
		APMInsight.startMonitoring() 
		// Below are Optional 
		APMInsight.enableErrorReporting() 
		// Pass array of URLs to ignore from HTTP tracking 
		APMInsight.enableHttpTracking([
			"https://api.example.com/analytics",
			"https://cdn.example.com"
		])
		return true
	}
 }

重要提示: 为确保崩溃报告正确符号化,设置 调试信息格式 设置为 为 DWARF 带 dSYM 文件 ,并确保此设置应用于发布构建,发布/发布应用前必须完成此操作。

开始一个事务

您通常应在长时间运行操作前启动事务,操作完成后停止事务。事务是线程安全的,可以在不同线程启动和停止。一个事务对象只能启动和停止一次。应用中同名事务会被平均处理。因此,当同一操作多次使用同一事务名称执行时,会记录其平均执行时间。

func listArticles() {

	let transaction = APMInsight.startTransaction(withName: "List Articles")

	// Perform long-running operation
	APMInsight.stopTransaction(transaction)

}

使用组件对事务内的操作进行分组 

您可以将事务内操作分组为不同类型的组件。可以使用预定义类型,如 APMHTTPComponent、APMSQLiteComponent、APMUIComponent 等,或指定自定义类型。组件是线程安全的,可以在不同线程中启动和停止。组件对象只能启动和停止一次,且组件不能在其父事务已停止后停止。一个事务中的多个组件可以重叠并行运行。事务中同名组件会做平均处理。

func listArticles() {
	let transaction = APMInsight.startTransaction(withName: "List Articles")
	let httpComponent = transaction?.startComponent(withType: APMHttpComponent)
	let articlesComponent = transaction?.startComponent(withType: "Download Articles")

	// Download articles
	transaction?.stopComponent(articlesComponent)
	for article in articles {
		let thumbnailComponent = transaction?.startComponent(withType: "Download Thumbnail")

		// Download thumbnail
		transaction?.stopComponent(thumbnailComponent)
	}

	transaction?.stopComponent(httpComponent)
	APMInsight.stopTransaction(transaction)
}

在上述示例中,HTTP 操作(下载文章和缩略图)的总耗时由 APMHttpComponent 测量,单独文章下载耗时由“Download Articles”测量。由于缩略图在循环中多次执行,其时间被平均并由“Download Thumbnail”记录。

刷新数据

有时需要手动刷新最近记录的数据。如果您在应用终止前记录事务且上传间隔较长(默认30秒),建议在适当时机手动刷新数据,以防应用在下次上传间隔前被终止。

APMInsight.flush()

自定义 API

自定义 API 用于设置用户 ID 的动态值、跟踪自定义事件、捕获屏幕等。本文档将解释各种可用的自定义 API 及其语法。以下是可用 API 列表:

  1. 初始化 SDK
  2. 自定义用户 ID
  3. 事务
  4. HTTP 调用
  5. 屏幕
  6. 环境
  7. 刷新
  8. 排除 HTTP 调用
  9. 排除屏幕

1. 初始化 SDK

您可以使用下面的 API 用自定义上传间隔初始化 SDK。默认上传间隔设置为 30 秒。

Swift 语法:

APMInsight.startMonitoring()

Objective-C 语法:

[APMInsight startMonitoring];

2. 自定义用户 ID 

默认情况下,SDK 会生成唯一的用户 ID。如果您想自定义用户 ID,可以使用以下语法。此功能在跟踪特定用户的指标或调试问题时非常有用。

注意: 不要向此函数传递任何个人身份信息(PII),如电子邮件地址、电话号码或姓名。仅使用匿名或非敏感标识符。

Swift 语法:

APMInsight.setUserId("user123")

Objective-C 语法:

[APMInsight setUserId:@"user123"];

3. 事务 

您可以使用下面的 API 跟踪具有特定时间的自定义事件。

Swift 语法:

let transaction = APMInsight.startTransaction(withName: "List Articles")
let httpComponent = transaction?.startComponent(withType: APMHttpComponent)
let articlesComponent = transaction?.startComponent(withType: "Download Articles") 
// Download articles 
transaction?.stopComponent(articlesComponent)
for article in articles {
	let thumbnailComponent = transaction?.startComponent(withType: "Download Thumbnail") 
	// Download thumbnail 
	transaction?.stopComponent(thumbnailComponent)
}
  
transaction?.stopComponent(httpComponent)
APMInsight.stopTransaction(transaction)

Objective-C 语法:

APMInsightTransaction *transaction = [APMInsight startTransactionWithName:@"List Articles"];
APMInsightComponent *httpComponent = [transaction startComponentWithType:APMHttpComponent];
  
APMInsightComponent *articlesComponent = [transaction startComponentWithType:@"Download Articles"]; 
// Download articles 
[transaction stopComponent:articlesComponent];
  
for (Article *article in articles) {
	APMInsightComponent *thumbnailComponent = [transaction startComponentWithType:@"Download Thumbnail"]; 
	// Download thumbnail 
	[transaction stopComponent:thumbnailComponent];
}
  
[transaction stopComponent:httpComponent];
[APMInsight stopTransaction:transaction];

4. HTTP 调用

默认情况下,使用以下网络库的网络请求会自动被捕获。

  • NSUrlSession
  • NSUrlConnection

如果您想添加更多 HTTP 调用,请使用以下 API:

Swift 语法:

/*!
Add a http call. This method is thread-safe.
@param name The http call name.
@param duration  The http call load time in milliseconds.
@param startTime  The http call start time in milliseconds.
@param respCode  The http call response code.
@param httpMethod  The http call method.
*/
APMInsight.addHttpCall("example.com", withTime: 30, startTime:1642750130310, 
respCode: 200, httpMethod: "GET")

Objective-C 语法:

/*!
Add a http call. This method is thread-safe.
@param name The http call name.
@param duration  The http call load time in milliseconds.
@param startTime  The http call start time in milliseconds.
@param respCode  The http call response code.
@param httpMethod  The http call method.
*/
[APMInsight addHttpCall:@"example.com" withTime:30 startTime: 1642750130310 
respCode:200 httpMethod:@"GET"];

5. 屏幕

默认情况下,会捕获所有屏幕;但是,如果您想添加遗漏的屏幕,请使用以下 API。

Swift 语法:

/*!
Add a screen. This method is thread-safe.
@param screen The screen name.
@param duration  The screen load time.
@param startTime  The screen start time. 
*/
APMInsight.addScreen("DetailScreen", withTime: 60, startTime: "1642750130310")

Objective-C 语法:

/*!
Add a screen. This method is thread-safe.
@param screen The screen name.
@param duration  The screen load time.
@param startTime  The screen start time.
*/
[APMInsight addScreen:@"DetailScreen" withTime:60 startTime:@"1642750130310"];

SwiftUI 视图跟踪

By default, SwiftUI views are not tracked automatically. To track SwiftUI views, you need to manually add the .trackScreen() modifier to your views.

SwiftUI 语法:

import SwiftUI
  
struct MainView: View {
	var body: some View {
		NavigationView {
			VStack {
				NavigationLink("Go to Detail", destination: DetailView())
			} 
			.trackScreen("MainView")
		}
	}
}
  
struct DetailView: View {
	var body: some View {
		VStack {
			Text("Detail Screen")
		} 
		.trackScreen("DetailView")
	}
}
 

注意:

  • The .trackScreen() modifier should be added to each SwiftUI view you want to track.
  • 屏幕名称参数应为描述性字符串,用于识别视图。

6. 环境

您可以使用以下 API 设置自定义环境详情,以便针对不同的环境配置(如开发、调试、生产或发布)筛选数据。

Swift 语法:

/*!
Sets the environment name for the APM agent.
@param environment A custom environment name (for example, "debug", "release", or "beta-release")
*/
APMInsight.setEnvironment("release")

Objective-C 语法:

/*!
Sets the environment name for the APM agent.
@param environment A custom environment name (for example, "debug", "release", or "beta-release")
*/
[APMInsight setEnvironment:@"release"];

7. 刷新 

您可以使用以下 API 立即上传记录的数据到 DEM Collector,而无需等待下次上传间隔。默认刷新间隔为 60 秒。

Swift 语法:

APMInsight.flush()

Objective-C 语法:

[APMInsight flush];

注意: 如果没有网络,则数据将保存在队列中等待下次上传。如果上传间隔设置较长,则可能需要手动刷新数据。

8. 排除 HTTP 调用 

您可以通过传递 URL 列表作为参数,排除自定义 URL 的跟踪。

Swift 语法:

APMInsight.enableHttpTracking(["example.com", "demo.com"])

Objective-C 语法:

[APMInsight enableHttpTracking:@[@"example.com", @"demo.com"]];

9. 排除屏幕 

您可以通过传递屏幕列表作为参数,排除自定义屏幕的跟踪。

Swift 语法:

APMInsight.ignoreScreens(["DetailScreen", "HomeScreen"])

Objective-C 语法:

[APMInsight ignoreScreens:@[@"DetailScreen", @"HomeScreen"]];

深受全球客户喜爱

“功能强大的卓越监控工具”

它允许我们跟踪响应时间、资源利用率、错误率和事务性能等关键指标。实时监控警报能够及时通知我们任何问题或异常,帮助我们迅速采取措施。

评论者角色:研发

carlos-rivero
“我喜欢 Applications Manager,因为它帮助我们检测服务器和 SQL 数据库中的问题。”
Carlos Rivero

Lexmark 技术支持经理

受全球数千家领先企业信赖