Integrate with HarmonyOS NEXT

更新时间:
复制 MD 格式

Mobile performance monitoring (APM) uses the Mobile Analytics client SDK to instrument your app, collect performance-related data, generate logs, and report them to the server. Based on the instrumentation data format agreed upon between the mPaaS client and server, the server extracts valid data from the instrumentation logs uploaded by the client to monitor and analyze client-side metrics.

This topic describes how to configure performance monitoring instrumentation in a HarmonyOS NEXT client, including network monitoring, H5 performance monitoring, page load monitoring, and startup speed monitoring.

Prerequisites

Performance monitoring

Unified initialization interface

After calling the unified initialization interface for performance monitoring, you do not need to separately initialize Crash and ANR monitoring or startup freeze monitoring. However, after the app finishes starting up, call the startup completion notification MPRemoteLogger.startupFinish().

import { MPRemoteLogger } from '@mpaas/masadapter';

/**
   * Initialize performance monitoring instrumentation (including crash, ANR, and startup freeze)
   * After calling this method, do not call initFaultTrack or startPerformanceMonitor
   * Note: When startup completes, manually call MPRemoteLogger.startupFinish() to notify completion
   */
MPRemoteLogger.initPerformanceTrack();

// Call the following code when app startup completes to notify completion
MPRemoteLogger.startupFinish();
Important

If you want to selectively enable Crash monitoring or startup freeze monitoring, do not call the unified initialization interface. Instead, call only the required interfaces based on your needs.

Crash and ANR monitoring

Run the following initialization to automatically enable Crash and ANR monitoring.

import { MPRemoteLogger } from '@mpaas/masadapter';

MPRemoteLogger.initFaultTrack();

Startup freeze monitoring

Run the following initialization to automatically enable startup freeze monitoring.

import { MPRemoteLogger } from '@mpaas/masadapter';

// Enable freeze monitoring at app startup
MPRemoteLogger.startPerformanceMonitor();
// Call the following code when app startup completes to notify completion
MPRemoteLogger.startupFinish();

Startup speed monitoring

Manually call the startup instrumentation reporting method when app startup completes.

import { MPRemoteLogger } from '@mpaas/masadapter';
const startupTime = startup; // Unit: milliseconds
MPRemoteLogger.reportStartupTime(startupTime);

Page performance monitoring

Initialize automatic page monitoring.

import { MPRemoteLogger } from '@mpaas/masadapter';

// Call the following method in your custom UIAbility to initialize automatic page monitoring
// Example
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    // Initialize full instrumentation for automatic page tracking
    MPRemoteLogger.initUIAutoTrack(this.context);
}

H5 performance monitoring

The mPaaS H5 container automatically handles H5 performance monitoring instrumentation. Simply integrate the H5 container—no manual instrumentation is needed.

Optional configuration

Use the following interfaces if you need custom configuration.

Instrumentation toggle

To disable instrumentation, use the following setting.

import { MPRemoteLogger } from '@mpaas/masadapter';
/**
* Disable instrumentation
*/
MPRemoteLogger.enableLog(false);

Force log upload

import { MPRemoteLogger } from '@mpaas/masadapter';
/**
* Force upload all pending instrumentation logs (avoid unless necessary—batch uploads consume network resources)
*/
MPRemoteLogger.uploadAllLog();

Common extension field

Set a common extension field for instrumentation.

import { MPRemoteLogger } from '@mpaas/masadapter';

MPRemoteLogger.setFoundationExtend('your custom field');

Retain already reported instrumentation

To retain successfully uploaded instrumentation logs, call the following method.

import { MPRemoteLogger } from '@mpaas/masadapter';

MPRemoteLogger.reserveUploadedLog(true);
Note

This method is intended for debugging during development and testing. Do not use it in production builds.

View instrumentation logs

  • To view instrumentation logs, check the phone directory /data/app/el2/100/base/bundleid (replace with your app's bundle ID)/haps/entry/files/mptrack.

  • Unuploaded logs can be found in the logs and upload directories.

  • If you enable retention of uploaded logs, successfully uploaded logs appear in the uploaded directory.

  • Instrumentation toggle settings are stored in the logConfig file.