Make native metrics logger write to statsd socket

Bug: 110537511
Test: compiles without failures and verified the correct metric
Change-Id: Ie8019a20a2112ed6cbbc0999d68592efb8f0f538
This commit is contained in:
Howard Ro 2018-10-15 21:42:34 -07:00
parent 89bf115a70
commit 34bc567a93
5 changed files with 53 additions and 5 deletions

View file

@ -11,7 +11,11 @@ cc_defaults {
export_include_dirs: ["include"],
local_include_dirs: ["include"],
shared_libs: ["liblog"],
shared_libs: [
"libbase",
"liblog",
"libstatssocket",
],
whole_static_libs: ["libgtest_prod"],
cflags: [
@ -23,17 +27,20 @@ cc_defaults {
// metricslogger shared library
// -----------------------------------------------------------------------------
cc_library_shared {
cc_library {
name: "libmetricslogger",
srcs: metricslogger_lib_src_files,
defaults: ["metricslogger_defaults"],
export_shared_lib_headers: ["libstatssocket"],
}
// static version of libmetricslogger, needed by a few art static binaries
// TODO(b/117829226): Remove once dependencies are cleaned up.
cc_library_static {
name: "libmetricslogger_static",
srcs: metricslogger_lib_src_files,
defaults: ["metricslogger_defaults"],
export_shared_lib_headers: ["libstatssocket"],
}
// metricslogger shared library, debug

View file

@ -15,6 +15,7 @@
*/
#include <log/log_event_list.h>
#include <stats_event_list.h>
#include <cstdint>
#include <string>
@ -43,6 +44,7 @@ void LogMultiAction(int32_t category, int32_t field, const std::string& value);
class ComplexEventLogger {
private:
android_log_event_list logger;
stats_event_list stats_logger;
public:
// Create a complex event with category|category|.

View file

@ -18,11 +18,15 @@
#include <cstdlib>
#include <android-base/chrono_utils.h>
#include <log/event_tag_map.h>
#include <log/log_event_list.h>
using namespace android;
namespace {
const static int kStatsEventTag = 1937006964;
const static int kKeyValuePairAtomId = 83;
#ifdef __ANDROID__
EventTagMap* kEventTagMap = android_openEventTagMap(nullptr);
const int kSysuiMultiActionTag = android_lookupEventTagNum(
@ -32,6 +36,12 @@ const int kSysuiMultiActionTag = android_lookupEventTagNum(
const int kSysuiMultiActionTag = 0;
#endif
int64_t getElapsedTimeNanoSinceBoot() {
return std::chrono::duration_cast<std::chrono::nanoseconds>(
android::base::boot_clock::now().time_since_epoch())
.count();
}
} // namespace
namespace android {
@ -42,6 +52,12 @@ void LogHistogram(const std::string& event, int32_t data) {
android_log_event_list log(kSysuiMultiActionTag);
log << LOGBUILDER_CATEGORY << LOGBUILDER_HISTOGRAM << LOGBUILDER_NAME << event
<< LOGBUILDER_BUCKET << data << LOGBUILDER_VALUE << 1 << LOG_ID_EVENTS;
stats_event_list stats_log(kStatsEventTag);
stats_log << getElapsedTimeNanoSinceBoot() << kKeyValuePairAtomId << LOGBUILDER_CATEGORY
<< LOGBUILDER_HISTOGRAM << LOGBUILDER_NAME << event << LOGBUILDER_BUCKET << data
<< LOGBUILDER_VALUE << 1;
stats_log.write(LOG_ID_STATS);
}
// Mirror com.android.internal.logging.MetricsLogger#count().
@ -49,6 +65,11 @@ void LogCounter(const std::string& name, int32_t val) {
android_log_event_list log(kSysuiMultiActionTag);
log << LOGBUILDER_CATEGORY << LOGBUILDER_COUNTER << LOGBUILDER_NAME << name << LOGBUILDER_VALUE
<< val << LOG_ID_EVENTS;
stats_event_list stats_log(kStatsEventTag);
stats_log << getElapsedTimeNanoSinceBoot() << kKeyValuePairAtomId << LOGBUILDER_CATEGORY
<< LOGBUILDER_COUNTER << LOGBUILDER_NAME << name << LOGBUILDER_VALUE << val;
stats_log.write(LOG_ID_STATS);
}
// Mirror com.android.internal.logging.MetricsLogger#action().
@ -56,34 +77,48 @@ void LogMultiAction(int32_t category, int32_t field, const std::string& value) {
android_log_event_list log(kSysuiMultiActionTag);
log << LOGBUILDER_CATEGORY << category << LOGBUILDER_TYPE << TYPE_ACTION
<< field << value << LOG_ID_EVENTS;
stats_event_list stats_log(kStatsEventTag);
stats_log << getElapsedTimeNanoSinceBoot() << kKeyValuePairAtomId << LOGBUILDER_CATEGORY
<< category << LOGBUILDER_TYPE << TYPE_ACTION << field << value;
stats_log.write(LOG_ID_STATS);
}
ComplexEventLogger::ComplexEventLogger(int category) : logger(kSysuiMultiActionTag) {
ComplexEventLogger::ComplexEventLogger(int category)
: logger(kSysuiMultiActionTag), stats_logger(kStatsEventTag) {
logger << LOGBUILDER_CATEGORY << category;
stats_logger << getElapsedTimeNanoSinceBoot() << kKeyValuePairAtomId << LOGBUILDER_CATEGORY
<< category;
}
void ComplexEventLogger::SetPackageName(const std::string& package_name) {
logger << LOGBUILDER_PACKAGENAME << package_name;
stats_logger << LOGBUILDER_PACKAGENAME << package_name;
}
void ComplexEventLogger::AddTaggedData(int tag, int32_t value) {
logger << tag << value;
stats_logger << tag << value;
}
void ComplexEventLogger::AddTaggedData(int tag, const std::string& value) {
logger << tag << value;
stats_logger << tag << value;
}
void ComplexEventLogger::AddTaggedData(int tag, int64_t value) {
logger << tag << value;
stats_logger << tag << value;
}
void ComplexEventLogger::AddTaggedData(int tag, float value) {
logger << tag << value;
stats_logger << tag << value;
}
void ComplexEventLogger::Record() {
logger << LOG_ID_EVENTS;
stats_logger.write(LOG_ID_STATS);
}
} // namespace metricslogger

View file

@ -17,12 +17,13 @@
// ==========================================================
// Native library to write stats log to statsd socket
// ==========================================================
cc_library_static {
cc_library {
name: "libstatssocket",
srcs: [
"stats_event_list.c",
"statsd_writer.c",
],
host_supported: true,
cflags: [
"-Wall",
"-Werror",
@ -32,6 +33,7 @@ cc_library_static {
],
export_include_dirs: ["include"],
shared_libs: [
"libcutils",
"liblog",
],
}

View file

@ -15,7 +15,9 @@
*/
#include "statsd_writer.h"
#include <cutils/fs.h>
#include <cutils/sockets.h>
#include <cutils/threads.h>
#include <endian.h>
#include <errno.h>
#include <fcntl.h>