/
info.go
193 lines (166 loc) · 5 KB
/
info.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package carbonserver
import (
"encoding/json"
"io/ioutil"
"net/http"
_ "net/http/pprof"
"strings"
"sync/atomic"
"time"
"go.uber.org/zap"
"github.com/go-graphite/go-whisper"
"github.com/go-graphite/carbonzipper/zipper/httpHeaders"
protov2 "github.com/go-graphite/protocol/carbonapi_v2_pb"
protov3 "github.com/go-graphite/protocol/carbonapi_v3_pb"
)
func (listener *CarbonserverListener) infoHandler(wr http.ResponseWriter, req *http.Request) {
// URL: /info/?target=the.metric.Name&format=json
t0 := time.Now()
ctx := req.Context()
atomic.AddUint64(&listener.metrics.InfoRequests, 1)
format := req.FormValue("format")
metrics := req.Form["target"]
accessLogger := TraceContextToZap(ctx, listener.accessLogger.With(
zap.String("handler", "info"),
zap.String("url", req.URL.RequestURI()),
zap.String("peer", req.RemoteAddr),
))
accepts := req.Header["Accept"]
for _, accept := range accepts {
if accept == httpHeaders.ContentTypeCarbonAPIv3PB {
format = "carbonapi_v3_pb"
break
}
}
if format == "" {
format = "json"
}
formatCode, ok := knownFormats[format]
if !ok || formatCode == pickleFormat {
atomic.AddUint64(&listener.metrics.InfoErrors, 1)
accessLogger.Error("info failed",
zap.Duration("runtime_seconds", time.Since(t0)),
zap.String("reason", "unsupported format"),
zap.Int("http_code", http.StatusBadRequest),
)
http.Error(wr, "Bad request (unsupported format)",
http.StatusBadRequest)
return
}
if formatCode == protoV3Format {
body, err := ioutil.ReadAll(req.Body)
if err != nil {
accessLogger.Error("info failed",
zap.Duration("runtime_seconds", time.Since(t0)),
zap.String("reason", err.Error()),
zap.Int("http_code", http.StatusBadRequest),
)
http.Error(wr, "Bad request (unsupported format)",
http.StatusBadRequest)
}
var pv3Request protov3.MultiGlobRequest
pv3Request.Unmarshal(body)
metrics = pv3Request.Metrics
}
accessLogger.With(
zap.Strings("targets", metrics),
zap.String("format", format),
)
response := protov3.MultiMetricsInfoResponse{}
var retentionsV2 []protov2.Retention
for i, metric := range metrics {
path := listener.whisperData + "/" + strings.Replace(metric, ".", "/", -1) + ".wsp"
w, err := whisper.Open(path)
if err != nil {
atomic.AddUint64(&listener.metrics.NotFound, 1)
accessLogger.Error("info served",
zap.Duration("runtime_seconds", time.Since(t0)),
zap.String("reason", "metric not found"),
zap.Int("http_code", http.StatusNotFound),
)
http.Error(wr, "Metric not found", http.StatusNotFound)
return
}
defer w.Close()
aggr := w.AggregationMethod()
maxr := int64(w.MaxRetention())
xfiles := float32(w.XFilesFactor())
rets := make([]protov3.Retention, 0, 4)
for _, retention := range w.Retentions() {
spp := int64(retention.SecondsPerPoint())
nop := int64(retention.NumberOfPoints())
rets = append(rets, protov3.Retention{
SecondsPerPoint: spp,
NumberOfPoints: nop,
})
//only one metric is enough - first metric
//TODO include support for multiple metrics
if i == 0 && formatCode == protoV2Format {
retentionsV2 = append(retentionsV2, protov2.Retention{
SecondsPerPoint: int32(retention.SecondsPerPoint()),
NumberOfPoints: int32(retention.NumberOfPoints()),
})
}
}
response.Metrics = append(response.Metrics, protov3.MetricsInfoResponse{
Name: metric,
ConsolidationFunc: aggr,
MaxRetention: maxr,
XFilesFactor: xfiles,
Retentions: rets,
})
}
if len(response.Metrics) == 0 {
atomic.AddUint64(&listener.metrics.InfoErrors, 1)
accessLogger.Error("info failed",
zap.String("reason", "Not Found"),
zap.Int("http_code", http.StatusNotFound),
zap.Error(errorNotFound{}),
)
http.Error(wr, "Not Found", http.StatusNotFound)
return
}
var b []byte
var err error
contentType := ""
switch formatCode {
case jsonFormat:
contentType = httpHeaders.ContentTypeJSON
b, err = json.Marshal(response)
case protoV2Format:
contentType = httpHeaders.ContentTypeCarbonAPIv2PB
var r protov3.MetricsInfoResponse
if len(response.Metrics) > 0 {
r = response.Metrics[0]
}
response := protov2.InfoResponse{
Name: r.Name,
AggregationMethod: r.ConsolidationFunc,
MaxRetention: int32(r.MaxRetention),
XFilesFactor: r.XFilesFactor,
Retentions: retentionsV2,
}
b, err = response.Marshal()
case protoV3Format:
contentType = httpHeaders.ContentTypeCarbonAPIv3PB
b, err = response.Marshal()
}
if err != nil {
atomic.AddUint64(&listener.metrics.InfoErrors, 1)
accessLogger.Error("info failed",
zap.String("reason", "response encode failed"),
zap.Int("http_code", http.StatusInternalServerError),
zap.Error(err),
)
http.Error(wr, "Failed to encode response: "+err.Error(),
http.StatusInternalServerError)
return
}
wr.Header().Set("Content-Type", contentType)
wr.Write(b)
accessLogger.Info("info served",
zap.Duration("runtime_seconds", time.Since(t0)),
zap.Int("http_code", http.StatusOK),
)
return
}