Commit dec5547e authored by DEPRECATED (Jonathan Mace) (Use @JonathanMace instead)'s avatar DEPRECATED (Jonathan Mace) (Use @JonathanMace instead)
Browse files

Add some handy macros for logging baggage invocations

parent 922196cd
......@@ -57,10 +57,19 @@ struct Baggage {
};
#define GET_CURRENT_BAGGAGE() ThreadLocalBaggage::Get(__FILE__, __LINE__)
#define TAKE_CURRENT_BAGGAGE() ThreadLocalBaggage::Take(__FILE__, __LINE__)
#define BRANCH_CURRENT_BAGGAGE() ThreadLocalBaggage::Branch(__FILE__, __LINE__)
#define DELETE_CURRENT_BAGGAGE() ThreadLocalBaggage::Delete(__FILE__, __LINE__)
#define SET_CURRENT_BAGGAGE(b) ThreadLocalBaggage::Set(b, __FILE__, __LINE__)
#define JOIN_CURRENT_BAGGAGE(b) ThreadLocalBaggage::Join(b, __FILE__, __LINE__)
#define SWAP_CURRENT_BAGGAGE(b) ThreadLocalBaggage::Swap(b, __FILE__, __LINE__)
// Helper methods for storing a baggage instance in thread-local storage. Usage is optional, but very useful
namespace ThreadLocalBaggage {
Baggage& Get(); // Get the current thread's baggage
Baggage& Get(); // Get the current thread's baggage
Baggage Take(); // Get the current thread's baggage, and clear the thread-local storage
Baggage Branch(); // Get a copy of the current thread's baggage
......@@ -69,6 +78,17 @@ namespace ThreadLocalBaggage {
void Join(Baggage &otherBaggage); // Merge the current thread's baggage with the provided baggage
Baggage Swap (Baggage otherBaggage); // Set the current thread's baggage to the provided baggage, and return the previous baggage
Baggage& Get(const char* file, int line);
Baggage Take(const char* file, int line);
Baggage Branch(const char* file, int line);
void Delete(const char* file, int line);
void Set(Baggage new_baggage, const char* file, int line);
void Join(Baggage &otherBaggage, const char* file, int line);
Baggage Swap (Baggage otherBaggage, const char* file, int line);
}
......
......@@ -2,21 +2,20 @@
#ifndef _XTRACE_H_
#define _XTRACE_H_
#include <cstring>
#include <string>
#include <vector>
#include <map>
#define XTRACE_REPORT_PROTOBUF_TOPIC "xtpb"
#define __XTRACE_FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
#define __SHORT_FILENAME__(x) (strrchr(x, '/') ? strrchr(x, '/') + 1 : x)
// Macro overloading fuckery
#define XTRACE1(msg) XTrace::Logger(__XTRACE_FILENAME__).log(msg, __FILE__, __LINE__)
#define XTRACE2(logger, msg) logger.log(msg, __FILE__, __LINE__)
#define GET_XTRACE_MACRO(_1,_2,NAME,...) NAME
#define XTRACE(...) GET_XTRACE_MACRO(__VA_ARGS__,XTRACE2,XTRACE1)(__VA_ARGS__)
#define XTRACE2(f, l, ...) XTrace::Logger(__SHORT_FILENAME__(f)).log(f, l, __VA_ARGS__)
#define XTRACE(...) XTRACE2(__FILE__, __LINE__, __VA_ARGS__)
namespace XTrace {
......@@ -28,7 +27,9 @@ public:
void log(std::string message);
void log(std::string message, std::string file, int line);
void log(std::string file, int line, std::string message);
void log(std::string file, int line, std::string message, std::map<std::string, std::string> annotations);
};
......
......@@ -7,6 +7,7 @@
#include "xtrace_baggage.h"
#include "lexvarint.h"
#include "baggageprotocol.h"
#include <map>
void test_vector(std::vector<int> testv) {
testv[0] = 5;
......@@ -21,27 +22,30 @@ void printvector(std::vector<uint8_t> bytes) {
}
int main(int argc, char *argv[]) {
XTRACE("a");
XTrace::StartTrace("main.cc");
std::map<std::string, std::string> mymap = {{"key1", "value1"}, {"key2", "value2"}};
XTRACE("a");
XTRACE("b");
XTRACE("c");
XTRACE("c", {{"key1", "value1"}, {"key2", "value2"}});
Baggage branched_baggage = ThreadLocalBaggage::Branch();
Baggage branched_baggage = BRANCH_CURRENT_BAGGAGE();
std::thread branched_thread([&branched_baggage]() {
ThreadLocalBaggage::Set(branched_baggage);
SET_CURRENT_BAGGAGE(branched_baggage);
XTRACE("f");
XTRACE("g");
branched_baggage = ThreadLocalBaggage::Take();
branched_baggage = TAKE_CURRENT_BAGGAGE();
});
XTRACE("d");
XTRACE("e");
branched_thread.join();
ThreadLocalBaggage::Join(branched_baggage);
JOIN_CURRENT_BAGGAGE(branched_baggage);
XTRACE("h");
......
#include "baggage.h"
#include "xtrace.h"
thread_local Baggage current_threads_baggage;
......@@ -8,29 +9,83 @@ Baggage& ThreadLocalBaggage::Get() {
}
Baggage ThreadLocalBaggage::Take() {
XTRACE("ThreadLocalBaggage::Take", {{"operation", "unset"}});
Baggage b = current_threads_baggage;
current_threads_baggage = Baggage();
return b;
}
Baggage ThreadLocalBaggage::Branch() {
XTRACE("ThreadLocalBaggage::Branch", {{"operation", "fork"}});
return current_threads_baggage;
}
void ThreadLocalBaggage::Join(Baggage &otherBaggage) {
current_threads_baggage = Baggage::merge(current_threads_baggage, otherBaggage);
XTRACE("ThreadLocalBaggage::Join", {{"operation", "fork"}});
}
void ThreadLocalBaggage::Set(Baggage new_baggage) {
current_threads_baggage = new_baggage;
void ThreadLocalBaggage::Set(Baggage new_baggage) {
if (current_threads_baggage.atoms.size() == 0) {
current_threads_baggage = new_baggage;
XTRACE("ThreadLocalBaggage::Set", {{"operation", "set"}});
} else {
current_threads_baggage = new_baggage;
}
}
Baggage ThreadLocalBaggage::Swap(Baggage other_baggage) {
XTRACE("ThreadLocalBaggage::Swap", {{"operation", "unset"}});
Baggage prev = current_threads_baggage;
current_threads_baggage = other_baggage;
XTRACE("ThreadLocalBaggage::Swap", {{"operation", "set"}});
return prev;
}
void ThreadLocalBaggage::Delete() {
XTRACE("ThreadLocalBaggage::Delete", {{"operation", "unset"}});
current_threads_baggage = Baggage();
}
Baggage& ThreadLocalBaggage::Get(const char* file, int line) {
return current_threads_baggage;
}
Baggage ThreadLocalBaggage::Take(const char* file, int line) {
XTRACE2(file, line, "ThreadLocalBaggage::Take", {{"operation", "unset"}});
Baggage b = current_threads_baggage;
current_threads_baggage = Baggage();
return b;
}
Baggage ThreadLocalBaggage::Branch(const char* file, int line) {
XTRACE2(file, line, "ThreadLocalBaggage::Branch", {{"operation", "fork"}});
return current_threads_baggage;
}
void ThreadLocalBaggage::Join(Baggage &otherBaggage, const char* file, int line) {
current_threads_baggage = Baggage::merge(current_threads_baggage, otherBaggage);
XTRACE2(file, line, "ThreadLocalBaggage::Join", {{"operation", "fork"}});
}
void ThreadLocalBaggage::Set(Baggage new_baggage, const char* file, int line) {
if (current_threads_baggage.atoms.size() == 0) {
current_threads_baggage = new_baggage;
XTRACE2(file, line, "ThreadLocalBaggage::Set", {{"operation", "set"}});
} else {
current_threads_baggage = new_baggage;
}
}
Baggage ThreadLocalBaggage::Swap(Baggage other_baggage, const char* file, int line) {
XTRACE2(file, line, "ThreadLocalBaggage::Swap", {{"operation", "unset"}});
Baggage prev = current_threads_baggage;
current_threads_baggage = other_baggage;
XTRACE2(file, line, "ThreadLocalBaggage::Swap", {{"operation", "set"}});
return prev;
}
void ThreadLocalBaggage::Delete(const char* file, int line) {
XTRACE2(file, line, "ThreadLocalBaggage::Delete", {{"operation", "unset"}});
current_threads_baggage = Baggage();
}
......@@ -80,7 +80,7 @@ void XTrace::Logger::log(std::string message) {
sendReport(report);
}
void XTrace::Logger::log(std::string message, std::string file, int line) {
void XTrace::Logger::log(std::string file, int line, std::string message) {
if (!XTraceBaggage::HasTaskID()) {
return;
}
......@@ -97,6 +97,28 @@ void XTrace::Logger::log(std::string message, std::string file, int line) {
sendReport(report);
}
void XTrace::Logger::log(std::string file, int line, std::string message, std::map<std::string, std::string> annotations) {
if (!XTraceBaggage::HasTaskID()) {
return;
}
XTraceReportv4 report = makeReport();
report.set_agent(this->agent);
report.set_label(message);
std::ostringstream ss;
ss << file << ":" << line;
std::string source = ss.str();
report.set_source(source);
for (std::map<std::string, std::string>::iterator it = annotations.begin(); it != annotations.end(); it++) {
report.add_key(it->first);
report.add_value(it->second);
}
sendReport(report);
}
void XTrace::StartTrace() {
XTraceBaggage::Clear(); // Clear old X-Trace metadata if it exists
XTraceBaggage::SetTaskID(make_event_id());
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment