github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/apiserver/metricsender/wireformat/wireformat.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 // Package wireformat defines the format that will be used to send metric 5 // batches to the collector and receive updates. 6 package wireformat 7 8 import ( 9 "time" 10 11 "github.com/juju/juju/state" 12 ) 13 14 // MetricBatch is a batch of metrics that will be sent to 15 // the metric collector 16 type MetricBatch struct { 17 UUID string `json:"uuid"` 18 EnvUUID string `json:"env-uuid"` 19 UnitName string `json:"unit-name"` 20 CharmUrl string `json:"charm-url"` 21 Created time.Time `json:"created"` 22 Metrics []Metric `json:"metrics"` 23 Credentials []byte `json:"credentials"` 24 } 25 26 // Metric represents a single Metric. 27 type Metric struct { 28 Key string `json:"key"` 29 Value string `json:"value"` 30 Time time.Time `json:"time"` 31 } 32 33 // ToWire converts the state.MetricBatch into a type 34 // that can be sent over the wire to the collector. 35 func ToWire(mb *state.MetricBatch) *MetricBatch { 36 metrics := make([]Metric, len(mb.Metrics())) 37 for i, m := range mb.Metrics() { 38 metrics[i] = Metric{ 39 Key: m.Key, 40 Value: m.Value, 41 Time: m.Time.UTC(), 42 } 43 } 44 return &MetricBatch{ 45 UUID: mb.UUID(), 46 EnvUUID: mb.EnvUUID(), 47 UnitName: mb.Unit(), 48 CharmUrl: mb.CharmURL(), 49 Created: mb.Created().UTC(), 50 Metrics: metrics, 51 Credentials: mb.Credentials(), 52 } 53 } 54 55 // Response represents the response from the metrics collector. 56 type Response struct { 57 UUID string `json:"uuid"` 58 EnvResponses EnvironmentResponses `json:"env-responses"` 59 } 60 61 type EnvironmentResponses map[string]EnvResponse 62 63 // Ack adds the specified the batch UUID to the list of acknowledged batches 64 // for the specified environment. 65 func (e EnvironmentResponses) Ack(envUUID, batchUUID string) { 66 env := e[envUUID] 67 68 env.AcknowledgedBatches = append(env.AcknowledgedBatches, batchUUID) 69 e[envUUID] = env 70 } 71 72 func (e EnvironmentResponses) SetStatus(envUUID, unitName, status, info string) { 73 s := UnitStatus{ 74 Status: status, 75 Info: info, 76 } 77 78 env := e[envUUID] 79 80 if env.UnitStatuses == nil { 81 env.UnitStatuses = map[string]UnitStatus{ 82 unitName: s, 83 } 84 } else { 85 env.UnitStatuses[unitName] = s 86 } 87 e[envUUID] = env 88 89 } 90 91 // EnvResponse contains the response data relevant to a concrete environment. 92 type EnvResponse struct { 93 AcknowledgedBatches []string `json:"acks,omitempty"` 94 UnitStatuses map[string]UnitStatus `json:"unit-statuses,omitempty"` 95 } 96 97 type UnitStatus struct { 98 Status string `json:"status"` 99 Info string `json:"info"` 100 }