|
| 1 | +package splunk |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +const ( |
| 9 | + bufferSize = 100 |
| 10 | + defaultInterval = 2 * time.Second |
| 11 | + defaultThreshold = 10 |
| 12 | + defaultRetries = 2 |
| 13 | +) |
| 14 | + |
| 15 | +// Writer is a threadsafe, aysnchronous splunk writer. |
| 16 | +// It implements io.Writer for usage in logging libraries, or whatever you want to send to splunk :) |
| 17 | +// Writer.Client's configuration determines what source, sourcetype & index will be used for events |
| 18 | +// Example for logrus: |
| 19 | +// splunkWriter := &splunk.Writer {Client: client} |
| 20 | +// logrus.SetOutput(io.MultiWriter(os.Stdout, splunkWriter)) |
| 21 | +type Writer struct { |
| 22 | + Client *Client |
| 23 | + // How often the write buffer should be flushed to splunk |
| 24 | + FlushInterval time.Duration |
| 25 | + // How many Write()'s before buffer should be flushed to splunk |
| 26 | + FlushThreshold int |
| 27 | + // Max number of retries we should do when we flush the buffer |
| 28 | + MaxRetries int |
| 29 | + dataChan chan *message |
| 30 | + errors chan error |
| 31 | + once sync.Once |
| 32 | +} |
| 33 | + |
| 34 | +// Associates some bytes with the time they were written |
| 35 | +// Helpful if we have long flush intervals to more precisely record the time at which |
| 36 | +// a message was written |
| 37 | +type message struct { |
| 38 | + data []byte |
| 39 | + writtenAt time.Time |
| 40 | +} |
| 41 | + |
| 42 | +// Writer asynchronously writes to splunk in batches |
| 43 | +func (w *Writer) Write(b []byte) (int, error) { |
| 44 | + // only initialize once. Keep all of our buffering in one thread |
| 45 | + w.once.Do(func() { |
| 46 | + // synchronously set up dataChan |
| 47 | + w.dataChan = make(chan *message, bufferSize) |
| 48 | + // Spin up single goroutine to listen to our writes |
| 49 | + w.errors = make(chan error, bufferSize) |
| 50 | + go w.listen() |
| 51 | + }) |
| 52 | + // Send the data to the channel |
| 53 | + w.dataChan <- &message{ |
| 54 | + data: b, |
| 55 | + writtenAt: time.Now(), |
| 56 | + } |
| 57 | + // We don't know if we've hit any errors yet, so just say we're good |
| 58 | + return len(b), nil |
| 59 | +} |
| 60 | + |
| 61 | +// Errors returns a buffered channel of errors. Might be filled over time, might not |
| 62 | +// Useful if you want to record any errors hit when sending data to splunk |
| 63 | +func (w *Writer) Errors() <-chan error { |
| 64 | + return w.errors |
| 65 | +} |
| 66 | + |
| 67 | +// listen for messages |
| 68 | +func (w *Writer) listen() { |
| 69 | + if w.FlushInterval <= 0 { |
| 70 | + w.FlushInterval = defaultInterval |
| 71 | + } |
| 72 | + if w.FlushThreshold == 0 { |
| 73 | + w.FlushThreshold = defaultThreshold |
| 74 | + } |
| 75 | + ticker := time.NewTicker(w.FlushInterval) |
| 76 | + buffer := make([]*message, 0) |
| 77 | + //Define function so we can flush in several places |
| 78 | + flush := func() { |
| 79 | + // Go send the data to splunk |
| 80 | + go w.send(buffer, w.MaxRetries) |
| 81 | + // Make a new array since the old one is getting used by the splunk client now |
| 82 | + buffer = make([]*message, 0) |
| 83 | + } |
| 84 | + for { |
| 85 | + select { |
| 86 | + case <-ticker.C: |
| 87 | + if len(buffer) > 0 { |
| 88 | + flush() |
| 89 | + } |
| 90 | + case d := <-w.dataChan: |
| 91 | + buffer = append(buffer, d) |
| 92 | + if len(buffer) > w.FlushThreshold { |
| 93 | + flush() |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// send sends data to splunk, retrying upon failure |
| 100 | +func (w *Writer) send(messages []*message, retries int) { |
| 101 | + // Create events from our data so we can send them to splunk |
| 102 | + events := make([]*Event, len(messages)) |
| 103 | + for i, m := range messages { |
| 104 | + // Use the configuration of the Client for the event |
| 105 | + events[i] = w.Client.NewEventWithTime(m.writtenAt.Unix(), m.data, w.Client.Source, w.Client.SourceType, w.Client.Index) |
| 106 | + } |
| 107 | + // Send the events to splunk |
| 108 | + err := w.Client.LogEvents(events) |
| 109 | + // If we had any failures, retry as many times as they requested |
| 110 | + if err != nil { |
| 111 | + for i := 0; i < retries; i++ { |
| 112 | + // retry |
| 113 | + err = w.Client.LogEvents(events) |
| 114 | + if err == nil { |
| 115 | + return |
| 116 | + } |
| 117 | + } |
| 118 | + // if we've exhausted our max retries, let someone know via Errors() |
| 119 | + // might not have retried if retries == 0 |
| 120 | + select { |
| 121 | + case w.errors <- err: |
| 122 | + // Don't block in case no one is listening or our errors channel is full |
| 123 | + default: |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments