-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeacon.go
More file actions
138 lines (124 loc) · 2.57 KB
/
Copy pathbeacon.go
File metadata and controls
138 lines (124 loc) · 2.57 KB
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
package stacktrail
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"sync"
"time"
)
const (
beaconQueueCapacity = 256
beaconWorkerCount = 4
beaconRequestTimeout = time.Second
)
type beaconPayload struct {
TraceID string `json:"trace_id"`
SpanID string `json:"span_id"`
JobName string `json:"job_name"`
StartedAt string `json:"started_at"`
Environment string `json:"environment"`
}
type beaconDispatcher struct {
endpoint string
apiKey string
client *http.Client
queue chan beaconPayload
done chan struct{}
ctx context.Context
cancel context.CancelFunc
mu sync.RWMutex
closed bool
}
func newBeaconDispatcher(endpoint, apiKey string) *beaconDispatcher {
ctx, cancel := context.WithCancel(context.Background())
dispatcher := &beaconDispatcher{
endpoint: endpoint,
apiKey: apiKey,
client: &http.Client{Timeout: beaconRequestTimeout},
queue: make(chan beaconPayload, beaconQueueCapacity),
done: make(chan struct{}),
ctx: ctx,
cancel: cancel,
}
var workers sync.WaitGroup
workers.Add(beaconWorkerCount)
for range beaconWorkerCount {
go func() {
defer workers.Done()
dispatcher.run()
}()
}
go func() {
workers.Wait()
close(dispatcher.done)
}()
return dispatcher
}
func beaconEndpoint(config sdkConfig) string {
if !config.hostedHTTP {
return ""
}
return "https://" + defaultHostedEndpoint + "/v1/job-started"
}
func (d *beaconDispatcher) enqueue(payload beaconPayload) {
d.mu.RLock()
defer d.mu.RUnlock()
if d.closed {
return
}
select {
case d.queue <- payload:
default:
}
}
func (d *beaconDispatcher) shutdown(ctx context.Context) error {
d.mu.Lock()
if !d.closed {
d.closed = true
close(d.queue)
}
d.mu.Unlock()
select {
case <-d.done:
return nil
case <-ctx.Done():
d.cancel()
<-d.done
return ctx.Err()
}
}
func (d *beaconDispatcher) run() {
for {
select {
case <-d.ctx.Done():
return
case payload, ok := <-d.queue:
if !ok {
return
}
d.send(payload)
}
}
}
func (d *beaconDispatcher) send(payload beaconPayload) {
body, err := json.Marshal(payload)
if err != nil {
return
}
ctx, cancel := context.WithTimeout(d.ctx, beaconRequestTimeout)
defer cancel()
request, err := http.NewRequestWithContext(ctx, http.MethodPost, d.endpoint, bytes.NewReader(body))
if err != nil {
return
}
request.Header.Set("Content-Type", "application/json")
request.Header.Set("X-API-Key", d.apiKey)
response, err := d.client.Do(request)
if err != nil {
return
}
_, _ = io.Copy(io.Discard, response.Body)
_ = response.Body.Close()
}