-
Notifications
You must be signed in to change notification settings - Fork 1
/
log_project.go
159 lines (140 loc) · 3.71 KB
/
log_project.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package ali
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/go-resty/resty/v2"
"github.com/golang/protobuf/proto"
"github.com/pierrec/lz4"
"github.com/ego-component/ealilogger/pb"
)
const (
version = "0.6.0" // SDK version
signatureMethod = "hmac-sha1" // Signature method
)
// errorMessage message in SLS HTTP response.
type errorMessage struct {
Code string `json:"errorCode"`
Message string `json:"errorMessage"`
}
// logProject defines the Ali project detail
type logProject struct {
name string // project name
endpoint string // IP or hostname of SLS endpoint
accessKeyID string
accessKeySecret string
host string
securityToken string
cli *resty.Client
}
// logStore stores the logs
type logStore struct {
Name string `json:"logstoreName"`
TTL int
ShardCount int
CreateTime uint32
LastModifyTime uint32
project *logProject
}
// listLogStore returns all logstore names of project p.
//func (p *logProject) listLogStore() (storeNames []string, err error) {
// h := map[string]string{
// "x-log-bodyrawsize": "0",
// }
//
// uri := "/logstores"
// r, err := p.request("GET", uri, h, nil)
// if err != nil {
// return
// }
//
// if r.StatusCode() != http.StatusOK {
// errMsg := &errorMessage{}
// if err = json.Unmarshal(r.Body(), errMsg); err != nil {
// return nil, fmt.Errorf("failed to unmarshal logstore response, %w", err)
// }
// return nil, fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
// }
//
// type Body struct {
// Count int
// LogStores []string
// }
// body := &Body{}
// if err = json.Unmarshal(r.Body(), body); err != nil {
// return
// }
// storeNames = body.LogStores
// return
//}
func (p *logProject) initHost() {
scheme := httpScheme // default to http scheme
host := p.endpoint
if strings.HasPrefix(p.endpoint, httpScheme) {
host = strings.TrimPrefix(p.endpoint, scheme)
} else if strings.HasPrefix(p.endpoint, httpsScheme) {
scheme = httpsScheme
host = strings.TrimPrefix(p.endpoint, scheme)
}
if p.name == "" {
p.host = fmt.Sprintf("%s%s", scheme, host)
} else {
p.host = fmt.Sprintf("%s%s.%s", scheme, p.name, host)
}
}
// getLogStore returns logstore according by logstore name.
func (p *logProject) getLogStore(name string) (s *logStore, err error) {
h := map[string]string{"x-log-bodyrawsize": "0"}
r, err := p.request("GET", "/logstores/"+name, h, nil)
if err != nil {
return
}
if r.StatusCode() != http.StatusOK {
errMsg := &errorMessage{}
if err = json.Unmarshal(r.Body(), errMsg); err != nil {
return nil, fmt.Errorf("failed to get logstore")
}
return nil, fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
}
s = &logStore{}
if err = json.Unmarshal(r.Body(), s); err != nil {
return
}
s.project = p
return
}
// putLogs puts logs into logstore.
// The callers should transform user logs into LogGroup.
func (s *logStore) putLogs(lg *pb.LogGroup) (err error) {
body, err := proto.Marshal(lg)
if err != nil {
return
}
// Compresse body with lz4
out := make([]byte, lz4.CompressBlockBound(len(body)))
n, err := lz4.CompressBlock(body, out, nil)
if err != nil {
return
}
h := map[string]string{
"x-log-compresstype": "lz4",
"x-log-bodyrawsize": strconv.Itoa(len(body)),
"Content-Type": "application/x-protobuf",
}
uri := fmt.Sprintf("/logstores/%v", s.Name)
r, err := s.project.request("POST", uri, h, out[:n])
if err != nil {
return
}
if r.StatusCode() != http.StatusOK {
errMsg := &errorMessage{}
err = json.Unmarshal(r.Body(), errMsg)
if err != nil {
return fmt.Errorf("failed to unmarshal putLogs response, %w", err)
}
return fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
}
return
}