-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
40 lines (32 loc) · 907 Bytes
/
Copy pathexample_test.go
File metadata and controls
40 lines (32 loc) · 907 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package trace_test
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"github.com/aatuh/api-toolkit/v3/middleware/trace"
)
type exampleIDGen struct {
value string
}
func (g exampleIDGen) New() string { return g.value }
func ExampleNew() {
middleware, err := trace.New(trace.Options{
TraceIDGen: exampleIDGen{value: "11111111111111111111111111111111"},
SpanIDGen: exampleIDGen{value: "2222222222222222"},
})
if err != nil {
panic(err)
}
var gotTraceID string
handler := middleware.Middleware()(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotTraceID = trace.GetTraceID(r)
w.WriteHeader(http.StatusNoContent)
}))
recorder := httptest.NewRecorder()
req := httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/widgets", nil)
handler.ServeHTTP(recorder, req)
fmt.Println(gotTraceID)
// Output:
// 11111111111111111111111111111111
}