-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample_httpserver_test.go
109 lines (87 loc) · 2.97 KB
/
example_httpserver_test.go
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package zipkin_test
import (
"log"
"net/http"
"net/http/httptest"
"os"
"time"
"github.com/gorilla/mux"
"github.com/openzipkin/zipkin-go"
zipkinhttp "github.com/openzipkin/zipkin-go/middleware/http"
logreporter "github.com/openzipkin/zipkin-go/reporter/log"
)
func Example() {
// set up a span reporter
reporter := logreporter.NewReporter(log.New(os.Stderr, "", log.LstdFlags))
defer reporter.Close()
// create our local service endpoint
endpoint, err := zipkin.NewEndpoint("myService", "localhost:0")
if err != nil {
log.Fatalf("unable to create local endpoint: %+v\n", err)
}
// initialize our tracer
tracer, err := zipkin.NewTracer(reporter, zipkin.WithLocalEndpoint(endpoint))
if err != nil {
log.Fatalf("unable to create tracer: %+v\n", err)
}
// create global zipkin http server middleware
serverMiddleware := zipkinhttp.NewServerMiddleware(
tracer, zipkinhttp.TagResponseSize(true),
)
// create global zipkin traced http client
client, err := zipkinhttp.NewClient(tracer, zipkinhttp.ClientTrace(true))
if err != nil {
log.Fatalf("unable to create client: %+v\n", err)
}
// initialize router
router := mux.NewRouter()
// start web service with zipkin http server middleware
ts := httptest.NewServer(serverMiddleware(router))
defer ts.Close()
// set-up handlers
router.Methods("GET").Path("/some_function").HandlerFunc(someFunc(client, ts.URL))
router.Methods("POST").Path("/other_function").HandlerFunc(otherFunc(client))
// initiate a call to some_func
req, err := http.NewRequest("GET", ts.URL+"/some_function", nil)
if err != nil {
log.Fatalf("unable to create http request: %+v\n", err)
}
res, err := client.DoWithAppSpan(req, "some_function")
if err != nil {
log.Fatalf("unable to do http request: %+v\n", err)
}
res.Body.Close()
// Output:
}
func someFunc(client *zipkinhttp.Client, url string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Printf("some_function called with method: %s\n", r.Method)
// retrieve span from context (created by server middleware)
span := zipkin.SpanFromContext(r.Context())
span.Tag("custom_key", "some value")
// doing some expensive calculations....
time.Sleep(25 * time.Millisecond)
span.Annotate(time.Now(), "expensive_calc_done")
newRequest, err := http.NewRequest("POST", url+"/other_function", nil)
if err != nil {
log.Printf("unable to create client: %+v\n", err)
http.Error(w, err.Error(), 500)
return
}
ctx := zipkin.NewContext(newRequest.Context(), span)
newRequest = newRequest.WithContext(ctx)
res, err := client.DoWithAppSpan(newRequest, "other_function")
if err != nil {
log.Printf("call to other_function returned error: %+v\n", err)
http.Error(w, err.Error(), 500)
return
}
res.Body.Close()
}
}
func otherFunc(client *zipkinhttp.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Printf("other_function called with method: %s\n", r.Method)
time.Sleep(50 * time.Millisecond)
}
}