forked from intelsdi-x/snap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metric.go
132 lines (114 loc) · 3.27 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 Intel Corporation
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 rest
import (
"fmt"
"net/http"
"sort"
"strconv"
"github.com/julienschmidt/httprouter"
"github.com/intelsdi-x/snap/core"
"github.com/intelsdi-x/snap/mgmt/rest/rbody"
)
func (s *Server) getMetrics(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
mets, err := s.mm.MetricCatalog()
if err != nil {
respond(500, rbody.FromError(err), w)
return
}
respondWithMetrics(r.Host, mets, w)
}
func (s *Server) getMetricsFromTree(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
ns := parseNamespace(params.ByName("namespace"))
var (
ver int
err error
)
q := r.URL.Query()
v := q.Get("ver")
if v == "" {
ver = -1
} else {
ver, err = strconv.Atoi(v)
if err != nil {
respond(400, rbody.FromError(err), w)
return
}
}
if ns[len(ns)-1] == "*" {
mets, err := s.mm.FetchMetrics(ns[:len(ns)-1], ver)
if err != nil {
respond(404, rbody.FromError(err), w)
return
}
respondWithMetrics(r.Host, mets, w)
return
}
mt, err := s.mm.GetMetric(ns, ver)
if err != nil {
respond(404, rbody.FromError(err), w)
return
}
b := &rbody.MetricReturned{}
mb := &rbody.Metric{
Namespace: core.JoinNamespace(mt.Namespace()),
Version: mt.Version(),
LastAdvertisedTimestamp: mt.LastAdvertisedTime().Unix(),
Href: catalogedMetricURI(r.Host, mt),
}
rt := mt.Policy().RulesAsTable()
policies := make([]rbody.PolicyTable, 0, len(rt))
for _, r := range rt {
policies = append(policies, rbody.PolicyTable{
Name: r.Name,
Type: r.Type,
Default: r.Default,
Required: r.Required,
Minimum: r.Minimum,
Maximum: r.Maximum,
})
}
mb.Policy = policies
b.Metric = mb
respond(200, b, w)
}
func respondWithMetrics(host string, mets []core.CatalogedMetric, w http.ResponseWriter) {
b := rbody.NewMetricsReturned()
for _, met := range mets {
rt := met.Policy().RulesAsTable()
policies := make([]rbody.PolicyTable, 0, len(rt))
for _, r := range rt {
policies = append(policies, rbody.PolicyTable{
Name: r.Name,
Type: r.Type,
Default: r.Default,
Required: r.Required,
Minimum: r.Minimum,
Maximum: r.Maximum,
})
}
b = append(b, rbody.Metric{
Namespace: core.JoinNamespace(met.Namespace()),
Version: met.Version(),
LastAdvertisedTimestamp: met.LastAdvertisedTime().Unix(),
Policy: policies,
Href: catalogedMetricURI(host, met),
})
}
sort.Sort(b)
respond(200, b, w)
}
func catalogedMetricURI(host string, mt core.CatalogedMetric) string {
return fmt.Sprintf("%s://%s/v1/metrics%s?version=%d", protocolPrefix, host, core.JoinNamespace(mt.Namespace()), mt.Version())
}