Skip to content

Commit

Permalink
fix(go): make sure metric and logs events are added to the current span
Browse files Browse the repository at this point in the history
  • Loading branch information
mhmd-azeez committed Aug 18, 2024
1 parent 7e03891 commit 6da796a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
14 changes: 8 additions & 6 deletions go/adapter/stdout/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ func (s *StdoutAdapter) printEvents(event observe.CallEvent, indentation int) {
for _, event := range event.Within() {
if call, ok := event.(observe.CallEvent); ok {
s.printEvents(call, indentation+1)
}
if alloc, ok := event.(observe.MemoryGrowEvent); ok {
log.Println(strings.Repeat(" ", indentation), "Allocated", alloc.MemoryGrowAmount(), "pages of memory in", name)
}
if spanTags, ok := event.(observe.SpanTagsEvent); ok {
log.Println(strings.Repeat(" ", indentation), "Span tags:", spanTags.Tags)
} else if alloc, ok := event.(observe.MemoryGrowEvent); ok {
log.Println(strings.Repeat(" ", indentation), " - Allocated", alloc.MemoryGrowAmount(), "pages of memory in", name)
} else if spanTags, ok := event.(observe.SpanTagsEvent); ok {
log.Println(strings.Repeat(" ", indentation), " - Span tags:", spanTags.Tags)
} else if metric, ok := event.(observe.MetricEvent); ok {
log.Println(strings.Repeat(" ", indentation), " - Metric:", metric.Message)
} else if logEvent, ok := event.(observe.LogEvent); ok {
log.Println(strings.Repeat(" ", indentation), " - Log:", logEvent.Message)
}
}
}
Expand Down
23 changes: 17 additions & 6 deletions go/trace_ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ func (t *TraceCtx) init(ctx context.Context, r wazero.Runtime) error {
t.pushFunction(fn)
}

metricFunc := func(ctx context.Context, m api.Module, f uint32, ptr uint32, len uint32) {
metricFunc := func(ctx context.Context, m api.Module, f uint32, ptr uint32, l uint32) {
format := MetricFormat(f)
buffer, ok := m.Memory().Read(ptr, len)
buffer, ok := m.Memory().Read(ptr, l)
if !ok {
log.Printf("metric: failed to read memory at offset %v with length %v\n", ptr, len)
log.Printf("metric: failed to read memory at offset %v with length %v\n", ptr, l)
}

event := MetricEvent{
Expand All @@ -169,7 +169,13 @@ func (t *TraceCtx) init(ctx context.Context, r wazero.Runtime) error {
Message: string(buffer),
}

t.events = append(t.events, event)
fn, ok := t.popFunction()
if !ok {
t.events = append(t.events, event)
return
}
fn.within = append(fn.within, event)
t.pushFunction(fn)
}

oldMetricFunc := func(ctx context.Context, m api.Module, f uint32, ptr uint64, len uint32) {
Expand Down Expand Up @@ -201,7 +207,6 @@ func (t *TraceCtx) init(ctx context.Context, r wazero.Runtime) error {
}
fn.within = append(fn.within, event)
t.pushFunction(fn)

}

oldSpanTagsFunc := func(ctx context.Context, m api.Module, ptr uint64, len uint32) {
Expand All @@ -226,7 +231,13 @@ func (t *TraceCtx) init(ctx context.Context, r wazero.Runtime) error {
Message: string(buffer),
}

t.events = append(t.events, event)
fn, ok := t.popFunction()
if !ok {
t.events = append(t.events, event)
return
}
fn.within = append(fn.within, event)
t.pushFunction(fn)
}

oldLogFunc := func(ctx context.Context, m api.Module, l uint32, ptr uint64, len uint32) {
Expand Down

0 comments on commit 6da796a

Please sign in to comment.