Skip to content

Commit 313748c

Browse files
authored
feat(observe api): seperate c++ bindings from c bindings, add c++ span 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
1 parent 45a56f5 commit 313748c

File tree

8 files changed

+79
-16
lines changed

8 files changed

+79
-16
lines changed

observe-api/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ build:
66
@cp test/rust/target/wasm32-wasi/debug/rust_guest.wasm test/rust_guest.wasm
77
$(WASICC) -o test/c_guest.wasm -I c test/c/main.c
88
$(WASICXX) -o test/cxx_guest.wasm -I c -x c++ test/c/main.c
9-
$(WASICXX) -o test/cxx_guest_2.wasm -fno-exceptions -I c test/c/main.cpp
10-
$(WASICXX) -o test/cxx_guest_3.wasm -fno-exceptions -I c test/c/main2.cpp
9+
$(WASICXX) -o test/cxx_guest_2.wasm -fno-exceptions -I cxx test/cxx/main.cpp
10+
$(WASICXX) -o test/cxx_guest_3.wasm -fno-exceptions -I cxx test/cxx/main2.cpp

observe-api/README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ We currently provide these language bindings to this API:
2222
* [example](test/rust/src/main.rs)
2323

2424

25-
### [c and c++](c/)
25+
### [c](c/) and [c++](cxx/)
2626

27-
Both the C and C++ bindings are implemented as header-only libraries. To use the C bindings,
27+
Both the C and C++ bindings are implemented as single header libraries. To use the C bindings,
2828
in __ONE__ source file:
2929

3030
```c
@@ -39,17 +39,16 @@ In other source files, just `#include "observe_api.h"`
3939
To use the C++ bindings, instead, in __ONE__ source file:
4040

4141
```c++
42-
#define OBSERVE_API_IMPLEMENTATION
4342
#define OBSERVE_API_CPP_IMPLEMENTATION
4443
#include "observe_api.hpp"
4544
```
4645

4746
In other source files, just `#include "observe_api.hpp"`
4847

49-
__NOTE:__ `observe_api.hpp` `#include`s `observe_api.h`
48+
* [functional example](test/cxx/main.cpp)
49+
* [OO example](test/cxx/main2.cpp)
5050

51-
* [functional example](test/c/main.cpp)
52-
* [OO example](test/c/main2.cpp)
51+
In C++, both bindings may be used at the same time without conflict.
5352

5453
### Other
5554

observe-api/c/observe_api.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
#include <stdint.h>
55

6-
#define IMPORT(a, b) __attribute__((import_module(a), import_name(b)))
7-
6+
#ifndef OBSERVE_API_ENUM
7+
#define OBSERVE_API_ENUM
88
enum DO_LOG_LEVEL {
99
DO_LL_ERROR = 1,
1010
DO_LL_WARN = 2,
@@ -14,6 +14,9 @@ enum DO_LOG_LEVEL {
1414
};
1515

1616
enum DO_METRIC_FMT { DO_MF_STATSD = 1 };
17+
#endif
18+
19+
#define IMPORT(a, b) __attribute__((import_module(a), import_name(b)))
1720

1821
IMPORT("dylibso:observe/api", "metric")
1922
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);
2831
IMPORT("dylibso:observe/api", "span-tags")
2932
extern void observe_api_span_tags_n(const char *tags, size_t tags_length);
3033

