Skip to content

Commit a610572

Browse files
authored
feat(trace): Added possibility to dump spans from test processor (#96)
1 parent e6767b4 commit a610572

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

docs/modules/fxtrace.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ You can use the provided [test assertion helpers](https://github.com/ankorstore/
9999
- `AssertContainTraceSpan`: to assert on exact name and partial attributes match
100100
- `AssertContainNotTraceSpan`: to assert on exact name and partial attributes non match
101101

102+
and use `Dump()` to print the current content of the [TestTraceExporter](https://github.com/ankorstore/yokai/blob/main/trace/tracetest/exporter.go).
103+
102104
For example:
103105

104106
```go title="internal/example_test.go"
@@ -130,6 +132,9 @@ func TestExample(t *testing.T) {
130132
defer span.End()
131133
}),
132134
)
135+
136+
//dump spans
137+
traceExporter.Dump()
133138
134139
// trace assertion example
135140
tracetest.AssertHasTraceSpan(

trace/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ You can use the provided [test assertion helpers](tracetest/assert.go) in your t
200200
- `AssertContainTraceSpan`: to assert on exact name and partial attributes match
201201
- `AssertContainNotTraceSpan`: to assert on exact name and partial attributes non match
202202

203+
and use `Dump()` to print the current content of the test span processor.
204+
203205
```go
204206
package main_test
205207

@@ -227,6 +229,9 @@ func TestTracer(t *testing.T) {
227229
attribute.Int("int attr name", 42),
228230
)
229231
span.End()
232+
233+
// dump spans
234+
ex.Dump()
230235

231236
// assertion success
232237
tracetest.AssertHasTraceSpan(

trace/tracetest/exporter.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type TestTraceExporter interface {
1818
Span(name string) (tracetest.SpanStub, error)
1919
HasSpan(expectedName string, expectedAttributes ...attribute.KeyValue) bool
2020
ContainSpan(expectedName string, expectedAttributes ...attribute.KeyValue) bool
21+
Dump()
2122
}
2223

2324
// DefaultTestTraceExporter is the default [TestTraceExporter] implementation.
@@ -137,3 +138,11 @@ func (e *DefaultTestTraceExporter) ContainSpan(expectedName string, expectedAttr
137138

138139
return false
139140
}
141+
142+
// Dump prints the [tracetest.SpanStubs] snapshots from the in memory internal exporter, for debugging purposes.
143+
func (e *DefaultTestTraceExporter) Dump() {
144+
for _, span := range e.Spans().Snapshots() {
145+
//nolint:forbidigo
146+
fmt.Printf("%v\n", span)
147+
}
148+
}

trace/tracetest/exporter_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package tracetest_test
22

33
import (
44
"context"
5+
"io"
6+
"os"
57
"testing"
68

79
"github.com/ankorstore/yokai/trace"
@@ -312,3 +314,46 @@ func TestContainSpan(t *testing.T) {
312314
),
313315
)
314316
}
317+
318+
func TestDump(t *testing.T) {
319+
t.Parallel()
320+
321+
exporter := tracetest.NewDefaultTestTraceExporter()
322+
323+
tracerProvider, err := trace.NewDefaultTracerProviderFactory().Create(
324+
trace.WithSpanProcessor(trace.NewTestSpanProcessor(exporter)),
325+
)
326+
assert.NoError(t, err)
327+
328+
tracer := tracerProvider.Tracer("test")
329+
330+
_, span := tracer.Start(
331+
context.Background(),
332+
"test span",
333+
oteltrace.WithAttributes(
334+
attribute.String("string attribute name", "string attribute value"),
335+
attribute.Int("int attribute name", 42),
336+
),
337+
)
338+
span.End()
339+
340+
defaultStdout := os.Stdout
341+
r, w, _ := os.Pipe()
342+
os.Stdout = w
343+
344+
exporter.Dump()
345+
346+
err = w.Close()
347+
assert.NoError(t, err)
348+
349+
out, err := io.ReadAll(r)
350+
assert.NoError(t, err)
351+
352+
os.Stdout = defaultStdout
353+
354+
outStr := string(out)
355+
356+
assert.Contains(t, outStr, "test span")
357+
assert.Contains(t, outStr, "string attribute value")
358+
assert.Contains(t, outStr, "42")
359+
}

0 commit comments

Comments
 (0)