forked from bodji/gopentsdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathput.go
53 lines (40 loc) · 952 Bytes
/
put.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
package gopentsdb
import (
"fmt"
"strings"
"time"
)
type Put struct {
metricName string
timestamp int64
tags map[string]string
value float64
}
func NewPut(metricName string, tags map[string]string, value float64) (p *Put) {
p = new(Put)
p.metricName = metricName
p.timestamp = time.Now().Unix()
p.tags = tags
p.value = value
return
}
func (p *Put) GetTimestamp() int64 {
return p.timestamp
}
func (p *Put) SetTimestamp(t int64) {
p.timestamp = t
}
// ToString return the line that should be pushed to OpenTSDB raw socket
// Example :
//
// put loadaverage 1.15 load=load15 hostname=localhost
//
func (p *Put) ToString() (s string) {
s = fmt.Sprintf("put %s %d %.3f ", p.metricName, p.timestamp, p.value)
for tagName, _ := range p.tags {
key := strings.ToLower(strings.Replace(tagName, " ", "_", 0))
value := strings.Replace(p.tags[tagName], " ", "_", 0)
s += key + "=" + value + " "
}
return s
}