@@ -20,51 +20,54 @@ type Event struct {
2020 Event map [string ]string `json:"event" binding:"required"` // throw any useful key/val pairs here
2121}
2222
23- // HTTPCollector handles the connection to the Splunk server. Once initialized, you just call the *HTTPCollector.Log
24- // function to send off an HTTP log event.
25- type HTTPCollector struct {
26- Url string `json:"url" binding:"required"`
27- Token string `json:"token" binding:"required"`
28- Source string `json:"source" binding:"required"`
29- SourceType string `json:"sourcetype" binding:"required"`
30- Index string `json:"index" binding:"required"`
23+ type Client struct {
24+ HTTPClient * http.Client // HTTP client used to communicate with the API
25+ URL string
26+ Token string
27+ Source string
28+ SourceType string
29+ Index string
3130}
3231
33- // Log takes in a map[string]string of key/val pairs that you would like sent to Splunk in a log event, and bundles them
34- // with the timestamp, hostname, source, sourcetype, and index specified in the HTTPCollector initialization.
35- //
36- // These can be any values/variables available to you that are of use.
37- // i.e. {"error": "critical info here", "status_code": "404"}
38- func (sl * HTTPCollector ) Log (event map [string ]string ) (err error ){
32+ func NewClient (httpClient * http.Client , URL string , Token string , Source string , SourceType string , Index string ) (* Client ) {
33+ // Create a new client
34+ if httpClient == nil {
35+ tr := & http.Transport {TLSClientConfig : & tls.Config {InsecureSkipVerify : true }} // turn off certificate checking
36+ httpClient = & http.Client {Timeout : time .Second * 20 , Transport : tr }
37+ }
38+
39+ c := & Client {HTTPClient : httpClient , URL : URL , Token : Token , Source : Source , SourceType : SourceType , Index : Index }
40+
41+ return c
42+ }
43+
44+ func NewEvent (event map [string ]string , source string , sourcetype string , index string ) (Event ) {
3945 hostname , _ := os .Hostname ()
46+ e := Event {Time : time .Now ().Unix (), Host : hostname , Source : source , SourceType : sourcetype , Index : index , Event : event }
47+
48+ return e
49+ }
50+
51+ func (c * Client ) Log (event map [string ]string ) (error ) {
4052 // create Splunk log
41- splunklog := Event {
42- Time : time .Now ().Unix (),
43- Host : hostname ,
44- Source : sl .Source ,
45- SourceType : sl .SourceType ,
46- Index : sl .Index ,
47- Event : event ,
48- }
53+ log := NewEvent (event , c .Source , c .SourceType , c .Index )
4954
5055 // Convert requestBody struct to byte slice to prep for http.NewRequest
51- b , err := json .Marshal (splunklog )
56+ b , err := json .Marshal (log )
5257 if err != nil {
5358 return err
5459 }
5560
5661 //log.Print(string(b[:])) // print what the splunk post body will be for checking/debugging
5762
5863 // make new request
59- url := sl . Url
64+ url := c . URL
6065 req , err := http .NewRequest ("POST" , url , bytes .NewBuffer (b ))
6166 req .Header .Add ("Content-Type" , "application/json" )
62- req .Header .Add ("Authorization" , "Splunk " + sl .Token )
63- tr := & http.Transport {TLSClientConfig : & tls.Config {InsecureSkipVerify : true }} // turn off certificate checking
64- client := & http.Client {Transport : tr }
67+ req .Header .Add ("Authorization" , "Splunk " + c .Token )
6568
6669 // receive response
67- res , err := client .Do (req )
70+ res , err := c . HTTPClient .Do (req )
6871 if err != nil {
6972 return err
7073 }
@@ -78,7 +81,9 @@ func (sl *HTTPCollector) Log(event map[string]string) (err error){
7881 buf .ReadFrom (res .Body )
7982 responseBody := buf .String ()
8083 err = errors .New (responseBody )
81- //log.Print(responseBody) // print error to screen for checking/debugging
84+
85+ //log.Print(responseBody) // print error to screen for checking/debugging
8286 }
87+
8388 return err
84- }
89+ }
0 commit comments