-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
120 lines (102 loc) · 3.75 KB
/
main.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
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"fmt"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/openzipkin/zipkin-go-opentracing"
"log"
"math/rand"
"net/http"
"time"
)
// Acts as our index page
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"))
span := opentracing.StartSpan("/home")
defer span.Finish()
asyncReq, _ := http.NewRequest("GET", "http://localhost:8080/async", nil)
err := span.Tracer().Inject(span.Context(), opentracing.TextMap, opentracing.HTTPHeadersCarrier(asyncReq.Header))
if err != nil {
log.Fatalf("Could not inject span context into header: %v", err)
}
go func() {
if _, err := http.DefaultClient.Do(asyncReq); err != nil {
ext.Error.Set(span, true)
span.LogKV("GET async error", err)
}
}()
srcReq, _ := http.NewRequest("GET", "http://localhost:8080/service", nil)
err = span.Tracer().Inject(span.Context(), opentracing.TextMap, opentracing.HTTPHeadersCarrier(srcReq.Header))
if err != nil {
log.Fatalf("Could not inject span context into header: %v", err)
}
if _, err := http.DefaultClient.Do(srcReq); err != nil {
ext.Error.Set(span, true)
span.LogKV("GET service error", err)
}
time.Sleep(time.Duration(rand.Intn(200)) * time.Millisecond)
w.Write([]byte("Request done!"))
}
// Mocks a service endpoint that makes a DB call
func serviceHandler(w http.ResponseWriter, r *http.Request) {
var span opentracing.Span
opName := r.URL.Path
wireContext, err := opentracing.GlobalTracer().Extract(opentracing.TextMap, opentracing.HTTPHeadersCarrier(r.Header))
if err != nil {
span = opentracing.StartSpan(opName)
} else {
span = opentracing.StartSpan(opName, opentracing.ChildOf(wireContext))
}
defer span.Finish()
dbReq, _ := http.NewRequest("GET", "http://localhost:8080/db", nil)
err = span.Tracer().Inject(span.Context(), opentracing.TextMap, opentracing.HTTPHeadersCarrier(dbReq.Header))
if err != nil {
log.Fatalf("Could not inject span context into header: %v", err)
}
if _, err = http.DefaultClient.Do(dbReq); err != nil {
ext.Error.Set(span, true)
span.LogKV("GET db error", err)
}
time.Sleep(time.Duration(rand.Intn(200)) * time.Millisecond)
// ...
}
// Mocks a DB call
func dbHandler(w http.ResponseWriter, r *http.Request) {
var sp opentracing.Span
opName := r.URL.Path
wireContext, err := opentracing.GlobalTracer().Extract(opentracing.TextMap, opentracing.HTTPHeadersCarrier(r.Header))
if err != nil {
sp = opentracing.StartSpan(opName)
} else {
sp = opentracing.StartSpan(opName, opentracing.ChildOf(wireContext))
}
defer sp.Finish()
time.Sleep(time.Duration(rand.Intn(200)) * time.Millisecond)
// here would be the actual call to a DB.
}
func main() {
port := 8080
addr := fmt.Sprintf(":%d", port)
zipkinCollector, err := zipkintracer.NewHTTPCollector("http://zipkin:9411/api/v1/spans")
if err != nil {
log.Fatalf("unable to create Zipkin HTTP collector: %+v", err)
}
defer zipkinCollector.Close()
zipkinRecorder := zipkintracer.NewRecorder(zipkinCollector, false, "0.0.0.0:8080", "payprocessor")
zipkinTracer, err := zipkintracer.NewTracer(zipkinRecorder, zipkintracer.ClientServerSameSpan(true), zipkintracer.TraceID128Bit(true))
if err != nil {
log.Fatalf("unable to create Zipkin tracer: %+v", err)
}
opentracing.InitGlobalTracer(zipkinTracer)
mux := http.NewServeMux()
mux.HandleFunc("/", indexHandler)
mux.HandleFunc("/home", homeHandler)
mux.HandleFunc("/async", serviceHandler)
mux.HandleFunc("/service", serviceHandler)
mux.HandleFunc("/db", dbHandler)
fmt.Printf("Go to http://localhost:%d/home to start a request!\n", port)
log.Fatal(http.ListenAndServe(addr, mux))
}