This repository has been archived by the owner on Jul 31, 2023. It is now read-only.
/
latency_injector_span_listener.go
77 lines (65 loc) · 1.92 KB
/
latency_injector_span_listener.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
package tracer
import (
"math/rand"
"time"
"github.com/thundra-io/thundra-lambda-agent-go/v2/constants"
)
var defaultDelay int64 = 100
type LatencyInjectorSpanListener struct {
Delay int64
InjectOnFinish bool
RandomizeDelay bool
AddInfoTags bool
}
func (l *LatencyInjectorSpanListener) OnSpanStarted(span *spanImpl) {
if !l.InjectOnFinish {
l.injectDelay(span)
}
}
func (l *LatencyInjectorSpanListener) OnSpanFinished(span *spanImpl) {
if l.InjectOnFinish {
l.injectDelay(span)
}
}
func (l *LatencyInjectorSpanListener) PanicOnError() bool {
return false
}
func (l *LatencyInjectorSpanListener) injectDelay(span *spanImpl) {
delay := l.Delay
if delay <= 0 {
delay = defaultDelay
}
if l.RandomizeDelay {
delay = rand.Int63n(delay)
}
if l.AddInfoTags {
l.addInfoTags(span, delay)
}
time.Sleep(time.Duration(delay) * time.Millisecond)
}
func (l *LatencyInjectorSpanListener) addInfoTags(span *spanImpl, injectedDelay int64) {
infoTags := map[string]interface{}{
"type": "latency_injecter_span_listener",
"inject_on_finish": l.InjectOnFinish,
"delay": l.Delay,
"injected_delay": injectedDelay,
}
span.SetTag(constants.ThundraLambdaSpanListenerInfoTag, infoTags)
}
// NewLatencyInjectorSpanListener creates and returns a new LatencyInjectorSpanListener from config
func NewLatencyInjectorSpanListener(config map[string]interface{}) ThundraSpanListener {
spanListener := &LatencyInjectorSpanListener{Delay: defaultDelay, AddInfoTags: true}
if delay, ok := config["delay"].(float64); ok {
spanListener.Delay = int64(delay)
}
if injectOnFinish, ok := config["injectOnFinish"].(bool); ok {
spanListener.InjectOnFinish = injectOnFinish
}
if randomizeDelay, ok := config["randomizeDelay"].(bool); ok {
spanListener.RandomizeDelay = randomizeDelay
}
if addInfoTags, ok := config["addInfoTags"].(bool); ok {
spanListener.AddInfoTags = addInfoTags
}
return spanListener
}