/
main.go
161 lines (145 loc) · 3.77 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
package main
import (
"context"
"crypto/sha256"
"fmt"
"math/rand"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"go.uber.org/zap"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"gopkg.in/DataDog/dd-trace-go.v1/profiler"
)
func main() {
logger, _ := zap.NewProduction()
defer logger.Sync()
tracer.Start(
tracer.WithLogStartup(true),
tracer.WithDebugMode(true),
)
defer tracer.Stop()
err := profiler.Start(
profiler.WithProfileTypes(
profiler.CPUProfile,
profiler.HeapProfile,
),
)
if err != nil {
logger.Error(fmt.Sprintf("failed to start a profiler: %s", err))
}
defer profiler.Stop()
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
defer cancel()
go generateSpanForever(
ctx,
logger,
envToString("SPAN_NAME", "span-generator"),
envToString("SPAN_TYPE", "custom"),
)
<-ctx.Done()
logger.Info("bye~~")
}
func generateSpanForever(ctx context.Context, logger *zap.Logger, name, spanType string) {
spanGenTicker := NewRandomTicker(
time.Millisecond*time.Duration(envVarToInt64("SPAN_INTERVAL_MIN", 1000)),
time.Millisecond*time.Duration(envVarToInt64("SPAN_INTERVAL_MAX", 10000)),
)
errGenTicker := NewRandomTicker(
time.Millisecond*time.Duration(envVarToInt64("ERROR_SPAN_INTERVAL_MIN", 1000)),
time.Millisecond*time.Duration(envVarToInt64("ERROR_SPAN_INTERVAL_MAX", 30000)),
)
for {
select {
case <-ctx.Done():
return
case t := <-spanGenTicker.C:
span := tracer.StartSpan(name, tracer.SpanType(spanType))
h := sha256.Sum256([]byte(t.String()))
logger.Info(
fmt.Sprintf("generate a span with the sha256 hashed timestamp(%x)", h),
zap.Uint64("dd.span_id", span.Context().SpanID()),
zap.Uint64("dd.trace_id", span.Context().TraceID()),
)
span.Finish()
case <-errGenTicker.C:
span := tracer.StartSpan(name, tracer.SpanType(spanType))
err := fmt.Errorf("generate an error span")
logger.Error(
err.Error(),
zap.Uint64("dd.span_id", span.Context().SpanID()),
zap.Uint64("dd.trace_id", span.Context().TraceID()),
)
span.Finish(tracer.WithError(err))
}
}
}
func envToString(key, fallback string) string {
if v := os.Getenv(key); v != "" {
return v
}
return fallback
}
func envVarToInt64(name string, fallback int64) int64 {
if v := os.Getenv(name); v != "" {
if i, err := strconv.ParseInt(v, 10, 64); err == nil {
return i
}
}
return fallback
}
// RandomTicker is similar to time.Ticker but ticks at random intervals between
// the min and max duration values (stored internally as int64 nanosecond
// counts).
type RandomTicker struct {
C chan time.Time
stopc chan chan struct{}
min int64
max int64
}
// NewRandomTicker returns a pointer to an initialized instance of the
// RandomTicker. Min and max are durations of the shortest and longest allowed
// ticks. Ticker will run in a goroutine until explicitly stopped.
func NewRandomTicker(min, max time.Duration) *RandomTicker {
rt := &RandomTicker{
C: make(chan time.Time),
stopc: make(chan chan struct{}),
min: min.Nanoseconds(),
max: max.Nanoseconds(),
}
go rt.loop()
return rt
}
// Stop terminates the ticker goroutine and closes the C channel.
func (rt *RandomTicker) Stop() {
c := make(chan struct{})
rt.stopc <- c
<-c
}
func (rt *RandomTicker) loop() {
defer close(rt.C)
t := time.NewTimer(rt.nextInterval())
for {
// either a stop signal or a timeout
select {
case c := <-rt.stopc:
t.Stop()
close(c)
return
case <-t.C:
select {
case rt.C <- time.Now():
t.Stop()
t = time.NewTimer(rt.nextInterval())
default:
// there could be no one receiving...
}
}
}
}
func (rt *RandomTicker) nextInterval() time.Duration {
interval := rand.Int63n(rt.max-rt.min) + rt.min
return time.Duration(interval) * time.Nanosecond
}