Skip to content

Commit e46c93a

Browse files
committed
Merged in feature/SCFF-21 (pull request #3)
Feature/SCFF-21
2 parents 122b8af + 80db4ae commit e46c93a

8 files changed

Lines changed: 218 additions & 32 deletions

File tree

eventRouting/eventRouting_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
. "bitbucket.org/mcplusa-ondemand/firehouse-to-sumologic/caching/cachingfakes"
55
. "bitbucket.org/mcplusa-ondemand/firehouse-to-sumologic/eventRouting"
66
. "bitbucket.org/mcplusa-ondemand/firehouse-to-sumologic/logging/loggingfakes"
7+
. "bitbucket.org/mcplusa-ondemand/firehouse-to-sumologic/sumoCFFirehose/sumoLog4gofakes" //**
78
. "github.com/cloudfoundry/sonde-go/events"
89
. "github.com/onsi/ginkgo"
910
. "github.com/onsi/gomega"
@@ -16,7 +17,8 @@ var _ = Describe("Events", func() {
1617
BeforeEach(func() {
1718
logging := new(FakeLogging)
1819
caching := new(FakeCaching)
19-
eventRouting = NewEventRouting(caching, logging)
20+
sLAppender := new(FakeSumoLog4go)
21+
eventRouting = NewEventRouting(caching, logging, sLAppender)
2022
eventRouting.SetupEventRouting("")
2123

2224
})

eventRouting/eventrouting.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,37 @@ package eventRouting
22

33
import (
44
"fmt"
5-
"github.com/Sirupsen/logrus"
6-
"bitbucket.org/mcplusa-ondemand/firehouse-to-sumologic/caching"
7-
fevents "bitbucket.org/mcplusa-ondemand/firehouse-to-sumologic/events"
8-
"bitbucket.org/mcplusa-ondemand/firehouse-to-sumologic/extrafields"
9-
"bitbucket.org/mcplusa-ondemand/firehouse-to-sumologic/logging"
10-
"github.com/cloudfoundry/sonde-go/events"
115
"os"
126
"sort"
137
"strings"
148
"sync"
159
"time"
10+
11+
"bitbucket.org/mcplusa-ondemand/firehouse-to-sumologic/caching"
12+
fevents "bitbucket.org/mcplusa-ondemand/firehouse-to-sumologic/events"
13+
"bitbucket.org/mcplusa-ondemand/firehouse-to-sumologic/extrafields"
14+
"bitbucket.org/mcplusa-ondemand/firehouse-to-sumologic/logging"
15+
"bitbucket.org/mcplusa-ondemand/firehouse-to-sumologic/sumoCFFirehose" //**
16+
"github.com/Sirupsen/logrus"
17+
"github.com/cloudfoundry/sonde-go/events"
1618
)
1719

1820
type EventRouting struct {
1921
CachingClient caching.Caching
2022
selectedEvents map[string]bool
2123
selectedEventsCount map[string]uint64
2224
mutex *sync.Mutex
25+
sLAppender sumoCFFirehose.SumoCFFirehose //**
2326
log logging.Logging
2427
ExtraFields map[string]string
2528
}
2629

27-
func NewEventRouting(caching caching.Caching, logging logging.Logging) *EventRouting {
30+
func NewEventRouting(caching caching.Caching, logging logging.Logging, sLAppender sumoCFFirehose.SumoCFFirehose) *EventRouting {
2831
return &EventRouting{
2932
CachingClient: caching,
3033
selectedEvents: make(map[string]bool),
3134
selectedEventsCount: make(map[string]uint64),
35+
sLAppender: sLAppender, //**
3236
log: logging,
3337
mutex: &sync.Mutex{},
3438
ExtraFields: make(map[string]string),
@@ -76,7 +80,8 @@ func (e *EventRouting) RouteEvent(msg *events.Envelope) {
7680
if ignored, hasIgnoredField := event.Fields["cf_ignored_app"]; ignored == true && hasIgnoredField {
7781
e.selectedEventsCount["ignored_app_message"]++
7882
} else {
79-
e.log.ShipEvents(event.Fields, event.Msg)
83+
e.sLAppender.AppendLogs(event.Fields) //**
84+
e.log.ShipEvents(event.Fields, event.Msg) // here we have to change the method for the one on sumoLogicAppender
8085
e.selectedEventsCount[eventType.String()]++
8186

8287
}

events/events.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package events
22

33
import (
44
"fmt"
5-
"github.com/Sirupsen/logrus"
5+
66
"bitbucket.org/mcplusa-ondemand/firehouse-to-sumologic/caching"
77
"bitbucket.org/mcplusa-ondemand/firehouse-to-sumologic/utils"
8+
"github.com/Sirupsen/logrus"
89
"github.com/cloudfoundry/sonde-go/events"
910
)
1011

firehoseclient/firehoseclient.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package firehoseclient
22

33
import (
44
"crypto/tls"
5+
"time"
6+
57
"bitbucket.org/mcplusa-ondemand/firehouse-to-sumologic/eventRouting"
68
"bitbucket.org/mcplusa-ondemand/firehouse-to-sumologic/logging"
79
"github.com/cloudfoundry-community/go-cfclient"
810
"github.com/cloudfoundry/noaa/consumer"
911
"github.com/cloudfoundry/sonde-go/events"
1012
"github.com/gorilla/websocket"
11-
"time"
1213
)
1314

1415
type FirehoseNozzle struct {

main.go

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import (
44
"fmt"
55
"log"
66
"os"
7+
78
"bitbucket.org/mcplusa-ondemand/firehouse-to-sumologic/caching"
89
"bitbucket.org/mcplusa-ondemand/firehouse-to-sumologic/eventRouting"
910
"bitbucket.org/mcplusa-ondemand/firehouse-to-sumologic/firehoseclient"
1011
"bitbucket.org/mcplusa-ondemand/firehouse-to-sumologic/logging"
12+
"bitbucket.org/mcplusa-ondemand/firehouse-to-sumologic/sumoCFFirehose"
1113
"github.com/cloudfoundry-community/go-cfclient"
1214
"github.com/pkg/profile"
1315
"gopkg.in/alecthomas/kingpin.v2"
@@ -17,8 +19,8 @@ var (
1719
debug = kingpin.Flag("debug", "Enable debug mode. This disables forwarding to syslog").Default("false").OverrideDefaultFromEnvar("DEBUG").Bool()
1820
apiEndpoint = "https://api.bosh-lite.com" //kingpin.Flag("api-endpoint", "Api endpoint address. For bosh-lite installation of CF: https://api.10.244.0.34.xip.io").OverrideDefaultFromEnvar("API_ENDPOINT").Required().String()
1921
dopplerEndpoint = kingpin.Flag("doppler-endpoint", "Overwrite default doppler endpoint return by /v2/info").OverrideDefaultFromEnvar("DOPPLER_ENDPOINT").String()
20-
syslogServer = "192.168.33.10:514"//kingpin.Flag("syslog-server", "Syslog server.").OverrideDefaultFromEnvar("SYSLOG_ENDPOINT").String()
21-
syslogProtocol = "udp"//kingpin.Flag("syslog-protocol", "Syslog protocol (tcp/udp).").Default("tcp").OverrideDefaultFromEnvar("SYSLOG_PROTOCOL").String()
22+
syslogServer = "192.168.33.10:514" //kingpin.Flag("syslog-server", "Syslog server.").OverrideDefaultFromEnvar("SYSLOG_ENDPOINT").String()
23+
syslogProtocol = "udp" //kingpin.Flag("syslog-protocol", "Syslog protocol (tcp/udp).").Default("tcp").OverrideDefaultFromEnvar("SYSLOG_PROTOCOL").String()
2224
subscriptionId = kingpin.Flag("subscription-id", "Id for the subscription.").Default("firehose").OverrideDefaultFromEnvar("FIREHOSE_SUBSCRIPTION_ID").String()
2325
user = kingpin.Flag("user", "Admin user.").Default("admin").OverrideDefaultFromEnvar("FIREHOSE_USER").String()
2426
password = kingpin.Flag("password", "Admin password.").Default("admin").OverrideDefaultFromEnvar("FIREHOSE_PASSWORD").String()
@@ -43,14 +45,12 @@ func main() {
4345
kingpin.Version(version)
4446
kingpin.Parse()
4547

46-
//Setup Logging
48+
//Setup Logging <-- this loggingClient has to be removed when sumoLog4go Library is working
4749
loggingClient := logging.NewLogging(syslogServer, syslogProtocol, *logFormatterType, *debug)
48-
//loggingClient, err := syslog.Dial(syslogProtocol, syslogServer, syslog.LOG_ERR, "demotag")
49-
//defer loggingClient.Close()
50-
//if err != nil {
51-
// log.Fatal("error")
52-
//}
53-
logging.LogStd(fmt.Sprintf("Starting firehose-to-syslog %s ", version), true)
50+
//Setup Loggin with sumoLog4go library
51+
loggingClientSumo := sumoCFFirehose.NewSumoLogicAppender("http://httpbin.org/post", 1000)
52+
53+
logging.LogStd(fmt.Sprintf("Starting firehose-to-sumo %s ", version), true)
5454

5555
if *modeProf != "" {
5656
switch *modeProf {
@@ -77,12 +77,6 @@ func main() {
7777
cfClient.Endpoint.DopplerEndpoint = *dopplerEndpoint
7878
}
7979

80-
logging.LogStd(fmt.Sprintf("Login with '%s' user", *user), true)
81-
logging.LogStd(fmt.Sprintf("using '%s' as user", c.Username), true)
82-
logging.LogStd(fmt.Sprintf("using '%s' as password", *password), true)
83-
84-
logging.LogStd(fmt.Sprintf("Using %s as doppler endpoint", cfClient.Endpoint.DopplerEndpoint), true)
85-
8680
//Creating Caching
8781
var cachingClient caching.Caching
8882
if caching.IsNeeded(*wantedEvents) {
@@ -91,7 +85,7 @@ func main() {
9185
cachingClient = caching.NewCachingEmpty()
9286
}
9387
//Creating Events
94-
events := eventRouting.NewEventRouting(cachingClient, loggingClient)
88+
events := eventRouting.NewEventRouting(cachingClient, loggingClient, loggingClientSumo)
9589
err := events.SetupEventRouting(*wantedEvents)
9690
if err != nil {
9791
log.Fatal("Error setting up event routing: ", err)
@@ -124,12 +118,10 @@ func main() {
124118
IdleTimeoutSeconds: *keepAlive,
125119
FirehoseSubscriptionID: *subscriptionId,
126120
}
127-
logging.LogStd(fmt.Sprintf("connect logging '%s'",loggingClient.Connect()), true)
128-
logging.LogStd(fmt.Sprintf("using '%s' as syslogServer", syslogServer), true)
129-
logging.LogStd(fmt.Sprintf("using '%s' as syslogServer", *debug), true)
130-
if loggingClient.Connect() || *debug {
131121

132-
logging.LogStd("Connected to Syslog Server! Connecting to Firehose...", true)
122+
if /*loggingClientSumo.Connect() ||*/ *debug {
123+
124+
logging.LogStd("Connected to Server! Connecting to Firehose...", true)
133125
firehoseClient := firehoseclient.NewFirehoseNozzle(cfClient, events, firehoseConfig)
134126
err = firehoseClient.Start()
135127
if err != nil {

sumoCFFirehose/sumoCFFirehose.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package sumoCFFirehose
2+
3+
type SumoCFFirehose interface {
4+
Connect() bool
5+
AppendLogs(map[string]interface{})
6+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// This file was generated by counterfeiter
2+
package sumoLog4gofakes
3+
4+
import (
5+
"sync"
6+
7+
"bitbucket.org/mcplusa-ondemand/firehouse-to-sumologic/sumoCFFirehose"
8+
)
9+
10+
type FakeSumoLog4go struct {
11+
ConnectStub func() bool
12+
connectMutex sync.RWMutex
13+
connectArgsForCall []struct{}
14+
connectReturns struct {
15+
result1 bool
16+
}
17+
AppendLogsStub func(map[string]interface{})
18+
AppendLogsMutex sync.RWMutex
19+
AppendLogsArgsForCall []struct {
20+
arg1 map[string]interface{}
21+
}
22+
invocations map[string][][]interface{}
23+
invocationsMutex sync.RWMutex
24+
}
25+
26+
func (fake *FakeSumoLog4go) Connect() bool {
27+
fake.connectMutex.Lock()
28+
fake.connectArgsForCall = append(fake.connectArgsForCall, struct{}{})
29+
fake.recordInvocation("Connect", []interface{}{})
30+
fake.connectMutex.Unlock()
31+
if fake.ConnectStub != nil {
32+
return fake.ConnectStub()
33+
} else {
34+
return fake.connectReturns.result1
35+
}
36+
}
37+
38+
func (fake *FakeSumoLog4go) ConnectCallCount() int {
39+
fake.connectMutex.RLock()
40+
defer fake.connectMutex.RUnlock()
41+
return len(fake.connectArgsForCall)
42+
}
43+
44+
func (fake *FakeSumoLog4go) ConnectReturns(result1 bool) {
45+
fake.ConnectStub = nil
46+
fake.connectReturns = struct {
47+
result1 bool
48+
}{result1}
49+
}
50+
51+
func (fake *FakeSumoLog4go) AppendLogs(arg1 map[string]interface{}) {
52+
fake.AppendLogsMutex.Lock()
53+
fake.AppendLogsArgsForCall = append(fake.AppendLogsArgsForCall, struct {
54+
arg1 map[string]interface{}
55+
//arg2 string
56+
}{arg1 /*, arg2*/})
57+
fake.recordInvocation("ShipEvents", []interface{}{arg1 /*, arg2*/})
58+
fake.AppendLogsMutex.Unlock()
59+
if fake.AppendLogsStub != nil {
60+
fake.AppendLogsStub(arg1 /*, arg2*/)
61+
}
62+
}
63+
64+
func (fake *FakeSumoLog4go) ShipEventsCallCount() int {
65+
fake.AppendLogsMutex.RLock()
66+
defer fake.AppendLogsMutex.RUnlock()
67+
return len(fake.AppendLogsArgsForCall)
68+
}
69+
70+
func (fake *FakeSumoLog4go) ShipEventsArgsForCall(i int) map[string]interface{} {
71+
fake.AppendLogsMutex.RLock()
72+
defer fake.AppendLogsMutex.RUnlock()
73+
return fake.AppendLogsArgsForCall[i].arg1
74+
}
75+
76+
func (fake *FakeSumoLog4go) Invocations() map[string][][]interface{} {
77+
fake.invocationsMutex.RLock()
78+
defer fake.invocationsMutex.RUnlock()
79+
fake.connectMutex.RLock()
80+
defer fake.connectMutex.RUnlock()
81+
fake.AppendLogsMutex.RLock()
82+
defer fake.AppendLogsMutex.RUnlock()
83+
return fake.invocations
84+
}
85+
86+
func (fake *FakeSumoLog4go) recordInvocation(key string, args []interface{}) {
87+
fake.invocationsMutex.Lock()
88+
defer fake.invocationsMutex.Unlock()
89+
if fake.invocations == nil {
90+
fake.invocations = map[string][][]interface{}{}
91+
}
92+
if fake.invocations[key] == nil {
93+
fake.invocations[key] = [][]interface{}{}
94+
}
95+
fake.invocations[key] = append(fake.invocations[key], args)
96+
}
97+
98+
var _ sumoCFFirehose.SumoCFFirehose = new(FakeSumoLog4go)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package sumoCFFirehose
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"net"
7+
"net/http"
8+
"time"
9+
)
10+
11+
type SumoLogicAppender struct {
12+
url string
13+
connectionTimeout int //10000
14+
httpClient http.Client
15+
}
16+
17+
func NewSumoLogicAppender(urlValue string, connectionTimeoutValue int) *SumoLogicAppender {
18+
return &SumoLogicAppender{
19+
url: urlValue,
20+
connectionTimeout: connectionTimeoutValue,
21+
httpClient: http.Client{Timeout: time.Duration(connectionTimeoutValue * int(time.Millisecond))},
22+
}
23+
}
24+
25+
func (s *SumoLogicAppender) Connect() bool {
26+
success := false
27+
if s.url != "" {
28+
conn, err := net.Dial("tcp", s.url)
29+
if err != nil {
30+
fmt.Printf(fmt.Sprintf("Unable to connect to sumo server [%s]!\n", s.url), err.Error())
31+
} else {
32+
33+
fmt.Printf(fmt.Sprintf("Connected to [%s]!\n", s.url), false)
34+
success = true
35+
defer conn.Close()
36+
}
37+
}
38+
39+
return success
40+
}
41+
42+
func (s *SumoLogicAppender) AppendLogs(Event map[string]interface{}) {
43+
//adding the message to the map
44+
if Event == nil {
45+
return
46+
}
47+
48+
if Event["msg"] == nil {
49+
return
50+
}
51+
52+
if Event["msg"] == "" {
53+
return
54+
}
55+
56+
Message := ""
57+
Message = Event["msg"].(string) + "\n"
58+
59+
s.SendToSumo(Message)
60+
61+
}
62+
63+
func (s *SumoLogicAppender) SendToSumo(log string) {
64+
request, err := http.NewRequest("POST", s.url, bytes.NewBufferString(log))
65+
if err != nil {
66+
fmt.Printf("http.NewRequest() error: %v\n", err)
67+
return
68+
}
69+
request.Header.Add("content-type", "application/json")
70+
//request.SetBasicAuth("admin", "admin")
71+
response, err := s.httpClient.Do(request)
72+
73+
if err != nil {
74+
fmt.Printf("http.Do() error: %v\n", err)
75+
return
76+
} else {
77+
fmt.Println("Do(Request) successful")
78+
}
79+
defer response.Body.Close()
80+
81+
}

0 commit comments

Comments
 (0)