diff --git a/docs/modules/fxtrace.md b/docs/modules/fxtrace.md index 0ccf43b..378f5db 100644 --- a/docs/modules/fxtrace.md +++ b/docs/modules/fxtrace.md @@ -99,6 +99,8 @@ You can use the provided [test assertion helpers](https://github.com/ankorstore/ - `AssertContainTraceSpan`: to assert on exact name and partial attributes match - `AssertContainNotTraceSpan`: to assert on exact name and partial attributes non match +and use `Dump()` to print the current content of the [TestTraceExporter](https://github.com/ankorstore/yokai/blob/main/trace/tracetest/exporter.go). + For example: ```go title="internal/example_test.go" @@ -130,6 +132,9 @@ func TestExample(t *testing.T) { defer span.End() }), ) + + //dump spans + traceExporter.Dump() // trace assertion example tracetest.AssertHasTraceSpan( diff --git a/trace/README.md b/trace/README.md index e544fcc..7356b64 100644 --- a/trace/README.md +++ b/trace/README.md @@ -200,6 +200,8 @@ You can use the provided [test assertion helpers](tracetest/assert.go) in your t - `AssertContainTraceSpan`: to assert on exact name and partial attributes match - `AssertContainNotTraceSpan`: to assert on exact name and partial attributes non match +and use `Dump()` to print the current content of the test span processor. + ```go package main_test @@ -227,6 +229,9 @@ func TestTracer(t *testing.T) { attribute.Int("int attr name", 42), ) span.End() + + // dump spans + ex.Dump() // assertion success tracetest.AssertHasTraceSpan( diff --git a/trace/tracetest/exporter.go b/trace/tracetest/exporter.go index aaa5530..b32926c 100644 --- a/trace/tracetest/exporter.go +++ b/trace/tracetest/exporter.go @@ -18,6 +18,7 @@ type TestTraceExporter interface { Span(name string) (tracetest.SpanStub, error) HasSpan(expectedName string, expectedAttributes ...attribute.KeyValue) bool ContainSpan(expectedName string, expectedAttributes ...attribute.KeyValue) bool + Dump() } // DefaultTestTraceExporter is the default [TestTraceExporter] implementation. @@ -137,3 +138,11 @@ func (e *DefaultTestTraceExporter) ContainSpan(expectedName string, expectedAttr return false } + +// Dump prints the [tracetest.SpanStubs] snapshots from the in memory internal exporter, for debugging purposes. +func (e *DefaultTestTraceExporter) Dump() { + for _, span := range e.Spans().Snapshots() { + //nolint:forbidigo + fmt.Printf("%v\n", span) + } +} diff --git a/trace/tracetest/exporter_test.go b/trace/tracetest/exporter_test.go index 5b033e9..37572f6 100644 --- a/trace/tracetest/exporter_test.go +++ b/trace/tracetest/exporter_test.go @@ -2,6 +2,8 @@ package tracetest_test import ( "context" + "io" + "os" "testing" "github.com/ankorstore/yokai/trace" @@ -312,3 +314,46 @@ func TestContainSpan(t *testing.T) { ), ) } + +func TestDump(t *testing.T) { + t.Parallel() + + exporter := tracetest.NewDefaultTestTraceExporter() + + tracerProvider, err := trace.NewDefaultTracerProviderFactory().Create( + trace.WithSpanProcessor(trace.NewTestSpanProcessor(exporter)), + ) + assert.NoError(t, err) + + tracer := tracerProvider.Tracer("test") + + _, span := tracer.Start( + context.Background(), + "test span", + oteltrace.WithAttributes( + attribute.String("string attribute name", "string attribute value"), + attribute.Int("int attribute name", 42), + ), + ) + span.End() + + defaultStdout := os.Stdout + r, w, _ := os.Pipe() + os.Stdout = w + + exporter.Dump() + + err = w.Close() + assert.NoError(t, err) + + out, err := io.ReadAll(r) + assert.NoError(t, err) + + os.Stdout = defaultStdout + + outStr := string(out) + + assert.Contains(t, outStr, "test span") + assert.Contains(t, outStr, "string attribute value") + assert.Contains(t, outStr, "42") +}