-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcls.go
129 lines (106 loc) · 2.91 KB
/
cls.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
/*
Package logruscls provides logrus hook for tencent cls.
It has these top-level structs:
Hook
CLSClient
CLSAsyncClient
*/
package logruscls
import (
"bytes"
"crypto/hmac"
"crypto/sha1"
"errors"
"fmt"
"hash"
"io/ioutil"
"net/http"
"time"
"github.com/chuangbo/logruscls/pb"
"github.com/golang/protobuf/proto"
)
var httpClient = &http.Client{
Timeout: 5 * time.Second,
}
// CLSClient is cls client
type CLSClient struct {
URL string
SecretID, SecretKey string
TopicID string
}
// NewCLSClient create a cls client
func NewCLSClient(region, secretID, secretKey, topicID string) (*CLSClient, error) {
if region == "" || secretID == "" || secretKey == "" || topicID == "" {
return nil, errors.New("please specific cls configurations")
}
return &CLSClient{
URL: fmt.Sprintf("http://%s.cls.myqcloud.com/", region),
SecretID: secretID,
SecretKey: secretKey,
TopicID: topicID,
}, nil
}
// Log upload one log directly to cls
func (c *CLSClient) Log(log *pb.Log) error {
return c.UploadStructuredLog(
&pb.LogGroupList{
LogGroupList: []*pb.LogGroup{
{
Logs: []*pb.Log{log},
},
},
},
)
}
// UploadStructuredLog upload structured log to tencent CLS
func (c *CLSClient) UploadStructuredLog(logGroupList *pb.LogGroupList) error {
requestBody, err := proto.Marshal(logGroupList)
if err != nil {
return err
}
r, err := http.NewRequest("POST", fmt.Sprintf("%sstructuredlog?topic_id=%s", c.URL, c.TopicID), bytes.NewReader(requestBody))
if err != nil {
return err
}
r.Header.Set("Content-Type", "application/x-protobuf")
r.Header.Set("Authorization", sign("post", "/structuredlog", c.SecretID, c.SecretKey))
resp, err := httpClient.Do(r)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return fmt.Errorf("error %d: %s", resp.StatusCode, string(body))
}
return nil
}
func hmacSHA1(src, key string) string {
var mac hash.Hash
mac = hmac.New(sha1.New, []byte(key))
mac.Write([]byte(src))
return fmt.Sprintf("%x", mac.Sum(nil))
}
// sign 对 cls 请求进行签名,我们暂时不对 header 和 query 进行签名
// https://cloud.tencent.com/document/product/614/12445
func sign(method, uri, secretID, secretKey string) string {
httpRequestInfo := fmt.Sprintf("%s\n%s\n\n\n", method, uri)
httpRequestInfoSHA1 := fmt.Sprintf("%x", sha1.Sum([]byte(httpRequestInfo)))
startTime := time.Now().Unix()
endTime := startTime + 300
stringToSign := fmt.Sprintf("sha1\n%d;%d\n%s\n", startTime, endTime, httpRequestInfoSHA1)
signKey := hmacSHA1(fmt.Sprintf("%d;%d", startTime, endTime), secretKey)
signature := hmacSHA1(stringToSign, signKey)
return fmt.Sprintf(
"q-sign-algorithm=sha1&q-ak=%s&q-sign-time=%d;%d&q-key-time=%d;%d&q-header-list=&q-url-param-list=&q-signature=%s",
secretID,
startTime,
endTime,
startTime,
endTime,
signature,
)
}