-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
metric_json.go
74 lines (62 loc) · 2.24 KB
/
metric_json.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
/*
Copyright 2017 Simon J Mudd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package discovery
// Collect discovery metrics and manage their storage and retrieval for monitoring purposes.
import (
"errors"
"fmt"
"time"
"vitess.io/vitess/go/vt/vtorc/collection"
)
// formattedFloat is to force the JSON output to show 3 decimal places
type formattedFloat float64
func (m formattedFloat) String() string {
return fmt.Sprintf("%.3f", m)
}
// MetricJSON holds a structure which represents some discovery latency information
type MetricJSON struct {
Timestamp time.Time
Hostname string
Port int
BackendLatencySeconds formattedFloat
InstanceLatencySeconds formattedFloat
TotalLatencySeconds formattedFloat
Err error
}
// JSONSince returns an API response of discovery metric collection information
// in a printable JSON format.
func JSONSince(c *collection.Collection, t time.Time) ([](MetricJSON), error) {
if c == nil {
return nil, errors.New("MetricCollection.JSONSince: c == nil")
}
raw, err := c.Since(t)
if err != nil {
return nil, err
}
// build up JSON response for each Metric we received
var s []MetricJSON
for i := range raw {
m := raw[i].(*Metric) // convert back to a real Metric rather than collection.Metric interface
mj := MetricJSON{
Timestamp: m.Timestamp,
Hostname: m.InstanceKey.Hostname,
Port: m.InstanceKey.Port,
BackendLatencySeconds: formattedFloat(m.BackendLatency.Seconds()),
InstanceLatencySeconds: formattedFloat(m.InstanceLatency.Seconds()),
TotalLatencySeconds: formattedFloat(m.TotalLatency.Seconds()),
Err: m.Err,
}
s = append(s, mj)
}
return s, nil
}