Skip to content

Commit 9936fa8

Browse files
Development (#1)
* changed to a reusable client and function model * doc update
1 parent 3e273ad commit 9936fa8

File tree

2 files changed

+47
-41
lines changed

2 files changed

+47
-41
lines changed

README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ go get "github.com/ZachtimusPrime/Go-Splunk-HTTP/splunk"
1919

2020
## Usage ##
2121

22-
Construct a new Splunk HTTP connection, then send log events as desired.
22+
Construct a new Splunk HTTP client, then send log events as desired.
2323

2424
For example:
2525

@@ -32,17 +32,18 @@ import (
3232

3333
func main() {
3434

35-
// Create new connection to splunk
36-
sl := splunk.HTTPCollector{
37-
Url: "https://{splunk-URL}:8088/services/collector",
38-
Token: "{your-token}",
39-
Source: "{your-source}",
40-
SourceType: "{your-sourcetype}",
41-
Index: "{your-index}",
42-
}
35+
// Create new Splunk client
36+
splunk := splunk.NewClient(
37+
nil,
38+
"https://{your-splunk-URL}:8088/services/collector",
39+
"{your-token}",
40+
"{your-source}",
41+
"{your-sourcetype}",
42+
"{your-index}"
43+
)
4344

44-
// Send a log event
45-
sl.Log(map[string]string{"msg": "send key/val pairs here", "msg2": "anything that is useful to you in the log event"})
45+
// Send a log event with the client
46+
splunk.Log(map[string]string{"msg": "send key/val pairs here", "msg2": "anything that is useful to you in the log event"})
4647
}
4748

4849
```

splunk/splunk.go

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)