Skip to content

Commit e8aef1b

Browse files
committed
Merged in feature/SCFF-31 (pull request #9)
Feature/SCFF-31
2 parents b1962d9 + ae75759 commit e8aef1b

5 files changed

Lines changed: 56 additions & 57 deletions

File tree

eventQueue/eventQueue.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package eventQueue
22

3-
import (
4-
. "bitbucket.org/mcplusa-ondemand/firehose-to-sumologic/events"
5-
)
3+
import . "bitbucket.org/mcplusa-ondemand/firehose-to-sumologic/events"
64

75
//Node to put in queue
86
type Node struct {
@@ -23,8 +21,8 @@ func NewNode(event Event) *Node {
2321
}
2422
}
2523

26-
func NewQueue(n []*Node) *Queue {
27-
return &Queue{
24+
func NewQueue(n []*Node) Queue {
25+
return Queue{
2826
Nodes: n,
2927
}
3028
}

eventRouting/eventrouting.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ type EventRouting struct {
2121
selectedEventsCount map[string]uint64
2222
mutex *sync.Mutex
2323
sLAppender sumoCFFirehose.SumoLogicAppender //**
24-
queue eventQueue.Queue
24+
queue *eventQueue.Queue
2525
}
2626

27-
func NewEventRouting(caching caching.Caching, sLAppender sumoCFFirehose.SumoLogicAppender, queue eventQueue.Queue) *EventRouting {
27+
func NewEventRouting(caching caching.Caching, sLAppender sumoCFFirehose.SumoLogicAppender, queue *eventQueue.Queue) *EventRouting {
2828
return &EventRouting{
2929
CachingClient: caching,
3030
selectedEvents: make(map[string]bool),
@@ -148,7 +148,6 @@ func (e *EventRouting) LogEventTotals(logTotalsTime time.Duration) {
148148

149149
//Push the event to the queue
150150
e.queue.Push(eventQueue.NewNode(*event))
151-
//e.sLAppender.AppendLogs(*event)
152151
}
153152
}()
154153
}

main.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"os"
77
"time"
88

9+
"runtime"
10+
911
"bitbucket.org/mcplusa-ondemand/firehose-to-sumologic/caching"
1012
"bitbucket.org/mcplusa-ondemand/firehose-to-sumologic/eventQueue"
1113
"bitbucket.org/mcplusa-ondemand/firehose-to-sumologic/eventRouting"
@@ -38,9 +40,7 @@ var (
3840
func main() {
3941
kingpin.Version(version)
4042
kingpin.Parse()
41-
42-
fmt.Println("this is the sumo endpoint")
43-
fmt.Println(sumoEndpoint)
43+
runtime.GOMAXPROCS(1)
4444

4545
fmt.Printf("Starting firehose-to-sumo %s \n", version)
4646

@@ -66,10 +66,11 @@ func main() {
6666

6767
//Creating queue
6868
queue := eventQueue.NewQueue(make([]*eventQueue.Node, 100))
69-
loggingClientSumo := sumoCFFirehose.NewSumoLogicAppender(*sumoEndpoint, 1000, *queue, *eventsBatchSize)
69+
loggingClientSumo := sumoCFFirehose.NewSumoLogicAppender(*sumoEndpoint, 1000, &queue, *eventsBatchSize)
70+
go loggingClientSumo.Start() //multi
7071

7172
//Creating Events
72-
events := eventRouting.NewEventRouting(cachingClient, *loggingClientSumo, *queue)
73+
events := eventRouting.NewEventRouting(cachingClient, *loggingClientSumo, &queue)
7374
err := events.SetupEventRouting(*wantedEvents)
7475
if err != nil {
7576
log.Fatal("Error setting up event routing: ", err)
@@ -99,17 +100,11 @@ func main() {
99100
fmt.Printf("Connected to Server! Connecting to Firehose... \n")
100101

101102
firehoseClient := firehoseclient.NewFirehoseNozzle(cfClient, events, firehoseConfig)
102-
err = firehoseClient.Start()
103-
if err != nil {
104-
fmt.Printf("Failed connecting to Firehose...Please check settings and try again! \n") //Log error
105-
106-
} else {
107-
fmt.Printf("Firehose Subscription Succesfull! Routing events... \n")
108-
}
103+
go firehoseClient.Start()
109104

110-
} else {
111-
fmt.Printf("Failed connecting to the Fluentd Server...Please check settings and try again! \n") //Log error
105+
defer firehoseClient.Start()
112106
}
113107

114108
defer cachingClient.Close()
109+
115110
}

sumoCFFirehose/sumoLogicAppender.go

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

109
"bitbucket.org/mcplusa-ondemand/firehose-to-sumologic/eventQueue"
1110
)
1211

1312
type 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

4856
func StringBuilder(node *eventQueue.Node) string {
@@ -61,17 +69,14 @@ func StringBuilder(node *eventQueue.Node) string {
6169

6270
func (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
}

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)