This repository was archived by the owner on Jul 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathencoder.go
More file actions
62 lines (55 loc) · 1.27 KB
/
encoder.go
File metadata and controls
62 lines (55 loc) · 1.27 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
package dendrite
import (
"encoding/json"
"fmt"
"io"
"net/url"
"strings"
)
type Encoder interface {
Encode(out map[string]Column, writer io.Writer)
}
type JsonEncoder struct{}
type StatsdEncoder struct{}
type RawStringEncoder struct{}
func NewEncoder(u *url.URL) (Encoder, error) {
a := strings.Split(u.Scheme, "+")
switch a[len(a)-1] {
case "json":
return new(JsonEncoder), nil
case "statsd":
return new(StatsdEncoder), nil
}
return new(RawStringEncoder), nil
}
func (*RawStringEncoder) Encode(out map[string]Column, writer io.Writer) {
for _, v := range out {
if v.Type == String {
writer.Write([]byte(v.Value.(string) + "\n"))
}
}
}
func (*JsonEncoder) Encode(out map[string]Column, writer io.Writer) {
stripped := make(map[string]interface{})
for k, v := range out {
stripped[k] = v.Value
}
bytes, err := json.Marshal(stripped)
if err != nil {
panic(err)
}
bytes = append(bytes, '\n')
writer.Write(bytes)
}
func (*StatsdEncoder) Encode(out map[string]Column, writer io.Writer) {
for k, v := range out {
switch v.Type {
case Gauge:
writer.Write([]byte(fmt.Sprintf("%s:%d|g", k, v.Value)))
case Metric:
writer.Write([]byte(fmt.Sprintf("%s:%d|m", k, v.Value)))
case Counter:
writer.Write([]byte(fmt.Sprintf("%s:%d|c", k, v.Value)))
}
}
}