Skip to content

Commit

Permalink
feat: go-sdk support older modules by always trying to provide all im…
Browse files Browse the repository at this point in the history
…ports, add message about old namespace
  • Loading branch information
G4Vi committed Nov 17, 2023
1 parent e445153 commit 666bc3a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 43 deletions.
81 changes: 44 additions & 37 deletions go/trace_ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,29 @@ import (
// It will collect events throughout the invocation of the function. Calling
// Finish() will then submit those events to the Adapter to be processed and sent
type TraceCtx struct {
adapter chan TraceEvent
raw chan RawEvent
events []Event
stack []CallEvent
Options *Options
names map[uint32]string
telemetryId TelemetryId
adapterMeta interface{}
isOldNamespace bool
adapter chan TraceEvent
raw chan RawEvent
events []Event
stack []CallEvent
Options *Options
names map[uint32]string
telemetryId TelemetryId
adapterMeta interface{}
}

// Creates a new TraceCtx. Used internally by the Adapter. The user should create the trace context from the Adapter.
func newTraceCtx(ctx context.Context, eventsChan chan TraceEvent, r wazero.Runtime, data []byte, opts *Options) (*TraceCtx, error) {
names, err, isOldNamespace := parseNames(data)
names, err := parseNames(data)
if err != nil {
return nil, err
}

traceCtx := &TraceCtx{
adapter: eventsChan,
raw: make(chan RawEvent, opts.ChannelBufferSize),
names: names,
telemetryId: NewTraceId(),
Options: opts,
isOldNamespace: isOldNamespace,
adapter: eventsChan,
raw: make(chan RawEvent, opts.ChannelBufferSize),
names: names,
telemetryId: NewTraceId(),
Options: opts,
}

err = traceCtx.init(ctx, r)
Expand Down Expand Up @@ -77,29 +75,17 @@ 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)
namespace := "dylibso:observe/instrument"
enterName := "enter"
exitName := "exit"
memoryGrowName := "memory-grow"
if t.isOldNamespace {
namespace = "dylibso_observe"
enterName = "instrument_enter"
exitName = "instrument_exit"
memoryGrowName = "instrument_memory_grow"
}
instrument := r.NewHostModuleBuilder(namespace)
instrFunctions := instrument.NewFunctionBuilder()

instrFunctions.WithFunc(func(ctx context.Context, m api.Module, i int32) {
enterFunc := 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(enterName)
}

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

}).Export(exitName)
}

instrFunctions.WithFunc(func(ctx context.Context, amt int32) {
memoryGrowFunc := func(ctx context.Context, amt int32) {
ev := <-t.raw
if ev.Kind != RawMemoryGrow {
log.Println("Expected event", MemoryGrow, "but got", ev.Kind)
Expand All @@ -179,12 +165,33 @@ func (t *TraceCtx) init(ctx context.Context, r wazero.Runtime) error {
}
fn.within = append(fn.within, event)
t.pushFunction(fn)
}).Export(memoryGrowName)
}

_, err := instrument.Instantiate(ctx)
if err != nil {
return err
{
instrument := r.NewHostModuleBuilder("dylibso:observe/instrument")
instrFunctions := instrument.NewFunctionBuilder()
instrFunctions.WithFunc(enterFunc).Export("enter")
instrFunctions.WithFunc(exitFunc).Export("exit")
instrFunctions.WithFunc(memoryGrowFunc).Export("memory-grow")
_, err := instrument.Instantiate(ctx)
if err != nil {
return err
}
}

//old api
{
instrument := r.NewHostModuleBuilder("dylibso_observe")
instrFunctions := instrument.NewFunctionBuilder()
instrFunctions.WithFunc(enterFunc).Export("instrument_enter")
instrFunctions.WithFunc(exitFunc).Export("instrument_exit")
instrFunctions.WithFunc(memoryGrowFunc).Export("instrument_memory_grow")
_, err := instrument.Instantiate(ctx)
if err != nil {
return err
}
}

return nil
}

Expand Down
13 changes: 7 additions & 6 deletions go/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ package observe

import (
"errors"
"log"

"github.com/tetratelabs/wabin/binary"
"github.com/tetratelabs/wabin/wasm"
)

// Parse the names of the functions out of the
// names custom section in the wasm binary.
func parseNames(data []byte) (map[uint32]string, error, bool) {
isOldNamespace := false
func parseNames(data []byte) (map[uint32]string, error) {
features := wasm.CoreFeaturesV2
m, err := binary.DecodeModule(data, features)
if err != nil {
return nil, err, isOldNamespace
return nil, err
}

if m.NameSection == nil {
return nil, errors.New("Name section not found"), isOldNamespace
return nil, errors.New("Name section not found")
}

names := make(map[uint32]string, len(m.NameSection.FunctionNames))
Expand All @@ -29,10 +29,11 @@ func parseNames(data []byte) (map[uint32]string, error, bool) {

for _, item := range m.ImportSection {
if item.Module == "dylibso_observe" {
isOldNamespace = true
log.Println("Module uses deprecated namespace \"dylibso_observe\"!\n" +
"Please consider reinstrumenting with newer wasm-instr!")
break
}
}

return names, nil, isOldNamespace
return names, nil
}

0 comments on commit 666bc3a

Please sign in to comment.