Skip to content

Commit 2d926c5

Browse files
authored
fix(go): make sure metric and logs events are added to the current span (#172)
1 parent 7e03891 commit 2d926c5

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

go/adapter/stdout/adapter.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,14 @@ func (s *StdoutAdapter) printEvents(event observe.CallEvent, indentation int) {
5555
for _, event := range event.Within() {
5656
if call, ok := event.(observe.CallEvent); ok {
5757
s.printEvents(call, indentation+1)
58-
}
59-
if alloc, ok := event.(observe.MemoryGrowEvent); ok {
60-
log.Println(strings.Repeat(" ", indentation), "Allocated", alloc.MemoryGrowAmount(), "pages of memory in", name)
61-
}
62-
if spanTags, ok := event.(observe.SpanTagsEvent); ok {
63-
log.Println(strings.Repeat(" ", indentation), "Span tags:", spanTags.Tags)
58+
} else if alloc, ok := event.(observe.MemoryGrowEvent); ok {
59+
log.Println(strings.Repeat(" ", indentation), " - Allocated", alloc.MemoryGrowAmount(), "pages of memory in", name)
60+
} else if spanTags, ok := event.(observe.SpanTagsEvent); ok {
61+
log.Println(strings.Repeat(" ", indentation), " - Span tags:", spanTags.Tags)
62+
} else if metric, ok := event.(observe.MetricEvent); ok {
63+
log.Println(strings.Repeat(" ", indentation), " - Metric:", metric.Message)
64+
} else if logEvent, ok := event.(observe.LogEvent); ok {
65+
log.Println(strings.Repeat(" ", indentation), " - Log:", logEvent.Message)
6466
}
6567
}
6668
}

go/trace_ctx.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,11 @@ func (t *TraceCtx) init(ctx context.Context, r wazero.Runtime) error {
156156
t.pushFunction(fn)
157157
}
158158

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

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

172-
t.events = append(t.events, event)
172+
fn, ok := t.popFunction()
173+
if !ok {
174+
t.events = append(t.events, event)
175+
return
176+
}
177+
fn.within = append(fn.within, event)
178+
t.pushFunction(fn)
173179
}
174180

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

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

229-
t.events = append(t.events, event)
234+
fn, ok := t.popFunction()
235+
if !ok {
236+
t.events = append(t.events, event)
237+
return
238+
}
239+
fn.within = append(fn.within, event)
240+
t.pushFunction(fn)
230241
}
231242

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

0 commit comments

Comments
 (0)