-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: observe api c++ bindings / improve observe api c bindings (#165)
* feat: improve observe api c bindings * namespace imports and give them `n` suffix to signify length param * use stronger typing on imports to type issues at compile time * rename observe_api_metric to observe_api_statsd * add observe_api_metric taking a format param * add observe_api_statsd_n * rename observe_api_write_log to observe_api_log for consistency * feat: add span tags to observe api c bindings * feat: convert observe_api to single header library * feat: start c++ observe api bindings * feat(c++ observe api): Span class * docs: add c and c++ header only library usage info
- Loading branch information
Showing
11 changed files
with
249 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#ifndef OBSERVE_API_HPP | ||
#define OBSERVE_API_HPP | ||
|
||
#include "observe_api.h" | ||
#include <string> | ||
#include <string_view> | ||
#include <vector> | ||
|
||
namespace observe_api { | ||
void span_enter(std::string_view name); | ||
void span_exit(); | ||
void metric(enum DO_METRIC_FMT format, std::string_view mtc); | ||
void log(enum DO_LOG_LEVEL level, std::string_view message); | ||
void span_tags(std::string_view tags); | ||
void statsd(std::string_view mtc); | ||
void span_tags(std::vector<std::string> &tags); | ||
|
||
class Span { | ||
public: | ||
Span(std::string_view name) { span_enter(name); } | ||
~Span() { span_exit(); } | ||
void metric(enum DO_METRIC_FMT format, std::string_view mtc) { | ||
observe_api::metric(format, mtc); | ||
} | ||
void tags(std::string_view tags) { span_tags(tags); } | ||
void statsd(std::string_view mtc) { observe_api::statsd(mtc); } | ||
void tags(std::vector<std::string> &tags) { span_tags(tags); } | ||
}; | ||
}; // namespace observe_api | ||
|
||
#endif // OBSERVE_API_HPP | ||
|
||
// avoid greying out the implementation section | ||
#if defined(Q_CREATOR_RUN) || defined(__INTELLISENSE__) || \ | ||
defined(_CDT_PARSER__) | ||
#define OBSERVE_API_CPP_IMPLEMENTATION | ||
#endif | ||
|
||
#ifdef OBSERVE_API_CPP_IMPLEMENTATION | ||
#ifndef OBSERVE_API_CPP | ||
#define OBSERVE_API_CPP | ||
|
||
#include "observe_api.h" | ||
#include <iterator> | ||
#include <sstream> | ||
#include <string> | ||
#include <string_view> | ||
#include <vector> | ||
|
||
namespace observe_api { | ||
|
||
void span_enter(std::string_view name) { | ||
observe_api_span_enter_n(name.data(), name.size()); | ||
} | ||
|
||
void span_exit() { observe_api_span_exit(); } | ||
|
||
void metric(enum DO_METRIC_FMT format, std::string_view mtc) { | ||
observe_api_metric_n(format, mtc.data(), mtc.size()); | ||
} | ||
|
||
void log(enum DO_LOG_LEVEL level, std::string_view message) { | ||
observe_api_log_n(level, message.data(), message.size()); | ||
} | ||
|
||
void span_tags(std::string_view tags) { | ||
observe_api_span_tags_n(tags.data(), tags.size()); | ||
} | ||
|
||
void statsd(std::string_view mtc) { | ||
observe_api_statsd_n(mtc.data(), mtc.size()); | ||
} | ||
|
||
void span_tags(std::vector<std::string> &tags) { | ||
const char *delim = ","; | ||
std::ostringstream imploded; | ||
std::copy(tags.begin(), tags.end(), | ||
std::ostream_iterator<std::string>(imploded, delim)); | ||
std::string str = imploded.str(); | ||
if (str.size() > 0) { | ||
observe_api_span_tags_n(str.data(), str.size() - 1); | ||
} | ||
} | ||
|
||
}; // namespace observe_api | ||
|
||
#endif // OBSERVE_API_CPP | ||
#endif // OBSERVE_API_CPP_IMPLEMENTATION |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#define OBSERVE_API_IMPLEMENTATION | ||
#define OBSERVE_API_CPP_IMPLEMENTATION | ||
#include "observe_api.hpp" | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string> | ||
#include <vector> | ||
|
||
void run() { | ||
auto span = observe_api::Span("printf"); | ||
span.statsd("ok:aaaaa"); | ||
observe_api::log(DO_LL_INFO, "bbbbb"); | ||
span.tags("abbc:def,(another:tag"); | ||
std::vector<std::string> tags = {"taga:one", "tagb:two"}; | ||
span.tags(tags); | ||
printf("Hello from Wasm!\n"); | ||
} | ||
|
||
int main(int argc, char *argv[]) { | ||
auto span = observe_api::Span("run"); | ||
run(); | ||
return 0; | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.