-
Notifications
You must be signed in to change notification settings - Fork 0
/
wireformat.go
101 lines (85 loc) · 2.59 KB
/
wireformat.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
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
// Package wireformat defines the format that will be used to send metric
// batches to the collector and receive updates.
package wireformat
import (
"time"
"github.com/juju/juju/state"
)
// MetricBatch is a batch of metrics that will be sent to
// the metric collector
type MetricBatch struct {
UUID string `json:"uuid"`
EnvUUID string `json:"env-uuid"`
UnitName string `json:"unit-name"`
CharmUrl string `json:"charm-url"`
Created time.Time `json:"created"`
Metrics []Metric `json:"metrics"`
Credentials []byte `json:"credentials"`
}
// Metric represents a single Metric.
type Metric struct {
Key string `json:"key"`
Value string `json:"value"`
Time time.Time `json:"time"`
}
// ToWire converts the state.MetricBatch into a type
// that can be sent over the wire to the collector.
func ToWire(mb *state.MetricBatch) *MetricBatch {
metrics := make([]Metric, len(mb.Metrics()))
for i, m := range mb.Metrics() {
metrics[i] = Metric{
Key: m.Key,
Value: m.Value,
Time: m.Time.UTC(),
}
}
return &MetricBatch{
UUID: mb.UUID(),
EnvUUID: mb.EnvUUID(),
UnitName: mb.Unit(),
CharmUrl: mb.CharmURL(),
Created: mb.Created().UTC(),
Metrics: metrics,
Credentials: mb.Credentials(),
}
}
// Response represents the response from the metrics collector.
type Response struct {
UUID string `json:"uuid"`
EnvResponses EnvironmentResponses `json:"env-responses"`
NewGracePeriod time.Duration `json:"new-grace-period"`
}
type EnvironmentResponses map[string]EnvResponse
// Ack adds the specified the batch UUID to the list of acknowledged batches
// for the specified environment.
func (e EnvironmentResponses) Ack(envUUID, batchUUID string) {
env := e[envUUID]
env.AcknowledgedBatches = append(env.AcknowledgedBatches, batchUUID)
e[envUUID] = env
}
func (e EnvironmentResponses) SetStatus(envUUID, unitName, status, info string) {
s := UnitStatus{
Status: status,
Info: info,
}
env := e[envUUID]
if env.UnitStatuses == nil {
env.UnitStatuses = map[string]UnitStatus{
unitName: s,
}
} else {
env.UnitStatuses[unitName] = s
}
e[envUUID] = env
}
// EnvResponse contains the response data relevant to a concrete environment.
type EnvResponse struct {
AcknowledgedBatches []string `json:"acks,omitempty"`
UnitStatuses map[string]UnitStatus `json:"unit-statuses,omitempty"`
}
type UnitStatus struct {
Status string `json:"status"`
Info string `json:"info"`
}