Integrate with iOS

更新时间:
复制 MD 格式

Application Performance Monitoring (APM) uses the mobile analysis client software development kit (SDK) for instrumentation. The SDK collects app performance data, generates logs, and uploads them to the server. The server then parses the instrumentation logs from the client based on a specified data format. This process allows the server to monitor and analyze various client-side metrics.

This topic describes how to configure performance monitoring instrumentation on the client for features such as network monitoring, H5 performance monitoring, page load monitoring, startup speed monitoring, and Mini Program monitoring.

Prerequisites

Network monitoring

Enable network monitoring

You can manually enable APM network performance monitoring instrumentation in your project code. During app startup, call the following code to enable network performance monitoring.

#import <MPMasAdapter/MPAnalysisHelper.h>

[MPAnalysisHelper startAPMNetMonitor];

mPaaS RPC network monitoring instrumentation

After you enable APM network monitoring, the built-in mPaaS network framework automatically performs instrumentation for mPaaS RPC. You do not need to add manual instrumentation to your code.

Custom business network monitoring instrumentation

If you use a network communication method other than mPaaS RPC and want to monitor its network quality, you must enable APM network monitoring during app startup. You must also add manual instrumentation for your custom network calls.

Call the following code for instrumentation:

#import <MPMasAdapter/MPRemoteLoggingInterface.h>

NSDictionary *param = @{
                @"totalRequestTimeInterval": @(totalRequestTime),
                @"dnsTimeInterval": @(dnsTime),
                @"tcpTimeInterval": @(tcpTime),
                @"sslTimeInterval": @(sslTime),
                @"firstPackageTimeInterval": @(firstPackageTime),
                @"transportTimeInterval": @(transportTime),
                @"downloadAverageSpeed": @(downloadAverageSpeed),
                @"httpStatusCode": @(httpStatusCode),
                @"rpcStatusCode": rpcStatusCode,
                @"operationType": operationType,
                @"clientIP": clientIP,
                @"gwIP": gwIP,
                @"traceId": traceId
            };
[MPRemoteLoggingInterface writeAPMNetLog:param];

The input parameters are as follows:

Parameter

Description

totalRequestTimeInterval

Total request time. Unit: ms.

dnsTimeInterval

DNS parsing time. Unit: ms.

tcpTimeInterval

TCP connection time. Unit: ms.

SSLTimeInterval

SSL connection time. Unit: ms.

firstPackageTimeInterval

Time to first byte (TTFB). This is the time from sending a request to receiving the first data packet from the server. Unit: ms.

transportTimeInterval

Data transfer time. This is the time from receiving the first data packet to receiving the last data packet. Unit: ms.

downloadAverageSpeed

Average download speed of the resource file.

httpStatusCode

HTTP status code. A value of 200 indicates normal.

rpcStatusCode

RPC status code. A value of 200 indicates normal.

operationType

Interface identifier. It identifies the gateway operation.

clientIP

Client IP address.

gwIP

Gateway IP address.

traceId

Trace ID of the client request.

H5 performance monitoring

The mPaaS H5 container automatically performs instrumentation for H5 performance monitoring. You only need to integrate the H5 container. No manual instrumentation is required.

Page load monitoring

The mPaaS native page automated instrumentation SDK performs instrumentation for native page load times. After you integrate the mobile analysis component, no manual instrumentation is required.

Startup speed monitoring

Based on the mPaaS framework

If your project integrates the mPaaS framework, the framework records the startup speed. After the app starts, for example, when the home page appears, you can record the startup time and send a startup completion notification.

// globalMonitorStartUpTime is a predefined variable. You can use it after you import the <mPaas/MPaaS+MonitorStartUpTime.h> header file.
double time = CFAbsoluteTimeGetCurrent() - globalMonitorStartUpTime;
[ [NSNotificationCenter defaultCenter] postNotificationName:@"APMonitor_Startup_Cost_Time" object:nil userInfo:@{@"CostTimeOnUserFeel": [NSString stringWithFormat:@"%f", time]}];

Based on the native framework

If your project does not integrate the mPaaS framework, you must manually call the API to upload the startup instrumentation log when the app starts.

#import <MPMasAdapter/MPAnalysisHelper.h>

//record the time interval used for the app startup
NSTimeInterval time = CFAbsoluteTimeGetCurrent() - __start_timestamp;
[[MPAnalysisHelper sharedInstance] writeLogForStartupWithTime:time];

Crash monitoring

Add crash instrumentation to collect crash-related data.

Based on the mPaaS framework

If you integrate the framework (your project contains the APMobileFramework library), the crash reporting module automatically captures crash logs and uploads them to the server. After you integrate the SDK, you only need to confirm that crash monitoring is enabled. To ensure that crash logs are reported promptly, call this interface in the main function.

Based on the native framework

If you do not integrate the mPaaS framework (your project does not contain the APMobileFramework library), you must enable crash monitoring at startup and report crash logs after startup.

Disaster recovery switch

By default, disaster recovery is triggered after four consecutive crashes. This process clears the files in the Documents folder to prevent crashes caused by dirty data. In versions 10.1.60 and later, you can manually call the following interface to enable or disable disaster recovery.

#import <MPMasAdapter/MPAnalysisHelper.h>
/**
 * Enables or disables crash disaster recovery. This feature is enabled by default.
 */
+ (void)enableDisasterRecovery:(BOOL)enable;
Important
  • Only crash logs from an app running on a physical device are captured and uploaded to the log server. To debug crash monitoring, you must disconnect from Xcode and not use an emulator.

  • To ensure that the version in the crash log matches the product version, set the bundle version and product version to the same value in your project's info.plist file.

Stuttering and freeze monitoring

Add performance instrumentation to collect data about stuttering and freezes.

Based on the mPaaS framework

  1. By default, stuttering and freeze monitoring is enabled for 10% of devices. You can use the following interface to set the enablement rate.

     [MPAnalysisHelper setLagMonitorPercent: 100]; // Enable monitoring for 100% of devices. This must be set before you call startPerformanceMonitor.
    Note

    Stuttering and freeze monitoring is enabled only on a physical device and when not debugging in Xcode.

  2. Call [MPAnalysisHelper startPerformanceMonitor] at startup in the -(void)application:(UIApplication *)application beforeDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions method.

Based on the native framework

The SDK provides a performance monitoring interface. Call [PerformanceHelper performanceMonitor] in the - (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions method of AppDelegate.

Note

Stuttering and freeze monitoring is enabled only on a physical device and when not debugging in Xcode.

    #import "PerformanceHelper.h"
    #import <MPAnalysis/MPAnalysisHelper.h>

    static NSTimeInterval __start_timestamp = 0;

    @implementation PerformanceHelper

    + (void)load
    {
        __start_timestamp = CFAbsoluteTimeGetCurrent();
    }

    + (void)performanceMonitor
    {
        //start performance monitor

       [MPAnalysisHelper setLagMonitorPercent: 100]; // Enable monitoring for 100% of devices. This must be set before you call startPerformanceMonitor.
        [MPAnalysisHelper startPerformanceMonitor];

        //record the time interval used for the app startup
        NSTimeInterval time = CFAbsoluteTimeGetCurrent() - __start_timestamp;
        [[MPAnalysisHelper sharedInstance] writeLogForStartupWithTime:time];
    }

    @end

Enable Mini Program monitoring

After you integrate the Mini Program component, the component automatically reports performance monitoring data. No manual instrumentation is required.