-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
143 lines (119 loc) · 2.98 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"context"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"time"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/opentracing/opentracing-go/log"
"github.com/uber/jaeger-client-go/config"
)
type Order struct {
ID int
Name string
}
func main() {
tracer, closer := initJeager()
defer func() {
err := closer.Close()
if err != nil {
fmt.Println(fmt.Sprintf("error while close tracer, got: %v", err))
}
}()
id := orderIDGenerator()
span := tracer.StartSpan("svc.order")
defer span.Finish()
opentracing.SetGlobalTracer(tracer)
orderData := Order{}
orderData.ID = id
orderData.Name = "author"
span.SetBaggageItem("data", fmt.Sprint(orderData))
span.SetTag("svc.order", fmt.Sprintf("order-%d", id))
span.LogFields(
log.String("event", "order"),
log.String("id", fmt.Sprint(id)),
)
ctx := opentracing.ContextWithSpan(context.Background(), span)
storeOrder(ctx, id)
updateOrder(ctx)
}
func initJeager() (opentracing.Tracer, io.Closer) {
cfg := &config.Configuration{
Sampler: &config.SamplerConfig{
Type: "const",
Param: 1,
},
Reporter: &config.ReporterConfig{
LogSpans: true,
},
ServiceName: "order.service-connector",
}
tracer, closer, err := cfg.NewTracer()
if err != nil {
panic(fmt.Sprintf("error while create intance for tracer, got: %v", err))
}
return tracer, closer
}
func orderIDGenerator() int {
rand.Seed(time.Now().UnixNano())
min := 10
max := 30
return rand.Intn(max-min+1) + min
}
func storeOrder(ctx context.Context, id int) {
span, _ := opentracing.StartSpanFromContext(ctx, "storing.order")
defer span.Finish()
url := "http://localhost:8080/collect"
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
panic(err)
}
ext.SpanKindRPCClient.Set(span)
ext.HTTPUrl.Set(span, url)
ext.HTTPMethod.Set(span, http.MethodGet)
err = span.Tracer().Inject(span.Context(), opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header))
if err != nil {
fmt.Println(fmt.Sprintf("error while inject span into http header: %v", err))
}
_, err = Do(req)
if err != nil {
panic(err.Error())
}
span.SetTag("store", id)
span.LogFields(
log.String("event", "store"),
log.String("id", fmt.Sprint(id)),
)
}
func updateOrder(ctx context.Context) {
span, _ := opentracing.StartSpanFromContext(ctx, "update.order")
defer span.Finish()
fmt.Println("processing...")
time.Sleep(3 * time.Second)
fmt.Println("process is done")
span.SetTag("update.order", "static")
}
func Do(req *http.Request) ([]byte, error) {
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer func() {
err = resp.Body.Close()
if err != nil {
fmt.Println(fmt.Errorf("error close response body, got: %v", err))
}
}()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("http status code: %d, body: %s", resp.StatusCode, body)
}
return body, nil
}