Skip to content

Commit 34a1c4f

Browse files
committed
Refactoring the event sending format to Logstash in the AWS plugin.
1 parent 2d92132 commit 34a1c4f

1 file changed

Lines changed: 10 additions & 20 deletions

File tree

aws/processor/sendData.go

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/json"
77
"fmt"
88
"net/http"
9-
"strings"
109
"time"
1110

1211
"github.com/threatwinds/logger"
@@ -27,45 +26,36 @@ var transport = &http.Transport{
2726
var client = &http.Client{Transport: transport, Timeout: 2 * time.Second}
2827

2928
func SendToLogstash(data []TransformedLog) *logger.Error {
30-
var logStrings []string
31-
for _, log := range data {
32-
body, err := json.Marshal(log)
29+
for _, str := range data {
30+
body, err := json.Marshal(str)
3331
if err != nil {
3432
utils.Logger.ErrorF("error encoding log to JSON: %v", err)
3533
continue
3634
}
37-
logStrings = append(logStrings, string(body))
38-
}
39-
40-
if len(logStrings) == 0 {
41-
return nil
42-
}
43-
44-
var logs string
45-
for _, str := range logStrings {
46-
logs += str + configuration.UTMLogSeparator
35+
if err := sendLogs(body); err != nil {
36+
utils.Logger.ErrorF("error sending logs to logstach: %v", err)
37+
continue
38+
}
4739
}
40+
return nil
41+
}
4842

43+
func sendLogs(log []byte) error {
4944
url := fmt.Sprintf(configuration.LogstashEndpoint, configuration.GetLogstashHost(), configuration.GetLogstashPort())
5045

51-
req, err := http.NewRequest("POST", url, bytes.NewBufferString(logs))
46+
req, err := http.NewRequest("POST", url, bytes.NewBuffer(log))
5247
if err != nil {
5348
return utils.Logger.ErrorF("error creating request: %v", err.Error())
5449
}
5550

5651
resp, err := client.Do(req)
5752
if err != nil {
58-
if !strings.Contains(err.Error(), "Client.Timeout exceeded while awaiting headers") {
59-
utils.Logger.ErrorF("error sending logs with error: %v", err.Error())
60-
}
6153
return utils.Logger.ErrorF("error sending logs: %v", err.Error())
6254
}
6355
defer resp.Body.Close()
6456

6557
if resp.StatusCode != http.StatusOK {
6658
return utils.Logger.ErrorF("error sending logs with http code %d", resp.StatusCode)
6759
}
68-
69-
utils.Logger.Info("successfully sent %d logs to Logstash", len(logStrings))
7060
return nil
7161
}

0 commit comments

Comments
 (0)