Skip to content

Commit

Permalink
feat(observe api): seperate c++ bindings from c bindings, add c++ spa…
Browse files Browse the repository at this point in the history
…n end function (#168)

* feat(observe api): seperate c++ bindings from c bindings

* feat: optional .end function to end spans, destruction still ends if not ended
  • Loading branch information
G4Vi authored Aug 8, 2024
1 parent 45a56f5 commit 313748c
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 16 deletions.
4 changes: 2 additions & 2 deletions observe-api/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
$(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
11 changes: 5 additions & 6 deletions observe-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
9 changes: 7 additions & 2 deletions observe-api/c/observe_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#include <stdint.h>

#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,
Expand All @@ -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,
Expand All @@ -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
Expand Down
46 changes: 41 additions & 5 deletions observe-api/c/observe_api.hpp → observe-api/cxx/observe_api.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
#ifndef OBSERVE_API_HPP
#define OBSERVE_API_HPP

#include "observe_api.h"
#include <string>
#include <string_view>
#include <vector>

#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();
Expand All @@ -16,9 +28,17 @@ void statsd(std::string_view mtc);
void span_tags(std::vector<std::string> &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);
}
Expand All @@ -40,13 +60,29 @@ class Span {
#ifndef OBSERVE_API_CPP
#define OBSERVE_API_CPP

#include "observe_api.h"
#include <iterator>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>

#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) {
Expand All @@ -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<std::string> &tags) {
Expand Down
24 changes: 24 additions & 0 deletions observe-api/test/cxx/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#define OBSERVE_API_CPP_IMPLEMENTATION
#include "observe_api.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

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<std::string> 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;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#define OBSERVE_API_IMPLEMENTATION
#define OBSERVE_API_CPP_IMPLEMENTATION
#include "observe_api.hpp"
#include <stdio.h>
Expand Down
Binary file modified observe-api/test/cxx_guest_2.wasm
Binary file not shown.
Binary file modified observe-api/test/cxx_guest_3.wasm
Binary file not shown.

0 comments on commit 313748c

Please sign in to comment.