-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwriter.go
More file actions
148 lines (134 loc) · 3.01 KB
/
writer.go
File metadata and controls
148 lines (134 loc) · 3.01 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
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
package zapsls
import (
"encoding/json"
"fmt"
sls "github.com/aliyun/aliyun-log-go-sdk"
"github.com/aliyun/aliyun-log-go-sdk/producer"
"github.com/gogo/protobuf/proto"
"io"
"strconv"
"sync"
"time"
)
const (
defaultMaxSize = 100
megabyte = 1024 * 1024
)
type Writer struct {
endpoint string
accessKeyId string
accessKeySecret string
project string
logstore string
m sync.Mutex
slsProducer *producer.Producer
maxSize int64
topic string
source string
}
func newDefaultWriter(config *Config) io.Writer {
return &Writer{
endpoint: config.Endpoint,
accessKeyId: config.AccessKeyId,
accessKeySecret: config.AccessKeySecret,
logstore: config.Logstore,
project: config.Project,
maxSize: config.MaxSize,
topic: config.Topic,
source: config.Source,
}
}
func (w *Writer) Write(p []byte) (int, error) {
w.m.Lock()
defer w.m.Unlock()
length := int64(len(p))
maxSize := func() int64 {
if w.maxSize == 0 {
return defaultMaxSize * megabyte
}
return w.maxSize * megabyte
}()
if length > maxSize {
return 0, fmt.Errorf("write length %d exceeds max size %d", length, maxSize)
}
if w.slsProducer == nil {
producerConfig := producer.GetDefaultProducerConfig()
producerConfig.Endpoint = w.endpoint
producerConfig.AccessKeyID = w.accessKeyId
producerConfig.AccessKeySecret = w.accessKeySecret
slsProducer := producer.InitProducer(producerConfig)
slsProducer.Start()
w.slsProducer = slsProducer
}
msg := map[string]interface{}{}
err := json.Unmarshal(p, &msg)
if err != nil {
return 0, err
}
logDetails := mapInterfaceToString(msg)
content := []*sls.LogContent{}
for k, v := range logDetails {
content = append(content, &sls.LogContent{
Key: proto.String(k),
Value: proto.String(v),
})
}
topic := w.topic
if topic == "" {
topic = "topic"
}
source := w.source
if source == "" {
source = "127.0.0.1"
}
log := &sls.Log{
Time: proto.Uint32(uint32(time.Now().Unix())),
Contents: content,
}
err = w.slsProducer.SendLog(w.project, w.logstore, topic, source, log)
if err != nil {
return 0, err
}
return int(length), nil
}
func (w *Writer) Close() error {
w.m.Lock()
defer w.m.Unlock()
if w.slsProducer == nil {
return nil
}
w.slsProducer.SafeClose()
w.slsProducer = nil
return nil
}
func interfaceToString(i interface{}) string {
str := ""
switch v := i.(type) {
case string:
str = v
case int:
str = strconv.Itoa(v)
case int64:
str = strconv.FormatInt(v, 10)
case float64:
str = strconv.FormatFloat(v, 'f', 2, 64)
case uint64:
str = strconv.FormatUint(v, 10)
case error:
str = v.Error()
// Add cases for other types that you want to handle...
default:
msgBytes, err := json.Marshal(i)
if err == nil {
str = string(msgBytes)
}
}
return str
}
func mapInterfaceToString(input map[string]interface{}) map[string]string {
ret := make(map[string]string, 0)
for k, v := range input {
ret[k] = interfaceToString(v)
}
return ret
}