/
aliyun.go
76 lines (63 loc) · 1.54 KB
/
aliyun.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
/**
* @Author : nopsky
* @Email : cnnopsky@gmail.com
* @Date : 2021/12/13 16:22
*/
package writer
import (
"io"
"os"
"time"
sls "github.com/aliyun/aliyun-log-go-sdk"
"github.com/aliyun/aliyun-log-go-sdk/producer"
jsoniter "github.com/json-iterator/go"
"google.golang.org/protobuf/proto"
)
type AliyunWriter struct {
opt *AliyunOption
instance *producer.Producer
}
type AliyunOption struct {
AccessKey string
AccessKeySecret string
EndPoint string
Project string
LogStore string
Topic string
}
func NewAliyunWriter(opt *AliyunOption) io.Writer {
a := &AliyunWriter{}
a.opt = opt
producerConfig := producer.GetDefaultProducerConfig()
producerConfig.Endpoint = a.opt.EndPoint
producerConfig.AccessKeyID = a.opt.AccessKey
producerConfig.AccessKeySecret = a.opt.AccessKeySecret
producerInstance := producer.InitProducer(producerConfig)
producerInstance.Start()
a.instance = producerInstance
return a
}
func (a AliyunWriter) Write(p []byte) (n int, err error) {
var data map[string]string
err = jsoniter.Unmarshal(p, &data)
if err != nil {
return -1, err
}
var content []*sls.LogContent
for k, v := range data {
content = append(content, &sls.LogContent{
Key: proto.String(k),
Value: proto.String(v),
})
}
log := &sls.Log{
Time: proto.Uint32(uint32(time.Now().Unix())),
Contents: content,
}
hostName, _ := os.Hostname()
err = a.instance.SendLog(a.opt.Project, a.opt.LogStore, a.opt.Topic, hostName, log)
if err != nil {
return -1, err
}
return len(p), nil
}