From 6da796a5ee72ac74d1f17e0161793e4fb9b49026 Mon Sep 17 00:00:00 2001 From: Muhammad Azeez Date: Sun, 18 Aug 2024 13:54:30 +0300 Subject: [PATCH] fix(go): make sure metric and logs events are added to the current span --- go/adapter/stdout/adapter.go | 14 ++++++++------ go/trace_ctx.go | 23 +++++++++++++++++------ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/go/adapter/stdout/adapter.go b/go/adapter/stdout/adapter.go index f6ce93e..896fc50 100644 --- a/go/adapter/stdout/adapter.go +++ b/go/adapter/stdout/adapter.go @@ -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) } } } diff --git a/go/trace_ctx.go b/go/trace_ctx.go index 901e27e..f754128 100644 --- a/go/trace_ctx.go +++ b/go/trace_ctx.go @@ -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{ @@ -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) { @@ -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) { @@ -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) {