-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogentries.go
194 lines (165 loc) · 4.27 KB
/
logentries.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
185
186
187
188
189
190
191
192
193
194
// Package logentries implements a logentries apex log handler.
package logentries
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"net"
"os"
"time"
"github.com/apex/log"
)
// ErrorHandling specifies how to deal with errors connecting to and
// delivering logs to LogEntries.
type ErrorHandling int
const (
// PanicOnError specifies that errors will panic, and connection issues
// will be logged.
PanicOnError ErrorHandling = iota
// LogOnError specifies that errors will be logged to stderr.
LogOnError
// IgnoreErrors specifies that errors will not be recorded or handled.
// Your logs may silently not be delivered.
IgnoreErrors
)
const (
// defaultQueueLen specifies the length of the internal queue. If this
// queue fills up, messages will be discarded.
defaultQueueLen = 1024
// dialTimeout specifies how much time is spent waiting for dial to
// complete.
dialTimeout = time.Second * 10
// writeTimeout specifies how much time is spent waiting for a write to
// complete.
writeTimeout = time.Second * 10
// retryDelay specifies how long to wait between reconnections. It is
// important that this value is not too large.
retryDelay = time.Second
)
var (
// defaultAddress contains the default addresses for LogEntries ingestion,
// keyed by a boolean specifying whether or not TLS is desired.
defaultAddress = map[bool]string{
false: "data.logentries.com:80",
true: "data.logentries.com:443",
}
)
// Config holds the configuration options for LogEntries output.
type Config struct {
// Logentries settings.
Token string // LogEntries token to use.
UseTLS bool // Whether or not to use encryption.
Address string // Address, if you want to override it.
TLSConfig *tls.Config // TLS configuration to use.
QueueLen int // Length of internal queue.
Discard bool // Specifies if logs are discarded when the queue fills.
ErrorHandling ErrorHandling // Specifies how errors should be handled.
}
// Handler implementation.
type Handler struct {
*Config
pfx []byte
ch chan *log.Entry
ctx context.Context
cancel context.CancelFunc
}
// New handler. This spawns a new Goroutine and connects to logentries
// immediately.
func New(config Config) *Handler {
if config.Address == "" {
config.Address = defaultAddress[config.UseTLS]
}
if config.QueueLen == 0 {
config.QueueLen = defaultQueueLen
}
// Create context for connectionLoop.
ctx, cancel := context.WithCancel(context.Background())
handler := &Handler{
Config: &config,
pfx: []byte(config.Token + " "),
ch: make(chan *log.Entry, config.QueueLen),
ctx: ctx,
cancel: cancel,
}
go handler.connectionLoop()
return handler
}
// Close closes the logentries connection and frees associated resources.
// Once this is called, logging with this handler will panic. Most users do
// not need to use this.
func (h *Handler) Close() {
h.cancel()
}
func (h *Handler) handleError(err error) {
switch h.ErrorHandling {
case PanicOnError:
panic(err)
case LogOnError:
fmt.Fprintln(os.Stderr, "apex-logentries:", err)
default:
// Ignore.
}
}
// connectionLoop handles the internal connection that sends the logs to
// LogEntries.
func (h *Handler) connectionLoop() {
var conn net.Conn
var err error
var enc *json.Encoder
for {
dialer := net.Dialer{
Timeout: dialTimeout,
KeepAlive: writeTimeout,
}
if h.UseTLS {
conn, err = tls.DialWithDialer(&dialer, "tcp", h.Address, h.TLSConfig)
} else {
conn, err = dialer.Dial("tcp", h.Address)
}
if err != nil {
h.handleError(err)
goto Error
}
enc = json.NewEncoder(conn)
for {
select {
case log := <-h.ch:
conn.SetWriteDeadline(time.Now().Add(writeTimeout))
conn.Write(h.pfx)
err := enc.Encode(log)
if err != nil {
h.handleError(err)
goto Error
}
break
case <-h.ctx.Done():
goto Done
}
}
Error:
select {
case <-time.After(retryDelay):
continue
case <-h.ctx.Done():
goto Done
}
}
Done:
err = conn.Close()
if err != nil {
h.handleError(err)
}
}
// HandleLog implements log.Handler.
func (h *Handler) HandleLog(e *log.Entry) error {
select {
case h.ch <- e:
return nil
default:
if h.Discard {
return fmt.Errorf("queue full")
}
panic("apex-logentries: queue full")
}
}