-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhooks.go
170 lines (150 loc) · 3.55 KB
/
hooks.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
package kinesis
import (
"context"
"sync"
"go.opencensus.io/stats"
"go.opencensus.io/tag"
)
type tagCache struct {
sync.RWMutex
tags map[tag.Key]map[string]tag.Mutator
}
func (c *tagCache) get(name tag.Key, value string) tag.Mutator {
c.RLock()
values, ok := c.tags[name]
c.RUnlock()
if !ok {
t := tag.Upsert(name, value)
c.Lock()
c.tags[name] = map[string]tag.Mutator{
value: t,
}
c.Unlock()
return t
}
c.RLock()
t, ok := values[value]
c.RUnlock()
if ok {
return t
}
t = tag.Upsert(name, value)
c.Lock()
values[value] = t
c.Unlock()
return t
}
// KinesisHooker interface abstracts away the hook so one can pass in a producer and inject your own implementation
type KinesisHooker interface {
OnDrain(bytes, length int64)
OnPutRecords(batches, spanlists, bytes, putLatencyMS int64, reason string)
OnPutErr(errCode string)
OnDropped(batches, spanlists, bytes int64)
OnSpanEnqueued()
OnSpanDequeued()
OnXLSpanDropped(size int)
OnPutSpanListFlushed(spans, bytes int64)
OnCompressed(original, compressed int64)
}
var _ KinesisHooker = &kinesisHooks{}
type kinesisHooks struct {
exporterName string
streamName string
commonTags []tag.Mutator
tagCache tagCache
}
func newKinesisHooks(name, streamName, shardID string) *kinesisHooks {
tags := []tag.Mutator{
tag.Upsert(tagExporterName, name),
tag.Upsert(tagStreamName, streamName),
}
if shardID != "" {
tags = append(tags, tag.Upsert(tagShardID, shardID))
}
return &kinesisHooks{
exporterName: name,
streamName: streamName,
commonTags: tags,
tagCache: tagCache{
tags: map[tag.Key]map[string]tag.Mutator{},
},
}
}
func (h *kinesisHooks) tags(name tag.Key, value string) []tag.Mutator {
tags := make([]tag.Mutator, 0, len(h.commonTags)+1)
copy(tags, h.commonTags)
tags = append(tags, h.tagCache.get(name, value))
return tags
}
func (h *kinesisHooks) OnDrain(bytes, length int64) {
_ = stats.RecordWithTags(
context.Background(),
h.commonTags,
statDrainBytes.M(bytes),
statDrainLength.M(length),
)
}
func (h *kinesisHooks) OnPutRecords(batches, spanlists, bytes, putLatencyMS int64, reason string) {
_ = stats.RecordWithTags(
context.Background(),
h.tags(tagFlushReason, reason),
statPutRequests.M(1),
statPutBatches.M(batches),
statPutSpanLists.M(spanlists),
statPutBytes.M(bytes),
statPutLatency.M(putLatencyMS),
)
}
func (h *kinesisHooks) OnPutErr(errCode string) {
_ = stats.RecordWithTags(
context.Background(),
h.tags(tagErrCode, errCode),
statPutErrors.M(1),
)
}
func (h *kinesisHooks) OnDropped(batches, spanlists, bytes int64) {
_ = stats.RecordWithTags(
context.Background(),
h.commonTags,
statDroppedBatches.M(batches),
statDroppedSpanLists.M(spanlists),
statDroppedBytes.M(bytes),
)
}
func (h *kinesisHooks) OnSpanEnqueued() {
_ = stats.RecordWithTags(
context.Background(),
h.commonTags,
statEnqueuedSpans.M(1),
)
}
func (h *kinesisHooks) OnSpanDequeued() {
_ = stats.RecordWithTags(
context.Background(),
h.commonTags,
statDequeuedSpans.M(1),
)
}
func (h *kinesisHooks) OnXLSpanDropped(size int) {
_ = stats.RecordWithTags(
context.Background(),
h.commonTags,
statXLSpansBytes.M(int64(size)),
statXLSpans.M(1),
)
}
func (h *kinesisHooks) OnPutSpanListFlushed(spans, bytes int64) {
_ = stats.RecordWithTags(
context.Background(),
h.commonTags,
statFlushedSpans.M(spans),
statSpanListBytes.M(bytes),
)
}
func (h *kinesisHooks) OnCompressed(original, compressed int64) {
_ = stats.RecordWithTags(
context.Background(),
h.commonTags,
statCompressFactor.M(original/compressed),
)
}