Skip to content

Commit ae75759

Browse files
committed
SCFF-31 handling a bytes buffer instead string
1 parent 302f54e commit ae75759

2 files changed

Lines changed: 17 additions & 38 deletions

File tree

sumoCFFirehose/sumoLogicAppender.go

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package sumoCFFirehose
33
import (
44
"bytes"
55
"fmt"
6-
"net"
76
"net/http"
87
"time"
98

@@ -17,7 +16,7 @@ type SumoLogicAppender struct {
1716
nozzleQueue *eventQueue.Queue
1817
eventsBatchSize int
1918
logEventsInCurrentBuffer int
20-
logStringToSend string
19+
logStringToSend *bytes.Buffer
2120
}
2221

2322
func NewSumoLogicAppender(urlValue string, connectionTimeoutValue int, nozzleQueue *eventQueue.Queue, eventsBatchSize int) *SumoLogicAppender {
@@ -28,50 +27,27 @@ func NewSumoLogicAppender(urlValue string, connectionTimeoutValue int, nozzleQue
2827
nozzleQueue: nozzleQueue,
2928
eventsBatchSize: eventsBatchSize,
3029
logEventsInCurrentBuffer: 0,
31-
logStringToSend: "",
30+
logStringToSend: bytes.NewBufferString(""),
3231
}
3332
}
3433

35-
func (s *SumoLogicAppender) Connect() bool {
36-
success := false
37-
if s.url != "" {
38-
conn, err := net.Dial("tcp", s.url)
39-
if err != nil {
40-
fmt.Printf(fmt.Sprintf("Unable to connect to sumo server [%s]!\n", s.url), err.Error())
41-
} else {
42-
43-
fmt.Printf(fmt.Sprintf("Connected to [%s]!\n", s.url), false)
44-
success = true
45-
defer conn.Close()
46-
}
47-
}
48-
49-
return success
50-
}
51-
5234
func (s *SumoLogicAppender) Start() {
5335
timer := time.Now()
5436
fmt.Println("Starting Appender Worker")
55-
s.logStringToSend = ""
5637
for {
5738
time.Sleep(300 * time.Millisecond)
58-
if s.nozzleQueue.GetCount() != 0 {
39+
if s.nozzleQueue.GetCount() != 0 { //if queue is not empty, AppendLogs
5940
s.AppendLogs()
60-
fmt.Println(s.logStringToSend)
6141
}
62-
if s.logEventsInCurrentBuffer >= s.eventsBatchSize {
63-
fmt.Println("Buffer full")
64-
fmt.Println(s.logStringToSend)
42+
if s.logEventsInCurrentBuffer >= s.eventsBatchSize { //if buffer is full, send logs to sumo
43+
fmt.Println("Buffer full, sending logs to sumo...")
44+
6545
s.SendToSumo(s.logStringToSend)
66-
s.logEventsInCurrentBuffer = 0 // reset counter
67-
s.logStringToSend = "" //reset String
68-
} else if time.Since(timer).Seconds() >= 10 {
46+
47+
} else if time.Since(timer).Seconds() >= 10 { // else if timer is up, send existing logs to sumo
6948
fmt.Println("timer finished, sending logs...")
70-
fmt.Println(s.logStringToSend)
7149
s.SendToSumo(s.logStringToSend)
72-
s.logEventsInCurrentBuffer = 0 // reset counter
73-
s.logStringToSend = "" //reset String
74-
timer = time.Now() //reset timer
50+
timer = time.Now() //reset timer
7551
}
7652

7753
}
@@ -94,12 +70,13 @@ func StringBuilder(node *eventQueue.Node) string {
9470
func (s *SumoLogicAppender) AppendLogs() {
9571
// the appender calls for the next message in the queue and parse it to a string
9672
//timer := time.NewTimer(60 * time.Second)
97-
s.logStringToSend = s.logStringToSend + StringBuilder(s.nozzleQueue.Pop())
73+
s.logStringToSend.Write([]byte(StringBuilder(s.nozzleQueue.Pop())))
9874
s.logEventsInCurrentBuffer++
9975
}
10076

101-
func (s *SumoLogicAppender) SendToSumo(log string) {
102-
request, err := http.NewRequest("POST", s.url, bytes.NewBufferString(log))
77+
func (s *SumoLogicAppender) SendToSumo(log *bytes.Buffer) {
78+
fmt.Println(log)
79+
request, err := http.NewRequest("POST", s.url, log)
10380
if err != nil {
10481
fmt.Printf("http.NewRequest() error: %v\n", err)
10582
return
@@ -114,6 +91,8 @@ func (s *SumoLogicAppender) SendToSumo(log string) {
11491
} else {
11592
fmt.Println("Do(Request) successful")
11693
}
94+
s.logEventsInCurrentBuffer = 0 // reset counter
95+
s.logStringToSend = bytes.NewBufferString("") //reset String
11796
defer response.Body.Close()
11897

11998
}

sumoCFFirehose/sumoLogicAppender_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package sumoCFFirehose
33
import (
44
"testing"
55

6+
"github.com/stretchr/testify/assert"
7+
68
. "bitbucket.org/mcplusa-ondemand/firehose-to-sumologic/eventQueue"
79
. "bitbucket.org/mcplusa-ondemand/firehose-to-sumologic/events"
8-
"github.com/stretchr/testify/assert"
910
)
1011

1112
func testAppenderStringBuilder(t *testing.T) {
@@ -55,5 +56,4 @@ func testAppenderStringBuilder(t *testing.T) {
5556
assert.Equal(t, finalString, "2016-12-12 16:02:41.828366387 -0300 CLST"+"\t"+"OUT"+"\t"+"index [01]"+"\n"+
5657
"2016-12-12 16:02:42.844737993 -0300 CLST"+"\t"+"OUT"+"\t"+"index [02]"+"\n"+
5758
"2016-12-12 16:02:43.862436654 -0300 CLST"+"\t"+"OUT"+"\t"+"index [03]"+"\n", "")
58-
5959
}

0 commit comments

Comments
 (0)