Use the SDK

更新时间:
复制 MD 格式

Prerequisites

  • Verify that the Mobile Analysis server-side address logGW is correctly configured in the mpaas.config file in the rawfile directory.

  • Before you use the Mobile Analysis SDK, you must initialize the mPaaS framework SDK. For more information, see the mPaaS framework integration guide.

Add permissions

Add the required network permissions to your project.

 "requestPermissions":[
      {
        "name" : "ohos.permission.GET_NETWORK_INFO"
      },
      {
        "name" : "ohos.permission.INTERNET"
      }
  ]

Initialize MPRemoteLogger

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

MPRemoteLogger.init();

Use the API

Report app activity

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

MPRemoteLogger.reportActive();

Report app logon activity

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

MPRemoteLogger.reportUserLogin(userId);
Important

Call this method whenever the user ID changes to update the `userId` for instrumentation.

App startup speed

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

const startupTime = startup; // In milliseconds
MPRemoteLogger.reportStartupTime(startupTime);

Custom instrumentation

You can use custom instrumentation to report business events.

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

let param = new Map<string, string>(); // Optional
param.set('a', 'b');
MPRemoteLogger.logBehavior('myBiz', 'myEventId', param);

Performance monitoring

  • General initialization API

    This is the general initialization API for performance monitoring. When you call this API, you do not need to separately initialize crash monitoring, Application Not Responding (ANR) monitoring, or startup freeze monitoring. However, after the application starts, you must call MPRemoteLogger.startupFinish() to send a startup completion notification.

    import { MPRemoteLogger } from '@mpaas/masadapter';
    
    /**
       * Initializes performance monitoring instrumentation, including for crashes, ANRs, and startup freezes.
       * After you call this method, you do not need to call the initFaultTrack and startPerformanceMonitor methods.
       * Note: When the startup is complete, you must manually call the MPRemoteLogger.startupFinish() method to send a startup completion notification.
       */
    MPRemoteLogger.initPerformanceTrack();
    
    // When the app startup is complete, call the following code to send a completion notification.
    MPRemoteLogger.startupFinish();
    
    Important

    If you want to enable only crash monitoring or startup freeze monitoring, do not call the general initialization API. Instead, call the specific APIs for the features you need.

  • Crash and ANR monitoring

    Initialize this feature to automatically monitor for crashes and ANRs.

    import { MPRemoteLogger } from '@mpaas/masadapter';
    
    MPRemoteLogger.initFaultTrack();
  • Startup freeze monitoring

    Initialize this feature to automatically monitor for startup freezes.

    import { MPRemoteLogger } from '@mpaas/masadapter';
    
    // When the app starts, enable hang monitoring
    MPRemoteLogger.startPerformanceMonitor();
    // When the app startup completes, call the following code to notify that startup has completed
    MPRemoteLogger.startupFinish();

Automated page monitoring

Initialize automated page monitoring.

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

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

Optional configurations

You can use the following APIs for custom configurations.

  • Instrumentation switch

    To disable instrumentation, use the following API call.

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

    import { MPRemoteLogger } from '@mpaas/masadapter';
    /**
    * Forcibly reports all instrumentation data that is in memory but has not yet been reported. Do not use this unless necessary. Batch reporting consumes network resources.
    */
    MPRemoteLogger.uploadAllLog();
  • General extension fields

    You can set general extension fields for instrumentation.

    import { MPRemoteLogger } from '@mpaas/masadapter';
    
    MPRemoteLogger.setFoundationExtend('your custom field');
  • Custom device ID

    Note

    The custom device ID feature is supported in baseline 10.2.3.40 and later.

    import { MPRemoteLogger } from '@mpaas/masadapter';
    
    MPRemoteLogger.setClientId("the-client-id");
  • Retain reported instrumentation data

    To retain successfully reported instrumentation files, call the following method.

    import { MPRemoteLogger } from '@mpaas/masadapter';
    
    MPRemoteLogger.reserveUploadedLog(true);
    Note

    This method is for debugging purposes only. Do not use it in production packages.

  • View instrumentation data

    To view instrumentation data, check the following directory on the device: /data/app/el2/100/base/bundleid (replace bundleid with your application's bundle ID)/haps/entry/files/mptrack .

    Unreported data is in the `logs` and `upload` directories. Successfully reported data is in the `uploaded` directory if you have enabled the API to retain reported data. The instrumentation switch configuration is in the `logConfig` file.