Skip to content

Commit

Permalink
feat(component): rename core module imports
Browse files Browse the repository at this point in the history
Align core module instrumentation naming with component model naming.

This relies on corresponding (undeployed, private) changes to the observe http api [1].

[1]: dylibso/wasm-instr#69
  • Loading branch information
chrisdickinson committed Oct 10, 2023
1 parent deb3e97 commit d349a12
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 61 deletions.
18 changes: 9 additions & 9 deletions go/trace_ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,19 @@ func (t *TraceCtx) withListener(ctx context.Context) context.Context {
// Should only be called once.
func (t *TraceCtx) init(ctx context.Context, r wazero.Runtime) error {
ctx = t.withListener(ctx)
observe := r.NewHostModuleBuilder("dylibso_observe")
functions := observe.NewFunctionBuilder()
instrument := r.NewHostModuleBuilder("dylibso:observe/instrument")
instrFunctions := instrument.NewFunctionBuilder()

functions.WithFunc(func(ctx context.Context, m api.Module, i int32) {
instrFunctions.WithFunc(func(ctx context.Context, m api.Module, i int32) {
start := time.Now()
ev := <-t.raw
if ev.Kind != RawEnter {
log.Println("Expected event", RawEnter, "but got", ev.Kind)
}
t.pushFunction(CallEvent{Raw: []RawEvent{ev}, Time: start})
}).Export("instrument_enter")
}).Export("instrument-enter")

functions.WithFunc(func(ctx context.Context, i int32) {
instrFunctions.WithFunc(func(ctx context.Context, i int32) {
end := time.Now()
ev := <-t.raw
if ev.Kind != RawExit {
Expand Down Expand Up @@ -140,9 +140,9 @@ func (t *TraceCtx) init(ctx context.Context, r wazero.Runtime) error {
t.pushFunction(f)
}

}).Export("instrument_exit")
}).Export("instrument-exit")

functions.WithFunc(func(ctx context.Context, amt int32) {
instrFunctions.WithFunc(func(ctx context.Context, amt int32) {
ev := <-t.raw
if ev.Kind != RawMemoryGrow {
log.Println("Expected event", MemoryGrow, "but got", ev.Kind)
Expand All @@ -167,9 +167,9 @@ func (t *TraceCtx) init(ctx context.Context, r wazero.Runtime) error {
}
fn.within = append(fn.within, event)
t.pushFunction(fn)
}).Export("instrument_memory_grow")
}).Export("instrument-memory-grow")

_, err := observe.Instantiate(ctx)
_, err := instrument.Instantiate(ctx)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions js/src/lib/collectors/span/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ export class SpanCollector implements Collector {

public getImportObject(): WebAssembly.Imports {
return {
"dylibso_observe": {
"instrument_enter": this.instrumentEnter,
"instrument_exit": this.instrumentExit,
"instrument_memory_grow": this.instrumentMemoryGrow,
"dylibso:observe/instrument": {
"instrument-enter": this.instrumentEnter,
"instrument-exit": this.instrumentExit,
"instrument-memory-grow": this.instrumentMemoryGrow,
},
};
}
Expand Down
10 changes: 5 additions & 5 deletions observe-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ This acts as the contract between the host and the guest layer. All data flows i
from the guest to the host. Most of these APIs are simply ways to pass observability data as strings
to the host layer.

* `dylibso_observe.metric(i32, i64, i32)`
* `dylibso_observe.log(i32, i64, i32)`
* `dylibso_observe.span_enter(i64, i32)`
* `dylibso_observe.span_exit()`
* `dylibso_observe.span_tags(i64, i32)`
* `dylibso:observe/api.metric(i32, i32, i32)`
* `dylibso:observe/api.log(i32, i32, i32)`
* `dylibso:observe/api.span-enter(i32, i32)`
* `dylibso:observe/api.span-exit()`
* `dylibso:observe/api.span-tags(i32, i32)`

Ideally, you will not call this API layer directly but instead use language specific bindings to call them. And for end users, eventually, open source observability clients will *export* data to this layer.

Expand Down
12 changes: 6 additions & 6 deletions observe-api/c/observe_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
#include <string.h>

void span_enter(const char *name) {
const uint64_t uint64_ptr = (uint64_t)name;
const uint32_t uint32_ptr = (uint32_t)name;
const uint32_t uint32_length = strlen(name);
_span_enter(uint64_ptr, uint32_length);
_span_enter(uint32_ptr, uint32_length);
}

void span_exit(void) { _span_exit(); }

void metric(const char *metric) {
const uint64_t uint64_ptr = (uint64_t)metric;
const uint32_t uint32_ptr = (uint32_t)metric;
const uint32_t uint32_length = strlen(metric);
_metric(1, uint64_ptr, uint32_length);
_metric(1, uint32_ptr, uint32_length);
}

void write_log(const enum DO_LOG_LEVEL level, const char *msg) {
const uint64_t uint64_ptr = (uint64_t)msg;
const uint32_t uint32_ptr = (uint32_t)msg;
const uint32_t uint32_length = strlen(msg);
const uint32_t uint32_level = level;
_log(uint32_level, uint64_ptr, uint32_length);
_log(uint32_level, uint32_ptr, uint32_length);
}
23 changes: 12 additions & 11 deletions observe-api/c/observe_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@

#define IMPORT(a, b) __attribute__((import_module(a), import_name(b)))

IMPORT("dylibso_observe", "metric")
extern void _metric(uint32_t, uint64_t, uint32_t);
IMPORT("dylibso_observe", "log")
extern void _log(uint32_t, uint64_t, uint32_t);
IMPORT("dylibso_observe", "span_enter")
extern void _span_enter(uint64_t, uint32_t);
IMPORT("dylibso_observe", "span_exit")
IMPORT("dylibso:observe/api", "metric")
extern void _metric(uint32_t, uint32_t, uint32_t);
IMPORT("dylibso:observe/api", "log")
extern void _log(uint32_t, uint32_t, uint32_t);
IMPORT("dylibso:observe/api", "span-enter")
extern void _span_enter(uint32_t, uint32_t);
IMPORT("dylibso:observe/api", "span-exit")
extern void _span_exit(void);

enum DO_LOG_LEVEL {
DO_LL_ERROR = 1,
DO_LL_WARN = 2,
DO_LL_INFO = 3,
DO_LL_DEBUG = 4
DO_LL_ERROR = 0,
DO_LL_WARN = 1,
DO_LL_INFO = 2,
DO_LL_DEBUG = 3,
DO_LL_TRACE = 4
};

void span_enter(const char *name);
Expand Down
36 changes: 17 additions & 19 deletions rust/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,6 @@ pub(crate) fn span_exit<T>(
Ok(())
}

const MODULE_NAME: &str = "dylibso_observe";

type EventChannel = (Sender<Event>, Receiver<Event>);

/// Link observability import functions required by instrumented wasm code
Expand All @@ -444,8 +442,8 @@ pub fn add_to_linker<T: 'static>(

let enter_ctx = ctx.clone();
linker.func_new(
MODULE_NAME,
"instrument_enter",
"dylibso:observe/instrument",
"enter",
t.clone(),
move |_caller: Caller<T>, params, results| {
instrument_enter(
Expand All @@ -459,16 +457,16 @@ pub fn add_to_linker<T: 'static>(

let exit_ctx = ctx.clone();
linker.func_new(
MODULE_NAME,
"instrument_exit",
"dylibso:observe/instrument",
"exit",
t.clone(),
move |_caller, params, results| instrument_exit(params, results, exit_ctx.clone()),
)?;

let grow_ctx = ctx.clone();
linker.func_new(
MODULE_NAME,
"instrument_memory_grow",
"dylibso:observe/instrument",
"memory-grow",
t,
move |_caller, params, results| instrument_memory_grow(params, results, grow_ctx.clone()),
)?;
Expand All @@ -477,8 +475,8 @@ pub fn add_to_linker<T: 'static>(

let span_enter_ctx = ctx.clone();
linker.func_new(
MODULE_NAME,
"span_enter",
"dylibso:observe/api",
"span-enter",
t.clone(),
move |mut caller, params, results| {
span_enter(&mut caller, params, results, span_enter_ctx.clone())
Expand All @@ -487,8 +485,8 @@ pub fn add_to_linker<T: 'static>(

let span_tags_ctx = ctx.clone();
linker.func_new(
MODULE_NAME,
"span_tags",
"dylibso:observe/api",
"span-tags",
t.clone(),
move |mut caller, params, results| {
span_tags(&mut caller, params, results, span_tags_ctx.clone())
Expand All @@ -499,23 +497,23 @@ pub fn add_to_linker<T: 'static>(

let metric_ctx = ctx.clone();
linker.func_new(
MODULE_NAME,
"dylibso:observe/api",
"metric",
t.clone(),
move |mut caller, params, results| metric(&mut caller, params, results, metric_ctx.clone()),
)?;

let log_ctx = ctx.clone();
linker.func_new(MODULE_NAME, "log", t, move |mut caller, params, results| {
linker.func_new("dylibso:observe/api", "log", t, move |mut caller, params, results| {
log_write(&mut caller, params, results, log_ctx.clone())
})?;

let t = FuncType::new([], []);

let span_exit_ctx = ctx.clone();
linker.func_new(
MODULE_NAME,
"span_exit",
"dylibso:observe/api",
"span-exit",
t,
move |mut caller, params, results| {
span_exit(&mut caller, params, results, span_exit_ctx.clone())
Expand Down Expand Up @@ -598,22 +596,22 @@ pub mod component {
}

impl InstrumentHost for ObserveSdkBindings {
fn instrument_memory_grow(&mut self, amount_in_pages: u32) -> wasmtime::Result<()> {
fn memory_grow(&mut self, amount_in_pages: u32) -> wasmtime::Result<()> {
if let Ok(mut cont) = self.instr_context.lock() {
cont.allocate(amount_in_pages)?;
}
Ok(())
}

fn instrument_enter(&mut self, func_id: u32) -> wasmtime::Result<()> {
fn enter(&mut self, func_id: u32) -> wasmtime::Result<()> {
let printname = self.wasm_instr_info.function_names.get(&func_id);
if let Ok(mut cont) = self.instr_context.lock() {
cont.enter(func_id, printname.map(|x| x.as_str()))?;
}
Ok(())
}

fn instrument_exit(&mut self, func_id: u32) -> wasmtime::Result<()> {
fn exit(&mut self, func_id: u32) -> wasmtime::Result<()> {
if let Ok(mut cont) = self.instr_context.lock() {
cont.exit(func_id)?;
}
Expand Down
8 changes: 4 additions & 4 deletions test/kitchensink.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

#define IMPORT(a, b) __attribute__((import_module(a), import_name(b)))

IMPORT("dylibso_observe", "metric")
IMPORT("dylibso:observe/api", "metric")
extern void metric(uint32_t, uint64_t, uint32_t);
IMPORT("dylibso_observe", "log")
IMPORT("dylibso:observe/api", "log")
extern void log_write(uint32_t, uint64_t, uint32_t);
IMPORT("dylibso_observe", "span_enter")
IMPORT("dylibso:observe/api", "span-enter")
extern void span_enter(uint64_t, uint32_t);
IMPORT("dylibso_observe", "span_exit") extern void span_exit();
IMPORT("dylibso:observe/api", "span-exit") extern void span_exit();

void custom_span_enter(char name[]) {
uintptr_t ptr = (uintptr_t)name;
Expand Down
Binary file modified test/kitchensink.c.instr.wasm
Binary file not shown.
Binary file modified test/nested.c.instr.wasm
Binary file not shown.
Binary file modified test/test.c.instr.wasm
Binary file not shown.
Binary file modified wit/observe.wasm
Binary file not shown.
6 changes: 3 additions & 3 deletions wit/observe.wit
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ interface api {
}

interface instrument {
instrument-memory-grow: func(amount-in-pages: u32)
instrument-enter: func(func-id: u32)
instrument-exit: func(func-id: u32)
memory-grow: func(amount-in-pages: u32)
enter: func(func-id: u32)
exit: func(func-id: u32)
}

world imports {
Expand Down

0 comments on commit d349a12

Please sign in to comment.