Applications Manager iOS 移动 APM 让您能够追踪实际终端用户设备上本地移动应用的性能。例如,新闻阅读应用可能会内部执行以下操作:
以上所有操作都是潜在的长时间运行操作,会影响用户体验,因此在各种设备上对其进行基准测试和优化非常重要。Applications Manager 移动 APM 通过在您的应用中嵌入以库形式存在的 APM 代理,收集并汇总来自全球所有用户的指标。
APM 代理通过事务和组件测量代码的执行时间。在上述示例中,从开始导航到渲染最终 UI 的整个操作序列可视为一个事务。各个操作可以分组到不同的组件类型中,如 HTTP、SQLite、文件系统、UI 等。简单操作可以仅用事务测量,复杂操作则可以使用带组件的事务进行测量。
浏览以下主题,了解更多关于配置 iOS 移动应用监控的信息:
按照以下步骤,使用 Swift Package Manager 从仓库集成 MEAPMInsight 包:
使用 Swift Package Manager 添加 MEAPMInsight 后,完成以下配置步骤:
如果您的 DEM Collector 使用自签名 SSL 证书:
这允许 MEAPMInsight SDK 安全地与 DEM Collector 通信。
使用自签名证书时,必须为 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 域名。
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 用于设置用户 ID 的动态值、跟踪自定义事件、捕获屏幕等。本文档将解释各种可用的自定义 API 及其语法。以下是可用 API 列表:
您可以使用下面的 API 用自定义上传间隔初始化 SDK。默认上传间隔设置为 30 秒。
Swift 语法:
APMInsight.startMonitoring()
Objective-C 语法:
[APMInsight startMonitoring];
默认情况下,SDK 会生成唯一的用户 ID。如果您想自定义用户 ID,可以使用以下语法。此功能在跟踪特定用户的指标或调试问题时非常有用。
注意: 不要向此函数传递任何个人身份信息(PII),如电子邮件地址、电话号码或姓名。仅使用匿名或非敏感标识符。
Swift 语法:
APMInsight.setUserId("user123")
Objective-C 语法:
[APMInsight setUserId:@"user123"];
您可以使用下面的 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];
默认情况下,使用以下网络库的网络请求会自动被捕获。
如果您想添加更多 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"];
默认情况下,会捕获所有屏幕;但是,如果您想添加遗漏的屏幕,请使用以下 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"];
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")
}
}
注意:
.trackScreen()
modifier should be added to each SwiftUI view you want to track.
您可以使用以下 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"];
您可以使用以下 API 立即上传记录的数据到 DEM Collector,而无需等待下次上传间隔。默认刷新间隔为 60 秒。
Swift 语法:
APMInsight.flush()
Objective-C 语法:
[APMInsight flush];
注意: 如果没有网络,则数据将保存在队列中等待下次上传。如果上传间隔设置较长,则可能需要手动刷新数据。
您可以通过传递 URL 列表作为参数,排除自定义 URL 的跟踪。
Swift 语法:
APMInsight.enableHttpTracking(["example.com", "demo.com"])
Objective-C 语法:
[APMInsight enableHttpTracking:@[@"example.com", @"demo.com"]];
您可以通过传递屏幕列表作为参数,排除自定义屏幕的跟踪。
Swift 语法:
APMInsight.ignoreScreens(["DetailScreen", "HomeScreen"])
Objective-C 语法:
[APMInsight ignoreScreens:@[@"DetailScreen", @"HomeScreen"]];