-
Notifications
You must be signed in to change notification settings - Fork 34
/
server.go
98 lines (85 loc) · 2.73 KB
/
server.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
package main
import (
"fmt"
"log"
"math/rand"
"net/http"
"time"
"github.com/opentracing/opentracing-go"
)
func indexHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<a href="/home"> Click here to start a request </a>`))
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Request started"))
sp := opentracing.StartSpan("GET /home") // Start a new root span.
defer sp.Finish()
asyncReq, _ := http.NewRequest("GET", "http://localhost:8080/async", nil)
// Inject the trace information into the HTTP Headers.
err := sp.Tracer().Inject(sp.Context(), opentracing.TextMap, opentracing.HTTPHeadersCarrier(asyncReq.Header))
if err != nil {
log.Fatalf("%s: Couldn't inject headers (%v)", r.URL.Path, err)
}
go func() {
sleepMilli(50)
if _, err := http.DefaultClient.Do(asyncReq); err != nil {
log.Printf("%s: Async call failed (%v)", r.URL.Path, err)
}
}()
sleepMilli(10)
syncReq, _ := http.NewRequest("GET", "http://localhost:8080/service", nil)
// Inject the trace info into the headers.
err = sp.Tracer().Inject(sp.Context(),
opentracing.TextMap,
opentracing.HTTPHeadersCarrier(syncReq.Header))
if err != nil {
log.Fatalf("%s: Couldn't inject headers (%v)", r.URL.Path, err)
}
if _, err = http.DefaultClient.Do(syncReq); err != nil {
log.Printf("%s: Synchronous call failed (%v)", r.URL.Path, err)
return
}
w.Write([]byte("... done!"))
}
func serviceHandler(w http.ResponseWriter, r *http.Request) {
opName := fmt.Sprintf("%s %s", r.Method, r.URL.Path)
var sp opentracing.Span
spCtx, err := opentracing.GlobalTracer().Extract(opentracing.TextMap,
opentracing.HTTPHeadersCarrier(r.Header))
if err == nil {
sp = opentracing.StartSpan(opName, opentracing.ChildOf(spCtx))
} else {
sp = opentracing.StartSpan(opName)
}
defer sp.Finish()
sleepMilli(50)
dbReq, _ := http.NewRequest("GET", "http://localhost:8080/db", nil)
err = sp.Tracer().Inject(sp.Context(),
opentracing.TextMap,
opentracing.HTTPHeadersCarrier(dbReq.Header))
if err != nil {
log.Fatalf("%s: Couldn't inject headers (%v)", r.URL.Path, err)
}
if _, err := http.DefaultClient.Do(dbReq); err != nil {
sp.LogEventWithPayload("db request error", err)
}
}
func dbHandler(w http.ResponseWriter, r *http.Request) {
var sp opentracing.Span
spanCtx, err := opentracing.GlobalTracer().Extract(opentracing.TextMap,
opentracing.HTTPHeadersCarrier(r.Header))
if err != nil {
log.Println("%s: Could not join trace (%v)", r.URL.Path, err)
return
}
if err == nil {
sp = opentracing.StartSpan("GET /db", opentracing.ChildOf(spanCtx))
} else {
sp = opentracing.StartSpan("GET /db")
}
defer sp.Finish()
sleepMilli(25)
}
func sleepMilli(min int) {
time.Sleep(time.Millisecond * time.Duration(min+rand.Intn(100)))
}