Skip to content

Commit

Permalink
feat(trace): Added possibility to dump spans from test processor (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekkinox authored Feb 23, 2024
1 parent e6767b4 commit a610572
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
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")
}

0 comments on commit a610572

Please sign in to comment.