Skip to content

Commit

Permalink
feat: add span tags to observe api c bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
G4Vi committed Jul 5, 2024
1 parent 62cd87a commit e336023
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
27 changes: 27 additions & 0 deletions observe-api/c/observe_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,30 @@ void observe_api_log(const enum DO_LOG_LEVEL level, const char *msg) {
const size_t msg_length = strlen(msg);
observe_api_log_n(level, msg, msg_length);
}

void observe_api_span_tags(const char *tags) {
const size_t tags_length = strlen(tags);
observe_api_span_tags_n(tags, tags_length);
}

void observe_api_span_tags_from_array(const char *const tags[],
size_t num_tags) {
char *tags_buf = NULL;
size_t tags_buf_size = 0;
for (size_t i = 0; i < num_tags; i++) {
size_t new_tag_length = strlen(tags[i]);
size_t new_tags_buf_size = tags_buf_size + new_tag_length + 1;
char *new_tags_buf = (char *)realloc(tags_buf, new_tags_buf_size);
if (!new_tags_buf) {
break;
}
memcpy(new_tags_buf + tags_buf_size, tags[i], new_tag_length);
new_tags_buf[new_tags_buf_size - 1] = ',';
tags_buf = new_tags_buf;
tags_buf_size = new_tags_buf_size;
}
if (tags_buf_size > 0) {
observe_api_span_tags_n(tags_buf, tags_buf_size - 1);
free(tags_buf);
}
}
5 changes: 5 additions & 0 deletions observe-api/c/observe_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ 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);

#ifdef __cplusplus
extern "C" {
Expand All @@ -35,6 +37,9 @@ void observe_api_metric(enum DO_METRIC_FMT format, const char *message);
void observe_api_statsd_n(const char *metric, const size_t metric_length);
void observe_api_statsd(const char *metric);
void observe_api_log(const enum DO_LOG_LEVEL level, const char *msg);
void observe_api_span_tags(const char *tags);
void observe_api_span_tags_from_array(const char *const tags[],
size_t num_tags);

#ifdef __cplusplus
}
Expand Down
3 changes: 3 additions & 0 deletions observe-api/test/c/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ 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");
const char *const tags[] = {"taga:one", "tagb:two"};
observe_api_span_tags_from_array(tags, sizeof(tags) / sizeof(tags[0]));
printf("Hello from Wasm!\n");
observe_api_span_exit();
}
Expand Down
Binary file modified observe-api/test/c_guest.wasm
Binary file not shown.
Binary file modified observe-api/test/cxx_guest.wasm
Binary file not shown.

0 comments on commit e336023

Please sign in to comment.