This repository has been archived by the owner on Jan 26, 2023. It is now read-only.
/
wavefront.go
184 lines (155 loc) · 3.81 KB
/
wavefront.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
// Package wavefront provides OpenCensus trace and stats support
// to push metrics, histograms and traces into Wavefront.
package wavefront
import (
"errors"
"log"
"sync"
"sync/atomic"
"github.com/wavefronthq/wavefront-sdk-go/application"
"github.com/wavefronthq/wavefront-sdk-go/histogram"
"github.com/wavefronthq/wavefront-sdk-go/senders"
"go.opencensus.io/stats/view"
"go.opencensus.io/trace"
)
const (
// DefaultQueueSize is used when QueueSize option is not specified
DefaultQueueSize = 1000
defaultSource = ""
nanoToMillis int64 = 1e6
)
// Options is all the configurable options
type Options struct {
Source string
Hgs map[histogram.Granularity]bool
appMap map[string]string
qSize int
VerboseLogging bool
DisableSelfHealth bool
}
// Option allows customization
type Option func(*Options)
type sendCmd func()
// Source overrides the deault source
func Source(source string) Option {
return func(o *Options) {
o.Source = source
}
}
// Granularity enables specified granularities when
// sending Wavefront histograms
func Granularity(hgs ...histogram.Granularity) Option {
return func(o *Options) {
for _, g := range hgs {
o.Hgs[g] = true
}
}
}
// AppTags allows setting Application, Service, etc...
// Shown in Wavefront UI
func AppTags(app application.Tags) Option {
return func(o *Options) {
o.appMap = app.Map()
}
}
// QueueSize sets the maximum number of queued metrics and spans.
// Spans/Metrics are dropped if the Queue is full
func QueueSize(queueSize int) Option {
return func(o *Options) {
o.qSize = queueSize
}
}
// DisableSelfHealth disables sending exporter health metrics
// such as dropped metrics and spans
func DisableSelfHealth() Option {
return func(o *Options) {
o.DisableSelfHealth = true
}
}
// VerboseLogging enables logging of errors per span/metric.
// Logs to stderr or equivalent
func VerboseLogging() Option {
return func(o *Options) {
o.VerboseLogging = true
}
}
// Exporter is the main exporter
type Exporter struct {
sender senders.Sender
wg sync.WaitGroup
lock sync.RWMutex
running bool
// Embeddings
Options
_SelfMetrics
}
// NewExporter returns a trace.Exporter configured to upload traces and views
// to the configured wavefront instance (via Wavefront Sender)
//
// Documentation for Wavefront Sender is available at
// https://github.com/wavefrontHQ/wavefront-sdk-go
//
// Option... add additional options to the exporter.
func NewExporter(sender senders.Sender, option ...Option) (*Exporter, error) {
defOptions := Options{
Source: defaultSource,
Hgs: map[histogram.Granularity]bool{},
qSize: DefaultQueueSize,
}
for _, o := range option {
o(&defOptions)
}
if defOptions.qSize < 0 {
return nil, errors.New("QueueSize cannot be negative")
}
exp := &Exporter{
sender: sender,
Options: defOptions,
running: true,
}
if !exp.DisableSelfHealth {
exp.ReportSelfHealth() // Disable by default?
}
return exp, nil
}
// Stop the exporter and flushes the sender
func (e *Exporter) Stop() {
e.lock.Lock()
e.running = false
e.lock.Unlock()
e.StopSelfHealth()
e.wg.Wait()
e.sender.Flush()
}
// ExportSpan exports given span to Wavefront
func (e *Exporter) ExportSpan(spanData *trace.SpanData) {
e.processSpan(spanData)
}
// ExportView exports given view to Wavefront
func (e *Exporter) ExportView(viewData *view.Data) {
e.processView(viewData)
}
// Helpers
func (e *Exporter) queueCmd(cmd sendCmd) bool {
e.lock.RLock()
defer e.lock.RUnlock()
if !e.running {
return false
}
e.wg.Add(1)
go func() {
cmd()
e.wg.Done()
}()
return true
}
func (e *Exporter) logError(msg string, errs ...error) {
for ei, err := range errs {
if err != nil {
atomic.AddUint64(&e.senderErrors, 1)
if e.VerboseLogging {
log.Printf("%s (%d): %s", msg, ei, err)
}
}
}
}