-
Notifications
You must be signed in to change notification settings - Fork 2
/
metric.go
66 lines (57 loc) · 2.11 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
package main
import (
"fmt"
"strings"
)
var metricTypes = map[string]string{
"iperf_tests_started": "counter",
"iperf_tests_finished": "counter",
"iperf_tests_failed": "counter",
"iperf_sent_bytes": "gauge",
"iperf_sent_packets": "gauge",
"iperf_sent_lost_packets": "gauge",
"iperf_sent_seconds": "gauge",
"iperf_received_bytes": "gauge",
"iperf_received_packets": "gauge",
"iperf_received_lost_packets": "gauge",
"iperf_received_seconds": "gauge",
}
var metricHelps = map[string]string{
"iperf_tests_started": "Number of tests that been started.",
"iperf_tests_finished": "Number of tests that have finished successfully.",
"iperf_tests_failed": "Number of tests that have failed.",
"iperf_sent_bytes": "Number of bytes sent during the test.",
"iperf_sent_packets": "Number of packets sent during the test (UDP only).",
"iperf_sent_lost_packets": "Number of packet lost during the test (UDP only).",
"iperf_sent_seconds": "Duration of the test on the sending side, in seconds.",
"iperf_received_bytes": "Number of bytes received during the test.",
"iperf_received_packets": "Number of packets received during the test (UDP only).",
"iperf_received_lost_packets": "Number of packets lost by the receiver during the test (UDP only).",
"iperf_received_seconds": "Duration of the test on the receiving side, in seconds.",
}
type Metric struct {
Label string
Tags map[string]string
Value float32
}
func (m *Metric) Format() string {
typeStr := ""
metricType, ok := metricTypes[m.Label]
if ok {
typeStr = fmt.Sprintf("# TYPE %s %s\n", m.Label, metricType)
}
helpStr := ""
metricHelp, ok := metricHelps[m.Label]
if ok {
helpStr = fmt.Sprintf("# HELP %s %s\n", m.Label, metricHelp)
}
tags := make([]string, 0)
for key, value := range m.Tags {
tags = append(tags, fmt.Sprintf("%s=\"%s\"", key, value))
}
tagStr := ""
if len(tags) > 0 {
tagStr = "{" + strings.Join(tags, ",") + "}"
}
return fmt.Sprintf("%s%s%s%s %f\n", typeStr, helpStr, m.Label, tagStr, m.Value)
}