Skip to content

Commit b81e76e

Browse files
committed
SCFF-21 added changes related to the HTTP post methods
1 parent ac1cb57 commit b81e76e

7 files changed

Lines changed: 215 additions & 60 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/sumoLog4go/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/sumoLog4go" //**
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 sumoLog4go.SumoLog4go //**
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 sumoLog4go.SumoLog4go) *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, event.Msg) //**
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
}

main.go

Lines changed: 17 additions & 14 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/sumoLog4go"
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()
@@ -45,11 +47,12 @@ func main() {
4547

4648
//Setup Logging
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-
//}
50+
loggingClientSumo := sumoLog4go.NewSumoLogicAppender("http://httpbin.org/post", 1000)
51+
//loggingClient, err := syslog.Dial(syslogProtocol, syslogServer, syslog.LOG_ERR, "demotag")
52+
//defer loggingClient.Close()
53+
//if err != nil {
54+
// log.Fatal("error")
55+
//}
5356
logging.LogStd(fmt.Sprintf("Starting firehose-to-syslog %s ", version), true)
5457

5558
if *modeProf != "" {
@@ -78,8 +81,8 @@ func main() {
7881
}
7982

8083
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)
84+
logging.LogStd(fmt.Sprintf("using '%s' as user", c.Username), true)
85+
logging.LogStd(fmt.Sprintf("using '%s' as password", *password), true)
8386

8487
logging.LogStd(fmt.Sprintf("Using %s as doppler endpoint", cfClient.Endpoint.DopplerEndpoint), true)
8588

@@ -91,7 +94,7 @@ func main() {
9194
cachingClient = caching.NewCachingEmpty()
9295
}
9396
//Creating Events
94-
events := eventRouting.NewEventRouting(cachingClient, loggingClient)
97+
events := eventRouting.NewEventRouting(cachingClient, loggingClient, loggingClientSumo)
9598
err := events.SetupEventRouting(*wantedEvents)
9699
if err != nil {
97100
log.Fatal("Error setting up event routing: ", err)
@@ -124,12 +127,12 @@ func main() {
124127
IdleTimeoutSeconds: *keepAlive,
125128
FirehoseSubscriptionID: *subscriptionId,
126129
}
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+
// logging.LogStd(fmt.Sprintf("connect logging '%s'", loggingClient.Connect()), true)
131+
//logging.LogStd(fmt.Sprintf("using '%s' as syslogServer", syslogServer), true)
132+
//logging.LogStd(fmt.Sprintf("using '%s' as syslogServer", *debug), true)
130133
if loggingClient.Connect() || *debug {
131134

132-
logging.LogStd("Connected to Syslog Server! Connecting to Firehose...", true)
135+
logging.LogStd("Connected to Server! Connecting to Firehose...", true)
133136
firehoseClient := firehoseclient.NewFirehoseNozzle(cfClient, events, firehoseConfig)
134137
err = firehoseClient.Start()
135138
if err != nil {

sumo-log4go/sumoLogicAppender.go

Lines changed: 0 additions & 37 deletions
This file was deleted.

sumoLog4go/sumoLog4go.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package sumoLog4go
2+
3+
type SumoLog4go interface {
4+
Connect() bool
5+
AppendLogs(map[string]interface{}, string)
6+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// This file was generated by counterfeiter
2+
package sumoLog4gofakes
3+
4+
import (
5+
"sync"
6+
7+
"bitbucket.org/mcplusa-ondemand/firehouse-to-sumologic/sumolog4go"
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{}, string)
18+
AppendLogsMutex sync.RWMutex
19+
AppendLogsArgsForCall []struct {
20+
arg1 map[string]interface{}
21+
arg2 string
22+
}
23+
invocations map[string][][]interface{}
24+
invocationsMutex sync.RWMutex
25+
}
26+
27+
func (fake *FakeSumoLog4go) Connect() bool {
28+
fake.connectMutex.Lock()
29+
fake.connectArgsForCall = append(fake.connectArgsForCall, struct{}{})
30+
fake.recordInvocation("Connect", []interface{}{})
31+
fake.connectMutex.Unlock()
32+
if fake.ConnectStub != nil {
33+
return fake.ConnectStub()
34+
} else {
35+
return fake.connectReturns.result1
36+
}
37+
}
38+
39+
func (fake *FakeSumoLog4go) ConnectCallCount() int {
40+
fake.connectMutex.RLock()
41+
defer fake.connectMutex.RUnlock()
42+
return len(fake.connectArgsForCall)
43+
}
44+
45+
func (fake *FakeSumoLog4go) ConnectReturns(result1 bool) {
46+
fake.ConnectStub = nil
47+
fake.connectReturns = struct {
48+
result1 bool
49+
}{result1}
50+
}
51+
52+
func (fake *FakeSumoLog4go) AppendLogs(arg1 map[string]interface{}, arg2 string) {
53+
fake.AppendLogsMutex.Lock()
54+
fake.AppendLogsArgsForCall = append(fake.AppendLogsArgsForCall, struct {
55+
arg1 map[string]interface{}
56+
arg2 string
57+
}{arg1, arg2})
58+
fake.recordInvocation("ShipEvents", []interface{}{arg1, arg2})
59+
fake.AppendLogsMutex.Unlock()
60+
if fake.AppendLogsStub != nil {
61+
fake.AppendLogsStub(arg1, arg2)
62+
}
63+
}
64+
65+
func (fake *FakeSumoLog4go) ShipEventsCallCount() int {
66+
fake.AppendLogsMutex.RLock()
67+
defer fake.AppendLogsMutex.RUnlock()
68+
return len(fake.AppendLogsArgsForCall)
69+
}
70+
71+
func (fake *FakeSumoLog4go) ShipEventsArgsForCall(i int) (map[string]interface{}, string) {
72+
fake.AppendLogsMutex.RLock()
73+
defer fake.AppendLogsMutex.RUnlock()
74+
return fake.AppendLogsArgsForCall[i].arg1, fake.AppendLogsArgsForCall[i].arg2
75+
}
76+
77+
func (fake *FakeSumoLog4go) Invocations() map[string][][]interface{} {
78+
fake.invocationsMutex.RLock()
79+
defer fake.invocationsMutex.RUnlock()
80+
fake.connectMutex.RLock()
81+
defer fake.connectMutex.RUnlock()
82+
fake.AppendLogsMutex.RLock()
83+
defer fake.AppendLogsMutex.RUnlock()
84+
return fake.invocations
85+
}
86+
87+
func (fake *FakeSumoLog4go) recordInvocation(key string, args []interface{}) {
88+
fake.invocationsMutex.Lock()
89+
defer fake.invocationsMutex.Unlock()
90+
if fake.invocations == nil {
91+
fake.invocations = map[string][][]interface{}{}
92+
}
93+
if fake.invocations[key] == nil {
94+
fake.invocations[key] = [][]interface{}{}
95+
}
96+
fake.invocations[key] = append(fake.invocations[key], args)
97+
}
98+
99+
var _ sumoLog4go.SumoLog4go = new(FakeSumoLog4go)

sumoLog4go/sumoLogicAppender.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package sumoLog4go
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"net"
8+
"net/http"
9+
"time"
10+
)
11+
12+
type SumoLogicAppender struct {
13+
url string
14+
connectionTimeout int //10000
15+
httpClient http.Client
16+
}
17+
18+
func NewSumoLogicAppender(urlValue string, connectionTimeoutValue int) *SumoLogicAppender {
19+
return &SumoLogicAppender{
20+
url: urlValue,
21+
connectionTimeout: connectionTimeoutValue,
22+
httpClient: http.Client{Timeout: time.Duration(connectionTimeoutValue * int(time.Millisecond))},
23+
}
24+
}
25+
26+
func (s *SumoLogicAppender) Connect() bool {
27+
success := false
28+
if s.url != "" {
29+
conn, err := net.Dial("tcp", s.url)
30+
fmt.Printf(fmt.Sprintf("Unable to connect to sumo server [%s]!\n", s.url), err.Error())
31+
if err != nil {
32+
fmt.Printf(fmt.Sprintf("Unable to connect to sumo server [%s]!\n", s.url), err.Error())
33+
} else {
34+
35+
fmt.Printf(fmt.Sprintf("Connected to [%s]!\n", s.url), false)
36+
success = true
37+
defer conn.Close()
38+
}
39+
}
40+
41+
return success
42+
}
43+
44+
func (s *SumoLogicAppender) AppendLogs(Event map[string]interface{}, Message string) {
45+
//adding the message to the map
46+
Event["msg"] = Message
47+
jsonEvent, err := json.Marshal(Event)
48+
if err == nil {
49+
fmt.Println("-----here are the logs to send to sumo-------")
50+
51+
//fmt.Println(string(jsonEvent)) //**
52+
s.SendToSumo(jsonEvent)
53+
//fmt.Println("---------------------------------------------")
54+
}
55+
56+
}
57+
58+
func (s *SumoLogicAppender) SendToSumo(log []byte) {
59+
request, err := http.NewRequest("POST", s.url, bytes.NewBuffer(log))
60+
if err != nil {
61+
fmt.Printf("http.NewRequest() error: %v\n", err)
62+
return
63+
}
64+
request.Header.Add("content-type", "application/json")
65+
//request.SetBasicAuth("admin", "admin")
66+
response, err := s.httpClient.Do(request)
67+
if err != nil {
68+
fmt.Printf("http.Do() error: %v\n", err)
69+
return
70+
//consume the body if you want to re-use the connection
71+
} else {
72+
fmt.Println("Do(Request) successful")
73+
}
74+
fmt.Println(response)
75+
defer response.Body.Close()
76+
77+
}

0 commit comments

Comments
 (0)