34+
#undef IMPORT
35+
3136
#ifdef __cplusplus
3237
extern "C" {
3338
#endif

observe-api/c/observe_api.hpp renamed to observe-api/cxx/observe_api.hpp

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
#ifndef OBSERVE_API_HPP
22
#define OBSERVE_API_HPP
33

4-
#include "observe_api.h"
54
#include <string>
65
#include <string_view>
76
#include <vector>
87

8+
#ifndef OBSERVE_API_ENUM
9+
#define OBSERVE_API_ENUM
10+
enum DO_LOG_LEVEL {
11+
DO_LL_ERROR = 1,
12+
DO_LL_WARN = 2,
13+
DO_LL_INFO = 3,
14+
DO_LL_DEBUG = 4,
15+
DO_LL_TRACE = 5
16+
};
17+
18+
enum DO_METRIC_FMT { DO_MF_STATSD = 1 };
19+
#endif
20+
921
namespace observe_api {
1022
void span_enter(std::string_view name);
1123
void span_exit();
@@ -16,9 +28,17 @@ void statsd(std::string_view mtc);
1628
void span_tags(std::vector<std::string> &tags);
1729

1830
class Span {
31+
bool ended;
32+
1933
public:
20-
Span(std::string_view name) { span_enter(name); }
21-
~Span() { span_exit(); }
34+
Span(std::string_view name) : ended(false) { span_enter(name); }
35+
void end() {
36+
if (!ended) {
37+
ended = true;
38+
span_exit();
39+
}
40+
}
41+
~Span() { end(); }
2242
void metric(enum DO_METRIC_FMT format, std::string_view mtc) {
2343
observe_api::metric(format, mtc);
2444
}
@@ -40,13 +60,29 @@ class Span {
4060
#ifndef OBSERVE_API_CPP
4161
#define OBSERVE_API_CPP
4262

43-
#include "observe_api.h"
4463
#include <iterator>
4564
#include <sstream>
4665
#include <string>
4766
#include <string_view>
4867
#include <vector>
4968

69+
#define IMPORT(a, b) __attribute__((import_module(a), import_name(b)))
70+
71+
IMPORT("dylibso:observe/api", "metric")
72+
extern void observe_api_metric_n(enum DO_METRIC_FMT format, const char *metric,
73+
size_t metric_length);
74+
IMPORT("dylibso:observe/api", "log")
75+
extern void observe_api_log_n(enum DO_LOG_LEVEL level, const char *message,
76+
size_t message_length);
77+
IMPORT("dylibso:observe/api", "span-enter")
78+
extern void observe_api_span_enter_n(const char *name, size_t name_length);
79+
IMPORT("dylibso:observe/api", "span-exit")
80+
extern void observe_api_span_exit(void);
81+
IMPORT("dylibso:observe/api", "span-tags")
82+
extern void observe_api_span_tags_n(const char *tags, size_t tags_length);
83+
84+
#undef IMPORT
85+
5086
namespace observe_api {
5187

5288
void span_enter(std::string_view name) {
@@ -68,7 +104,7 @@ void span_tags(std::string_view tags) {
68104
}
69105

70106
void statsd(std::string_view mtc) {
71-
observe_api_statsd_n(mtc.data(), mtc.size());
107+
observe_api_metric_n(DO_MF_STATSD, mtc.data(), mtc.size());
72108
}
73109

74110
void span_tags(std::vector<std::string> &tags) {

observe-api/test/cxx/main.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#define OBSERVE_API_CPP_IMPLEMENTATION
2+
#include "observe_api.hpp"
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string>
6+
#include <vector>
7+
8+
void run() {
9+
observe_api::span_enter("printf");
10+
observe_api::statsd("ok:aaaaa");
11+
observe_api::log(DO_LL_INFO, "bbbbb");
12+
observe_api::span_tags("abbc:def,(another:tag");
13+
std::vector<std::string> tags = {"taga:one", "tagb:two"};
14+
observe_api::span_tags(tags);
15+
printf("Hello from Wasm!\n");
16+
observe_api::span_exit();
17+
}
18+
19+
int main(int argc, char *argv[]) {
20+
observe_api::span_enter("run");
21+
run();
22+
observe_api::span_exit();
23+
return 0;
24+
}

observe-api/test/c/main2.cpp renamed to observe-api/test/cxx/main2.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#define OBSERVE_API_IMPLEMENTATION
21
#define OBSERVE_API_CPP_IMPLEMENTATION
32
#include "observe_api.hpp"
43
#include <stdio.h>

observe-api/test/cxx_guest_2.wasm

-114 Bytes
Binary file not shown.

observe-api/test/cxx_guest_3.wasm

36 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)