Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/modules/fxtrace.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -130,6 +132,9 @@ func TestExample(t *testing.T) {
defer span.End()
}),
)

//dump spans
traceExporter.Dump()

// trace assertion example
tracetest.AssertHasTraceSpan(
Expand Down
5 changes: 5 additions & 0 deletions trace/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand Down
9 changes: 9 additions & 0 deletions trace/tracetest/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
}
45 changes: 45 additions & 0 deletions trace/tracetest/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package tracetest_test

import (
"context"
"io"
"os"
"testing"

"github.com/ankorstore/yokai/trace"
Expand Down Expand Up @@ -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")
}