Basic APIs

Updated at:

This topic describes the basic APIs of the software development kit (SDK).

1. Initialization

Feature description

Initializes the basic configuration items for the App Monitor SDK and starts the SDK.

API definition

The definitions are as follows:

export declare namespace APM {
  /**
   * The public API for @aliyun/apm 2.0.0. This API is used for application performance management (APM) initialization. 
   * The config parameter specifies the common configuration for all APM sub-products. 
   * The products parameter specifies the sub-products to initialize. Currently, Crash Analytics and App Performance Analytics are supported.
   * @param config
   * @param products
   */
  function init(config: APMConfig, products?: IPlugin[]): void;
  /**
   * Starts APM monitoring.
   */
  function start(): void;
}

Code example

You can construct the input parameters for SDK initialization by creating an initialization configuration object. The following code provides an example:

import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
import { APM, APMConfig, Logger } from '@aliyun/apm';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { crashAnalysisApi } from '@aliyun/apm_crash';
import { performanceApi } from '@aliyun/apm_perf';

class MyCustomLog implements Logger {
  print(domain: number, tag: string, level: hilog.LogLevel, msg: string): void {
    switch (level) {
      case hilog.LogLevel.DEBUG:
        console.debug(`Custom log msg:${msg}`);
        break;
      case hilog.LogLevel.INFO:
        console.info(`Custom log msg:${msg}`);
        break;
      case hilog.LogLevel.WARN:
        console.warn(`Custom log msg:${msg}`);
        break;
      case hilog.LogLevel.ERROR:
      case hilog.LogLevel.FATAL:
        console.error(`Custom log msg:${msg}`);
        break;
    }
  }
}

export default class EntryAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');

    const apm_config: APMConfig = {
      context: this.context,  // Required
      appKey: 'Your AppKey',    // Required
      appSecret: 'Your AppSecret',  // Required
      nick: 'User nickname',  // Optional
      userId: 'User ID',  // Optional
      channel: 'User channel',  // Optional
      hiLog: true, // The switch for hilog in the SDK. Optional.
      customLogger: new MyCustomLog(), // The custom log interface. Optional.
    }
    // When you call init, select the crash or performance API based on the feature you want to use.
    APM.init(apm_config, [crashAnalysisApi, performanceApi])
    APM.start();

  }

  // Omit other code.
}

The following describes the parameters of APMConfig.

Parameter

Description

context

The application context.

[Data type] Context object

[Required] Yes

[Can be empty] No

[Default value] None

appKey

The AppKey of the application.

[Data type] String

[Required] Yes

[Can be empty] No

[Default value] None

appSecret

The AppSecret of the application.

[Data type] String

[Required] Yes

[Can be empty] No

[Default value] None

channel

The channel identity. This value is reported to the server-side to distinguish between different channels.

[Data type] String

[Required] No

[Can be empty] Yes

[Default value] None

nick

The user nickname. This value is reported to the server-side to distinguish between different users. You can later retrieve data based on this parameter.

[Data type] String

[Required] No

[Can be empty] Yes

[Default value] None

userId

The user ID. This value is reported to the server-side to distinguish between different users. You can later retrieve data based on this parameter.

[Data type] String

[Required] No

[Can be empty] Yes

[Default value] None

hiLog

The switch for the hilog log of the SDK.

[Data type] Boolean

[Valid values] true/false

[Required] No

[Can be empty] Yes

[Default value] false

customLogger

The interface for SDK log export.

[Data type] Logger interface object

[Required] No

[Can be empty] Yes

[Default value] None

2. Set user ID and nickname

Feature description

This feature lets you modify the userId and nickname in APMConfig during the SDK runtime.

API definition

The API is defined as follows:

export declare namespace APM {
  /**
   * Modifies the userId and userNick.
   */
   function setUserId(id: string): void;
   
   function setUserNick(nick: string): void;
}

Code example

APM.setUserId("id");

APM.setUserNick("nick");