-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathhandler.go
More file actions
95 lines (79 loc) · 2.07 KB
/
handler.go
File metadata and controls
95 lines (79 loc) · 2.07 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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
)
type dogstatsdJsonMetric struct {
Namespace string `json:"namespace"`
Name string `json:"name"`
Path string `json:"path"`
Value float64 `json:"value"`
Extras []string `json:"extras"`
SampleRate float64 `json:"sample_rate"`
Tags []string `json:"tags"`
}
func newJsonDogstatsdMsgHandler(extraTags []string) msgHandler {
return func(msg []byte) error {
dMsg, err := parseDogstatsdMsg(msg)
if err != nil {
log.Println(err.Error())
}
if dMsg.Type() != metricMsgType {
log.Println("Unable to serialize non metric messages to JSON yet")
return nil
}
metric, ok := dMsg.(dogstatsdMetric)
if !ok {
log.Fatalf("Programming error: invalid Type() = type matching")
}
jsonMsg := dogstatsdJsonMetric{
Namespace: metric.namespace,
Name: metric.name,
Path: fmt.Sprintf("%s.%s", metric.namespace, metric.name),
Value: metric.floatValue,
Extras: metric.extras,
SampleRate: metric.sampleRate,
Tags: metric.tags,
}
enc := json.NewEncoder(os.Stdout)
if err := enc.Encode(&jsonMsg); err != nil {
log.Println("JSON serialize error:", err.Error())
return nil
}
return nil
}
}
func newHumanDogstatsdMsgHandler(extraTags []string) msgHandler {
return func(msg []byte) error {
dMsg, err := parseDogstatsdMsg(msg)
if err != nil {
log.Println(err.Error())
return nil
}
metric, ok := dMsg.(dogstatsdMetric)
if dMsg.Type() != metricMsgType || !ok {
return nil
}
tmpl := "metric:%s|%s.%s|%.2f"
str := fmt.Sprintf(tmpl, metric.metricType.String(), metric.namespace, metric.name, metric.floatValue)
if metric.metricType == timerMetricType {
str += "ms"
}
// iterate through tags
for _, tag := range append(extraTags, metric.tags...) {
str += " " + tag
}
fmt.Fprintf(os.Stdout, str)
fmt.Fprintf(os.Stdout, "\n")
return nil
}
}
func newRawDogstatsdMsgHandler() msgHandler {
return func(msg []byte) error {
fmt.Fprintf(os.Stdout, string(msg))
fmt.Fprintf(os.Stdout, "\n")
return nil
}
}