-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
186 lines (162 loc) · 5.24 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"flag"
"io"
"math/rand"
"strconv"
"strings"
"time"
"github.com/lightstep/lightstep-tracer-go"
"github.com/opentracing/opentracing-go"
log "github.com/sirupsen/logrus"
)
var (
randomChars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
)
type random struct {
maxTagKeys int
tagKeySize int
rand *rand.Rand
spanName string
tagKeys []string
tracer lightstep.Tracer
}
func (r *random) Read(p []byte) (int, error) {
for i := 0; i < len(p); i++ {
p[i] = randomChars[r.rand.Intn(len(randomChars))]
}
return len(p), nil
}
func (r *random) createSpans(num int, size int64) {
log.Infof("create %d spans of size %d", num, size)
for i := 0; i < num; i++ {
r.createSpan(i%r.maxTagKeys, size)
}
}
func (r *random) createSpan(tagKey int, size int64) {
name := r.tagKeys[tagKey]
value, err := io.ReadAll(io.LimitReader(r, size-int64(r.tagKeySize)))
if err != nil {
log.Fatalf("Failed to read value: %v", err)
}
span := r.tracer.StartSpan(r.spanName)
defer span.Finish()
span.SetTag(string(name), string(value))
}
func main() {
r := &random{
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
}
var (
useGRPC bool
logEvents bool
spanName string
componentName string
token string
hostPort string
maxBufferedSpans int
grpcMaxMsgSize int
genTagKeys int
genSpans int
genSpanSize int64
genInterval time.Duration
maxReportingPeriod time.Duration
minReportingPeriod time.Duration
stopAfter time.Duration
propagators = make(map[opentracing.BuiltinFormat]lightstep.Propagator)
)
flag.BoolVar(&useGRPC, "grpc", true, "enable to use GRPC")
flag.BoolVar(&logEvents, "log-events", true, "enable to log all events")
flag.StringVar(&spanName, "span-name", "myspan", "set span name")
flag.StringVar(&componentName, "component-name", "mycomponent", "set component name")
flag.StringVar(&token, "token", "", "set lighstep token")
flag.StringVar(&hostPort, "host", "", "set destination to satellites")
flag.IntVar(&maxBufferedSpans, "max-buffered-spans", 1024, "set maxbufferedspans")
flag.IntVar(&grpcMaxMsgSize, "max-grpc-msg-size", 102400, "set max grpc message size")
flag.DurationVar(&maxReportingPeriod, "max-reporting-period", 2500*time.Millisecond, "set max reporting period")
flag.DurationVar(&minReportingPeriod, "min-reporting-period", 500*time.Millisecond, "set min reporting period")
flag.IntVar(&genTagKeys, "generate-tag-keys", 1, "number of different tag keys to generate")
flag.IntVar(&r.tagKeySize, "generate-tag-key-size", 10, "Size of tag keys to generate")
flag.IntVar(&genSpans, "generate-spans", 10, "number of spans to generate")
flag.Int64Var(&genSpanSize, "generate-span-size", 1024, "size of one span")
flag.DurationVar(&genInterval, "generate-interval", 50*time.Millisecond, "generate span interval")
flag.DurationVar(&stopAfter, "stop-after", time.Second, "")
flag.Parse()
r.spanName = spanName
if genSpanSize <= 10 {
log.Fatalf("Span size has to be bigger than 10, %d", genSpanSize)
}
r.maxTagKeys = genTagKeys
r.tagKeys = make([]string, genTagKeys)
for i := 0; i < genTagKeys; i++ {
k, err := io.ReadAll(io.LimitReader(r, int64(r.tagKeySize)))
if err != nil {
log.Fatalf("Failed to read random: %v", err)
}
r.tagKeys[i] = string(k)
}
a := strings.Split(hostPort, ":")
if len(a) != 2 {
log.Fatalf("-host=<hostport> hostport needs to be host:port, but is %s", hostPort)
}
host, portS := a[0], a[1]
port, err := strconv.Atoi(portS)
if err != nil {
log.Fatalf("Failed to convert port to number: %v", err)
}
prStack := lightstep.PropagatorStack{}
prStack.PushPropagator(lightstep.LightStepPropagator)
propagators[opentracing.HTTPHeaders] = prStack
if logEvents {
lightstep.SetGlobalEventHandler(createEventLogger())
}
tags := map[string]interface{}{
lightstep.ComponentNameKey: componentName,
}
opts := lightstep.Options{
AccessToken: token,
Collector: lightstep.Endpoint{
Host: host,
Port: port,
Plaintext: false,
},
UseGRPC: useGRPC,
Tags: tags,
MaxBufferedSpans: maxBufferedSpans,
GRPCMaxCallSendMsgSizeBytes: grpcMaxMsgSize,
ReportingPeriod: maxReportingPeriod,
MinReportingPeriod: minReportingPeriod,
Propagators: propagators,
}
r.tracer = lightstep.NewTracer(opts)
quit := make(chan struct{})
go func() {
time.Sleep(stopAfter)
quit <- struct{}{}
}()
ticker := time.NewTicker(genInterval)
for {
select {
case <-quit:
log.Info("quit")
return
case <-ticker.C:
r.createSpans(genSpans, genSpanSize)
}
}
}
func createEventLogger() lightstep.EventHandler {
return func(event lightstep.Event) {
if e, ok := event.(lightstep.ErrorEvent); ok {
log.WithError(e).Warn("LightStep tracer received an error event")
} else if e, ok := event.(lightstep.EventStatusReport); ok {
log.WithFields(log.Fields{
"duration": e.Duration(),
"sent_spans": e.SentSpans(),
"dropped_spans": e.DroppedSpans(),
}).Debugf("Sent a report to the collectors")
} else if _, ok := event.(lightstep.EventTracerDisabled); ok {
log.Warn("LightStep tracer has been disabled")
}
}
}