@@ -3,46 +3,54 @@ package sumoCFFirehose
33import (
44 "bytes"
55 "fmt"
6- "net"
76 "net/http"
87 "time"
98
109 "bitbucket.org/mcplusa-ondemand/firehose-to-sumologic/eventQueue"
1110)
1211
1312type SumoLogicAppender struct {
14- url string
15- connectionTimeout int //10000
16- httpClient http.Client
17- nozzleQueue eventQueue.Queue
18- eventsBatchSize int
13+ url string
14+ connectionTimeout int //10000
15+ httpClient http.Client
16+ nozzleQueue * eventQueue.Queue
17+ eventsBatchSize int
18+ logEventsInCurrentBuffer int
19+ logStringToSend * bytes.Buffer
1920}
2021
21- func NewSumoLogicAppender (urlValue string , connectionTimeoutValue int , nozzleQueue eventQueue.Queue , eventsBatchSize int ) * SumoLogicAppender {
22+ func NewSumoLogicAppender (urlValue string , connectionTimeoutValue int , nozzleQueue * eventQueue.Queue , eventsBatchSize int ) * SumoLogicAppender {
2223 return & SumoLogicAppender {
23- url : urlValue ,
24- connectionTimeout : connectionTimeoutValue ,
25- httpClient : http.Client {Timeout : time .Duration (connectionTimeoutValue * int (time .Millisecond ))},
26- nozzleQueue : nozzleQueue ,
27- eventsBatchSize : eventsBatchSize ,
24+ url : urlValue ,
25+ connectionTimeout : connectionTimeoutValue ,
26+ httpClient : http.Client {Timeout : time .Duration (connectionTimeoutValue * int (time .Millisecond ))},
27+ nozzleQueue : nozzleQueue ,
28+ eventsBatchSize : eventsBatchSize ,
29+ logEventsInCurrentBuffer : 0 ,
30+ logStringToSend : bytes .NewBufferString ("" ),
2831 }
2932}
3033
31- func (s * SumoLogicAppender ) Connect () bool {
32- success := false
33- if s .url != "" {
34- conn , err := net .Dial ("tcp" , s .url )
35- if err != nil {
36- fmt .Printf (fmt .Sprintf ("Unable to connect to sumo server [%s]!\n " , s .url ), err .Error ())
37- } else {
34+ func (s * SumoLogicAppender ) Start () {
35+ timer := time .Now ()
36+ fmt .Println ("Starting Appender Worker" )
37+ for {
38+ time .Sleep (300 * time .Millisecond )
39+ if s .nozzleQueue .GetCount () != 0 { //if queue is not empty, AppendLogs
40+ s .AppendLogs ()
41+ }
42+ if s .logEventsInCurrentBuffer >= s .eventsBatchSize { //if buffer is full, send logs to sumo
43+ fmt .Println ("Buffer full, sending logs to sumo..." )
44+
45+ s .SendToSumo (s .logStringToSend )
3846
39- fmt .Printf (fmt .Sprintf ("Connected to [%s]!\n " , s .url ), false )
40- success = true
41- defer conn .Close ()
47+ } else if time .Since (timer ).Seconds () >= 10 { // else if timer is up, send existing logs to sumo
48+ fmt .Println ("timer finished, sending logs..." )
49+ s .SendToSumo (s .logStringToSend )
50+ timer = time .Now () //reset timer
4251 }
43- }
4452
45- return success
53+ }
4654}
4755
4856func StringBuilder (node * eventQueue.Node ) string {
@@ -61,17 +69,14 @@ func StringBuilder(node *eventQueue.Node) string {
6169
6270func (s * SumoLogicAppender ) AppendLogs () {
6371 // the appender calls for the next message in the queue and parse it to a string
64- logMessage := ""
65- if s .nozzleQueue .GetCount () > s .eventsBatchSize { //when the batch limit is met, call stringBuilder
66- logMessage = logMessage + StringBuilder (s .nozzleQueue .Pop ())
67-
68- }
69- s .SendToSumo (logMessage )
70-
72+ //timer := time.NewTimer(60 * time.Second)
73+ s .logStringToSend .Write ([]byte (StringBuilder (s .nozzleQueue .Pop ())))
74+ s .logEventsInCurrentBuffer ++
7175}
7276
73- func (s * SumoLogicAppender ) SendToSumo (log string ) {
74- 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 )
7580 if err != nil {
7681 fmt .Printf ("http.NewRequest() error: %v\n " , err )
7782 return
@@ -86,6 +91,8 @@ func (s *SumoLogicAppender) SendToSumo(log string) {
8691 } else {
8792 fmt .Println ("Do(Request) successful" )
8893 }
94+ s .logEventsInCurrentBuffer = 0 // reset counter
95+ s .logStringToSend = bytes .NewBufferString ("" ) //reset String
8996 defer response .Body .Close ()
9097
9198}
0 commit comments