Skip to content

Commit

Permalink
Added option to define custom headers to log requests (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomerf authored Apr 1, 2024
1 parent 7cb8bf2 commit 72e4690
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
20 changes: 19 additions & 1 deletion constant.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package coralogix

import "time"
import (
"bufio"
"net/http"
"net/textproto"
"strings"
"time"
)

const (
// MaxLogBufferSize is maximum log buffer size (default=128MiB)
Expand Down Expand Up @@ -49,4 +55,16 @@ var (

// TimeDeltaURL is the Coralogix time delay url endpoint
TimeDeltaURL string = GetEnv("CORALOGIX_TIME_DELTA_URL", "https://api.coralogix.com:443/sdk/v1/time")

// Headers is the list of headers added to each send logs request
Headers http.Header = func() http.Header {
headers := GetEnv("CORALOGIX_HEADERS", "")
tp := textproto.NewReader(bufio.NewReader(strings.NewReader(headers)))
mimeHeader, err := tp.ReadMIMEHeader()
if err != nil {
mimeHeader = map[string][]string{}
}
mimeHeader.Set("Content-Type", "application/json")
return http.Header(mimeHeader)
}()
)
12 changes: 9 additions & 3 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package coralogix

import (
"bytes"
"io/ioutil"
"io"
"net/http"
"strconv"
"time"
Expand All @@ -17,8 +17,14 @@ func SendRequest(Bulk *Bulk) int {
for Attempt := 1; uint(Attempt) <= HTTPSendRetryCount; Attempt++ {
DebugLogger.Println("About to send bulk to Coralogix server. Attempt number:", Attempt)

response, err := client.Post(LogURL, "application/json", bytes.NewBuffer(Bulk.ToJSON()))
request, err := http.NewRequest("POST", LogURL, bytes.NewBuffer(Bulk.ToJSON()))
if err != nil {
DebugLogger.Println("Can't create HTTP request:", err)
continue
}
request.Header = Headers

response, err := client.Do(request)
if err != nil {
DebugLogger.Println("Can't execute HTTP request:", err)
continue
Expand Down Expand Up @@ -53,7 +59,7 @@ func GetTimeSync() (bool, float64) {
}

if response.StatusCode == 200 {
response, _ := ioutil.ReadAll(response.Body)
response, _ := io.ReadAll(response.Body)
ServerTime, err := strconv.ParseFloat(string(response), 64)

if err != nil {
Expand Down

0 comments on commit 72e4690

Please sign in to comment.