HarmonyOS SDK Access
The APM HarmonyOS SDK collects and reports information about app crashes, ANRs, errors, network requests, devices, and custom events.
Supported Platform
API 12 or later.
Transport Protocol
HTTPS
Integrating the SDK
- Introduce the HarmonyOS SDK to the project.
Method 1: Pull the SDK from the HarmonyOS repository.
- Run the following command to pull the SDK from the HarmonyOS repository.
ohpm install apm_harmony_sdk
- Add dependencies to the configuration file and import the SDK.
Add dependencies to the app/oh-package.json5 file. dependencies { ... "apm_harmony_sdk": "2.0.6" ... } ..//Run the following command to install the SDK: ohpm install
Method 2: Manually import the SDK.
- Download the APM SDK package.
- Add the downloaded HAR static library file to your project. The following uses the plugin folder in the root directory as an example.
- Add dependencies to the app/oh-package.json5 file.
dependencies { ... "apm_harmony_sdk": "file:../plugin/apm_harmony_sdk.har" //HAR package storage address ... }
- Run the following command to enable the SDK.
ohpm install
- Run the following command to pull the SDK from the HarmonyOS repository.
- Get required permissions to report data and obtain the network status by setting the module.json5 file. The file path is application->src->main->module.json5.
"requestPermissions":[ { "name": "ohos.permission.INTERNET" }, { "name": "ohos.permission.GET_NETWORK_INFO" } ],
Setting Parameters
- Log in to the APM console and create a HarmonyOS app by referring to .
- Download the configuration file apm-sdk-config.json and drag it into the project's resource directory (src -> main -> resources -> rawfile).
Table 1 HarmonyOS parameters Parameter
Description
Mandatory
Default Value
appId
APM mobile app ID
Yes
-
authorization
Used for app authentication.
Yes
-
region
Region where APM is located
Yes
-
uid
Custom ID.
No
-
tag
Custom tag. Use commas (,) to separate multiple tags.
No
-
url
Public domain name for reporting data to APM.
No
-
networkWhiteList
Network monitoring whitelist.
No
-
cacheThreshold
Cache threshold.
No
200
timeInterval
Scheduled reporting interval.
No
60
reportBackground
Whether to report a message when an app is switched to the background.
No
true
logLevel
Debug log level. Options: debug, info, warn, error, and off.
No
off
enableNetwork
Whether to enable network collection.
No
false
enableCrash
Whether to enable crash collection.
No
false
enableANR
Whether to enable ANR collection.
No
false
enableError
Whether to enable error collection.
No
false
enableDevice
Whether to enable device information collection.
No
false
enableEvent
Whether to enable custom statistics collection.
No
false
traceType
Trace type. Options: otel and apm.
No
apm
SDK Enablement
- Import the dependency module.
import { APMSDK } from "apm_harmony_sdk";
- Set startup and custom parameters.
//Start the SDK and add the following code to the onCreate function: APMSDK.start(this.context.getApplicationContext()); // Set a custom ID. APMSDK.setUid (Custom ID) // Set a custom tag. APMSDK.addTag (Custom tag);
Other Methods
- Customize the error data to be reported.
// Specify the errors to capture and configure the function of reporting data to APM. // 1. Capture "Error". import { APMSDK } from "apm_harmony_sdk"; try { ... } catch(e) { APMSDK.reportError(e); } // 2. Capture "BusinessError". const response = await session.fetch(request).then((rep: rcp.Response) => { ... }).catch((err: BusinessError) => { ... APMSDK.reportBusinessError(err); });
- Use the RCP interceptor for reporting API data to APM.
//Code example: import { APMRcpInterceptor, ResponseCache } from "apm_harmony_sdk"; async function testInterceptor() { const cache: ResponseCache = new ResponseCache(); const session = rcp.createSession({ interceptors: [new APMRcpInterceptor(cache)] }); const response = await session.get('report address'); }
- Customize tracking for proactive data reporting.
// APMCallbackHttp: Listens to requests that contain the url, OPTIONS, and CALLBACK parameters. import { APMCallbackHttp } from "apm_harmony_sdk"; try { APMCallbackHttp( // Customize EXAMPLE_URL in extraData on your own. You can determine whether to add parameters to the URL. "EXAMPLE_URL", { method: http.RequestMethod.GET, // Optional. The default value is http.RequestMethod.GET. // You can add header fields based on service requirements. header: { 'Content-Type': 'application/json' }}, (err: BusinessError, data: http.HttpResponse) => { if (!err) { // data.result carries the HTTP response. Parse the response based on service requirements. console.info('Result:' + JSON.stringify(data.result)); console.info('code:' + JSON.stringify(data.responseCode)); // data.header is the HTTP response header. Parse the response based on service requirements. console.info('header:' + JSON.stringify(data.header)); console.info('cookies:' + JSON.stringify(data.cookies)); // 8+ } else { console.error('error:' + JSON.stringify(err)); } }); } catch (e) { console.log(e); } // APMPromiseHttp: Listens to the requests that contain the url and OPTIONS parameters and return PROMISE. import { APMPromiseHttp } from "apm_harmony_sdk"; try { let httpRequest = http.createHttp(); const res = await APMPromiseHttp( // Customize EXAMPLE_URL in extraData on your own. You can determine whether to add parameters to the URL. "http://EXAMPLE_URL/apm2/web/cmdb/business/v1/test2", { method: http.RequestMethod.GET, // Optional. The default value is http.RequestMethod.GET. // You can add header fields based on service requirements. header: { 'Content-Type': 'application/json' }}); console.log('res:' + JSON.stringify(res)); } catch (e) { console.log(e); }
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot