1 /*
2 * Copyright (C) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "api_event_reporter.h"
16 #include <thread>
17 #include <mutex>
18 #include "app_event.h"
19 #include "app_event_processor_mgr.h"
20 #include "app_domain_verify_hilog.h"
21 #include "config_parser.h"
22
23 namespace OHOS::AppDomainVerify::Dfx {
24 #define API_REPORT_CONFIG_PATH "/system/etc/app_domain_verify/api_report.conf"
25 static int64_t g_processId = -1;
26 static std::mutex g_mutex;
27 constexpr int TRIGGER_COND_TIMEOUT = 90;
28 constexpr int TRIGGER_COND_ROW = 30;
29 static const std::string SDK_NAME("AbilityKit");
30
ApiEventReporter(const std::string & apiName)31 ApiEventReporter::ApiEventReporter(const std::string& apiName) : apiName_(apiName)
32 {
33 transId_ = std::string("transId_") + std::to_string(std::rand());
34 startTime_ = std::chrono::duration_cast<std::chrono::milliseconds>(
35 std::chrono::system_clock::now().time_since_epoch())
36 .count();
37 std::unique_lock<std::mutex> lock(g_mutex);
38 if (g_processId == -1) {
39 g_processId = AddProcessor();
40 }
41 }
42
AddProcessor()43 int64_t ApiEventReporter::AddProcessor()
44 {
45 HiviewDFX::HiAppEvent::ReportConfig config;
46 config.appId = "app_domain_verify_ohos_sdk_ocg";
47 config.name = "app_domain_verify_processor";
48 ConfigParser parser;
49 if (parser.load(API_REPORT_CONFIG_PATH)) {
50 config.appId = parser.get("report_appId");
51 config.name = parser.get("report_name");
52 }
53 config.routeInfo = "AUTO";
54 config.triggerCond.timeout = TRIGGER_COND_TIMEOUT;
55 config.triggerCond.row = TRIGGER_COND_ROW;
56
57 APP_DOMAIN_VERIFY_HILOGD(APP_DOMAIN_VERIFY_MODULE_JS_NAPI,
58 "AddProcessor appId:%{public}s, name:%{public}s, routeInfo:%{public}s", config.appId.c_str(),
59 config.name.c_str(), config.routeInfo.c_str());
60
61 config.eventConfigs.clear();
62 {
63 HiviewDFX::HiAppEvent::EventConfig event1;
64 event1.domain = "api_diagnostic";
65 event1.name = "api_exec_end";
66 event1.isRealTime = false;
67 config.eventConfigs.push_back(event1);
68 }
69
70 {
71 HiviewDFX::HiAppEvent::EventConfig event2;
72 event2.domain = "api_diagnostic";
73 event2.name = "api_called_stat";
74 event2.isRealTime = true;
75 config.eventConfigs.push_back(event2);
76 }
77
78 {
79 HiviewDFX::HiAppEvent::EventConfig event3;
80 event3.domain = "api_diagnostic";
81 event3.name = "api_called_stat_cnt";
82 event3.isRealTime = true;
83 config.eventConfigs.push_back(event3);
84 }
85 return HiviewDFX::HiAppEvent::AppEventProcessorMgr::AddProcessor(config);
86 }
87
WriteEndEvent(const int result,const int32_t errCode)88 void ApiEventReporter::WriteEndEvent(const int result, const int32_t errCode)
89 {
90 int64_t endTime = std::chrono::duration_cast<std::chrono::milliseconds>(
91 std::chrono::system_clock::now().time_since_epoch())
92 .count();
93 HiviewDFX::HiAppEvent::Event event("api_diagnostic", "api_exec_end", HiviewDFX::HiAppEvent::BEHAVIOR);
94 event.AddParam("trans_id", transId_);
95 event.AddParam("api_name", apiName_);
96 event.AddParam("sdk_name", SDK_NAME);
97 event.AddParam("begin_time", startTime_);
98 event.AddParam("end_time", endTime);
99 event.AddParam("result", result);
100 event.AddParam("error_code", errCode);
101 int ret = Write(event);
102 APP_DOMAIN_VERIFY_HILOGD(APP_DOMAIN_VERIFY_MODULE_JS_NAPI,
103 "WriteEndEvent transId:%{public}s, apiName:%{public}s, sdkName:%{public}s, result:%{public}d, "
104 "errCode:%{public}d, ret:%{public}d",
105 transId_.c_str(), apiName_.c_str(), SDK_NAME.c_str(), result, errCode, ret);
106 }
107 }