This repository has been archived by the owner on Mar 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetric.go
117 lines (102 loc) · 3.02 KB
/
metric.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
package metrics
import (
"encoding/json"
"fmt"
"strings"
)
type Metric struct {
worker Worker
name string
tags Tags
description string
storageKey []byte
}
func (metric *Metric) considerHiddenTags() {
considerHiddenTags(metric.tags)
}
func (metric *Metric) generateStorageKey() *preallocatedStringerBuffer {
return generateStorageKey(metric.worker.GetType(), metric.name, metric.tags)
}
func (metric *Metric) GetWorker() Worker {
return metric.worker
}
func (metric *Metric) IsRunning() bool {
return metric.worker.IsRunning()
}
func (metric *Metric) SetGCEnabled(enable bool) {
metric.worker.SetGCEnabled(enable)
}
func (metric *Metric) Stop() {
metric.worker.Stop()
}
func (metric *Metric) GetName() string {
return metric.name
}
func (metric *Metric) GetTags() Tags {
return metric.tags.Copy()
}
func (metric *Metric) MarshalJSON() ([]byte, error) {
switch metric.worker.GetType() {
case MetricTypeTiming:
return metric.marshalJSONTiming()
}
return metric.marshalJSONDefault()
}
func (metric *Metric) marshalJSONTiming() ([]byte, error) {
worker := metric.worker.(WorkerTiming)
values := worker.GetValuePointers()
var jsonValues []string
considerValue := func(label string) func(data *TimingValue) {
return func(data *TimingValue) {
if data.Count == 0 {
return
}
jsonValues = append(jsonValues, fmt.Sprintf(`"%v":{"count":%d,"min":%d,"mid":%d,"avg":%d,"per99":%d,"max":%d}`,
label,
data.Count,
data.Min,
data.Mid,
data.Avg,
data.Per99,
data.Max,
))
}
}
values.Last.LockDo(considerValue(`last`))
values.S1.LockDo(considerValue(`1_second`))
values.S5.LockDo(considerValue(`5_seconds`))
values.M1.LockDo(considerValue(`1_minute`))
values.M5.LockDo(considerValue(`5_minutes`))
values.H1.LockDo(considerValue(`1_hour`))
values.H6.LockDo(considerValue(`6_hours`))
values.D1.LockDo(considerValue(`1_day`))
values.Total.LockDo(considerValue(`total`))
nameJSON, _ := json.Marshal(metric.name)
descriptionJSON, _ := json.Marshal(metric.description)
tagsJSON, _ := json.Marshal(string(metric.storageKey[:strings.IndexByte(string(metric.storageKey), '@')]))
typeJSON, _ := json.Marshal(string(metric.worker.GetType()))
valueJSON := `{` + strings.Join(jsonValues, `,`) + `}`
metricJSON := fmt.Sprintf(`{"name":%s,"tags":%s,"value":%s,"description":%s,"type":%s}`,
string(nameJSON),
tagsJSON,
valueJSON,
string(descriptionJSON),
string(typeJSON),
)
return []byte(metricJSON), nil
}
func (metric *Metric) marshalJSONDefault() ([]byte, error) {
nameJSON, _ := json.Marshal(metric.name)
descriptionJSON, _ := json.Marshal(metric.description)
tagsJSON, _ := json.Marshal(string(metric.storageKey[:strings.IndexByte(string(metric.storageKey), '@')]))
typeJSON, _ := json.Marshal(string(metric.worker.GetType()))
value := metric.worker.Get()
metricJSON := fmt.Sprintf(`{"name":%s,"tags":%s,"value":%d,"description":%s,"type":%s}`,
string(nameJSON),
string(tagsJSON),
value,
string(descriptionJSON),
string(typeJSON),
)
return []byte(metricJSON), nil
}