Summary: Here's my proposal for making our LOGs easier to read by machines. The idea is to dump all events as JSON objects. JSON is easy to read by humans, but more importantly, it's easy to read by machines. That way, we can parse this, load into SQLite/mongo and then query or visualize. I started with table_create and table_delete events, but if everybody agrees, I'll continue by adding more events (flush/compaction/etc etc) Test Plan: Ran db_bench. Observed: 2015/01/15-14:13:25.788019 1105ef000 EVENT_LOG_v1 {"time_micros": 1421360005788015, "event": "table_file_creation", "file_number": 12, "file_size": 1909699} 2015/01/15-14:13:25.956500 110740000 EVENT_LOG_v1 {"time_micros": 1421360005956498, "event": "table_file_deletion", "file_number": 12} Reviewers: yhchiang, rven, dhruba, MarkCallaghan, lgalanis, sdong Reviewed By: sdong Subscribers: dhruba, leveldb Differential Revision: https://reviews.facebook.net/D31647main
parent
756532daf5
commit
52d8347a91
@ -0,0 +1,35 @@ |
|||||||
|
// Copyright (c) 2014, Facebook, Inc. All rights reserved.
|
||||||
|
// This source code is licensed under the BSD-style license found in the
|
||||||
|
// LICENSE file in the root directory of this source tree. An additional grant
|
||||||
|
// of patent rights can be found in the PATENTS file in the same directory.
|
||||||
|
|
||||||
|
#ifndef __STDC_FORMAT_MACROS |
||||||
|
#define __STDC_FORMAT_MACROS |
||||||
|
#endif |
||||||
|
|
||||||
|
#include "util/event_logger.h" |
||||||
|
|
||||||
|
#include <inttypes.h> |
||||||
|
#include <cassert> |
||||||
|
#include <chrono> |
||||||
|
#include <sstream> |
||||||
|
#include <string> |
||||||
|
|
||||||
|
#include "util/string_util.h" |
||||||
|
|
||||||
|
namespace rocksdb { |
||||||
|
|
||||||
|
const char* kEventLoggerPrefix = "EVENT_LOG_v1"; |
||||||
|
|
||||||
|
EventLoggerStream::EventLoggerStream(Logger* logger) |
||||||
|
: logger_(logger), json_writter_(nullptr) {} |
||||||
|
|
||||||
|
EventLoggerStream::~EventLoggerStream() { |
||||||
|
if (json_writter_) { |
||||||
|
json_writter_->EndObject(); |
||||||
|
Log(logger_, "%s %s", kEventLoggerPrefix, json_writter_->Get().c_str()); |
||||||
|
delete json_writter_; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} // namespace rocksdb
|
@ -0,0 +1,158 @@ |
|||||||
|
// Copyright (c) 2014, Facebook, Inc. All rights reserved.
|
||||||
|
// This source code is licensed under the BSD-style license found in the
|
||||||
|
// LICENSE file in the root directory of this source tree. An additional grant
|
||||||
|
// of patent rights can be found in the PATENTS file in the same directory.
|
||||||
|
|
||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <memory> |
||||||
|
#include <sstream> |
||||||
|
#include <string> |
||||||
|
|
||||||
|
#include "rocksdb/env.h" |
||||||
|
|
||||||
|
namespace rocksdb { |
||||||
|
|
||||||
|
// JSONWritter doesn't support objects in arrays yet. There wasn't a need for
|
||||||
|
// that.
|
||||||
|
class JSONWritter { |
||||||
|
public: |
||||||
|
JSONWritter() : state_(kExpectKey), first_element_(true) { stream_ << "{"; } |
||||||
|
|
||||||
|
void AddKey(const std::string& key) { |
||||||
|
assert(state_ == kExpectKey); |
||||||
|
if (!first_element_) { |
||||||
|
stream_ << ", "; |
||||||
|
} |
||||||
|
stream_ << "\"" << key << "\": "; |
||||||
|
state_ = kExpectValue; |
||||||
|
first_element_ = false; |
||||||
|
} |
||||||
|
|
||||||
|
void AddValue(const char* value) { |
||||||
|
assert(state_ == kExpectValue || state_ == kInArray); |
||||||
|
if (state_ == kInArray && !first_element_) { |
||||||
|
stream_ << ", "; |
||||||
|
} |
||||||
|
stream_ << "\"" << value << "\""; |
||||||
|
if (state_ != kInArray) { |
||||||
|
state_ = kExpectKey; |
||||||
|
} |
||||||
|
first_element_ = false; |
||||||
|
} |
||||||
|
|
||||||
|
template <typename T> |
||||||
|
void AddValue(const T& value) { |
||||||
|
assert(state_ == kExpectValue || state_ == kInArray); |
||||||
|
if (state_ == kInArray && !first_element_) { |
||||||
|
stream_ << ", "; |
||||||
|
} |
||||||
|
stream_ << value; |
||||||
|
if (state_ != kInArray) { |
||||||
|
state_ = kExpectKey; |
||||||
|
} |
||||||
|
first_element_ = false; |
||||||
|
} |
||||||
|
|
||||||
|
void StartArray() { |
||||||
|
assert(state_ == kExpectKey); |
||||||
|
state_ = kInArray; |
||||||
|
if (!first_element_) { |
||||||
|
stream_ << ", "; |
||||||
|
} |
||||||
|
stream_ << "["; |
||||||
|
first_element_ = true; |
||||||
|
} |
||||||
|
|
||||||
|
void EndArray() { |
||||||
|
assert(state_ == kInArray); |
||||||
|
state_ = kExpectKey; |
||||||
|
stream_ << "]"; |
||||||
|
first_element_ = false; |
||||||
|
} |
||||||
|
|
||||||
|
void StartObject() { |
||||||
|
assert(state_ == kExpectValue); |
||||||
|
stream_ << "{"; |
||||||
|
first_element_ = true; |
||||||
|
} |
||||||
|
|
||||||
|
void EndObject() { |
||||||
|
assert(state_ == kExpectKey); |
||||||
|
stream_ << "}"; |
||||||
|
first_element_ = false; |
||||||
|
} |
||||||
|
|
||||||
|
std::string Get() const { return stream_.str(); } |
||||||
|
|
||||||
|
JSONWritter& operator<<(const char* val) { |
||||||
|
if (state_ == kExpectKey) { |
||||||
|
AddKey(val); |
||||||
|
} else { |
||||||
|
AddValue(val); |
||||||
|
} |
||||||
|
return *this; |
||||||
|
} |
||||||
|
|
||||||
|
JSONWritter& operator<<(const std::string& val) { |
||||||
|
return *this << val.c_str(); |
||||||
|
} |
||||||
|
|
||||||
|
template <typename T> |
||||||
|
JSONWritter& operator<<(const T& val) { |
||||||
|
assert(state_ != kExpectKey); |
||||||
|
AddValue(val); |
||||||
|
return *this; |
||||||
|
} |
||||||
|
|
||||||
|
private: |
||||||
|
enum JSONWritterState { |
||||||
|
kExpectKey, |
||||||
|
kExpectValue, |
||||||
|
kInArray, |
||||||
|
}; |
||||||
|
JSONWritterState state_; |
||||||
|
bool first_element_; |
||||||
|
std::ostringstream stream_; |
||||||
|
}; |
||||||
|
|
||||||
|
class EventLoggerStream { |
||||||
|
public: |
||||||
|
template <typename T> |
||||||
|
EventLoggerStream& operator<<(const T& val) { |
||||||
|
MakeStream(); |
||||||
|
*json_writter_ << val; |
||||||
|
return *this; |
||||||
|
} |
||||||
|
~EventLoggerStream(); |
||||||
|
|
||||||
|
private: |
||||||
|
void MakeStream() { |
||||||
|
if (!json_writter_) { |
||||||
|
json_writter_ = new JSONWritter(); |
||||||
|
*this << "time_micros" |
||||||
|
<< std::chrono::duration_cast<std::chrono::microseconds>( |
||||||
|
std::chrono::system_clock::now().time_since_epoch()).count(); |
||||||
|
} |
||||||
|
} |
||||||
|
friend class EventLogger; |
||||||
|
explicit EventLoggerStream(Logger* logger); |
||||||
|
Logger* logger_; |
||||||
|
// ownership
|
||||||
|
JSONWritter* json_writter_; |
||||||
|
}; |
||||||
|
|
||||||
|
// here is an example of the output that will show up in the LOG:
|
||||||
|
// 2015/01/15-14:13:25.788019 1105ef000 EVENT_LOG_v1 {"time_micros":
|
||||||
|
// 1421360005788015, "event": "table_file_creation", "file_number": 12,
|
||||||
|
// "file_size": 1909699}
|
||||||
|
class EventLogger { |
||||||
|
public: |
||||||
|
explicit EventLogger(Logger* logger) : logger_(logger) {} |
||||||
|
EventLoggerStream Log() { return EventLoggerStream(logger_); } |
||||||
|
|
||||||
|
private: |
||||||
|
Logger* logger_; |
||||||
|
}; |
||||||
|
|
||||||
|
} // namespace rocksdb
|
@ -0,0 +1,42 @@ |
|||||||
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
||||||
|
// This source code is licensed under the BSD-style license found in the
|
||||||
|
// LICENSE file in the root directory of this source tree. An additional grant
|
||||||
|
// of patent rights can be found in the PATENTS file in the same directory.
|
||||||
|
|
||||||
|
#include <string> |
||||||
|
|
||||||
|
#include "util/event_logger.h" |
||||||
|
#include "util/testharness.h" |
||||||
|
|
||||||
|
namespace rocksdb { |
||||||
|
|
||||||
|
class EventLoggerTest {}; |
||||||
|
|
||||||
|
class StringLogger : public Logger { |
||||||
|
public: |
||||||
|
using Logger::Logv; |
||||||
|
virtual void Logv(const char* format, va_list ap) override { |
||||||
|
vsnprintf(buffer_, sizeof(buffer_), format, ap); |
||||||
|
} |
||||||
|
char* buffer() { return buffer_; } |
||||||
|
|
||||||
|
private: |
||||||
|
char buffer_[1000]; |
||||||
|
}; |
||||||
|
|
||||||
|
TEST(EventLoggerTest, SimpleTest) { |
||||||
|
StringLogger logger; |
||||||
|
EventLogger event_logger(&logger); |
||||||
|
event_logger.Log() << "id" << 5 << "event" |
||||||
|
<< "just_testing"; |
||||||
|
std::string output(logger.buffer()); |
||||||
|
ASSERT_TRUE(output.find("\"event\": \"just_testing\"") != std::string::npos); |
||||||
|
ASSERT_TRUE(output.find("\"id\": 5") != std::string::npos); |
||||||
|
ASSERT_TRUE(output.find("\"time_micros\"") != std::string::npos); |
||||||
|
} |
||||||
|
|
||||||
|
} // namespace rocksdb
|
||||||
|
|
||||||
|
int main(int argc, char** argv) { |
||||||
|
return rocksdb::test::RunAllTests(); |
||||||
|
} |
Loading…
Reference in new issue