Skip to content

Commit 87e94c9

Browse files
committed
SCFF-35 added wait period between posts to sumo, modifying Buffer
1 parent 2c426fc commit 87e94c9

1 file changed

Lines changed: 68 additions & 30 deletions

File tree

sumoCFFirehose/sumoLogicAppender.go

Lines changed: 68 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package sumoCFFirehose
22

33
import (
44
"bytes"
5+
"fmt"
56
"net/http"
7+
"runtime"
68
"time"
79

810
"bitbucket.org/mcplusa-ondemand/firehose-to-sumologic/eventQueue"
@@ -20,6 +22,12 @@ type SumoLogicAppender struct {
2022
logStringToSend *bytes.Buffer
2123
sumoPostMinimumDelay time.Duration
2224
timerPostMinimum time.Time
25+
bufferToSend SumoBuffer
26+
}
27+
28+
type SumoBuffer struct {
29+
logStringToSend *bytes.Buffer
30+
logEventsInCurrentBuffer int
2331
}
2432

2533
func NewSumoLogicAppender(urlValue string, connectionTimeoutValue int, nozzleQueue *eventQueue.Queue, eventsBatchSize int, sumoPostMinimumDelay time.Duration) *SumoLogicAppender {
@@ -32,28 +40,50 @@ func NewSumoLogicAppender(urlValue string, connectionTimeoutValue int, nozzleQue
3240
logEventsInCurrentBuffer: 0,
3341
logStringToSend: bytes.NewBufferString(""),
3442
sumoPostMinimumDelay: sumoPostMinimumDelay,
43+
timerPostMinimum: time.Now(),
44+
bufferToSend: SumoBuffer{
45+
logStringToSend: bytes.NewBufferString(""),
46+
logEventsInCurrentBuffer: 0,
47+
},
3548
}
3649
}
3750

3851
func (s *SumoLogicAppender) Start() {
52+
runtime.GOMAXPROCS(1)
3953
timer := time.Now()
40-
s.timerPostMinimum = time.Now() //starting timer for sumo post minimum
54+
tempBuffer := &SumoBuffer{
55+
logStringToSend: bytes.NewBufferString(""),
56+
logEventsInCurrentBuffer: 0,
57+
}
58+
4159
logging.Info.Println("Starting Appender Worker")
4260
for {
4361
time.Sleep(300 * time.Millisecond) //delay
4462
// while queue is not empty && s.eventsBatchSize not completed, queue.POP (appendLogs)
45-
for s.nozzleQueue.GetCount() != 0 && s.logEventsInCurrentBuffer <= s.eventsBatchSize {
46-
s.AppendLogs() //this method POP an event from queue
47-
timer = time.Now() //reset timer
48-
if s.logEventsInCurrentBuffer == s.eventsBatchSize { //if buffer is full, send logs to sumo
49-
logging.Trace.Println("Batch Size complete")
63+
for s.nozzleQueue.GetCount() != 0 && s.bufferToSend.logEventsInCurrentBuffer < s.eventsBatchSize {
64+
s.AppendLogs(&s.bufferToSend) //this method POP an event from queue to Buffer
65+
timer = time.Now() //reset timer
66+
if s.bufferToSend.logEventsInCurrentBuffer == s.eventsBatchSize && tempBuffer.logEventsInCurrentBuffer < s.eventsBatchSize { //if buffer is full, send logs to sumo
67+
logging.Info.Println("Batch Size complete, filling temp buffer")
68+
s.AppendLogs(tempBuffer)
5069
break
5170
} else if time.Since(timer).Seconds() >= 10 { // else if timer is up, send existing logs to sumo
52-
logging.Trace.Println("Sending current batch of logs after timer exceeded limit")
71+
logging.Info.Println("Sending current batch of logs after timer exceeded limit")
72+
go s.SendToSumo(s.bufferToSend.logStringToSend)
5373
break
5474
}
5575
}
56-
s.SendToSumo(s.logStringToSend)
76+
//if batch size is met, send to sumo and reset temp buffer
77+
if s.bufferToSend.logEventsInCurrentBuffer == s.eventsBatchSize {
78+
go s.SendToSumo(s.bufferToSend.logStringToSend)
79+
s.bufferToSend.logStringToSend = tempBuffer.logStringToSend
80+
s.bufferToSend.logEventsInCurrentBuffer = tempBuffer.logEventsInCurrentBuffer
81+
tempBuffer = &SumoBuffer{ //reset temp buffer
82+
logStringToSend: bytes.NewBufferString(""),
83+
logEventsInCurrentBuffer: 0,
84+
}
85+
}
86+
5787
timer = time.Now() //reset timer
5888
}
5989
}
@@ -72,35 +102,43 @@ func StringBuilder(event *events.Event) string {
72102
return buf.String()
73103
}
74104

75-
func (s *SumoLogicAppender) AppendLogs() {
105+
func (s *SumoLogicAppender) AppendLogs(buffer *SumoBuffer) {
76106
// the appender calls for the next message in the queue and parse it to a string
77-
s.logStringToSend.Write([]byte(StringBuilder(s.nozzleQueue.Pop())))
78-
s.logEventsInCurrentBuffer++
107+
// then fills a buffer with the message
108+
buffer.logStringToSend.Write([]byte(StringBuilder(s.nozzleQueue.Pop())))
109+
buffer.logEventsInCurrentBuffer++
110+
fmt.Println(s.bufferToSend.logEventsInCurrentBuffer)
111+
79112
}
80113

81114
func (s *SumoLogicAppender) SendToSumo(log *bytes.Buffer) {
82115
//wait period between posts to Sumo
83-
if time.Since(s.timerPostMinimum) >= s.sumoPostMinimumDelay {
84-
logging.Trace.Println("Sending logs to Sumologic...")
85-
request, err := http.NewRequest("POST", s.url, log)
86-
if err != nil {
87-
logging.Error.Printf("http.NewRequest() error: %v\n", err)
88-
return
89-
}
90-
//request.Header.Add("content-type", "application/json")
91-
//request.SetBasicAuth("admin", "admin")
92-
response, err := s.httpClient.Do(request)
116+
fmt.Println(log.String())
117+
fmt.Println(".............")
118+
for time.Since(s.timerPostMinimum) < s.sumoPostMinimumDelay {
119+
time.Sleep(30 * time.Millisecond) // wait to retry
120+
}
121+
logging.Trace.Println("Sending logs to Sumologic...")
122+
request, err := http.NewRequest("POST", s.url, log)
123+
if err != nil {
124+
logging.Error.Printf("http.NewRequest() error: %v\n", err)
125+
return
126+
}
127+
//request.Header.Add("content-type", "application/json")
128+
//request.SetBasicAuth("admin", "admin")
129+
response, err := s.httpClient.Do(request)
93130

94-
if err != nil {
95-
logging.Error.Printf("http.Do() error: %v\n", err)
96-
return
97-
} else {
98-
logging.Trace.Println("Do(Request) successful")
99-
}
100-
s.logEventsInCurrentBuffer = 0 // reset counter
101-
s.logStringToSend = bytes.NewBufferString("") //reset String
102-
defer response.Body.Close()
131+
if err != nil {
132+
logging.Error.Printf("http.Do() error: %v\n", err)
133+
return
134+
} else {
135+
logging.Trace.Println("Do(Request) successful")
103136
s.timerPostMinimum = time.Now() //reset timer post minimum
104137
}
138+
s.bufferToSend = SumoBuffer{
139+
logStringToSend: bytes.NewBufferString(""), //reset String
140+
logEventsInCurrentBuffer: 0, //reset counter
141+
}
142+
defer response.Body.Close()
105143

106144
}

0 commit comments

Comments
 (0)