-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathhandler_getLogs.go
More file actions
112 lines (93 loc) · 2.97 KB
/
handler_getLogs.go
File metadata and controls
112 lines (93 loc) · 2.97 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package serviceLogs
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"time"
)
type Response struct {
Items []Data `json:"items"`
}
type Data struct {
Timestamp string `json:"timestamp"`
Version int `json:"version"`
Hostname string `json:"hostname"`
Content string `json:"content"`
Client string `json:"client"`
Facility int `json:"facility"`
FacilityLabel string `json:"facilityLabel"`
Id string `json:"id"`
MsgId string `json:"msgId"`
Priority int `json:"priority"`
ProcId string `json:"procId"`
Severity int `json:"severity"`
SeverityLabel string `json:"severityLabel"`
StructuredData string `json:"structuredData"`
Tag string `json:"tag"`
TlsPeer string `json:"tlsPeer"`
AppName string `json:"appName"`
Message string `json:"message"`
}
var (
ErrInvalidRequest = errors.New("invalid request")
ErrLogResponse = errors.New("log response error")
ErrInvalidResponse = errors.New("invalid response")
)
type InvalidRequestError struct {
FuncName string
Msg string
Err error
}
func (e *InvalidRequestError) Error() string {
return fmt.Sprintf("%s: %s: %v", e.FuncName, e.Msg, e.Err)
}
func NewInvalidRequestError(funcName, msg string, err error) error {
return &InvalidRequestError{FuncName: funcName, Msg: msg, Err: err}
}
type LogResponseError struct {
StatusCode int
Msg string
Err error
}
func (e *LogResponseError) Error() string {
return fmt.Sprintf("status code: %d: %s: %v", e.StatusCode, e.Msg, e.Err)
}
func NewLogResponseError(statusCode int, msg string, err error) error {
return &LogResponseError{StatusCode: statusCode, Msg: msg, Err: err}
}
func getLogs(ctx context.Context, method, url, format, formatTemplate, mode string) error {
c := http.Client{Timeout: time.Duration(1) * time.Minute}
req, err := http.NewRequest(method, url, nil)
if err != nil {
return NewInvalidRequestError("getLogs", "failed to create request", err)
}
req = req.WithContext(ctx)
req.Header.Add("Content-Type", "application/json")
resp, err := c.Do(req)
if err != nil {
return NewInvalidRequestError("getLogs", "failed to execute request", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return NewLogResponseError(resp.StatusCode, "failed to read response body", err)
}
if resp.StatusCode != http.StatusOK {
return NewLogResponseError(resp.StatusCode, fmt.Sprintf("unexpected status code: %d", resp.StatusCode), nil)
}
jsonData, err := parseResponse(body)
if err != nil {
return NewLogResponseError(resp.StatusCode, "failed to parse response", err)
}
return parseResponseByFormat(jsonData, format, formatTemplate, mode)
}
func parseResponse(body []byte) (Response, error) {
var response Response
if err := json.Unmarshal(body, &response); err != nil {
return Response{}, NewLogResponseError(0, "failed to unmarshal response", err)
}
return response, nil
}