-
Notifications
You must be signed in to change notification settings - Fork 10
/
http.go
84 lines (67 loc) · 2.12 KB
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package coralogix
import (
"bytes"
"io"
"net/http"
"strconv"
"time"
)
// SendRequest send logs data to Coralogix server
func SendRequest(Bulk *Bulk) int {
client := &http.Client{
Timeout: time.Duration(HTTPTimeout) * time.Second,
}
for Attempt := 1; uint(Attempt) <= HTTPSendRetryCount; Attempt++ {
DebugLogger.Println("About to send bulk to Coralogix server. Attempt number:", Attempt)
request, err := http.NewRequest(http.MethodPost, 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
}
if response.StatusCode != 200 {
DebugLogger.Println("HTTP requests was failed with code:", response.StatusCode)
} else {
DebugLogger.Println("Successfully sent bulk to Coralogix server. Result is:", response.StatusCode)
return response.StatusCode
}
time.Sleep(time.Duration(HTTPSendRetryInterval) * time.Second)
}
return 0
}
// GetTimeSync synchronize logs time with Coralogix servers time
func GetTimeSync() (bool, float64) {
DebugLogger.Println("Syncing time with Coralogix server...")
client := &http.Client{
Timeout: time.Duration(TimeDelayTimeout) * time.Second,
}
request, err := http.NewRequest(http.MethodGet, TimeDeltaURL, nil)
if err != nil {
DebugLogger.Println("Can't create HTTP request:", err)
return false, 0
}
request.Header = Headers
response, err := client.Do(request)
if err != nil {
DebugLogger.Println("Can't execute HTTP request:", err)
return false, 0
}
if response.StatusCode == 200 {
response, _ := io.ReadAll(response.Body)
ServerTime, err := strconv.ParseFloat(string(response), 64)
if err != nil {
DebugLogger.Println("Can't parse HTTP response:", err)
return false, 0
}
ServerTime = ServerTime / 1e4
TimeDelta := ServerTime - float64(time.Now().UnixMilli())
return true, TimeDelta * 1e3 // convert to microseconds, because log timestamp is in microseconds.
}
DebugLogger.Println("Can't get server time")
return false, 0
}