Skip to content

Commit c37bf6d

Browse files
authored
[feature](selectdb-cloud) Add log annotation tag (apache#1584)
Support log annotation tag. Here is an example. ```c++ AnnotateTag tag_instance_id("instance_id", instance_id); // This should log: "Here is an example instance_id=\"test_instance\"" LOG_INFO("Here is an example"); ```
1 parent d775e6e commit c37bf6d

5 files changed

Lines changed: 178 additions & 0 deletions

File tree

‎cloud/src/common/logging.cpp‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "logging.h"
44
#include "config.h"
55

6+
#include <bthread/bthread.h>
7+
#include <bthread/types.h>
68
#include <glog/logging.h>
79
#include <glog/vlog_is_on.h>
810

@@ -12,6 +14,49 @@
1214

1315
namespace selectdb {
1416

17+
static butil::LinkedList<AnnotateTag>* get_annotate_tag_list() {
18+
static std::once_flag log_annotated_tags_key_once;
19+
static bthread_key_t log_annotated_tags_key;
20+
std::call_once(
21+
log_annotated_tags_key_once,
22+
+[](bthread_key_t* key) {
23+
bthread_key_create(
24+
key, +[](void* value) {
25+
delete reinterpret_cast<butil::LinkedList<AnnotateTag>*>(value);
26+
});
27+
},
28+
&log_annotated_tags_key);
29+
30+
auto* tag_list = reinterpret_cast<butil::LinkedList<AnnotateTag>*>(
31+
bthread_getspecific(log_annotated_tags_key));
32+
if (!tag_list) {
33+
tag_list = new butil::LinkedList<AnnotateTag>();
34+
bthread_setspecific(log_annotated_tags_key, tag_list);
35+
}
36+
return tag_list;
37+
}
38+
39+
AnnotateTag::AnnotateTag(default_tag_t, std::string_view key, std::string value)
40+
: key_(key), value_(std::move(value)) {
41+
get_annotate_tag_list()->Append(this);
42+
}
43+
44+
AnnotateTag::AnnotateTag(std::string_view key, std::string_view value)
45+
: AnnotateTag(default_tag, key, fmt::format("\"{}\"", value)) {}
46+
47+
AnnotateTag::~AnnotateTag() {
48+
RemoveFromList();
49+
}
50+
51+
void AnnotateTag::format_tag_list(std::ostream& stream) {
52+
butil::LinkedList<AnnotateTag>* list = get_annotate_tag_list();
53+
butil::LinkNode<AnnotateTag>* head = list->head();
54+
const butil::LinkNode<AnnotateTag>* end = list->end();
55+
for (; head != end; head = head->next()) {
56+
stream << ' ' << head->value()->key_ << '=' << head->value()->value_;
57+
}
58+
}
59+
1560
/**
1661
* @param basename the basename of log file
1762
* @return true for success

‎cloud/src/common/logging.h‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11

22
#pragma once
33

4+
#include <butil/containers/linked_list.h>
45
#include <fmt/format.h>
56
#include <glog/logging.h>
67

8+
#include <string_view>
79
#include <type_traits>
810

911
namespace selectdb {
@@ -17,6 +19,29 @@ bool init_glog(const char* basename);
1719
#define LOG_ERROR(...) ::selectdb::TaggableLogger(LOG(ERROR), ##__VA_ARGS__)
1820
#define LOG_FATAL(...) ::selectdb::TaggableLogger(LOG(FATAL), ##__VA_ARGS__)
1921

22+
class AnnotateTag final : public butil::LinkNode<AnnotateTag> {
23+
struct default_tag_t {};
24+
constexpr static default_tag_t default_tag {};
25+
26+
public:
27+
template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>, T>>
28+
AnnotateTag(std::string_view key, T value)
29+
: AnnotateTag(default_tag, key, std::to_string(value)) {}
30+
AnnotateTag(std::string_view key, std::string_view value);
31+
~AnnotateTag();
32+
33+
static void format_tag_list(std::ostream& stream);
34+
35+
static void* operator new(size_t) = delete;
36+
static void* operator new[](size_t) = delete;
37+
38+
private:
39+
explicit AnnotateTag(default_tag_t, std::string_view key, std::string value);
40+
41+
std::string_view key_;
42+
std::string value_;
43+
};
44+
2045
class TaggableLogger {
2146
public:
2247
template <typename... Args>
@@ -26,6 +51,7 @@ class TaggableLogger {
2651
} else {
2752
stream_ << fmt::format(fmt, std::forward<Args>(args)...);
2853
}
54+
AnnotateTag::format_tag_list(stream_);
2955
};
3056

3157
template <typename V>

‎cloud/src/meta-service/meta_service.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ void MetaServiceImpl::begin_txn(::google::protobuf::RpcController* controller,
289289
msg = ss.str();
290290
return;
291291
}
292+
292293
RPC_RATE_LIMIT(begin_txn)
293294
//1. Generate version stamp for txn id
294295
std::unique_ptr<Transaction> txn;

‎cloud/test/CMakeLists.txt‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ add_executable(encryption_test
5555
add_executable(metric_test
5656
metric_test.cpp
5757
)
58+
59+
add_executable(log_test
60+
log_test.cpp
61+
)
62+
5863
message("Meta-service test dependencies: ${TEST_LINK_LIBS}")
5964
target_link_libraries(sync_point_test
6065
${TEST_LINK_LIBS}
@@ -87,6 +92,11 @@ target_link_libraries(encryption_test
8792
target_link_libraries(metric_test
8893
${TEST_LINK_LIBS}
8994
)
95+
96+
target_link_libraries(log_test
97+
${TEST_LINK_LIBS}
98+
)
99+
90100
# FDB related tests need to be linked with libfdb_c
91101
set(FDB_LINKER_FLAGS "-lfdb_c -L${THIRDPARTY_DIR}/lib")
92102

‎cloud/test/log_test.cpp‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// clang-format off
2+
#include "common/logging.h"
3+
4+
#include <gtest/gtest.h>
5+
#include <bthread/bthread.h>
6+
7+
#include <cstring>
8+
#include <random>
9+
#include <thread>
10+
// clang-format on
11+
12+
using selectdb::AnnotateTag;
13+
using selectdb::TaggableLogger;
14+
15+
int main(int argc, char** argv) {
16+
if (!selectdb::init_glog("log_test")) {
17+
std::cerr << "failed to init glog" << std::endl;
18+
return -1;
19+
}
20+
::testing::InitGoogleTest(&argc, argv);
21+
return RUN_ALL_TESTS();
22+
}
23+
24+
TEST(LogTest, ConstructionTest) {
25+
// Heap allocation is disabled.
26+
// new AnnotateTag();
27+
28+
// Arithmetics
29+
{
30+
char c = 0;
31+
bool b = false;
32+
int8_t i8 = 0;
33+
uint8_t u8 = 0;
34+
int16_t i16 = 0;
35+
uint16_t u16 = 0;
36+
int32_t i32 = 0;
37+
uint32_t u32 = 0;
38+
int64_t i64 = 0;
39+
uint64_t u64 = 0;
40+
41+
AnnotateTag tag_char("char", c);
42+
AnnotateTag tag_bool("bool", b);
43+
AnnotateTag tag_i8("i8", i8);
44+
AnnotateTag tag_u8("u8", u8);
45+
AnnotateTag tag_i16("i16", i16);
46+
AnnotateTag tag_u16("u16", u16);
47+
AnnotateTag tag_i32("i32", i32);
48+
AnnotateTag tag_u32("u32", u32);
49+
AnnotateTag tag_i64("i64", i64);
50+
AnnotateTag tag_u64("u64", u64);
51+
LOG_INFO("hello");
52+
}
53+
54+
// String literals.
55+
{
56+
const char* text = "hello";
57+
AnnotateTag tag_text("hello", text);
58+
LOG_INFO("hello");
59+
}
60+
61+
// String view.
62+
{
63+
std::string test("abc");
64+
AnnotateTag tag_text("hello", std::string_view(test));
65+
LOG_INFO("hello");
66+
}
67+
68+
// Const string.
69+
{
70+
const std::string test("abc");
71+
AnnotateTag tag_text("hello", test);
72+
LOG_INFO("hello");
73+
}
74+
}
75+
76+
TEST(LogTest, ThreadTest) {
77+
// In pthread.
78+
{
79+
ASSERT_EQ(bthread_self(), 0);
80+
AnnotateTag tag("run_in_bthread", true);
81+
LOG_INFO("thread test");
82+
}
83+
84+
// In bthread.
85+
{
86+
auto fn = +[](void*) -> void* {
87+
EXPECT_NE(bthread_self(), 0);
88+
AnnotateTag tag("run_in_bthread", true);
89+
LOG_INFO("thread test");
90+
return nullptr;
91+
};
92+
bthread_t tid;
93+
ASSERT_EQ(bthread_start_background(&tid, nullptr, fn, nullptr), 0);
94+
ASSERT_EQ(bthread_join(tid, nullptr), 0);
95+
}
96+
}

0 commit comments

Comments
 (0)