未验证 提交 f8d8ffb1 编写于 作者: O openharmony_ci 提交者: Gitee

!23916 【0815monthly分支挑单】hisysevent/hitracechain开发指导样例代码ArkTs语法规范整改

Merge pull request !23916 from honghecun/cherry-pick-1693899037
......@@ -30,6 +30,7 @@ hiTraceChain是基于云计算分布式跟踪调用链思想,在端侧业务
```ts
import hiTraceChain from '@ohos.hiTraceChain';
import hiSysEvent from '@ohos.hiSysEvent';
import { BusinessError } from '@ohos.base';
@Entry
@Component
......@@ -50,26 +51,28 @@ hiTraceChain是基于云计算分布式跟踪调用链思想,在端侧业务
// 业务开始前,开启分布式跟踪。
let traceId = hiTraceChain.begin("Write a new system event", hiTraceChain.HiTraceFlag.INCLUDE_ASYNC);
// 业务开始:完成一次系统事件打点。
hiSysEvent.write({
let customizedParams: Record<string, string | number> = {
'PID': 1,
'UID': 1,
'PACKAGE_NAME': "com.demo.hitracechain",
'PROCESS_NAME': "hitracechaindemo",
'MSG': "no msg."
}
let eventInfo: hiSysEvent.SysEventInfo = {
domain: "RELIABILITY",
name: "STACK",
eventType: hiSysEvent.EventType.FAULT,
params: {
PID: 1,
UID: 1,
PACKAGE_NAME: "com.demo.hitracechain",
PROCESS_NAME: "hitracechaindemo",
MSG: "no msg."
}
}).then((val) => {
params: customizedParams
}
hiSysEvent.write(eventInfo).then((val: number) => {
console.info(`write result is ${val}`);
// 业务结束,关闭分布式跟踪。
hiTraceChain.end(traceId);
}).catch((err) => {
}).catch((err: BusinessError) => {
console.error(`error message is ${err.message}`);
});
} catch (err) {
console.error(`error message is ${err.message}`);
console.error(`error message is ${(err as BusinessError).message}`);
}
})
}
......
......@@ -10,7 +10,7 @@
## 导入模块
```js
```ts
import hiSysEvent from '@ohos.hiSysEvent';
```
......@@ -73,26 +73,29 @@ write(info: SysEventInfo, callback: AsyncCallback&lt;void&gt;): void
**示例:**
```js
```ts
import hiSysEvent from '@ohos.hiSysEvent';
import { BusinessError } from '@ohos.base';
try {
hiSysEvent.write({
domain: "RELIABILITY",
name: "STACK",
eventType: hiSysEvent.EventType.FAULT,
params: {
PID: 487,
UID: 103,
PACKAGE_NAME: "com.ohos.hisysevent.test",
PROCESS_NAME: "syseventservice",
MSG: "no msg."
}
}, (err, val) => {
// do something here.
})
} catch (error) {
console.error(`error code: ${error.code}, error msg: ${error.message}`);
let customizedParams: Record<string, string | number> = {
'PID': 487,
'UID': 103,
'PACKAGE_NAME': "com.ohos.hisysevent.test",
'PROCESS_NAME': "syseventservice",
'MSG': "no msg."
};
let eventInfo: hiSysEvent.SysEventInfo = {
domain: "RELIABILITY",
name: "STACK",
eventType: hiSysEvent.EventType.FAULT,
params: customizedParams
};
hiSysEvent.write(eventInfo, (err: BusinessError, val: number) => {
// do something here.
});
} catch (err) {
console.error(`error code: ${(err as BusinessError).code}, error msg: ${(err as BusinessError).message}`);
}
```
......@@ -134,32 +137,35 @@ write(info: SysEventInfo): Promise&lt;void&gt;
**示例:**
```js
```ts
import hiSysEvent from '@ohos.hiSysEvent';
import { BusinessError } from '@ohos.base';
try {
hiSysEvent.write({
domain: "RELIABILITY",
name: "STACK",
eventType: hiSysEvent.EventType.FAULT,
params: {
PID: 487,
UID: 103,
PACKAGE_NAME: "com.ohos.hisysevent.test",
PROCESS_NAME: "syseventservice",
MSG: "no msg."
}
}).then(
(val) => {
// do something here.
}
).catch(
(err) => {
// do something here.
}
)
} catch (error) {
console.error(`error code: ${error.code}, error msg: ${error.message}`);
let customizedParams: Record<string, string | number> = {
'PID': 487,
'UID': 103,
'PACKAGE_NAME': "com.ohos.hisysevent.test",
'PROCESS_NAME': "syseventservice",
'MSG': "no msg."
};
let eventInfo: hiSysEvent.SysEventInfo = {
domain: "RELIABILITY",
name: "STACK",
eventType: hiSysEvent.EventType.FAULT,
params: customizedParams
};
hiSysEvent.write(eventInfo).then(
(val: number) => {
// do something here.
}
).catch(
(err: BusinessError) => {
console.error(`error code: ${err.code}, error msg: ${err.message}`);
}
)
} catch (err) {
console.error(`error code: ${(err as BusinessError).code}, error msg: ${(err as BusinessError).message}`);
}
```
......@@ -227,27 +233,29 @@ addWatcher(watcher: Watcher): void
**示例:**
```js
```ts
import hiSysEvent from '@ohos.hiSysEvent';
let watcher = {
rules: [{
domain: "RELIABILITY",
name: "STACK",
tag: "STABILITY",
ruleType: hiSysEvent.RuleType.WHOLE_WORD,
}],
onEvent: (info) => {
// do something here.
},
onServiceDied: () => {
// do something here.
}
import { BusinessError } from '@ohos.base';
let watchRules: hiSysEvent.WatchRule[] = [{
domain: "RELIABILITY",
name: "STACK",
tag: "STABILITY",
ruleType: hiSysEvent.RuleType.WHOLE_WORD,
} as hiSysEvent.WatchRule];
let watcher: hiSysEvent.Watcher = {
rules: watchRules,
onEvent: (info: hiSysEvent.SysEventInfo) => {
// do something here.
},
onServiceDied: () => {
// do something here.
}
}
try {
hiSysEvent.addWatcher(watcher)
} catch (error) {
console.error(`error code: ${error.code}, error msg: ${error.message}`);
hiSysEvent.addWatcher(watcher)
} catch (err) {
console.error(`error code: ${(err as BusinessError).code}, error msg: ${(err as BusinessError).message}`);
}
```
......@@ -277,28 +285,30 @@ removeWatcher(watcher: Watcher): void
**示例:**
```js
```ts
import hiSysEvent from '@ohos.hiSysEvent';
let watcher = {
rules: [{
domain: "RELIABILITY",
name: "STACK",
tag: "STABILITY",
ruleType: hiSysEvent.RuleType.WHOLE_WORD,
}],
onEvent: (info) => {
// do something here.
},
onServiceDied: () => {
// do something here.
}
import { BusinessError } from '@ohos.base';
let watchRules: hiSysEvent.WatchRule[] = [{
domain: "RELIABILITY",
name: "STACK",
tag: "STABILITY",
ruleType: hiSysEvent.RuleType.WHOLE_WORD,
} as hiSysEvent.WatchRule ]
let watcher: hiSysEvent.Watcher = {
rules: watchRules,
onEvent: (info: hiSysEvent.SysEventInfo) => {
// do something here.
},
onServiceDied: () => {
// do something here.
}
}
try {
hiSysEvent.addWatcher(watcher)
hiSysEvent.removeWatcher(watcher)
} catch (error) {
console.error(`error code: ${error.code}, error msg: ${error.message}`);
hiSysEvent.addWatcher(watcher)
hiSysEvent.removeWatcher(watcher)
} catch (err) {
console.error(`error code: ${(err as BusinessError).code}, error msg: ${(err as BusinessError).message}`);
}
```
......@@ -370,41 +380,48 @@ query(queryArg: QueryArg, rules: QueryRule[], querier: Querier): void
**示例:**
```js
```ts
import hiSysEvent from '@ohos.hiSysEvent';
import { BusinessError } from '@ohos.base';
try {
hiSysEvent.write({
domain: "RELIABILITY",
name: "STACK",
eventType: hiSysEvent.EventType.FAULT,
params: {
PID: 487,
UID: 103,
PACKAGE_NAME: "com.ohos.hisysevent.test",
PROCESS_NAME: "syseventservice",
MSG: "no msg."
}
}, (err, val) => {
// do something here.
})
hiSysEvent.query({
beginTime: -1,
endTime: -1,
maxEvents: 5,
}, [{
domain: "RELIABILITY",
names: ["STACK"],
}], {
onQuery: function (infos) {
// do something here.
},
onComplete: function(reason, total) {
// do something here.
}
})
} catch (error) {
console.error(`error code: ${error.code}, error msg: ${error.message}`);
let customizedParams: Record<string, string | number> = {
'PID': 487,
'UID': 103,
'PACKAGE_NAME': "com.ohos.hisysevent.test",
'PROCESS_NAME': "syseventservice",
'MSG': "no msg."
};
let eventInfo: hiSysEvent.SysEventInfo = {
domain: "RELIABILITY",
name: "STACK",
eventType: hiSysEvent.EventType.FAULT,
params: customizedParams
};
hiSysEvent.write(eventInfo, (err: BusinessError, val: number) => {
// do something here.
})
let queryArg: hiSysEvent.QueryArg = {
beginTime: -1,
endTime: -1,
maxEvents: 5,
};
let queryRules: hiSysEvent.QueryRule[] = [{
domain: "RELIABILITY",
names: ["STACK"],
} as hiSysEvent.QueryRule]
let querier: hiSysEvent.Querier = {
onQuery: (infos: hiSysEvent.SysEventInfo[]) => {
// do something here.
},
onComplete: (reason: number, total: number) => {
// do something here.
}
}
hiSysEvent.query(queryArg, queryRules, querier)
} catch (err) {
console.error(`error code: ${(err as BusinessError).code}, error msg: ${(err as BusinessError).message}`);
}
```
......@@ -443,50 +460,55 @@ exportSysEvents(queryArg: QueryArg, rules: QueryRule[]): number
**示例:**
```
```ts
import hiSysEvent from '@ohos.hiSysEvent';
import fs from '@ohos.file.fs';
import { BusinessError } from '@ohos.base';
try {
hiSysEvent.write({
domain: "RELIABILITY",
name: "STACK",
eventType: hiSysEvent.EventType.FAULT,
params: {
PID: 487,
UID: 103,
PACKAGE_NAME: "com.ohos.hisysevent.test",
PROCESS_NAME: "syseventservice",
MSG: "no msg."
}
}, (err, val) => {
// do something here.
})
let time = hiSysEvent.exportSysEvents({
beginTime: -1,
endTime: -1,
maxEvents: 1,
}, [{
domain: "RELIABILITY",
names: ["STACK"],
}])
console.log(`receive export task time is : ${time}`);
// 延迟读取本次导出的事件
setTimeout(function() {
let eventDir = '/data/storage/el2/base/cache/hiview/event';
let filenames = fs.listFileSync(eventDir);
for (let i = 0; i < filenames.length; i++) {
if (filenames[i].indexOf(time.toString()) != -1) {
let res = fs.readTextSync(eventDir + '/' + filenames[i]);
let events = JSON.parse('[' + res.slice(0, res.length - 1) + ']');
console.log("read file end, events is :" + JSON.stringify(events));
}
}
}, 10000)
} catch (error) {
console.error(`error code: ${error.code}, error msg: ${error.message}`);
let customizedParams: Record<string, string | number> = {
'PID': 487,
'UID': 103,
'PACKAGE_NAME': "com.ohos.hisysevent.test",
'PROCESS_NAME': "syseventservice",
'MSG': "no msg."
};
let eventInfo: hiSysEvent.SysEventInfo = {
domain: "RELIABILITY",
name: "STACK",
eventType: hiSysEvent.EventType.FAULT,
params: customizedParams
};
hiSysEvent.write(eventInfo, (err: BusinessError, val: number) => {
// do something here.
})
let queryArg: hiSysEvent.QueryArg = {
beginTime: -1,
endTime: -1,
maxEvents: 1,
}
let queryRules: hiSysEvent.QueryRule[] = [{
domain: "RELIABILITY",
names: ["STACK"],
} as hiSysEvent.QueryRule]
let time = hiSysEvent.exportSysEvents(queryArg, queryRules)
console.log(`receive export task time is : ${time}`);
// 延迟读取本次导出的事件
setTimeout(() => {
let eventDir = '/data/storage/el2/base/cache/hiview/event';
let filenames = fs.listFileSync(eventDir);
for (let i = 0; i < filenames.length; i++) {
if (filenames[i].indexOf(time.toString()) != -1) {
let res = fs.readTextSync(eventDir + '/' + filenames[i]);
let events: string = JSON.parse('[' + res.slice(0, res.length - 1) + ']');
console.log("read file end, events is :" + JSON.stringify(events));
}
}
}, 10000)
} catch catch (err) {
console.error(`error code: ${(err as BusinessError).code}, error msg: ${(err as BusinessError).message}`);
}
```
......@@ -523,45 +545,51 @@ subscribe(rules: QueryRule[]): number
**示例:**
```
```ts
import hiSysEvent from '@ohos.hiSysEvent';
import fs from '@ohos.file.fs';
import { BusinessError } from '@ohos.base';
try {
hiSysEvent.subscribe([{
domain: "RELIABILITY",
names: ["STACK"],
},{
domain: "BUNDLE_MANAGER",
names: ["BUNDLE_UNINSTALL"],
}])
hiSysEvent.write({
domain: "RELIABILITY",
name: "STACK",
eventType: hiSysEvent.EventType.FAULT,
params: {
PID: 487,
UID: 103,
PACKAGE_NAME: "com.ohos.hisysevent.test",
PROCESS_NAME: "syseventservice",
MSG: "no msg."
}
}, (err, val) => {
// do something here.
})
// 延迟读取订阅的事件
setTimeout(function() {
let eventDir = '/data/storage/el2/base/cache/hiview/event';
let filenames = fs.listFileSync(eventDir);
for (let i = 0; i < filenames.length; i++) {
let res = fs.readTextSync(eventDir + '/' + filenames[i]);
let events = JSON.parse('[' + res.slice(0, res.length - 1) + ']');
console.log("read file end, events is :" + JSON.stringify(events));
}
}, 10000)
} catch (error) {
console.error(`error code: ${error.code}, error msg: ${error.message}`);
let rules: hiSysEvent.QueryRule[] = [{
domain: "RELIABILITY",
names: ["STACK"],
} as hiSysEvent.QueryRule,
{
domain: "BUNDLE_MANAGER",
names: ["BUNDLE_UNINSTALL"],
} as hiSysEvent.QueryRule];
hiSysEvent.subscribe(rules)
let customizedParams: Record<string, string | number> = {
'PID': 487,
'UID': 103,
'PACKAGE_NAME': "com.ohos.hisysevent.test",
'PROCESS_NAME': "syseventservice",
'MSG': "no msg."
};
let eventInfo: hiSysEvent.SysEventInfo = {
domain: "RELIABILITY",
name: "STACK",
eventType: hiSysEvent.EventType.FAULT,
params: customizedParams
};
hiSysEvent.write(eventInfo, (err: BusinessError, val: number) => {
// do something here.
})
// 延迟读取订阅的事件
setTimeout(() => {
let eventDir = '/data/storage/el2/base/cache/hiview/event';
let filenames = fs.listFileSync(eventDir);
for (let i = 0; i < filenames.length; i++) {
let res = fs.readTextSync(eventDir + '/' + filenames[i]);
let events: string = JSON.parse('[' + res.slice(0, res.length - 1) + ']');
console.log("read file end, events is :" + JSON.stringify(events));
}
}, 10000)
} catch catch (err) {
console.error(`error code: ${(err as BusinessError).code}, error msg: ${(err as BusinessError).message}`);
}
```
......@@ -585,20 +613,23 @@ unsubscribe(): void
**示例:**
```
```ts
import hiSysEvent from '@ohos.hiSysEvent';
import { BusinessError } from '@ohos.base';
try {
hiSysEvent.subscribe([{
domain: "RELIABILITY",
names: ["STACK"],
},{
domain: "BUNDLE_MANAGER",
names: ["BUNDLE_UNINSTALL","BUNDLE_INSTALL"],
}])
hiSysEvent.unsubscribe();
} catch (error) {
console.error(`error code: ${error.code}, error msg: ${error.message}`);
let rules: hiSysEvent.QueryRule[] = [{
domain: "RELIABILITY",
names: ["STACK"],
} as hiSysEvent.QueryRule,
{
domain: "BUNDLE_MANAGER",
names: ["BUNDLE_UNINSTALL"],
} as hiSysEvent.QueryRule];
hiSysEvent.subscribe(rules)
hiSysEvent.unsubscribe();
} catch (err) {
console.error(`error code: ${(err as BusinessError).code}, error msg: ${(err as BusinessError).message}`);
}
```
......@@ -8,7 +8,7 @@
## 导入模块
```js
```ts
import hiTraceChain from '@ohos.hiTraceChain';
```
......@@ -93,7 +93,7 @@ begin(name: string, flags?: number): HiTraceId
**示例:**
```js
```ts
let asyncTraceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.INCLUDE_ASYNC | hiTraceChain.HiTraceFlag.DONOT_CREATE_SPAN);
```
......@@ -113,7 +113,7 @@ end(id: HiTraceId): void
**示例:**
```js
```ts
let asyncTraceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.DEFAULT);
// 若干业务逻辑完成后,结束跟踪。
hiTraceChain.end(asyncTraceId);
......@@ -135,7 +135,7 @@ getId(): HiTraceId
**示例:**
```js
```ts
let traceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.DEFAULT);
// 若干业务逻辑完成后,获取当前HiTraceId。
let curTraceId = hiTraceChain.getId();
......@@ -157,10 +157,11 @@ setId(id: HiTraceId): void
**示例:**
```js
let asyncTraceId;
```ts
let asyncTraceId: hiTraceChain.HiTraceId;
hiTraceChain.end(asyncTraceId);
let traceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.DEFAULT);
// 若干业务逻辑完成后,设置当前HiTraceId。
// 若干业务逻辑完成后,将之前的traceId设置为当前traceId。
hiTraceChain.setId(asyncTraceId);
```
......@@ -174,7 +175,7 @@ clearId(): void
**示例:**
```js
```ts
let traceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.DEFAULT);
// 若干业务逻辑完成后,清除当前HiTraceId。
hiTraceChain.clearId();
......@@ -196,7 +197,7 @@ createSpan(): HiTraceId
**示例:**
```js
```ts
let traceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.DEFAULT);
// 若干业务逻辑完成后,创建跟踪分支。
let spanTraceId = hiTraceChain.createSpan();
......@@ -221,7 +222,7 @@ tracepoint(mode: HiTraceCommunicationMode, type: HiTraceTracepointType, id: HiTr
**示例:**
```js
```ts
let asyncTraceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.INCLUDE_ASYNC | hiTraceChain.HiTraceFlag.DONOT_CREATE_SPAN);
// 若干业务逻辑完成后,触发信息埋点操作。
hiTraceChain.tracepoint(hiTraceChain.HiTraceCommunicationMode.THREAD, hiTraceChain.HiTraceTracepointType.SS, asyncTraceId, "Just a example");
......@@ -249,7 +250,7 @@ isValid(id: HiTraceId): boolean
**示例:**
```js
```ts
let traceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.DEFAULT);
let traceIdIsvalid = hiTraceChain.isValid(traceId);
```
......@@ -277,7 +278,7 @@ isFlagEnabled(id: HiTraceId, flag: HiTraceFlag): boolean
**示例:**
```js
```ts
let asyncTraceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.INCLUDE_ASYNC);
// enabledDoNotCreateSpanFlag为true
let enabledDoNotCreateSpanFlag = hiTraceChain.isFlagEnabled(asyncTraceId, hiTraceChain.HiTraceFlag.INCLUDE_ASYNC);
......@@ -300,7 +301,7 @@ enableFlag(id: HiTraceId, flag: HiTraceFlag): void
**示例:**
```js
```ts
let asyncTraceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.INCLUDE_ASYNC);
hiTraceChain.enableFlag(asyncTraceId, hiTraceChain.HiTraceFlag.DONOT_CREATE_SPAN);
// enabledDoNotCreateSpanFlag为true
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册