diff --git a/observe-api/Makefile b/observe-api/Makefile index 2659d2e..98ad772 100644 --- a/observe-api/Makefile +++ b/observe-api/Makefile @@ -6,5 +6,5 @@ build: @cp test/rust/target/wasm32-wasi/debug/rust_guest.wasm test/rust_guest.wasm $(WASICC) -o test/c_guest.wasm -I c test/c/main.c $(WASICXX) -o test/cxx_guest.wasm -I c -x c++ test/c/main.c - $(WASICXX) -o test/cxx_guest_2.wasm -fno-exceptions -I c test/c/main.cpp - $(WASICXX) -o test/cxx_guest_3.wasm -fno-exceptions -I c test/c/main2.cpp \ No newline at end of file + $(WASICXX) -o test/cxx_guest_2.wasm -fno-exceptions -I cxx test/cxx/main.cpp + $(WASICXX) -o test/cxx_guest_3.wasm -fno-exceptions -I cxx test/cxx/main2.cpp \ No newline at end of file diff --git a/observe-api/README.md b/observe-api/README.md index e3008c6..aac252f 100644 --- a/observe-api/README.md +++ b/observe-api/README.md @@ -22,9 +22,9 @@ We currently provide these language bindings to this API: * [example](test/rust/src/main.rs) -### [c and c++](c/) +### [c](c/) and [c++](cxx/) -Both the C and C++ bindings are implemented as header-only libraries. To use the C bindings, +Both the C and C++ bindings are implemented as single header libraries. To use the C bindings, in __ONE__ source file: ```c @@ -39,17 +39,16 @@ In other source files, just `#include "observe_api.h"` To use the C++ bindings, instead, in __ONE__ source file: ```c++ -#define OBSERVE_API_IMPLEMENTATION #define OBSERVE_API_CPP_IMPLEMENTATION #include "observe_api.hpp" ``` In other source files, just `#include "observe_api.hpp"` -__NOTE:__ `observe_api.hpp` `#include`s `observe_api.h` +* [functional example](test/cxx/main.cpp) +* [OO example](test/cxx/main2.cpp) -* [functional example](test/c/main.cpp) -* [OO example](test/c/main2.cpp) +In C++, both bindings may be used at the same time without conflict. ### Other diff --git a/observe-api/c/observe_api.h b/observe-api/c/observe_api.h index 5296058..ff88ab7 100644 --- a/observe-api/c/observe_api.h +++ b/observe-api/c/observe_api.h @@ -3,8 +3,8 @@ #include -#define IMPORT(a, b) __attribute__((import_module(a), import_name(b))) - +#ifndef OBSERVE_API_ENUM +#define OBSERVE_API_ENUM enum DO_LOG_LEVEL { DO_LL_ERROR = 1, DO_LL_WARN = 2, @@ -14,6 +14,9 @@ enum DO_LOG_LEVEL { }; enum DO_METRIC_FMT { DO_MF_STATSD = 1 }; +#endif + +#define IMPORT(a, b) __attribute__((import_module(a), import_name(b))) IMPORT("dylibso:observe/api", "metric") extern void observe_api_metric_n(enum DO_METRIC_FMT format, const char *metric, @@ -28,6 +31,8 @@ extern void observe_api_span_exit(void); IMPORT("dylibso:observe/api", "span-tags") extern void observe_api_span_tags_n(const char *tags, size_t tags_length); +#undef IMPORT + #ifdef __cplusplus extern "C" { #endif diff --git a/observe-api/c/observe_api.hpp b/observe-api/cxx/observe_api.hpp similarity index 64% rename from observe-api/c/observe_api.hpp rename to observe-api/cxx/observe_api.hpp index f2635ae..c1ead64 100644 --- a/observe-api/c/observe_api.hpp +++ b/observe-api/cxx/observe_api.hpp @@ -1,11 +1,23 @@ #ifndef OBSERVE_API_HPP #define OBSERVE_API_HPP -#include "observe_api.h" #include #include #include +#ifndef OBSERVE_API_ENUM +#define OBSERVE_API_ENUM +enum DO_LOG_LEVEL { + DO_LL_ERROR = 1, + DO_LL_WARN = 2, + DO_LL_INFO = 3, + DO_LL_DEBUG = 4, + DO_LL_TRACE = 5 +}; + +enum DO_METRIC_FMT { DO_MF_STATSD = 1 }; +#endif + namespace observe_api { void span_enter(std::string_view name); void span_exit(); @@ -16,9 +28,17 @@ void statsd(std::string_view mtc); void span_tags(std::vector &tags); class Span { + bool ended; + public: - Span(std::string_view name) { span_enter(name); } - ~Span() { span_exit(); } + Span(std::string_view name) : ended(false) { span_enter(name); } + void end() { + if (!ended) { + ended = true; + span_exit(); + } + } + ~Span() { end(); } void metric(enum DO_METRIC_FMT format, std::string_view mtc) { observe_api::metric(format, mtc); } @@ -40,13 +60,29 @@ class Span { #ifndef OBSERVE_API_CPP #define OBSERVE_API_CPP -#include "observe_api.h" #include #include #include #include #include +#define IMPORT(a, b) __attribute__((import_module(a), import_name(b))) + +IMPORT("dylibso:observe/api", "metric") +extern void observe_api_metric_n(enum DO_METRIC_FMT format, const char *metric, + size_t metric_length); +IMPORT("dylibso:observe/api", "log") +extern void observe_api_log_n(enum DO_LOG_LEVEL level, const char *message, + size_t message_length); +IMPORT("dylibso:observe/api", "span-enter") +extern void observe_api_span_enter_n(const char *name, size_t name_length); +IMPORT("dylibso:observe/api", "span-exit") +extern void observe_api_span_exit(void); +IMPORT("dylibso:observe/api", "span-tags") +extern void observe_api_span_tags_n(const char *tags, size_t tags_length); + +#undef IMPORT + namespace observe_api { void span_enter(std::string_view name) { @@ -68,7 +104,7 @@ void span_tags(std::string_view tags) { } void statsd(std::string_view mtc) { - observe_api_statsd_n(mtc.data(), mtc.size()); + observe_api_metric_n(DO_MF_STATSD, mtc.data(), mtc.size()); } void span_tags(std::vector &tags) { diff --git a/observe-api/test/cxx/main.cpp b/observe-api/test/cxx/main.cpp new file mode 100644 index 0000000..da04d37 --- /dev/null +++ b/observe-api/test/cxx/main.cpp @@ -0,0 +1,24 @@ +#define OBSERVE_API_CPP_IMPLEMENTATION +#include "observe_api.hpp" +#include +#include +#include +#include + +void run() { + observe_api::span_enter("printf"); + observe_api::statsd("ok:aaaaa"); + observe_api::log(DO_LL_INFO, "bbbbb"); + observe_api::span_tags("abbc:def,(another:tag"); + std::vector tags = {"taga:one", "tagb:two"}; + observe_api::span_tags(tags); + printf("Hello from Wasm!\n"); + observe_api::span_exit(); +} + +int main(int argc, char *argv[]) { + observe_api::span_enter("run"); + run(); + observe_api::span_exit(); + return 0; +} diff --git a/observe-api/test/c/main2.cpp b/observe-api/test/cxx/main2.cpp similarity index 93% rename from observe-api/test/c/main2.cpp rename to observe-api/test/cxx/main2.cpp index db732f3..887c4df 100644 --- a/observe-api/test/c/main2.cpp +++ b/observe-api/test/cxx/main2.cpp @@ -1,4 +1,3 @@ -#define OBSERVE_API_IMPLEMENTATION #define OBSERVE_API_CPP_IMPLEMENTATION #include "observe_api.hpp" #include diff --git a/observe-api/test/cxx_guest_2.wasm b/observe-api/test/cxx_guest_2.wasm index 812eecf..0e41387 100755 Binary files a/observe-api/test/cxx_guest_2.wasm and b/observe-api/test/cxx_guest_2.wasm differ diff --git a/observe-api/test/cxx_guest_3.wasm b/observe-api/test/cxx_guest_3.wasm index b159b8a..f7f6901 100755 Binary files a/observe-api/test/cxx_guest_3.wasm and b/observe-api/test/cxx_guest_3.wasm differ