-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: Refactor Go architecture #43
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2855a52
refactor: Refactor Go architecture
bhelx c0b1580
fix code formatting
bhelx 1d47a65
fix formatting
bhelx ce3e1c2
combine new and init
bhelx 5aef365
remove vistigial parts of adapter struct
bhelx 219f803
Add back and refactor other adapters
bhelx f1d84e1
move some more out of trace_ctx
bhelx 3f3e157
Add 128bit support to TelemetryId, add comments
bhelx 8054748
fix datadog adapter
bhelx 687af36
apply tid changes from comments
bhelx a2cb84a
Fix otel adapter memory grow events
bhelx d17c0a8
fix: include memory grow events as allocation attributes
nilslice File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,125 +1,63 @@ | ||
package observe | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"math/rand" | ||
"time" | ||
"context" | ||
|
||
"github.com/tetratelabs/wabin/leb128" | ||
"github.com/tetratelabs/wabin/wasm" | ||
"github.com/tetratelabs/wazero" | ||
) | ||
|
||
// The primary interface that every Adapter needs to follow | ||
// Start() and Stop() can just call the implementations on AdapterBase | ||
// or provide some custom logic. HandleTraceEvent is called after | ||
// an invocation of a wasm module is done and all events are collected. | ||
type Adapter interface { | ||
Start(collector *Collector, wasm []byte) error | ||
Stop(collector *Collector) | ||
Event(Event) | ||
Start() | ||
Stop() | ||
HandleTraceEvent(TraceEvent) | ||
} | ||
|
||
type AdapterBase struct { | ||
Collectors map[*Collector]chan bool | ||
// The payload that contains all the Events | ||
// from a single wasm module invocation | ||
type TraceEvent struct { | ||
Events []Event | ||
TelemetryId TelemetryId | ||
} | ||
|
||
func checkVersion(m *wasm.Module) error { | ||
var minorGlobal *wasm.Export = nil | ||
var majorGlobal *wasm.Export = nil | ||
for _, export := range m.ExportSection { | ||
if export.Type != wasm.ExternTypeGlobal { | ||
continue | ||
} | ||
|
||
if export.Name == "wasm_instr_version_minor" { | ||
minorGlobal = export | ||
} else if export.Name == "wasm_instr_version_major" { | ||
majorGlobal = export | ||
} | ||
} | ||
|
||
if minorGlobal == nil || majorGlobal == nil { | ||
return errors.New("wasm_instr_version functions not found") | ||
} | ||
|
||
minor, _, err := leb128.DecodeUint32(bytes.NewReader(m.GlobalSection[minorGlobal.Index].Init.Data)) | ||
if err != nil { | ||
return err | ||
} | ||
major, _, err := leb128.DecodeUint32(bytes.NewReader(m.GlobalSection[majorGlobal.Index].Init.Data)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if major != wasmInstrVersionMajor || minor < wasmInstrVersionMinor { | ||
return errors.New(fmt.Sprintf("Expected instrumentation version >= %d.%d but got %d.%d", wasmInstrVersionMajor, wasmInstrVersionMinor, major, minor)) | ||
} | ||
|
||
return nil | ||
// Shared implementation for all Adapters | ||
type AdapterBase struct { | ||
TraceEvents chan TraceEvent | ||
stop chan bool | ||
} | ||
|
||
func (a *AdapterBase) Wait(collector *Collector, timeout time.Duration, callback func()) { | ||
for { | ||
select { | ||
case <-time.After(timeout): | ||
if len(collector.Events) > 0 { | ||
if callback != nil { | ||
callback() | ||
} | ||
continue | ||
} | ||
a.RemoveCollector(collector) | ||
return | ||
} | ||
func (a *AdapterBase) NewTraceCtx(ctx context.Context, r wazero.Runtime, wasm []byte, config *Config) (*TraceCtx, error) { | ||
if config == nil { | ||
config = NewDefaultConfig() | ||
} | ||
return newTraceCtx(ctx, a, r, wasm, config) | ||
} | ||
|
||
func NewAdapterBase() AdapterBase { | ||
a := AdapterBase{ | ||
Collectors: map[*Collector]chan bool{}, | ||
} | ||
return a | ||
} | ||
|
||
func (a *AdapterBase) Start(collector *Collector, wasm []byte) error { | ||
a.Collectors[collector] = make(chan bool, 1) | ||
return collector.GetNames(wasm) | ||
} | ||
|
||
func (a *AdapterBase) RemoveCollector(collector *Collector) { | ||
delete(a.Collectors, collector) | ||
} | ||
|
||
func (a *AdapterBase) Stop(collector *Collector) { | ||
stop, ok := a.Collectors[collector] | ||
if ok { | ||
stop <- true | ||
a.RemoveCollector(collector) | ||
return AdapterBase{ | ||
// TODO set to some kind of max, add dump logic | ||
TraceEvents: make(chan TraceEvent, 100), | ||
} | ||
} | ||
|
||
func (a AdapterBase) StopChan(collector *Collector) chan bool { | ||
return a.Collectors[collector] | ||
} | ||
|
||
type TelemetryId uint64 | ||
|
||
var rng rand.Source | ||
func (b *AdapterBase) Start(a Adapter) { | ||
b.stop = make(chan bool) | ||
|
||
func init() { | ||
rng = rand.NewSource(time.Now().UnixNano()) | ||
} | ||
|
||
func NewTraceId() TelemetryId { | ||
return TelemetryId(rng.Int63()) | ||
} | ||
|
||
func NewSpanId() TelemetryId { | ||
return TelemetryId(rng.Int63()) | ||
} | ||
|
||
func (t TelemetryId) ToHex8() string { | ||
return fmt.Sprintf("%016x", t) | ||
go func() { | ||
for { | ||
select { | ||
case event := <-b.TraceEvents: | ||
a.HandleTraceEvent(event) | ||
case <-b.stop: | ||
return | ||
} | ||
} | ||
}() | ||
} | ||
|
||
func (t TelemetryId) ToHex16() string { | ||
return fmt.Sprintf("%032x", t) | ||
func (b *AdapterBase) Stop() { | ||
b.stop <- true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sends to this channel will block once the channel reaches 100 events. I think thats probably ok, but wondering if this is intentional. We can omit the buffered size and let the channel grow as needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah i think non-buffered might be the way to go but let me think about it for a moment. my intention with using a buffered channel is if the adapter is busy then it may take some time to read off this channel which would block the sender right? By default sending a message to a channel blocks until the receiver synchronizes. Ideally the adapter just pulls messages off this channel and only puts them in a local slice, then it submits them offline in another goroutine. but i haven't done that yet. so this is acting at the bucket.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see -- yea that makes sense!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll make a follow up for that. Goal will be to add the bucket on the adapter side and test that this channel can't get blocked. We just need to make sure that the adapter's main routine only reads off this channel as fast as possible. And if it can't put the events in a bucket it should just throw them away and log an error. But never block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#44